| 1 | package POE::Declare::Object; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | POE::Declare::Object - Base object for POE::Declare classes | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | L<POE::Declare::Object> provides the base package that delivers the core | ||
| 12 | functionality for all instantiated L<POE::Declare> objects. | ||
| 13 | |||
| 14 | Functionality and methods defined here are available in all L<POE::Declare> | ||
| 15 | objects. | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use 5.008007; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use attributes (); | ||
| 25 | use Carp (); | ||
| 26 | use Scalar::Util (); | ||
| 27 | use Params::Util (); | ||
| 28 | use POE; | ||
| 29 | use POE::Session (); | ||
| 30 | use POE::Declare (); | ||
| 31 | |||
| 32 | use vars qw{$VERSION}; | ||
| 33 | BEGIN { | ||
| 34 | $VERSION = '0.23_01'; | ||
| 35 | } | ||
| 36 | |||
| 37 | # Inside-out storage of internal values | ||
| 38 | my %ID = (); | ||
| 39 | |||
| 40 | # Set default attributes | ||
| 41 | POE::Declare::declare( Alias => 'Param' ); | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | ##################################################################### | ||
| 48 | # Attribute Hooks | ||
| 49 | |||
| 50 | # Only events are supported for now | ||
| 51 | sub MODIFY_CODE_ATTRIBUTES { | ||
| 52 | my ($class, $code, $name, @params) = @_; | ||
| 53 | |||
| 54 | # Register an event | ||
| 55 | if ( $name eq 'Event' ) { | ||
| 56 | # Add to the coderef event register | ||
| 57 | $POE::Declare::EVENT{Scalar::Util::refaddr($code)} = [ | ||
| 58 | 'POE::Declare::Meta::Event', | ||
| 59 | ]; | ||
| 60 | return (); | ||
| 61 | } | ||
| 62 | |||
| 63 | # Register a timeout | ||
| 64 | if ( $name =~ /^Timeout\b/ ) { | ||
| 65 | unless ( $name =~ /^Timeout\((.+)\)$/ ) { | ||
| 66 | Carp::croak("Missing or invalid timeout"); | ||
| 67 | } | ||
| 68 | my $delay = $1; | ||
| 69 | unless ( Params::Util::_POSINT($delay) ) { | ||
| 70 | Carp::croak("Missing or invalid timeout"); | ||
| 71 | } | ||
| 72 | $POE::Declare::EVENT{Scalar::Util::refaddr($code)} = [ | ||
| 73 | 'POE::Declare::Meta::Timeout', | ||
| 74 | delay => $delay, | ||
| 75 | ]; | ||
| 76 | return (); | ||
| 77 | } | ||
| 78 | |||
| 79 | # Unknown method type | ||
| 80 | Carp::croak("Unknown or unsupported attribute $name"); | ||
| 81 | } | ||
| 82 | |||
| 83 | =pod | ||
| 84 | |||
| 85 | =head2 meta | ||
| 86 | |||
| 87 | The C<meta> method can be run on either a class or instances of that class, | ||
| 88 | and returns the L<POE::Declare::Meta> metadata object for that class. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | # Moved to code generation | ||
| 93 | # sub meta ($) { | ||
| 94 | # POE::Declare::meta( ref $_[0] || $_[0] ); | ||
| 95 | # } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | ##################################################################### | ||
| 102 | # Constructor | ||
| 103 | |||
| 104 | =pod | ||
| 105 | |||
| 106 | =head2 new | ||
| 107 | |||
| 108 | # Create an object, but do not spawn it | ||
| 109 | my $object = My::Class->new( | ||
| 110 | Param1 => 'value', | ||
| 111 | Param2 => 'value', | ||
| 112 | ); | ||
| 113 | |||
| 114 | The C<new> constructor is used to create a L<POE::Declare> component | ||
| 115 | B<WITHOUT> immediately starting it up. | ||
| 116 | |||
| 117 | This is typically assemble to build heirachies of interlinked | ||
| 118 | components and services, without the need to start all of them | ||
| 119 | simultaneously. | ||
| 120 | |||
| 121 | Instead, a startup routine in the top object of the heirachy can | ||
| 122 | undertake a controlled startup process, bootstrapping each piece of | ||
| 123 | the overall application. | ||
| 124 | |||
| 125 | All constructors take a series of named params and return a new instance, | ||
| 126 | or throw an exception on error. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub new { | ||
| 131 | my $class = shift; | ||
| 132 | my $meta = $class->meta; | ||
| 133 | my $self = bless { }, $class; | ||
| 134 | my %param = @_; | ||
| 135 | |||
| 136 | # Check the Alias | ||
| 137 | if ( exists $param{Alias} ) { | ||
| 138 | unless ( Params::Util::_STRING($param{Alias}) ) { | ||
| 139 | Carp::croak("Did not provide a valid Alias param, must be a string"); | ||
| 140 | } | ||
| 141 | $self->{Alias} = delete $param{Alias}; | ||
| 142 | } else { | ||
| 143 | $self->{Alias} = $meta->next_alias; | ||
| 144 | } | ||
| 145 | |||
| 146 | # Check and default params | ||
| 147 | foreach ( $meta->_params ) { | ||
| 148 | next unless exists $param{$_}; | ||
| 149 | $self->{$_} = delete $param{$_}; | ||
| 150 | } | ||
| 151 | |||
| 152 | # Check for unsupported params | ||
| 153 | if ( %param ) { | ||
| 154 | my $names = join ', ', sort keys %param; | ||
| 155 | die("Unknown or unsupported $class param(s) $names"); | ||
| 156 | } | ||
| 157 | |||
| 158 | # Check and normalize message registration | ||
| 159 | foreach ( $meta->_messages ) { | ||
| 160 | next unless exists $self->{$_}; | ||
| 161 | $self->{$_} = _CALLBACK($self->{$_}); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Clear out any accidentally set internal values | ||
| 165 | delete $ID{Scalar::Util::refaddr($self)}; | ||
| 166 | |||
| 167 | $self; | ||
| 168 | } | ||
| 169 | |||
| 170 | # Check the validity of a provided message handler. | ||
| 171 | sub _CALLBACK { | ||
| 172 | my $it = $_[0]; | ||
| 173 | |||
| 174 | # The callback is an anonymous subroutine | ||
| 175 | return $it if Params::Util::_CODE($it); | ||
| 176 | |||
| 177 | # Otherwise, we also allow a reference to an array, | ||
| 178 | # which contains two identifiers (like foo_bar). | ||
| 179 | # This will be converted to a call to the relevant | ||
| 180 | # POE session.event | ||
| 181 | if ( | ||
| 182 | Params::Util::_ARRAY0($it) | ||
| 183 | and | ||
| 184 | scalar(@$it) == 2 | ||
| 185 | and | ||
| 186 | _ALIAS($it->[0]) | ||
| 187 | and | ||
| 188 | Params::Util::_IDENTIFIER($it->[1]) | ||
| 189 | ) { | ||
| 190 | # Create a closure for the call | ||
| 191 | my $session = $it->[0]; | ||
| 192 | my $event = $it->[1]; | ||
| 193 | my $closure = sub { | ||
| 194 | $poe_kernel->call( $session, $event, @_ ); | ||
| 195 | }; | ||
| 196 | return $closure; | ||
| 197 | } | ||
| 198 | |||
| 199 | # Otherwise, not valid | ||
| 200 | Carp::croak('Invalid message event handler'); | ||
| 201 | } | ||
| 202 | |||
| 203 | # Check the format of an alias | ||
| 204 | sub _ALIAS { | ||
| 205 | Params::Util::_IDENTIFIER($_[0]) | ||
| 206 | or ( | ||
| 207 | defined(Params::Util::_STRING($_[0])) | ||
| 208 | and | ||
| 209 | $_[0] =~ /\.\d+$/ | ||
| 210 | ) ? $_[0] : undef; | ||
| 211 | } | ||
| 212 | |||
| 213 | =pod | ||
| 214 | |||
| 215 | =head2 Alias | ||
| 216 | |||
| 217 | The C<Alias> method returns the L<POE::Session> alias that will be used with | ||
| 218 | this object instance. | ||
| 219 | |||
| 220 | These will typically be of the form C<'My::Class.123'> but may be a different | ||
| 221 | value if a custom C<Alias> param has been explicitly passed to the constructor. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | # This is auto-generated | ||
| 226 | # sub Alias { | ||
| 227 | # $_[0]->{Alias}; | ||
| 228 | # } | ||
| 229 | |||
| 230 | =pod | ||
| 231 | |||
| 232 | =head2 spawn | ||
| 233 | |||
| 234 | # Spawn (i.e. startup) an existing object | ||
| 235 | $object->spawn; | ||
| 236 | |||
| 237 | # Create the start the object in one call | ||
| 238 | my $alias = My::Class->spawn( | ||
| 239 | Param1 => 'value', | ||
| 240 | Param2 => 'value', | ||
| 241 | ); | ||
| 242 | |||
| 243 | The C<spawn> method is used to create the L<POE::Session> for this object. | ||
| 244 | |||
| 245 | It returns the session alias as a convenience, or throws an exception on error. | ||
| 246 | |||
| 247 | When called on the class instead of an object, it provides a shortcut method | ||
| 248 | for a one-shot construction and spawning of an object, returning the object | ||
| 249 | instead of the session alias. | ||
| 250 | |||
| 251 | Throws an exception on error. | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub spawn { | ||
| 256 | # Handle the class context | ||
| 257 | unless ( ref $_[0] ) { | ||
| 258 | my $class = shift; | ||
| 259 | my $self = $class->new( @_ ); | ||
| 260 | $self->spawn; | ||
| 261 | return $self; | ||
| 262 | } | ||
| 263 | |||
| 264 | # Create the session | ||
| 265 | my $self = shift; | ||
| 266 | my $meta = $self->meta; | ||
| 267 | POE::Session->create( | ||
| 268 | heap => $self, | ||
| 269 | package_states => [ | ||
| 270 | $meta->name => [ $meta->_package_states ], | ||
| 271 | ], | ||
| 272 | )->ID; | ||
| 273 | |||
| 274 | # Return the alias | ||
| 275 | $self->Alias; | ||
| 276 | } | ||
| 277 | |||
| 278 | =pod | ||
| 279 | |||
| 280 | =head2 spawned | ||
| 281 | |||
| 282 | The C<spawned> method returns true if the L<POE::Session> for a B<POE::Declare> | ||
| 283 | object has been created, or false if not. | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | sub spawned { | ||
| 288 | !! $ID{Scalar::Util::refaddr($_[0])}; | ||
| 289 | |||
| 290 | } | ||
| 291 | |||
| 292 | =pod | ||
| 293 | |||
| 294 | =head2 session_id | ||
| 295 | |||
| 296 | The C<session_id> accessor finds and returns the internal L<POE::Session> | ||
| 297 | id for this instance, or C<undef> if the object has not been spawned. | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub session_id { | ||
| 302 | $ID{Scalar::Util::refaddr($_[0])}; | ||
| 303 | } | ||
| 304 | |||
| 305 | =pod | ||
| 306 | |||
| 307 | =head2 session_id | ||
| 308 | |||
| 309 | The C<session_id> accessor finds and returns the internal L<POE::Session> | ||
| 310 | object for this instance, or C<undef> if the object has not been spawned. | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub session { | ||
| 315 | my $id = $ID{Scalar::Util::refaddr($_[0])} or return undef; | ||
| 316 | $poe_kernel->ID_id_to_session($id) or return undef; | ||
| 317 | } | ||
| 318 | |||
| 319 | =pod | ||
| 320 | |||
| 321 | =head2 kernel | ||
| 322 | |||
| 323 | The C<kernel> method is provided as a convenience. It returns the | ||
| 324 | L<POE::Kernel> object that objects of this class will run in. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | use constant kernel => $poe_kernel; | ||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | ##################################################################### | ||
| 335 | # POE::Session Wrappers | ||
| 336 | |||
| 337 | =pod | ||
| 338 | |||
| 339 | =head2 ID | ||
| 340 | |||
| 341 | The C<ID> is a wrapper for the equivalent L<POE::Session> method, and | ||
| 342 | returns the id number for the L<POE::Session>. | ||
| 343 | |||
| 344 | Returns an integer, or C<undef> if the heap object has not spawned. | ||
| 345 | |||
| 346 | =cut | ||
| 347 | |||
| 348 | sub ID { | ||
| 349 | $ID{Scalar::Util::refaddr($_[0])}; | ||
| 350 | } | ||
| 351 | |||
| 352 | =pod | ||
| 353 | |||
| 354 | =head2 postback | ||
| 355 | |||
| 356 | my $handler = $object->postback( | ||
| 357 | 'event_name', | ||
| 358 | $first_param, | ||
| 359 | 'second_param', | ||
| 360 | ); | ||
| 361 | $handler->( $third_param, $first_param ); | ||
| 362 | |||
| 363 | The C<postback> method is a wrapper for the equivalent L<POE::Session> | ||
| 364 | method, and creates an anonymous subroutine that triggers a C<post> for | ||
| 365 | a named event of the heap object. | ||
| 366 | |||
| 367 | Returns a C<CODE> reference, or dies if the heap object has not been | ||
| 368 | spawned. | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | sub postback { | ||
| 373 | shift->session->postback(@_); | ||
| 374 | } | ||
| 375 | |||
| 376 | =pod | ||
| 377 | |||
| 378 | =head2 callback | ||
| 379 | |||
| 380 | my $handler = $object->callback( | ||
| 381 | 'event_name', | ||
| 382 | $first_param, | ||
| 383 | 'second_param', | ||
| 384 | ); | ||
| 385 | $handler->( $third_param, $first_param ); | ||
| 386 | |||
| 387 | The C<callback> method is a wrapper for the equivalent L<POE::Session> | ||
| 388 | method, and creates an anonymous subroutine that triggers a C<post> for | ||
| 389 | a named event of the heap object. | ||
| 390 | |||
| 391 | Please don't confuse this for a method relating to "callback events" | ||
| 392 | mentioned earlier, it is not related to them. | ||
| 393 | |||
| 394 | Returns a C<CODE> reference, or dies if the heap object has not been | ||
| 395 | spawned. | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | sub callback { | ||
| 400 | shift->session->callback(@_); | ||
| 401 | } | ||
| 402 | |||
| 403 | =pod | ||
| 404 | |||
| 405 | =head2 lookback | ||
| 406 | |||
| 407 | sub create_foo { | ||
| 408 | my $self = shift; | ||
| 409 | my $thing = Other::Class->new( | ||
| 410 | ConnectEvent => $self->lookback('it_connected'), | ||
| 411 | ConnectError => $self->lookback('it_failed'), | ||
| 412 | ); | ||
| 413 | |||
| 414 | ... | ||
| 415 | } | ||
| 416 | |||
| 417 | The C<lookback> method is a safe alias for C< [ $self->Alias, 'event_name' ] >. | ||
| 418 | |||
| 419 | When creating the lookback, the name will be double checked to verify that | ||
| 420 | the handler actually exists and is registered. | ||
| 421 | |||
| 422 | Returns a reference to an C<ARRAY> containing the heap object's alias and | ||
| 423 | the event name. | ||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | sub lookback { | ||
| 428 | my $self = shift; | ||
| 429 | my $class = ref($self); | ||
| 430 | my $name = Params::Util::_IDENTIFIER($_[0]); | ||
| 431 | unless ( $name ) { | ||
| 432 | Carp::croak("Invalid identifier name '$_[0]'"); | ||
| 433 | } | ||
| 434 | |||
| 435 | # Does the event exist? | ||
| 436 | my $attr = $self->meta->attr($name); | ||
| 437 | unless ( $attr and $attr->isa('POE::Declare::Meta::Event') ) { | ||
| 438 | Carp::croak("$class does not have the event '$name'"); | ||
| 439 | } | ||
| 440 | |||
| 441 | return [ $self->Alias, $name ]; | ||
| 442 | } | ||
| 443 | |||
| 444 | |||
| 445 | |||
| 446 | |||
| 447 | |||
| 448 | ##################################################################### | ||
| 449 | # POE::Kernel Wrappers | ||
| 450 | |||
| 451 | =pod | ||
| 452 | |||
| 453 | =head2 post | ||
| 454 | |||
| 455 | The C<post> method runs a POE kernel C<post> for a named event for the | ||
| 456 | heap object's session. | ||
| 457 | |||
| 458 | Returns void. | ||
| 459 | |||
| 460 | =cut | ||
| 461 | |||
| 462 | sub post { | ||
| 463 | $poe_kernel->post( shift->Alias, @_ ); | ||
| 464 | } | ||
| 465 | |||
| 466 | =pod | ||
| 467 | |||
| 468 | =head2 call | ||
| 469 | |||
| 470 | The C<call> method runs a POE kernel C<call> for a named event for the | ||
| 471 | heap object's session. | ||
| 472 | |||
| 473 | Returns as for the particular event handler, but generally returns void. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | sub call { | ||
| 478 | $poe_kernel->call( shift->Alias, @_ ); | ||
| 479 | } | ||
| 480 | |||
| 481 | ### Wrapper for the (new) POE timer API | ||
| 482 | |||
| 483 | =pod | ||
| 484 | |||
| 485 | =head2 alarm_set | ||
| 486 | |||
| 487 | The C<alarm_set> method is equivalent to the L<POE::Kernel> method | ||
| 488 | of the same name, setting an alarm for a named event of the heap object's | ||
| 489 | session. | ||
| 490 | |||
| 491 | =cut | ||
| 492 | |||
| 493 | sub alarm_set { | ||
| 494 | shift; | ||
| 495 | $poe_kernel->alarm_set( @_ ); | ||
| 496 | } | ||
| 497 | |||
| 498 | =pod | ||
| 499 | |||
| 500 | =head2 alarm_adjust | ||
| 501 | |||
| 502 | The C<alarm_adjust> method is equivalent to the L<POE::Kernel> method | ||
| 503 | of the same name, adjusting an alarm for a named event of the heap | ||
| 504 | object's session. | ||
| 505 | |||
| 506 | =cut | ||
| 507 | |||
| 508 | sub alarm_adjust { | ||
| 509 | shift; | ||
| 510 | $poe_kernel->alarm_adjust( @_ ); | ||
| 511 | } | ||
| 512 | |||
| 513 | =pod | ||
| 514 | |||
| 515 | =head2 alarm_remove | ||
| 516 | |||
| 517 | The C<alarm_remove> method is equivalent to the L<POE::Kernel> method | ||
| 518 | of the same name, removing an alarm for a named event of the heap | ||
| 519 | object's session. | ||
| 520 | |||
| 521 | =cut | ||
| 522 | |||
| 523 | sub alarm_remove { | ||
| 524 | shift; | ||
| 525 | $poe_kernel->alarm_remove( @_ ); | ||
| 526 | } | ||
| 527 | |||
| 528 | =pod | ||
| 529 | |||
| 530 | =head2 alarm_clear | ||
| 531 | |||
| 532 | The C<alarm_clear> method is a convenience method. It takes the name of | ||
| 533 | a hash key for the object, containing a timer id. If the ID is set, it | ||
| 534 | is cleared. If not, the method shortcuts. | ||
| 535 | |||
| 536 | =cut | ||
| 537 | |||
| 538 | sub alarm_clear { | ||
| 539 | $_[0]->{$_[1]} or return 1; | ||
| 540 | $_[0]->alarm_remove(delete $_[0]->{$_[1]}); | ||
| 541 | } | ||
| 542 | |||
| 543 | =pod | ||
| 544 | |||
| 545 | =head2 delay_set | ||
| 546 | |||
| 547 | The C<delay_set> method is equivalent to the L<POE::Kernel> method | ||
| 548 | of the same name, setting a delayed alarm for a named event of the | ||
| 549 | heap object's session. | ||
| 550 | |||
| 551 | =cut | ||
| 552 | |||
| 553 | sub delay_set { | ||
| 554 | shift; | ||
| 555 | $poe_kernel->delay_set( @_ ); | ||
| 556 | } | ||
| 557 | |||
| 558 | =pod | ||
| 559 | |||
| 560 | =head2 delay_adjust | ||
| 561 | |||
| 562 | The C<delay_adjust> method is equivalent to the L<POE::Kernel> method | ||
| 563 | of the same name, adjusting a delayed alarm for a named event of the | ||
| 564 | heap object's session. | ||
| 565 | |||
| 566 | =cut | ||
| 567 | |||
| 568 | sub delay_adjust { | ||
| 569 | shift; | ||
| 570 | $poe_kernel->delay_adjust( @_ ); | ||
| 571 | } | ||
| 572 | |||
| 573 | |||
| 574 | |||
| 575 | |||
| 576 | |||
| 577 | ##################################################################### | ||
| 578 | # Events | ||
| 579 | |||
| 580 | =pod | ||
| 581 | |||
| 582 | =head1 EVENTS | ||
| 583 | |||
| 584 | The following POE events are provided for all classes | ||
| 585 | |||
| 586 | =head2 _start | ||
| 587 | |||
| 588 | The default C<_start> implementation is used to register the alias for | ||
| 589 | the heap object with the kernel. As such, if you need to do your own | ||
| 590 | tasks in C<_start> you should always call it first. | ||
| 591 | |||
| 592 | sub _start { | ||
| 593 | my $self = $_[HEAP]; | ||
| 594 | $_[0]->SUPER::_start(@_[1..$#_]); | ||
| 595 | |||
| 596 | # Additional tasks here | ||
| 597 | ... | ||
| 598 | } | ||
| 599 | |||
| 600 | Please note though that the super call will break @_ in the current | ||
| 601 | subroutine, and so you should not use C<$_[KERNEL]> style expressions | ||
| 602 | after the SUPER call. | ||
| 603 | |||
| 604 | =cut | ||
| 605 | |||
| 606 | sub _start : Event { | ||
| 607 | $ID{Scalar::Util::refaddr($_[HEAP])} = $_[SESSION]->ID; | ||
| 608 | |||
| 609 | # Cheaper to do this blind detach rather than testing first to | ||
| 610 | # make sure we have a session as a parent. | ||
| 611 | SCOPE: { | ||
| 612 | local $!; | ||
| 613 | $poe_kernel->detach_myself; | ||
| 614 | } | ||
| 615 | |||
| 616 | $poe_kernel->call( $_[SESSION], '_alias_set'); | ||
| 617 | } | ||
| 618 | |||
| 619 | =pod | ||
| 620 | |||
| 621 | =head2 _stop | ||
| 622 | |||
| 623 | The default C<_stop> implementation is used to clean up our resources | ||
| 624 | and aliases in the kernel. As such, if you need to do your own | ||
| 625 | tasks in C<_stop> you should always do them first and then call the | ||
| 626 | SUPER last. | ||
| 627 | |||
| 628 | sub _stop { | ||
| 629 | my $self = $_[HEAP]; | ||
| 630 | |||
| 631 | # Additional tasks here | ||
| 632 | ... | ||
| 633 | |||
| 634 | shift->SUPER::_stop(@_); | ||
| 635 | } | ||
| 636 | |||
| 637 | =cut | ||
| 638 | |||
| 639 | sub _stop : Event { | ||
| 640 | delete $ID{Scalar::Util::refaddr($_[HEAP])}; | ||
| 641 | } | ||
| 642 | |||
| 643 | =pod | ||
| 644 | |||
| 645 | =head2 _alias_set | ||
| 646 | |||
| 647 | During the period in which a L<POE::Declare> object is active, it will | ||
| 648 | register an alias with the L<POE> kernel (so that the session will not be | ||
| 649 | cleaned up if it has no queued events of it's own and it only waiting for | ||
| 650 | other sessions to send it a message). | ||
| 651 | |||
| 652 | The C<_alias_set> method (which takes no parameters) will set the alias | ||
| 653 | for the current object. This will be done automatically for you during | ||
| 654 | the C<spawn> process (in the C<_start> event). | ||
| 655 | |||
| 656 | =cut | ||
| 657 | |||
| 658 | sub _alias_set : Event { | ||
| 659 | ### This will fail is sessions have clashing aliases | ||
| 660 | my $alias = $_[HEAP]->Alias; | ||
| 661 | unless ( defined $poe_kernel->alias_resolve($alias) ) { | ||
| 662 | if ( $poe_kernel->alias_set($alias) ) { | ||
| 663 | # Failed to set alias | ||
| 664 | Carp::croak("Failed to set alias '$alias'"); | ||
| 665 | } | ||
| 666 | } | ||
| 667 | } | ||
| 668 | |||
| 669 | =pod | ||
| 670 | |||
| 671 | =head2 _alias_remove | ||
| 672 | |||
| 673 | Each L<POE::Declare> object has a POE session and a unique alias, which is | ||
| 674 | registered with the POE core when it is spawned. | ||
| 675 | |||
| 676 | Even if no events, timers, callbacks or handles are registered for the | ||
| 677 | session, the existance of the alias will keep the session alive. | ||
| 678 | |||
| 679 | The C<_alias_remove> event is used to trigger the stopping of the session | ||
| 680 | as soon as all currently live events have been cleared. | ||
| 681 | |||
| 682 | This method is usually called at the beginning of a shutdown process, to | ||
| 683 | indicate that the session is no longer permanent and should shutdown when | ||
| 684 | possible. | ||
| 685 | |||
| 686 | =cut | ||
| 687 | |||
| 688 | sub _alias_remove : Event { | ||
| 689 | my $self = $_[HEAP]; | ||
| 690 | my $alias = $self->Alias; | ||
| 691 | my $session = $poe_kernel->alias_resolve($alias); | ||
| 692 | my $poe_id = $session->ID; | ||
| 693 | my $self_id = $ID{Scalar::Util::refaddr($self)}; | ||
| 694 | unless ( defined $poe_id and defined $self_id ) { | ||
| 695 | return; | ||
| 696 | } | ||
| 697 | unless ( $poe_id == $self_id ) { | ||
| 698 | Carp::croak("Session id mismatch error"); | ||
| 699 | } | ||
| 700 | $poe_kernel->alias_remove($alias); | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | |||
| 705 | |||
| 706 | |||
| 707 | ##################################################################### | ||
| 708 | # Compile the POE::Declare form of POE::Declare::Object itself | ||
| 709 | |||
| 710 | POE::Declare::compile; | ||
| 711 | |||
| 712 | =pod | ||
| 713 | |||
| 714 | =head1 SUPPORT | ||
| 715 | |||
| 716 | Bugs should be always be reported via the CPAN bug tracker at | ||
| 717 | |||
| 718 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Declare> | ||
| 719 | |||
| 720 | For other issues, or commercial enhancement or support, contact the author. | ||
| 721 | |||
| 722 | =head1 AUTHORS | ||
| 723 | |||
| 724 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 725 | |||
| 726 | =head1 SEE ALSO | ||
| 727 | |||
| 728 | L<POE>, L<POE::Declare>, L<http://ali.as/> | ||
| 729 | |||
| 730 | =head1 COPYRIGHT | ||
| 731 | |||
| 732 | Copyright 2006 - 2009 Adam Kennedy. | ||
| 733 | |||
| 734 | This program is free software; you can redistribute | ||
| 735 | it and/or modify it under the same terms as Perl itself. | ||
| 736 | |||
| 737 | The full text of the license can be found in the | ||
| 738 | LICENSE file included with this module. | ||
| 739 | |||
| 740 | =cut |