| 1 | package XAS::Lib::Net::POE::Client; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use POE; | ||
| 6 | use Try::Tiny; | ||
| 7 | use Socket ':all'; | ||
| 8 | use POE::Filter::Line; | ||
| 9 | use POE::Wheel::ReadWrite; | ||
| 10 | use POE::Wheel::SocketFactory; | ||
| 11 | |||
| 12 | use XAS::Class | ||
| 13 | debug => 0, | ||
| 14 | version => $VERSION, | ||
| 15 | base => 'XAS::Lib::Service', | ||
| 16 | mixin => 'XAS::Lib::Mixins::Keepalive', | ||
| 17 | accessors => 'wheel host port listener', | ||
| 18 | vars => { | ||
| 19 | PARAMS => { | ||
| 20 | -host => 1, | ||
| 21 | -port => 1, | ||
| 22 | -retry_reconnect => { optional => 1, default => 1 }, | ||
| 23 | -tcp_keepalive => { optional => 1, default => 0 }, | ||
| 24 | -filter => { optional => 1, default => undef }, | ||
| 25 | -alias => { optional => 1, default => 'client' }, | ||
| 26 | -eol => { optional => 1, default => "\012\015" }, | ||
| 27 | } | ||
| 28 | } | ||
| 29 | ; | ||
| 30 | |||
| 31 | our @ERRORS = qw(0 32 68 73 78 79 110 104 111); | ||
| 32 | our @RECONNECTIONS = qw(60 120 240 480 960 1920 3840); | ||
| 33 | |||
| 34 | #use Data::Dumper; | ||
| 35 | |||
| 36 | # ---------------------------------------------------------------------- | ||
| 37 | # Public Events | ||
| 38 | # ---------------------------------------------------------------------- | ||
| 39 | |||
| 40 | # --------------------------------------------------------------------- | ||
| 41 | # Public Methods | ||
| 42 | # --------------------------------------------------------------------- | ||
| 43 | |||
| 44 | sub session_intialize { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | my $alias = $self->alias; | ||
| 48 | |||
| 49 | $self->log->debug("$alias: entering session_initialize()"); | ||
| 50 | |||
| 51 | # private events | ||
| 52 | |||
| 53 | $self->log->debug("$alias: doing private events"); | ||
| 54 | |||
| 55 | # private events | ||
| 56 | |||
| 57 | $poe_kernel->state('server_connected', $self, '_server_connected'); | ||
| 58 | $poe_kernel->state('server_connect', $self, '_server_connect'); | ||
| 59 | $poe_kernel->state('server_error', $self, '_server_error'); | ||
| 60 | $poe_kernel->state('server_message', $self, '_server_message'); | ||
| 61 | |||
| 62 | # public events | ||
| 63 | |||
| 64 | $self->log->debug("$alias: doing public events"); | ||
| 65 | |||
| 66 | $poe_kernel->state('read_data', $self); | ||
| 67 | $poe_kernel->state('write_data', $self); | ||
| 68 | $poe_kernel->state('connection_up', $self); | ||
| 69 | $poe_kernel->state('connection_down', $self); | ||
| 70 | $poe_kernel->state('handle_connection', $self); | ||
| 71 | |||
| 72 | # walk the chain | ||
| 73 | |||
| 74 | $self->SUPER::session_initialize(); | ||
| 75 | |||
| 76 | $self->log->debug("$alias: leaving session_initialize()"); | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | sub session_startup { | ||
| 81 | my ($self) = @_[OBJECT]; | ||
| 82 | |||
| 83 | my $alias = $self->alias; | ||
| 84 | |||
| 85 | $self->log->debug("$alias: session_startup"); | ||
| 86 | |||
| 87 | $poe_kernel->post($alias, 'server_connect'); | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | sub session_shutdown { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | $self->{wheel} = undef; | ||
| 95 | $self->{listener} = undef; | ||
| 96 | |||
| 97 | # walk the chain | ||
| 98 | |||
| 99 | $self->SUPER::session_shutdown(); | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | sub session_pause { | ||
| 104 | my ($self) = @_[OBJECT]; | ||
| 105 | |||
| 106 | my $alias = $self->alias; | ||
| 107 | |||
| 108 | $self->log->debug("$alias: session_pause"); | ||
| 109 | |||
| 110 | $poe_kernel->call($alias, 'connection_down'); | ||
| 111 | |||
| 112 | } | ||
| 113 | |||
| 114 | sub session_resume { | ||
| 115 | my ($self) = @_[OBJECT]; | ||
| 116 | |||
| 117 | my $alias = $self->alias; | ||
| 118 | |||
| 119 | $self->log->debug("$alias: session_resume"); | ||
| 120 | |||
| 121 | $poe_kernel->call($alias, 'connection_up'); | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | # --------------------------------------------------------------------- | ||
| 126 | # Public Events | ||
| 127 | # --------------------------------------------------------------------- | ||
| 128 | |||
| 129 | sub handle_connection { | ||
| 130 | my ($self) = @_[OBJECT]; | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | sub connection_down { | ||
| 135 | my ($self) = @_[OBJECT]; | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | sub connection_up { | ||
| 140 | my ($self) = @_[OBJECT]; | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | sub read_data { | ||
| 145 | my ($self, $data) = @_[OBJECT,ARG0]; | ||
| 146 | |||
| 147 | my $alias = $self->alias; | ||
| 148 | |||
| 149 | $poe_kernel->post($alias, 'write_data', $data); | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | sub write_data { | ||
| 154 | my ($self, $data) = @_[OBJECT, ARG0]; | ||
| 155 | |||
| 156 | my @packet; | ||
| 157 | |||
| 158 | push(@packet, $data); | ||
| 159 | |||
| 160 | if (my $wheel = $self->wheel) { | ||
| 161 | |||
| 162 | $wheel->put(@packet); | ||
| 163 | |||
| 164 | } | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | # --------------------------------------------------------------------- | ||
| 169 | # Private Events | ||
| 170 | # --------------------------------------------------------------------- | ||
| 171 | |||
| 172 | sub _server_message { | ||
| 173 | my ($self, $data, $wheel_id) = @_[OBJECT, ARG0, ARG1]; | ||
| 174 | |||
| 175 | my $alias = $self->alias; | ||
| 176 | |||
| 177 | $self->log->debug("$alias: _server_message()"); | ||
| 178 | |||
| 179 | $poe_kernel->post($alias, 'read_data', $data); | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | sub _server_connected { | ||
| 184 | my ($self, $socket, $peeraddr, $peerport, $wheel_id) = @_[OBJECT,ARG0..ARG3]; | ||
| 185 | |||
| 186 | my $alias = $self->alias; | ||
| 187 | |||
| 188 | $self->log->debug("$alias: _server_connected()"); | ||
| 189 | |||
| 190 | if ($self->tcp_keepalive) { | ||
| 191 | |||
| 192 | $self->log->debug("$alias: keepalive activated"); | ||
| 193 | |||
| 194 | $self->enable_keepalive($socket); | ||
| 195 | |||
| 196 | } | ||
| 197 | |||
| 198 | my $wheel = POE::Wheel::ReadWrite->new( | ||
| 199 | Handle => $socket, | ||
| 200 | Filter => $self->filter, | ||
| 201 | InputEvent => 'server_message', | ||
| 202 | ErrorEvent => 'server_error', | ||
| 203 | ); | ||
| 204 | |||
| 205 | my $host = gethostbyaddr($peeraddr, AF_INET); | ||
| 206 | |||
| 207 | $self->{attempts} = 0; | ||
| 208 | $self->{wheel} = $wheel; | ||
| 209 | $self->{host} = $host; | ||
| 210 | $self->{port} = $peerport; | ||
| 211 | |||
| 212 | $poe_kernel->post($alias, 'handle_connection'); | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | sub _server_connect { | ||
| 217 | my ($self) = @_[OBJECT]; | ||
| 218 | |||
| 219 | my $alias = $self->alias; | ||
| 220 | |||
| 221 | $self->log->debug("$alias: _server_connect()"); | ||
| 222 | |||
| 223 | $self->{listner} = POE::Wheel::SocketFactory->new( | ||
| 224 | RemoteAddress => $self->host, | ||
| 225 | RemotePort => $self->port, | ||
| 226 | SocketType => SOCK_STREAM, | ||
| 227 | SocketDomain => AF_INET, | ||
| 228 | Reuse => 'no', | ||
| 229 | SocketProtocol => 'tcp', | ||
| 230 | SuccessEvent => 'server_connected', | ||
| 231 | FailureEvent => 'server_connection_failed', | ||
| 232 | ); | ||
| 233 | |||
| 234 | } | ||
| 235 | |||
| 236 | sub _server_connection_failed { | ||
| 237 | my ($self, $operation, $errnum, $errstr, $wheel_id) = @_[OBJECT,ARG0..ARG3]; | ||
| 238 | |||
| 239 | my $alias = $self->alias; | ||
| 240 | |||
| 241 | $self->log->debug("$alias: _server_connection_failed()"); | ||
| 242 | $self->log->error("$alias: operation: $operation; reason: $errnum - $errstr"); | ||
| 243 | |||
| 244 | delete $self->{listner}; | ||
| 245 | delete $self->{wheel}; | ||
| 246 | |||
| 247 | foreach my $error (@ERRORS) { | ||
| 248 | |||
| 249 | $self->_reconnect($kernel) if ($errnum == $error); | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | sub _server_error { | ||
| 256 | my ($self, $operation, $errnum, $errstr, $wheel_id) = @_[OBJECT,ARG0..ARG3]; | ||
| 257 | |||
| 258 | my $alias = $self->alias; | ||
| 259 | |||
| 260 | $self->log->debug("$alias: _server_error()"); | ||
| 261 | $self->log->error("$alias: operation: $operation; reason: $errnum - $errstr"); | ||
| 262 | |||
| 263 | delete $self->{listner}; | ||
| 264 | delete $self->{wheel}; | ||
| 265 | |||
| 266 | $poe_kernel->post($alias, 'connection_down'); | ||
| 267 | |||
| 268 | foreach my $error (@ERRORS) { | ||
| 269 | |||
| 270 | $self->_reconnect($kernel) if ($errnum == $error); | ||
| 271 | |||
| 272 | } | ||
| 273 | |||
| 274 | } | ||
| 275 | |||
| 276 | # --------------------------------------------------------------------- | ||
| 277 | # Private Methods | ||
| 278 | # --------------------------------------------------------------------- | ||
| 279 | |||
| 280 | sub init { | ||
| 281 | my $class = shift; | ||
| 282 | |||
| 283 | my $self = $class->SUPER::init(@_); | ||
| 284 | |||
| 285 | $self->{attempts} = 0; | ||
| 286 | $self->{count} = scalar(@RECONNECTIONS); | ||
| 287 | |||
| 288 | $self->init_keepalive(); # init tcp keepalive definations | ||
| 289 | |||
| 290 | unless (defined($self->{filter})) { | ||
| 291 | |||
| 292 | $self->{filter} = POE::Filter::Line->new( | ||
| 293 | InputLiteral => $self->eol, | ||
| 294 | OutputLiteral => $self->eol, | ||
| 295 | ); | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | return $self; | ||
| 300 | |||
| 301 | } | ||
| 302 | |||
| 303 | sub _reconnect { | ||
| 304 | my ($self) = shift; | ||
| 305 | |||
| 306 | my $retry; | ||
| 307 | my $alias = $self->alias; | ||
| 308 | |||
| 309 | $self->log->debug("$alias: attempts: $self->{attempts}, count: $self->{count}"); | ||
| 310 | |||
| 311 | if ($self->{attempts} < $self->{count}) { | ||
| 312 | |||
| 313 | my $delay = $RECONNECTIONS[$self->{attempts}]; | ||
| 314 | $self->log->warn("$alias: attempting reconnection: $self->{attempts}, waiting: $delay seconds"); | ||
| 315 | $self->{attempts} += 1; | ||
| 316 | $poe_kernel->delay('server_connect', $delay); | ||
| 317 | |||
| 318 | } else { | ||
| 319 | |||
| 320 | $retry = $self->retry_reconnect || 0; | ||
| 321 | |||
| 322 | if ($retry) { | ||
| 323 | |||
| 324 | $self->log->warn("$alias: cycling reconnection attempts, but not shutting down..."); | ||
| 325 | $self->{attempts} = 0; | ||
| 326 | $poe_kernel->post($alias, 'server_connect'); | ||
| 327 | |||
| 328 | } else { | ||
| 329 | |||
| 330 | $self->log->warn("$alias: shutting down, to many reconnection attempts"); | ||
| 331 | $poe_kernel->post($alias, 'shutdown'); | ||
| 332 | |||
| 333 | } | ||
| 334 | |||
| 335 | } | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | 1; | ||
| 340 | |||
| 341 | __END__ | ||
| 342 | |||
| 343 | =head1 NAME | ||
| 344 | |||
| 345 | XAS::Lib::Net::POE::Client - An asynchronous network client based on POE | ||
| 346 | |||
| 347 | =head1 SYNOPSIS | ||
| 348 | |||
| 349 | This module is a class used to create network clients. | ||
| 350 | |||
| 351 | package Client; | ||
| 352 | |||
| 353 | use POE; | ||
| 354 | use XAS::Class | ||
| 355 | version => '1.0', | ||
| 356 | base => 'XAS::Lib::Net::POE::Client' | ||
| 357 | ; | ||
| 358 | |||
| 359 | sub handle_connection { | ||
| 360 | my ($self) = @_[OBJECT]; | ||
| 361 | |||
| 362 | my $packet = "hello!"; | ||
| 363 | |||
| 364 | $poe_kernel->yield('write_data', $packet); | ||
| 365 | |||
| 366 | } | ||
| 367 | |||
| 368 | =head1 DESCRIPTION | ||
| 369 | |||
| 370 | This module handles the nitty-gritty details of setting up the communications | ||
| 371 | channel to a server. You will need to sub-class this module with your own for | ||
| 372 | it to be useful. | ||
| 373 | |||
| 374 | An attempt to maintain that channel will be made when/if that server should | ||
| 375 | happen to disappear off the network. There is nothing more unpleasant then | ||
| 376 | having to go around to dozens of servers and restarting processes. | ||
| 377 | |||
| 378 | =head1 METHODS | ||
| 379 | |||
| 380 | =head2 new | ||
| 381 | |||
| 382 | This method initializes the class and starts a session to handle the | ||
| 383 | communications channel. It takes the following parameters: | ||
| 384 | |||
| 385 | =over 4 | ||
| 386 | |||
| 387 | =item B<-alias> | ||
| 388 | |||
| 389 | The session alias, defaults to 'client'. | ||
| 390 | |||
| 391 | =item B<-server> | ||
| 392 | |||
| 393 | The servers host name. | ||
| 394 | |||
| 395 | =item B<-port> | ||
| 396 | |||
| 397 | The servers port number. | ||
| 398 | |||
| 399 | =item B<-retry_count> | ||
| 400 | |||
| 401 | Wither to attempt reconnections after they run out. Defaults to true. | ||
| 402 | |||
| 403 | =item B<-tcp_keepalive> | ||
| 404 | |||
| 405 | For those pesky firewalls, defaults to false | ||
| 406 | |||
| 407 | =back | ||
| 408 | |||
| 409 | =head2 read_data | ||
| 410 | |||
| 411 | This event is triggered when data is received for the server. | ||
| 412 | |||
| 413 | =head2 write_data | ||
| 414 | |||
| 415 | You use this event to send data to the server. | ||
| 416 | |||
| 417 | =head2 handle_connection | ||
| 418 | |||
| 419 | This event is triggered upon initial connection to the server. | ||
| 420 | |||
| 421 | =head2 connection_down | ||
| 422 | |||
| 423 | This event is triggered to allow you to be notified if | ||
| 424 | the connection to the server is currently down. | ||
| 425 | |||
| 426 | =head2 connection_up | ||
| 427 | |||
| 428 | This event is triggered to allow you to be notified when the connection | ||
| 429 | to the server is restored. | ||
| 430 | |||
| 431 | =head1 SEE ALSO | ||
| 432 | |||
| 433 | =over 4 | ||
| 434 | |||
| 435 | =item L<XAS|XAS> | ||
| 436 | |||
| 437 | =back | ||
| 438 | |||
| 439 | =head1 AUTHOR | ||
| 440 | |||
| 441 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 442 | |||
| 443 | =head1 COPYRIGHT AND LICENSE | ||
| 444 | |||
| 445 | Copyright (C) 2014 Kevin L. Esteb | ||
| 446 | |||
| 447 | This library is free software; you can redistribute it and/or modify | ||
| 448 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 449 | at your option, any later version of Perl 5 you may have available. | ||
| 450 | |||
| 451 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 452 | |||
| 453 | =cut |