| 1 | package XAS::Lib::Stomp::POE::Client; | ||
| 2 | |||
| 3 | our $VERSION = '0.03'; | ||
| 4 | |||
| 5 | use POE; | ||
| 6 | use Try::Tiny; | ||
| 7 | use XAS::Lib::Stomp::Utils; | ||
| 8 | use XAS::Lib::Stomp::POE::Filter; | ||
| 9 | |||
| 10 | use XAS::Class | ||
| 11 | debug => 0, | ||
| 12 | version => $VERSION, | ||
| 13 | base => 'XAS::Lib::Net::Client::POE', | ||
| 14 | accessors => 'stomp', | ||
| 15 | vars => { | ||
| 16 | PARAMS => { | ||
| 17 | -alias => { optional => 1, default => 'stomp-client' }, | ||
| 18 | -host => { optional => 1, default => 'localhost' }, | ||
| 19 | -port => { optional => 1, default => 61613 }, | ||
| 20 | -login => { optional => 1, default => 'guest' }, | ||
| 21 | -passcode => { optional => 1, default => 'guest' }, | ||
| 22 | -target => { optional => 1, default => '1.0', regex => qr/(1\.0|1\.1|1\.2)/ }, | ||
| 23 | } | ||
| 24 | } | ||
| 25 | ; | ||
| 26 | |||
| 27 | #use Data::Dumper; | ||
| 28 | |||
| 29 | # ---------------------------------------------------------------------- | ||
| 30 | # Public Events | ||
| 31 | # ---------------------------------------------------------------------- | ||
| 32 | |||
| 33 | # --------------------------------------------------------------------- | ||
| 34 | # Public Methods | ||
| 35 | # --------------------------------------------------------------------- | ||
| 36 | |||
| 37 | sub session_initalize { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | my $alias = $self->alias; | ||
| 41 | |||
| 42 | $self->log->debug("$alias: session_initialize()"); | ||
| 43 | |||
| 44 | # public events | ||
| 45 | |||
| 46 | $self->log->debug("$alias: doing public events"); | ||
| 47 | |||
| 48 | $poe_kernel->state('handle_noop', $self); | ||
| 49 | $poe_kernel->state('handle_error', $self); | ||
| 50 | $poe_kernel->state('handle_message', $self); | ||
| 51 | $poe_kernel->state('handle_receipt', $self); | ||
| 52 | $poe_kernel->state('handle_connected', $self); | ||
| 53 | |||
| 54 | # walk the chain | ||
| 55 | |||
| 56 | $self->SUPER::session_initialize(); | ||
| 57 | |||
| 58 | $self->log->debug("$alias: leaving session_initialize()"); | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | sub session_shutdown { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | my $alias = $self->alias; | ||
| 66 | my $frame = $self->stomp->disconnect( | ||
| 67 | -receipt => 'disconnecting' | ||
| 68 | ); | ||
| 69 | |||
| 70 | $poe_kernel->call($alias, 'write_data', $frame); | ||
| 71 | $self->SUPER::session_shutdown(); | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | # --------------------------------------------------------------------- | ||
| 76 | # Public Events | ||
| 77 | # --------------------------------------------------------------------- | ||
| 78 | |||
| 79 | sub handle_connection { | ||
| 80 | my ($self) = @_[OBJECT]; | ||
| 81 | |||
| 82 | my $alias = $self->alias; | ||
| 83 | my $frame = $self->frame->connect( | ||
| 84 | -login => $self->login, | ||
| 85 | -passcode => $self->passcode | ||
| 86 | ); | ||
| 87 | |||
| 88 | $self->log->info_msg('connected', $alias, $self->host, $self->port); | ||
| 89 | $poe_kernel->post($alias, 'write_data', $frame); | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | sub handle_connected { | ||
| 94 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 95 | |||
| 96 | my $alias = $self->alias; | ||
| 97 | |||
| 98 | $poe_kernel->post($alias, 'connection_up'); | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | sub handle_message { | ||
| 103 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | sub handle_receipt { | ||
| 108 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | sub handle_error { | ||
| 113 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 114 | |||
| 115 | my $alias = $self->alias; | ||
| 116 | |||
| 117 | $self->log->error_msg('errors', | ||
| 118 | $alias, | ||
| 119 | $frame->header->message_id, | ||
| 120 | $frame->header->message, | ||
| 121 | $frame->body | ||
| 122 | ); | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | sub handle_noop { | ||
| 127 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 | # --------------------------------------------------------------------- | ||
| 132 | # Private Events | ||
| 133 | # --------------------------------------------------------------------- | ||
| 134 | |||
| 135 | sub _server_message { | ||
| 136 | my ($self, $frame, $wheel_id) = @_[OBJECT, ARG0, ARG1]; | ||
| 137 | |||
| 138 | my $alias = $self->alias; | ||
| 139 | |||
| 140 | $self->log->debug("$alias: _server_message()"); | ||
| 141 | |||
| 142 | if ($frame->command eq 'CONNECTED') { | ||
| 143 | |||
| 144 | $self->log->debug("$alias: received a \"CONNECTED\" message"); | ||
| 145 | $poe_kernel->post($alias, 'handle_connected', $frame); | ||
| 146 | |||
| 147 | } elsif ($frame->command eq 'MESSAGE') { | ||
| 148 | |||
| 149 | $self->log->debug("$alias: received a \"MESSAGE\" message"); | ||
| 150 | $poe_kernel->post($alias, 'handle_message', $frame); | ||
| 151 | |||
| 152 | } elsif ($frame->command eq 'RECEIPT') { | ||
| 153 | |||
| 154 | $self->log->debug("$alias: received a \"RECEIPT\" message"); | ||
| 155 | $poe_kernel->post($alias, 'handle_receipt', $frame); | ||
| 156 | |||
| 157 | } elsif ($frame->command eq 'ERROR') { | ||
| 158 | |||
| 159 | $self->log->debug("$alias: received an \"ERROR\" message"); | ||
| 160 | $poe_kernel->post($alias, 'handle_error', $frame); | ||
| 161 | |||
| 162 | } elsif ($frame->command eq 'NOOP') { | ||
| 163 | |||
| 164 | $self->log->debug("$alias: received an \"NOOP\" message"); | ||
| 165 | $poe_kernel->post($alias, 'handle_noop', $frame); | ||
| 166 | |||
| 167 | } else { | ||
| 168 | |||
| 169 | $self->log->warn("$alias: unknown message type: $frame->command"); | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | # --------------------------------------------------------------------- | ||
| 176 | # Private Methods | ||
| 177 | # --------------------------------------------------------------------- | ||
| 178 | |||
| 179 | sub init { | ||
| 180 | my $class = shift; | ||
| 181 | |||
| 182 | my $self = $class->SUPER::init(@_); | ||
| 183 | |||
| 184 | $self->{stomp} = XAS::Lib::Stomp::Utils->new(); | ||
| 185 | |||
| 186 | unless defined($self->{filter}) { | ||
| 187 | |||
| 188 | $self->{filter} = XAS::Lib::Stomp::POE::Filter->new( | ||
| 189 | -target => $self->target | ||
| 190 | ); | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | return $self; | ||
| 195 | |||
| 196 | } | ||
| 197 | |||
| 198 | 1; | ||
| 199 | |||
| 200 | __END__ | ||
| 201 | |||
| 202 | =head1 NAME | ||
| 203 | |||
| 204 | XAS::Lib::Stomp::POE::Client - A STOMP client for the POE Environment | ||
| 205 | |||
| 206 | =head1 SYNOPSIS | ||
| 207 | |||
| 208 | This module is a class used to create clients that need to access a | ||
| 209 | message server that communicates with the STOMP protocol. Your program could | ||
| 210 | look as follows: | ||
| 211 | |||
| 212 | package Client; | ||
| 213 | |||
| 214 | use POE; | ||
| 215 | use XAS::Class | ||
| 216 | version => '1.0', | ||
| 217 | base => 'XAS::Lib::Stomp::POE::Client', | ||
| 218 | ; | ||
| 219 | |||
| 220 | sub handle_connection { | ||
| 221 | my ($self) = @_[OBJECT]; | ||
| 222 | |||
| 223 | my $nframe = $self->stomp->connect( | ||
| 224 | -login => 'testing', | ||
| 225 | -passcode => 'testing' | ||
| 226 | ); | ||
| 227 | |||
| 228 | $poe_kernel->yield('send_data', $nframe); | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | sub handle_connected { | ||
| 233 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 234 | |||
| 235 | my $nframe = $self->stomp->subscribe( | ||
| 236 | -queue => $self->queue, | ||
| 237 | -ack => 'client' | ||
| 238 | ); | ||
| 239 | |||
| 240 | $poe_kernel->yield('send_data', $nframe); | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | sub handle_message { | ||
| 245 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 246 | |||
| 247 | my $nframe = $self->stomp->ack( | ||
| 248 | -message_id => $frame->header->message_id | ||
| 249 | ); | ||
| 250 | |||
| 251 | $poe_kernel->yield('send_data', $nframe); | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | package main; | ||
| 256 | |||
| 257 | use POE; | ||
| 258 | use strict; | ||
| 259 | |||
| 260 | Client->new( | ||
| 261 | -alias => 'testing', | ||
| 262 | -queue => '/queue/testing', | ||
| 263 | ); | ||
| 264 | |||
| 265 | $poe_kernel->run(); | ||
| 266 | |||
| 267 | exit 0; | ||
| 268 | |||
| 269 | |||
| 270 | =head1 DESCRIPTION | ||
| 271 | |||
| 272 | This module handles the nitty-gritty details of setting up the communications | ||
| 273 | channel to a message queue server. You will need to sub-class this module | ||
| 274 | with your own for it to be useful. | ||
| 275 | |||
| 276 | An attempt to maintain that channel will be made when/if that server should | ||
| 277 | happen to disappear off the network. There is nothing more unpleasant then | ||
| 278 | having to go around to dozens of servers and restarting processes. | ||
| 279 | |||
| 280 | When messages are received, specific events are generated. Those events are | ||
| 281 | based on the message type. If you are interested in those events you should | ||
| 282 | override the default behavior for those events. The default behavior is to | ||
| 283 | do nothing. | ||
| 284 | |||
| 285 | =head1 METHODS | ||
| 286 | |||
| 287 | =head2 new | ||
| 288 | |||
| 289 | This method initializes the class and starts a session to handle the | ||
| 290 | communications channel. It takes the following parameters: | ||
| 291 | |||
| 292 | =over 4 | ||
| 293 | |||
| 294 | =item B<-alias> | ||
| 295 | |||
| 296 | The session alias, defaults to 'stomp-client'. | ||
| 297 | |||
| 298 | =item B<-server> | ||
| 299 | |||
| 300 | The servers host name, defaults to 'localhost'. | ||
| 301 | |||
| 302 | =item B<-port> | ||
| 303 | |||
| 304 | The servers port number, defaults to '61613'. | ||
| 305 | |||
| 306 | =item B<-target> | ||
| 307 | |||
| 308 | The STOMP protocol version that is targeted. Defaults to '1.0'. | ||
| 309 | |||
| 310 | =item B<-retry_count> | ||
| 311 | |||
| 312 | Wither to attempt reconnections after they run out. Defaults to true. | ||
| 313 | |||
| 314 | =item B<-enable_keepalive> | ||
| 315 | |||
| 316 | For those pesky firewalls, defaults to false | ||
| 317 | |||
| 318 | =back | ||
| 319 | |||
| 320 | =head2 send_data | ||
| 321 | |||
| 322 | You use this event to send Stomp frames to the server. | ||
| 323 | |||
| 324 | =over 4 | ||
| 325 | |||
| 326 | =item Example | ||
| 327 | |||
| 328 | $kernel->yield('send_data', $frame); | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =head2 handle_connection | ||
| 333 | |||
| 334 | This event is signaled and the corresponding method is called upon initial | ||
| 335 | connection to the message server. For the most part you should send a | ||
| 336 | "CONNECT" frame to the server. | ||
| 337 | |||
| 338 | Example | ||
| 339 | |||
| 340 | sub handle_connection { | ||
| 341 | my ($self) = @_[$OBJECT]; | ||
| 342 | |||
| 343 | my $nframe = $self->stomp->connect( | ||
| 344 | -login => 'testing', | ||
| 345 | -passcode => 'testing' | ||
| 346 | ); | ||
| 347 | |||
| 348 | $poe_kernel->yield('send_data', $nframe); | ||
| 349 | |||
| 350 | } | ||
| 351 | |||
| 352 | =head2 handled_connected | ||
| 353 | |||
| 354 | This event and corresponding method is called when a "CONNECT" frame is | ||
| 355 | received from the server. This means the server will allow you to start | ||
| 356 | generating/processing frames. | ||
| 357 | |||
| 358 | Example | ||
| 359 | |||
| 360 | sub handle_connected { | ||
| 361 | my ($self, $frame) = @_[OBJECT,ARG0]; | ||
| 362 | |||
| 363 | my $nframe = $self->stomp->subscribe( | ||
| 364 | -queue => $self->queue, | ||
| 365 | -ack => 'client' | ||
| 366 | ); | ||
| 367 | |||
| 368 | $poe_kernel->yield('send_data', $nframe); | ||
| 369 | |||
| 370 | } | ||
| 371 | |||
| 372 | This example shows you how to subscribe to a particular queue. The queue name | ||
| 373 | was passed as a parameter to new(). | ||
| 374 | |||
| 375 | =head2 handle_message | ||
| 376 | |||
| 377 | This event and corresponding method is used to process "MESSAGE" frames. | ||
| 378 | |||
| 379 | Example | ||
| 380 | |||
| 381 | sub handle_message { | ||
| 382 | my ($self, $frame) = @_[OBJECT,ARG0]; | ||
| 383 | |||
| 384 | my $nframe = $self->stomp->ack( | ||
| 385 | -message_id => $frame->header->message_id | ||
| 386 | ); | ||
| 387 | |||
| 388 | $poe_kernel->yield('send_data', $nframe); | ||
| 389 | |||
| 390 | } | ||
| 391 | |||
| 392 | This example really doesn't do much other then "ack" the messages that are | ||
| 393 | received. | ||
| 394 | |||
| 395 | =head2 handle_receipt | ||
| 396 | |||
| 397 | This event and corresponding method is used to process "RECEIPT" frames. | ||
| 398 | |||
| 399 | Example | ||
| 400 | |||
| 401 | sub handle_receipt { | ||
| 402 | my ($self, $frame) = @_[OBJECT,ARG0]; | ||
| 403 | |||
| 404 | my $receipt = $frame->header->receipt; | ||
| 405 | |||
| 406 | } | ||
| 407 | |||
| 408 | This example really doesn't do much, and you really don't need to worry about | ||
| 409 | receipts unless you ask for one when you send a frame to the server. So this | ||
| 410 | method could be safely left with the default. | ||
| 411 | |||
| 412 | =head2 handle_error | ||
| 413 | |||
| 414 | This event and corresponding method is used to process "ERROR" frames. | ||
| 415 | |||
| 416 | Example | ||
| 417 | |||
| 418 | sub handle_error { | ||
| 419 | my ($self, $frame) = @_[OBJECT,ARG0]; | ||
| 420 | |||
| 421 | } | ||
| 422 | |||
| 423 | This example really doesn't do much. Error handling is pretty much what the | ||
| 424 | process needs to do when something unexpected happens. | ||
| 425 | |||
| 426 | =head2 handle_noop | ||
| 427 | |||
| 428 | This event and corresponding method is used to process "NOOP" frames. | ||
| 429 | |||
| 430 | Example | ||
| 431 | |||
| 432 | sub handle_noop { | ||
| 433 | my ($self, $frame) = @_[OBJECT,ARG0]; | ||
| 434 | |||
| 435 | } | ||
| 436 | |||
| 437 | This example really doesn't do much. | ||
| 438 | |||
| 439 | =head2 gather_data | ||
| 440 | |||
| 441 | This event and corresponding method is used to "gather data". How that is done | ||
| 442 | is up to your program. But usually a "send_data" event is generated. | ||
| 443 | |||
| 444 | Example | ||
| 445 | |||
| 446 | sub gather_data { | ||
| 447 | my ($self) = @_[OBJECT]; | ||
| 448 | |||
| 449 | # doing something here | ||
| 450 | |||
| 451 | $poe_kernel->yield('send_data', $frame); | ||
| 452 | |||
| 453 | } | ||
| 454 | |||
| 455 | =head2 connection_down | ||
| 456 | |||
| 457 | This event and corresponding method is a hook to allow you to be notified if | ||
| 458 | the connection to the server is currently down. By default it does nothing. | ||
| 459 | But it would be useful to notify "gather_data" to temporarily stop doing | ||
| 460 | whatever it is currently doing. | ||
| 461 | |||
| 462 | Example | ||
| 463 | |||
| 464 | sub connection_down { | ||
| 465 | my ($self) = @_[OBJECT]; | ||
| 466 | |||
| 467 | # do something here | ||
| 468 | |||
| 469 | } | ||
| 470 | |||
| 471 | =head2 connection_up | ||
| 472 | |||
| 473 | This event and corresponding method is a hook to allow you to be notified | ||
| 474 | when the connection to the server up. By default it does nothing. | ||
| 475 | But it would be useful to notify "gather_data" to start doing | ||
| 476 | whatever it supposed to do. | ||
| 477 | |||
| 478 | Example | ||
| 479 | |||
| 480 | sub connection_up { | ||
| 481 | my ($self) = @_[OBJECT]; | ||
| 482 | |||
| 483 | # do something here | ||
| 484 | |||
| 485 | } | ||
| 486 | |||
| 487 | =head2 session_cleanup | ||
| 488 | |||
| 489 | This method is a hook and should be overridden to do "shutdown" stuff. By | ||
| 490 | default it sends a "DISCONNECT" message to the message queue server. | ||
| 491 | |||
| 492 | Example | ||
| 493 | |||
| 494 | sub session_cleanup { | ||
| 495 | my $self = shift; | ||
| 496 | |||
| 497 | # do something here | ||
| 498 | |||
| 499 | $self->SUPER::session_cleanup(); | ||
| 500 | |||
| 501 | } | ||
| 502 | |||
| 503 | =head2 session_reload | ||
| 504 | |||
| 505 | This method is a hook and should be overridden to do "reload" stuff. By | ||
| 506 | default it executes POE's sig_handled() method. | ||
| 507 | |||
| 508 | Example | ||
| 509 | |||
| 510 | sub session_reload { | ||
| 511 | my $self = shift; | ||
| 512 | |||
| 513 | $poe_kernel->sig_handled(); | ||
| 514 | |||
| 515 | } | ||
| 516 | |||
| 517 | =head1 ACCESSORS | ||
| 518 | |||
| 519 | =head2 stomp | ||
| 520 | |||
| 521 | This returns an object to the internal XAS::Lib::Stomp::Utils | ||
| 522 | object. This is very useful for creating STOMP frames. | ||
| 523 | |||
| 524 | Example | ||
| 525 | |||
| 526 | $frame = $self->stomp->connect( | ||
| 527 | -login => 'testing', | ||
| 528 | -passcode => 'testing' | ||
| 529 | ); | ||
| 530 | |||
| 531 | $poe_kernel->yield('send_data', $frame); | ||
| 532 | |||
| 533 | =head1 SEE ALSO | ||
| 534 | |||
| 535 | =over 4 | ||
| 536 | |||
| 537 | =item L<XAS|XAS> | ||
| 538 | |||
| 539 | =back | ||
| 540 | |||
| 541 | For details on the protocol see L<http://stomp.github.io/>. | ||
| 542 | |||
| 543 | =head1 AUTHOR | ||
| 544 | |||
| 545 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 546 | |||
| 547 | =head1 COPYRIGHT AND LICENSE | ||
| 548 | |||
| 549 | Copyright (C) 2014 Kevin L. Esteb | ||
| 550 | |||
| 551 | This library is free software; you can redistribute it and/or modify | ||
| 552 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 553 | at your option, any later version of Perl 5 you may have available. | ||
| 554 | |||
| 555 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 556 | |||
| 557 | =cut |