| 1 | # Declare our package | ||
| 2 | package POE::Component::Lightspeed::Server; | ||
| 3 | |||
| 4 | # Standard stuff to catch errors | ||
| 5 | use strict qw(subs vars refs); # Make sure we can't mess up | ||
| 6 | use warnings FATAL => 'all'; # Enable warnings to catch errors | ||
| 7 | |||
| 8 | # Initialize our version | ||
| 9 | our $VERSION = '1.' . sprintf( "%04d", (qw($Revision: 1082 $))[1] ); | ||
| 10 | |||
| 11 | # Set some constants | ||
| 12 | BEGIN { | ||
| 13 | # Debug fun! | ||
| 14 | if ( ! defined &DEBUG ) { | ||
| 15 | eval "sub DEBUG () { 0 }"; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | # Import what we need | ||
| 20 | use Carp qw( croak ); | ||
| 21 | use Socket qw( inet_ntoa ); | ||
| 22 | use Time::HiRes qw( gettimeofday ); | ||
| 23 | use POE; | ||
| 24 | use POE::Wheel::SocketFactory; | ||
| 25 | use POE::Wheel::ReadWrite; | ||
| 26 | use POE::Driver::SysRW; | ||
| 27 | use POE::Filter::Line; | ||
| 28 | use POE::Filter::Reference; | ||
| 29 | use POE::Component::Lightspeed::Router; | ||
| 30 | use POE::Component::Lightspeed::Constants qw( MSG_TIMESTAMP MSG_ACTION ); | ||
| 31 | |||
| 32 | # Spawn the Router session | ||
| 33 | POE::Component::Lightspeed::Router->spawn(); | ||
| 34 | |||
| 35 | # Spawns an instance of the server | ||
| 36 | sub spawn { | ||
| 37 | # Get the OOP's type | ||
| 38 | my $type = shift; | ||
| 39 | |||
| 40 | # Sanity checking | ||
| 41 | if ( @_ & 1 ) { | ||
| 42 | croak( 'POE::Component::Lightspeed::Server->spawn needs even number of options' ); | ||
| 43 | } | ||
| 44 | |||
| 45 | # The options hash | ||
| 46 | my %opt = @_; | ||
| 47 | |||
| 48 | # Our own options | ||
| 49 | my ( $ALIAS, $KERNEL, $ADDRESS, $PORT, $SERIALIZERS, $COMPRESSION, $SSLKEYCERT, $PASSWORD, $AUTHCLIENT ); | ||
| 50 | |||
| 51 | # Get the session alias | ||
| 52 | if ( exists $opt{'ALIAS'} and defined $opt{'ALIAS'} and length( $opt{'ALIAS'} ) ) { | ||
| 53 | $ALIAS = delete $opt{'ALIAS'}; | ||
| 54 | } else { | ||
| 55 | # Debugging info... | ||
| 56 | if ( DEBUG ) { | ||
| 57 | warn 'Using default ALIAS = lightspeed_server'; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Set the default | ||
| 61 | $ALIAS = 'lightspeed_server'; | ||
| 62 | |||
| 63 | # Remove any lingering ALIAS | ||
| 64 | if ( exists $opt{'ALIAS'} ) { | ||
| 65 | delete $opt{'ALIAS'}; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | # Get the KERNEL | ||
| 70 | if ( exists $opt{'KERNEL'} and defined $opt{'KERNEL'} and length( $opt{'KERNEL'} ) ) { | ||
| 71 | $KERNEL = delete $opt{'KERNEL'}; | ||
| 72 | } else { | ||
| 73 | # Debugging info... | ||
| 74 | if ( DEBUG ) { | ||
| 75 | warn 'Using default KERNEL = Supplied by POE'; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Set the default | ||
| 79 | $KERNEL = $poe_kernel->ID; | ||
| 80 | |||
| 81 | if ( exists $opt{'KERNEL'} ) { | ||
| 82 | delete $opt{'KERNEL'}; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | # Get the ADDRESS | ||
| 87 | if ( exists $opt{'ADDRESS'} and defined $opt{'ADDRESS'} and length( $opt{'ADDRESS'} ) ) { | ||
| 88 | $ADDRESS = delete $opt{'ADDRESS'}; | ||
| 89 | } else { | ||
| 90 | warn 'POE::Component::Lightspeed::Server->spawn must get at least 1 argument - the address to connect!'; | ||
| 91 | return undef; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Get the PORT | ||
| 95 | if ( exists $opt{'PORT'} and defined $opt{'PORT'} and length( $opt{'PORT'} ) ) { | ||
| 96 | $PORT = delete $opt{'PORT'}; | ||
| 97 | } else { | ||
| 98 | # Debugging info... | ||
| 99 | if ( DEBUG ) { | ||
| 100 | warn 'Using default PORT = 9876'; | ||
| 101 | } | ||
| 102 | |||
| 103 | # Set the default | ||
| 104 | $PORT = 9876; | ||
| 105 | |||
| 106 | if ( exists $opt{'PORT'} ) { | ||
| 107 | delete $opt{'PORT'}; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | # Get the SSLKEYCERT | ||
| 112 | if ( exists $opt{'SSLKEYCERT'} and defined $opt{'SSLKEYCERT'} ) { | ||
| 113 | # Test if it is an array | ||
| 114 | if ( ref( $opt{'SSLKEYCERT'} ) eq 'ARRAY' and scalar( @{ $opt{'SSLKEYCERT'} } ) == 2 ) { | ||
| 115 | $SSLKEYCERT = $opt{'SSLKEYCERT'}; | ||
| 116 | delete $opt{'SSLKEYCERT'}; | ||
| 117 | |||
| 118 | # Okay, pull in what is necessary | ||
| 119 | eval { | ||
| 120 | use POE::Component::SSLify qw( SSLify_Options Server_SSLify ); | ||
| 121 | SSLify_Options( @$SSLKEYCERT ); | ||
| 122 | }; | ||
| 123 | if ( $@ ) { | ||
| 124 | if ( DEBUG ) { | ||
| 125 | warn "Unable to load POE::Component::SSLify -> $@"; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Force ourself to not use SSL | ||
| 129 | $SSLKEYCERT = undef; | ||
| 130 | } | ||
| 131 | } else { | ||
| 132 | if ( DEBUG ) { | ||
| 133 | warn 'The SSLKEYCERT option must be an array with exactly 2 elements in it!'; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } else { | ||
| 137 | # Debugging info... | ||
| 138 | if ( DEBUG ) { | ||
| 139 | warn 'Using default SSLKEYCERT = undef'; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Set the default | ||
| 143 | $SSLKEYCERT = undef; | ||
| 144 | |||
| 145 | if ( exists $opt{'SSLKEYCERT'} ) { | ||
| 146 | delete $opt{'SSLKEYCERT'}; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | # Get the PASSWORD | ||
| 151 | if ( exists $opt{'PASSWORD'} and defined $opt{'PASSWORD'} and length( $opt{'PASSWORD'} ) ) { | ||
| 152 | $PASSWORD = delete $opt{'PASSWORD'}; | ||
| 153 | } else { | ||
| 154 | # Debugging info... | ||
| 155 | if ( DEBUG ) { | ||
| 156 | warn 'Using default PASSWORD = undef'; | ||
| 157 | } | ||
| 158 | |||
| 159 | # Set the default | ||
| 160 | $PASSWORD = undef; | ||
| 161 | |||
| 162 | if ( exists $opt{'PASSWORD'} ) { | ||
| 163 | delete $opt{'PASSWORD'}; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # Get the AUTHCLIENT | ||
| 168 | if ( exists $opt{'AUTHCLIENT'} and defined $opt{'AUTHCLIENT'} and ref( $opt{'AUTHCLIENT'} ) and ref( $opt{'AUTHCLIENT'} ) eq 'CODE' ) { | ||
| 169 | $AUTHCLIENT = delete $opt{'AUTHCLIENT'}; | ||
| 170 | } else { | ||
| 171 | # Debugging info... | ||
| 172 | if ( DEBUG ) { | ||
| 173 | warn 'Using default AUTHCLIENT = undef'; | ||
| 174 | } | ||
| 175 | |||
| 176 | # Set the default | ||
| 177 | $AUTHCLIENT = undef; | ||
| 178 | |||
| 179 | if ( exists $opt{'AUTHCLIENT'} ) { | ||
| 180 | delete $opt{'AUTHCLIENT'}; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | # Create the POE Session! | ||
| 185 | POE::Session->create( | ||
| 186 | 'inline_states' => { | ||
| 187 | # Generic stuff | ||
| 188 | '_start' => \&StartServer, | ||
| 189 | '_stop' => sub {}, | ||
| 190 | |||
| 191 | # Public interface | ||
| 192 | 'shutdown' => \&StopServer, | ||
| 193 | |||
| 194 | # SocketFactory events | ||
| 195 | 'GotConnection' => \&GotConnection, | ||
| 196 | 'SFError' => \&SFError, | ||
| 197 | |||
| 198 | # ReadWrite events | ||
| 199 | 'InputLine' => \&InputLine, | ||
| 200 | 'InputHash' => \&InputHash, | ||
| 201 | 'RWFlushed' => \&RWFlush, | ||
| 202 | 'RWError' => \&RWError, | ||
| 203 | |||
| 204 | # Router events | ||
| 205 | 'send' => \&Send_Packet, | ||
| 206 | 'killclient' => \&KillClient, | ||
| 207 | }, | ||
| 208 | |||
| 209 | # Our own heap | ||
| 210 | 'heap' => { | ||
| 211 | 'ALIAS' => $ALIAS, | ||
| 212 | 'ADDRESS' => $ADDRESS, | ||
| 213 | 'PORT' => $PORT, | ||
| 214 | 'SSLKEYCERT' => $SSLKEYCERT, | ||
| 215 | 'PASSWORD' => $PASSWORD, | ||
| 216 | 'AUTHCLIENT' => $AUTHCLIENT, | ||
| 217 | |||
| 218 | # Our ReadWrite wheels here | ||
| 219 | 'RW' => {}, | ||
| 220 | |||
| 221 | # The socketfactory itself | ||
| 222 | 'SF' => undef, | ||
| 223 | |||
| 224 | # Our kernel name | ||
| 225 | 'MYKERNEL' => $KERNEL, | ||
| 226 | |||
| 227 | # Are we shutting down? | ||
| 228 | 'SHUTDOWN' => 0, | ||
| 229 | }, | ||
| 230 | ) or die 'Unable to create a new session!'; | ||
| 231 | |||
| 232 | # Return success | ||
| 233 | return 1; | ||
| 234 | } | ||
| 235 | |||
| 236 | # Starts listening for other lightspeed connections | ||
| 237 | sub StartServer { | ||
| 238 | # Okay, can we take the alias? | ||
| 239 | if ( ! $_[KERNEL]->_resolve_session( $_[HEAP]->{'ALIAS'} ) ) { | ||
| 240 | # Set it! | ||
| 241 | $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} ); | ||
| 242 | } else { | ||
| 243 | # Loop over fake numbers | ||
| 244 | my $num = 0; | ||
| 245 | while ( ++$num ) { | ||
| 246 | if ( ! $_[KERNEL]->_resolve_session( $_[HEAP]->{'ALIAS'} . '_' . $num ) ) { | ||
| 247 | # Set this as our new alias | ||
| 248 | $_[HEAP]->{'ALIAS'} = $_[HEAP]->{'ALIAS'} . '_' . $num; | ||
| 249 | |||
| 250 | # Debug stuff | ||
| 251 | if ( DEBUG ) { | ||
| 252 | warn "Finally found an alias to use -> " . $_[HEAP]->{'ALIAS'}; | ||
| 253 | } | ||
| 254 | |||
| 255 | # Actually set it! | ||
| 256 | $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} ); | ||
| 257 | last; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | # Okay, time to create the server! | ||
| 263 | $_[HEAP]->{'SF'} = POE::Wheel::SocketFactory->new( | ||
| 264 | 'BindPort' => $_[HEAP]->{'PORT'}, | ||
| 265 | 'BindAddress' => $_[HEAP]->{'ADDRESS'}, | ||
| 266 | 'Reuse' => 'yes', | ||
| 267 | 'SuccessEvent' => 'GotConnection', | ||
| 268 | 'FailureEvent' => 'SFError', | ||
| 269 | ); | ||
| 270 | |||
| 271 | # All done! | ||
| 272 | return 1; | ||
| 273 | } | ||
| 274 | |||
| 275 | # Shuts ourself down | ||
| 276 | sub StopServer { | ||
| 277 | # Remove the alias | ||
| 278 | $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} ); | ||
| 279 | |||
| 280 | # Shutdown the listening socket | ||
| 281 | undef $_[HEAP]->{'SF'}; | ||
| 282 | |||
| 283 | # We are shutting down... | ||
| 284 | $_[HEAP]->{'SHUTDOWN'} = 1; | ||
| 285 | |||
| 286 | # Broadcast to the entire network that we are going down | ||
| 287 | $_[KERNEL]->call( | ||
| 288 | $POE::Component::Lightspeed::Router::SES_ALIAS, | ||
| 289 | 'killserver', | ||
| 290 | [ keys %{ $_[HEAP]->{'RW'} } ], | ||
| 291 | ); | ||
| 292 | |||
| 293 | # All done! | ||
| 294 | return 1; | ||
| 295 | } | ||
| 296 | |||
| 297 | # Shuts down a client | ||
| 298 | sub KillClient { | ||
| 299 | # Get the wheel id | ||
| 300 | my $id = $_[ARG0]; | ||
| 301 | |||
| 302 | # Delete that wheel | ||
| 303 | if ( exists $_[HEAP]->{'RW'}->{ $id } ) { | ||
| 304 | delete $_[HEAP]->{'RW'}->{ $id }; | ||
| 305 | } | ||
| 306 | |||
| 307 | # All done! | ||
| 308 | return 1; | ||
| 309 | } | ||
| 310 | |||
| 311 | # Got a connection from another lightspeed client! | ||
| 312 | sub GotConnection { | ||
| 313 | # ARG0 = Socket, ARG1 = Remote Address, ARG2 = Remote Port, ARG3 = wheelid | ||
| 314 | my( $socket, $id ) = @_[ ARG0, ARG3 ]; | ||
| 315 | |||
| 316 | # Authenticate this client | ||
| 317 | if ( defined $_[HEAP]->{'AUTHCLIENT'} and ! $_[HEAP]->{'AUTHCLIENT'}->( $_[HEAP]->{'ADDRESS'}, $_[HEAP]->{'PORT'}, $_[ARG1], $_[ARG2] ) ) { | ||
| 318 | if ( DEBUG ) { | ||
| 319 | warn "AUTHCLIENT returned false, closing socket from $_[ARG1]"; | ||
| 320 | } | ||
| 321 | |||
| 322 | # Get rid of this client | ||
| 323 | close $socket; | ||
| 324 | return 1; | ||
| 325 | } | ||
| 326 | |||
| 327 | # Should we SSLify it? | ||
| 328 | if ( defined $_[HEAP]->{'SSLKEYCERT'} ) { | ||
| 329 | # SSLify it! | ||
| 330 | eval { $socket = Server_SSLify( $socket ) }; | ||
| 331 | if ( $@ ) { | ||
| 332 | if ( DEBUG ) { | ||
| 333 | warn "Unable to turn socket into SSL -> $@"; | ||
| 334 | } | ||
| 335 | |||
| 336 | # Get rid of this client | ||
| 337 | close $socket; | ||
| 338 | return 1; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | # Set up the Wheel to read from the socket | ||
| 343 | my $wheel = POE::Wheel::ReadWrite->new( | ||
| 344 | 'Handle' => $socket, | ||
| 345 | 'Driver' => POE::Driver::SysRW->new(), | ||
| 346 | 'Filter' => POE::Filter::Line->new(), | ||
| 347 | 'InputEvent' => 'InputLine', | ||
| 348 | 'FlushedEvent' => 'RWFlushed', | ||
| 349 | 'ErrorEvent' => 'RWError', | ||
| 350 | ); | ||
| 351 | |||
| 352 | # Save this wheel! | ||
| 353 | $_[HEAP]->{'RW'}->{ $wheel->ID } = { | ||
| 354 | 'WHEEL' => $wheel, | ||
| 355 | 'PHASE' => 'no', | ||
| 356 | 'COMPRESSION' => 0, | ||
| 357 | 'FILTER' => undef, | ||
| 358 | 'REMOTE_IP' => inet_ntoa( $_[ARG1] ), | ||
| 359 | 'REMOTE_PORT' => $_[ARG2], | ||
| 360 | 'CLIENT_VER' => undef, | ||
| 361 | 'CLIENT_KERNEL' => undef, | ||
| 362 | 'PACKETS_IN' => 0, | ||
| 363 | 'PACKETS_OUT' => 0, | ||
| 364 | }; | ||
| 365 | |||
| 366 | # Send the welcome line | ||
| 367 | $wheel->put( 'SERVER Lightspeed v/' . $VERSION . ' kernel ' . $_[HEAP]->{'MYKERNEL'} ); | ||
| 368 | |||
| 369 | # Debug stuff | ||
| 370 | if ( DEBUG ) { | ||
| 371 | warn "GotConnection completed creation of ReadWrite wheel ( " . $wheel->ID . " )"; | ||
| 372 | } | ||
| 373 | |||
| 374 | # Success! | ||
| 375 | return 1; | ||
| 376 | } | ||
| 377 | |||
| 378 | # Got some sort of error from SocketFactory | ||
| 379 | sub SFError { | ||
| 380 | # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID | ||
| 381 | my ( $operation, $errnum, $errstr, $wheel_id ) = @_[ ARG0 .. ARG3 ]; | ||
| 382 | |||
| 383 | # Debug stuff | ||
| 384 | if ( DEBUG ) { | ||
| 385 | warn "SocketFactory Wheel $wheel_id generated $operation error $errnum: $errstr\n"; | ||
| 386 | } | ||
| 387 | |||
| 388 | # Success! | ||
| 389 | return 1; | ||
| 390 | } | ||
| 391 | |||
| 392 | # Got some input! | ||
| 393 | sub InputLine { | ||
| 394 | # ARG0 = input, ARG1 = wheel id | ||
| 395 | my( $line, $id ) = @_[ ARG0, ARG1 ]; | ||
| 396 | |||
| 397 | # Skip empty lines | ||
| 398 | return if $line eq ''; | ||
| 399 | |||
| 400 | # Ok, what stage of negotiations are we on? | ||
| 401 | if ( $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} eq 'no' ) { | ||
| 402 | # Should be the welcome line | ||
| 403 | if ( $line =~ /^CLIENT\s+Lightspeed\s+v\/(.*)\s+kernel\s+(.*)$/ ) { | ||
| 404 | # Okay, this client talks lightspeed :) | ||
| 405 | $_[HEAP]->{'RW'}->{ $id }->{'CLIENT_VER'} = $1; | ||
| 406 | $_[HEAP]->{'RW'}->{ $id }->{'CLIENT_KERNEL'} = $2; | ||
| 407 | |||
| 408 | # Determine what phase we need to be in | ||
| 409 | if ( defined $_[HEAP]->{'PASSWORD'} ) { | ||
| 410 | $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} = 'password'; | ||
| 411 | } else { | ||
| 412 | $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} = 'compression'; | ||
| 413 | } | ||
| 414 | } else { | ||
| 415 | # Doesn't talk lightspeed? | ||
| 416 | if ( DEBUG ) { | ||
| 417 | warn "Client doesn't talk Lightspeed -> input was: $line"; | ||
| 418 | } | ||
| 419 | |||
| 420 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'ERROR UNKNOWN DATA' ); | ||
| 421 | $_[HEAP]->{'RW'}->{ $id }->{'SHUTDOWN'} = 1; | ||
| 422 | } | ||
| 423 | } elsif ( $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} eq 'password' ) { | ||
| 424 | # We require a password, if the client doesn't send it, throw them an error | ||
| 425 | if ( $line =~ /^PASSWORD\s+(.*)$/ ) { | ||
| 426 | my $pass = $1; | ||
| 427 | |||
| 428 | # Check to see if it matches | ||
| 429 | if ( $pass eq $_[HEAP]->{'PASSWORD'} ) { | ||
| 430 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'PASSWORD OK' ); | ||
| 431 | $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} = 'compression'; | ||
| 432 | } else { | ||
| 433 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'ERROR PASSWORD NOT OK' ); | ||
| 434 | $_[HEAP]->{'RW'}->{ $id }->{'SHUTDOWN'} = 1; | ||
| 435 | } | ||
| 436 | } else { | ||
| 437 | # Ah, they didn't have a password set | ||
| 438 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'ERROR PASSWORD REQUIRED' ); | ||
| 439 | $_[HEAP]->{'RW'}->{ $id }->{'SHUTDOWN'} = 1; | ||
| 440 | } | ||
| 441 | } elsif ( $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} eq 'compression' ) { | ||
| 442 | # Should be the compress line | ||
| 443 | if ( $line =~ /^COMPRESSION\s+(ON|OFF)$/ ) { | ||
| 444 | # On or off? | ||
| 445 | if ( $1 eq 'ON' ) { | ||
| 446 | $_[HEAP]->{'RW'}->{ $id }->{'COMPRESSION'} = 1; | ||
| 447 | } else { | ||
| 448 | $_[HEAP]->{'RW'}->{ $id }->{'COMPRESSION'} = 0; | ||
| 449 | } | ||
| 450 | |||
| 451 | # Move on to the next phase | ||
| 452 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'COMPRESSION OK' ); | ||
| 453 | $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} = 'serializer'; | ||
| 454 | } elsif ( $line =~ /^PASSWORD/ ) { | ||
| 455 | # Ahh, client sent password, it wasn't necessary | ||
| 456 | if ( DEBUG ) { | ||
| 457 | warn "Client sent password, but it wasn't necessary"; | ||
| 458 | } | ||
| 459 | |||
| 460 | # Simulate password success so they'll send us the COMPRESSION line | ||
| 461 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'PASSWORD OK' ); | ||
| 462 | } else { | ||
| 463 | # Doesn't talk lightspeed? | ||
| 464 | if ( DEBUG ) { | ||
| 465 | warn "Client doesn't talk Lightspeed -> input was: $line"; | ||
| 466 | } | ||
| 467 | |||
| 468 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'ERROR UNKNOWN DATA' ); | ||
| 469 | $_[HEAP]->{'RW'}->{ $id }->{'SHUTDOWN'} = 1; | ||
| 470 | } | ||
| 471 | } elsif ( $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} eq 'serializer' ) { | ||
| 472 | # Should be the serializer line | ||
| 473 | if ( $line =~ /^SERIALIZER\s+(.*)$/ ) { | ||
| 474 | # Let's see if it is a valid serializer | ||
| 475 | eval { $_[HEAP]->{'RW'}->{ $id }->{'FILTER'} = POE::Filter::Reference->new( $1, $_[HEAP]->{'RW'}->{ $id }->{'COMPRESSION'} ) }; | ||
| 476 | |||
| 477 | # Check for errors | ||
| 478 | if ( $@ ) { | ||
| 479 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'SERIALIZER NOT OK' ); | ||
| 480 | } else { | ||
| 481 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'SERIALIZER OK' ); | ||
| 482 | $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} = 'done'; | ||
| 483 | } | ||
| 484 | } else { | ||
| 485 | # Doesn't talk lightspeed? | ||
| 486 | if ( DEBUG ) { | ||
| 487 | warn "Client doesn't talk Lightspeed -> input was: $line"; | ||
| 488 | } | ||
| 489 | |||
| 490 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'ERROR UNKNOWN DATA' ); | ||
| 491 | $_[HEAP]->{'RW'}->{ $id }->{'SHUTDOWN'} = 1; | ||
| 492 | } | ||
| 493 | } elsif ( $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} eq 'done' ) { | ||
| 494 | # Should be the "negotiation complete" line | ||
| 495 | if ( $line eq 'DONE' ) { | ||
| 496 | # Allright! | ||
| 497 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->event( 'InputEvent', 'InputHash' ); | ||
| 498 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->set_filter( $_[HEAP]->{'RW'}->{ $id }->{'FILTER'} ); | ||
| 499 | |||
| 500 | # Set our phase | ||
| 501 | $_[HEAP]->{'RW'}->{ $id }->{'PHASE'} = 'connected'; | ||
| 502 | |||
| 503 | # Let the Router know a link is up | ||
| 504 | $_[KERNEL]->call( $POE::Component::Lightspeed::Router::SES_ALIAS, 'link_up', $id, $_[HEAP]->{'MYKERNEL'}, $_[HEAP]->{'RW'}->{ $id }->{'CLIENT_KERNEL'}, 'Server' ); | ||
| 505 | } else { | ||
| 506 | # Doesn't talk lightspeed? | ||
| 507 | if ( DEBUG ) { | ||
| 508 | warn "Client doesn't talk Lightspeed -> input was: $line"; | ||
| 509 | } | ||
| 510 | |||
| 511 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( 'ERROR UNKNOWN DATA' ); | ||
| 512 | $_[HEAP]->{'RW'}->{ $id }->{'SHUTDOWN'} = 1; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | # All done! | ||
| 517 | return 1; | ||
| 518 | } | ||
| 519 | |||
| 520 | # The main method of communication | ||
| 521 | sub InputHash { | ||
| 522 | # ARG0 = data, ARG1 = wheel id | ||
| 523 | my( $msg, $id ) = @_[ ARG0, ARG1 ]; | ||
| 524 | |||
| 525 | if ( DEBUG ) { | ||
| 526 | warn "Received packet from link $id:\n" . Data::Dumper::Dumper( $msg ); | ||
| 527 | } | ||
| 528 | |||
| 529 | # Send it off to the Router! | ||
| 530 | $_[KERNEL]->post( $POE::Component::Lightspeed::Router::SES_ALIAS, 'ACTION_' . $msg->[ MSG_ACTION ], $msg, $id ); | ||
| 531 | |||
| 532 | # Increment the packet count | ||
| 533 | $_[HEAP]->{'RW'}->{ $id }->{'PACKETS_IN'}++; | ||
| 534 | |||
| 535 | # All done! | ||
| 536 | return 1; | ||
| 537 | } | ||
| 538 | |||
| 539 | # ReadWrite error | ||
| 540 | sub RWError { | ||
| 541 | # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID | ||
| 542 | my ( $operation, $errnum, $errstr, $wheel_id ) = @_[ ARG0 .. ARG3 ]; | ||
| 543 | |||
| 544 | # Debug stuff | ||
| 545 | if ( DEBUG ) { | ||
| 546 | warn "ReadWrite Wheel $wheel_id generated $operation error $errnum: $errstr\n"; | ||
| 547 | } | ||
| 548 | |||
| 549 | # Do this only if we aren't shutting down | ||
| 550 | if ( ! $_[HEAP]->{'SHUTDOWN'} ) { | ||
| 551 | # Also, if the link was up | ||
| 552 | if ( $_[HEAP]->{'RW'}->{ $wheel_id }->{'PHASE'} eq 'connected' ) { | ||
| 553 | # Let the router know a link is down | ||
| 554 | $_[KERNEL]->call( $POE::Component::Lightspeed::Router::SES_ALIAS, 'link_down', $wheel_id ); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | # Delete this wheel | ||
| 559 | delete $_[HEAP]->{'RW'}->{ $wheel_id }; | ||
| 560 | |||
| 561 | # Success! | ||
| 562 | return 1; | ||
| 563 | } | ||
| 564 | |||
| 565 | # ReadWrite flush event | ||
| 566 | sub RWFlush { | ||
| 567 | # ARG0 = wheel id | ||
| 568 | |||
| 569 | # Are we shutting down? | ||
| 570 | if ( $_[HEAP]->{'SHUTDOWN'} or $_[HEAP]->{'RW'}->{ $_[ARG0] }->{'SHUTDOWN'} ) { | ||
| 571 | # Delete this wheel | ||
| 572 | delete $_[HEAP]->{'RW'}->{ $_[ARG0] }; | ||
| 573 | } | ||
| 574 | |||
| 575 | # All done! | ||
| 576 | return 1; | ||
| 577 | } | ||
| 578 | |||
| 579 | # Sends a packet down the wire | ||
| 580 | sub Send_Packet { | ||
| 581 | my( $id, $packet ) = @_[ ARG0, ARG1 ]; | ||
| 582 | |||
| 583 | # Sanity check | ||
| 584 | if ( ! defined $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'} ) { | ||
| 585 | if ( DEBUG ) { | ||
| 586 | warn "Link $id is down, unable to send packet!"; | ||
| 587 | } | ||
| 588 | return undef; | ||
| 589 | } | ||
| 590 | |||
| 591 | # Add the timestamp | ||
| 592 | if ( ! defined $packet->[ MSG_TIMESTAMP ] ) { | ||
| 593 | $packet->[ MSG_TIMESTAMP ] = gettimeofday(); | ||
| 594 | } | ||
| 595 | |||
| 596 | if ( DEBUG ) { | ||
| 597 | warn "Sending packet to link $id:\n" . Data::Dumper::Dumper( $packet ); | ||
| 598 | } | ||
| 599 | |||
| 600 | # Send it to the proper RW wheel! | ||
| 601 | $_[HEAP]->{'RW'}->{ $id }->{'WHEEL'}->put( $packet ); | ||
| 602 | |||
| 603 | # Increment the packet count | ||
| 604 | $_[HEAP]->{'RW'}->{ $id }->{'PACKETS_OUT'}++; | ||
| 605 | |||
| 606 | # All done! | ||
| 607 | return 1; | ||
| 608 | } | ||
| 609 | |||
| 610 | # End of module | ||
| 611 | 1; | ||
| 612 | __END__ |