| 1 | # | ||
| 2 | # This file is part of POE-Component-SpreadClient | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2014 by Apocalypse. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | 1 | ▶ | use strict; use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | package POE::Component::SpreadClient; | ||
| 11 | # git description: release-1.002-8-gd0ea32d | ||
| 12 | $POE::Component::SpreadClient::VERSION = '1.003'; | ||
| 13 | our $AUTHORITY = 'cpan:APOCAL'; | ||
| 14 | |||
| 15 | # ABSTRACT: Handle Spread communications in POE | ||
| 16 | |||
| 17 | # Load our stuff | ||
| 18 | 1 | ▶ | use 5.006; # to silence Perl::Critic's Compatibility::ProhibitThreeArgumentOpen |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use POE::Session; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use POE::Wheel::ReadWrite; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 0 | ▶ | use POE::Driver::SpreadClient; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use POE::Filter::SpreadClient; | ||
| 24 | |||
| 25 | # Thanks to RT#66904 for this craziness! | ||
| 26 | { | ||
| 27 | no warnings; | ||
| 28 | use Spread 3.017 qw( :MESS :ERROR ); | ||
| 29 | } | ||
| 30 | |||
| 31 | # Generate our states! | ||
| 32 | use parent 'POE::Session::AttributeBased'; | ||
| 33 | |||
| 34 | # Set some constants | ||
| 35 | BEGIN { | ||
| 36 | if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } } | ||
| 37 | } | ||
| 38 | |||
| 39 | # Create our instance! | ||
| 40 | sub spawn { | ||
| 41 | # Get the OOP's type | ||
| 42 | my $type = shift; $type = $type; # shutup UnusedVars | ||
| 43 | |||
| 44 | # Our own options | ||
| 45 | my $ALIAS = shift; | ||
| 46 | |||
| 47 | # Get the session alias | ||
| 48 | if ( ! defined $ALIAS ) { | ||
| 49 | # Debugging info... | ||
| 50 | if ( DEBUG ) { | ||
| 51 | warn 'Using default ALIAS = SpreadClient'; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Set the default | ||
| 55 | $ALIAS = 'SpreadClient'; | ||
| 56 | } | ||
| 57 | |||
| 58 | # Okay, create our session! | ||
| 59 | my $sess = POE::Session->create( | ||
| 60 | __PACKAGE__->inline_states(), ## no critic ( RequireExplicitInclusion ) | ||
| 61 | 'heap' => { | ||
| 62 | 'ALIAS' => $ALIAS, | ||
| 63 | }, | ||
| 64 | ); | ||
| 65 | |||
| 66 | # return the session's ID in case the caller needs it | ||
| 67 | return $sess->ID; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _start : State { | ||
| 71 | # Debugging | ||
| 72 | if ( DEBUG ) { | ||
| 73 | warn "SpreadClient was started!"; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Set our own alias | ||
| 77 | if ( $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} ) != 0 ) { | ||
| 78 | die "unable to set alias: " . $_[HEAP]->{'ALIAS'}; | ||
| 79 | } | ||
| 80 | |||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _stop : State { | ||
| 85 | # Debugging | ||
| 86 | if ( DEBUG ) { | ||
| 87 | warn "SpreadClient was stopped!"; | ||
| 88 | } | ||
| 89 | |||
| 90 | # Wow, go disconnect ourself! | ||
| 91 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 92 | |||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub connect : State { | ||
| 97 | # Server info, private name | ||
| 98 | my( $server, $priv ) = @_[ ARG0, ARG1 ]; | ||
| 99 | |||
| 100 | # Tack on the default port if needed | ||
| 101 | unless ( $server =~ /^\d+$/ or $server =~ /@/ ) { | ||
| 102 | # Debugging | ||
| 103 | if ( DEBUG ) { | ||
| 104 | warn "using default port 4803"; | ||
| 105 | } | ||
| 106 | |||
| 107 | $server = '4803@' . $server; | ||
| 108 | } | ||
| 109 | |||
| 110 | # Automatically set private name | ||
| 111 | if ( ! defined $priv ) { | ||
| 112 | # Debugging | ||
| 113 | if ( DEBUG ) { | ||
| 114 | warn "using default priv-name: spread-PID"; | ||
| 115 | } | ||
| 116 | |||
| 117 | $priv = 'spread-' . $$; | ||
| 118 | } | ||
| 119 | |||
| 120 | # Automatically add the sender session to listeners | ||
| 121 | if ( ! exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) { | ||
| 122 | $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } = 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | # Fire up Spread itself | ||
| 126 | my( $mbox, $priv_group ); | ||
| 127 | eval { | ||
| 128 | ( $mbox, $priv_group ) = Spread::connect( { | ||
| 129 | 'private_name' => $priv, | ||
| 130 | 'spread_name' => $server, | ||
| 131 | } ); | ||
| 132 | }; | ||
| 133 | if ( $@ ) { | ||
| 134 | # Inform our registered listeners | ||
| 135 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 136 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'CONNECT', $@, $server, $priv ); | ||
| 137 | } | ||
| 138 | |||
| 139 | # We're not connected... | ||
| 140 | $_[HEAP]->{'DISCONNECTED'} = 1; | ||
| 141 | } else { | ||
| 142 | # Sanity | ||
| 143 | if ( ! defined $mbox ) { | ||
| 144 | # Inform our registered listeners | ||
| 145 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 146 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'CONNECT', $sperrno, $server, $priv ); | ||
| 147 | } | ||
| 148 | |||
| 149 | # We're not connected... | ||
| 150 | $_[HEAP]->{'DISCONNECTED'} = 1; | ||
| 151 | } else { | ||
| 152 | # Debugging | ||
| 153 | if ( DEBUG ) { | ||
| 154 | warn "creating RW wheel for Spread"; | ||
| 155 | } | ||
| 156 | |||
| 157 | # Set our data | ||
| 158 | $_[HEAP]->{'SERVER'} = $server; | ||
| 159 | $_[HEAP]->{'PRIV_NAME'} = $priv; | ||
| 160 | $_[HEAP]->{'PRIV_GROUP'} = $priv_group; | ||
| 161 | $_[HEAP]->{'MBOX'} = $mbox; | ||
| 162 | |||
| 163 | # Create a FH to feed into Wheel::ReadWrite | ||
| 164 | # we retry because... there seems to be several microseconds until fileno() works! | ||
| 165 | # TODO we need to investigate the underlying cause of this... | ||
| 166 | my $retries = 0; | ||
| 167 | while ( ++$retries < 10 ) { | ||
| 168 | open $_[HEAP]->{'FH'}, '<&=', $mbox or do { warn "SpreadClient: open failure ($!)" if DEBUG }; | ||
| 169 | if ( $_[HEAP]->{'FH'} && fileno( $_[HEAP]->{'FH'} ) ) { | ||
| 170 | last; | ||
| 171 | } else { | ||
| 172 | warn "SpreadClient: fileno failure, retrying!" if DEBUG; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | if ( $retries == 10 ) { | ||
| 176 | if ( DEBUG ) { | ||
| 177 | warn "SpreadClient: UNABLE to create FH from mbox!"; | ||
| 178 | } | ||
| 179 | |||
| 180 | # Inform our registered listeners | ||
| 181 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 182 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'CONNECT', 'BADFH', $server, $priv ); | ||
| 183 | } | ||
| 184 | |||
| 185 | # We're not connected... | ||
| 186 | $_[HEAP]->{'DISCONNECTED'} = 1; | ||
| 187 | } else { | ||
| 188 | # Finally, create the wheel! | ||
| 189 | $_[HEAP]->{'WHEEL'} = POE::Wheel::ReadWrite->new( | ||
| 190 | 'Handle' => $_[HEAP]->{'FH'}, | ||
| 191 | 'Driver' => POE::Driver::SpreadClient->new( $mbox ), | ||
| 192 | 'Filter' => POE::Filter::SpreadClient->new(), | ||
| 193 | |||
| 194 | 'InputEvent' => 'RW_GotPacket', | ||
| 195 | 'ErrorEvent' => 'RW_Error' | ||
| 196 | ); | ||
| 197 | |||
| 198 | # Inform our registered listeners | ||
| 199 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 200 | $_[KERNEL]->post( $l, '_sp_connect', $priv, $priv_group ); | ||
| 201 | } | ||
| 202 | |||
| 203 | # We're connected... | ||
| 204 | delete $_[HEAP]->{'DISCONNECTED'} if exists $_[HEAP]->{'DISCONNECTED'}; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | # All done! | ||
| 210 | return; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub disconnect : State { | ||
| 214 | # Sanity | ||
| 215 | if ( exists $_[HEAP]->{'WHEEL'} and defined $_[HEAP]->{'WHEEL'} ) { | ||
| 216 | # Debugging | ||
| 217 | if ( DEBUG ) { | ||
| 218 | warn "SpreadClient is disconnecting!"; | ||
| 219 | } | ||
| 220 | |||
| 221 | # Shutdown the input/output | ||
| 222 | $_[HEAP]->{'WHEEL'}->shutdown_input(); | ||
| 223 | $_[HEAP]->{'WHEEL'}->shutdown_output(); | ||
| 224 | |||
| 225 | # Get rid of it! | ||
| 226 | undef $_[HEAP]->{'WHEEL'}; | ||
| 227 | } | ||
| 228 | |||
| 229 | # Sanity | ||
| 230 | if ( ! exists $_[HEAP]->{'DISCONNECTED'} ) { | ||
| 231 | if ( DEBUG ) { | ||
| 232 | warn "calling sp_disconnect"; | ||
| 233 | } | ||
| 234 | |||
| 235 | # Set it in our heap that we've disconnected | ||
| 236 | $_[HEAP]->{'DISCONNECTED'} = 1; | ||
| 237 | |||
| 238 | # Inform our registered listeners | ||
| 239 | # FIXME Should I use POST here instead? | ||
| 240 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 241 | $_[KERNEL]->call( $l, '_sp_disconnect', $_[HEAP]->{'PRIV_NAME'} ); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | # All done! | ||
| 246 | return; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub destroy : State { | ||
| 250 | # Okay, destroy ourself! | ||
| 251 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 252 | |||
| 253 | # Get rid of our alias | ||
| 254 | $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} ); | ||
| 255 | |||
| 256 | # All done! | ||
| 257 | return; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub publish : State { | ||
| 261 | my( $groups, $message, $mess_type, $flag ) = @_[ ARG0 .. ARG3 ]; | ||
| 262 | |||
| 263 | # Shortcut | ||
| 264 | if ( ! defined $_[HEAP]->{'WHEEL'} ) { | ||
| 265 | # Inform our registered listeners | ||
| 266 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 267 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'PUBLISH', CONNECTION_CLOSED, $groups, $message ); | ||
| 268 | } | ||
| 269 | |||
| 270 | # All done! | ||
| 271 | return; | ||
| 272 | } | ||
| 273 | |||
| 274 | # Sanity | ||
| 275 | if ( ! defined $mess_type ) { | ||
| 276 | $mess_type = 0; | ||
| 277 | } | ||
| 278 | if ( ! defined $flag ) { | ||
| 279 | $flag = SAFE_MESS; | ||
| 280 | } | ||
| 281 | |||
| 282 | # Spread.pm doesn't like one-member group via arrayref... | ||
| 283 | if ( defined $groups ) { | ||
| 284 | if ( ref $groups and ref( $groups ) eq 'ARRAY' and scalar @$groups == 1 ) { | ||
| 285 | $groups = $groups->[0]; | ||
| 286 | } | ||
| 287 | } else { | ||
| 288 | # Inform our registered listeners | ||
| 289 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 290 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'PUBLISH', ILLEGAL_GROUP, undef, $message ); | ||
| 291 | } | ||
| 292 | |||
| 293 | # All done! | ||
| 294 | return; | ||
| 295 | } | ||
| 296 | |||
| 297 | # Send it! | ||
| 298 | my $rtn; | ||
| 299 | eval { | ||
| 300 | $rtn = Spread::multicast( $_[HEAP]->{'MBOX'}, $flag, $groups, $mess_type, $message ); | ||
| 301 | }; | ||
| 302 | if ( $@ or ! defined $rtn or $rtn < 0 ) { | ||
| 303 | # Check for disconnect | ||
| 304 | if ( defined $sperrno and $sperrno == CONNECTION_CLOSED ) { | ||
| 305 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 306 | } | ||
| 307 | |||
| 308 | # Inform our registered listeners | ||
| 309 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 310 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'PUBLISH', $sperrno, $groups, $message ); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | # All done! | ||
| 315 | return; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub subscribe : State { | ||
| 319 | # The groups to join | ||
| 320 | my $groups = $_[ARG0]; | ||
| 321 | |||
| 322 | # Automatically add the sender session to listeners | ||
| 323 | if ( ! exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) { | ||
| 324 | $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } = 1; | ||
| 325 | } | ||
| 326 | |||
| 327 | # Shortcut | ||
| 328 | if ( ! defined $_[HEAP]->{'WHEEL'} ) { | ||
| 329 | # Inform our registered listeners | ||
| 330 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 331 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', CONNECTION_CLOSED, $groups ); | ||
| 332 | } | ||
| 333 | |||
| 334 | # All done! | ||
| 335 | return; | ||
| 336 | } | ||
| 337 | |||
| 338 | # sanity check | ||
| 339 | if ( ! defined $groups ) { | ||
| 340 | # Inform our registered listeners | ||
| 341 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 342 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', ILLEGAL_GROUP, undef ); | ||
| 343 | } | ||
| 344 | |||
| 345 | # All done! | ||
| 346 | return; | ||
| 347 | } | ||
| 348 | |||
| 349 | eval { | ||
| 350 | # try to join each group | ||
| 351 | foreach my $g ( ref $groups ? @$groups : $groups ) { | ||
| 352 | if ( ! Spread::join( $_[HEAP]->{'MBOX'}, $g ) ) { | ||
| 353 | # Check for disconnect | ||
| 354 | if ( defined $sperrno and $sperrno == CONNECTION_CLOSED ) { | ||
| 355 | die "disconnected"; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Inform our registered listeners | ||
| 359 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 360 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', $sperrno, $g ); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | }; | ||
| 365 | if ( $@ ) { | ||
| 366 | # Inform our registered listeners | ||
| 367 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 368 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', $sperrno, $groups ); | ||
| 369 | } | ||
| 370 | |||
| 371 | if ( $@ eq "disconnected" ) { | ||
| 372 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | # All done! | ||
| 377 | return; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub unsubscribe : State { | ||
| 381 | # The groups to unsub | ||
| 382 | my $groups = $_[ARG0]; | ||
| 383 | |||
| 384 | # Shortcut | ||
| 385 | if ( ! defined $_[HEAP]->{'WHEEL'} ) { | ||
| 386 | # Inform our registered listeners | ||
| 387 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 388 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', CONNECTION_CLOSED, $groups ); | ||
| 389 | } | ||
| 390 | |||
| 391 | # All done! | ||
| 392 | return; | ||
| 393 | } | ||
| 394 | |||
| 395 | # sanity | ||
| 396 | if ( ! defined $groups ) { | ||
| 397 | # Inform our registered listeners | ||
| 398 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 399 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', ILLEGAL_GROUP, undef ); | ||
| 400 | } | ||
| 401 | |||
| 402 | # All done! | ||
| 403 | return; | ||
| 404 | } | ||
| 405 | |||
| 406 | eval { | ||
| 407 | # try to leave each group | ||
| 408 | foreach my $g ( ref $groups ? @$groups : $groups ) { | ||
| 409 | if ( ! Spread::leave( $_[HEAP]->{'MBOX'}, $g ) ) { | ||
| 410 | # Check for disconnect | ||
| 411 | if ( defined $sperrno and $sperrno == CONNECTION_CLOSED ) { | ||
| 412 | die "disconnected"; | ||
| 413 | } | ||
| 414 | |||
| 415 | # Inform our registered listeners | ||
| 416 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 417 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', $sperrno, $g ); | ||
| 418 | } | ||
| 419 | } | ||
| 420 | } | ||
| 421 | }; | ||
| 422 | if ( $@ ) { | ||
| 423 | # Inform our registered listeners | ||
| 424 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 425 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', $sperrno, $groups ); | ||
| 426 | } | ||
| 427 | |||
| 428 | if ( $@ eq "disconnected" ) { | ||
| 429 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | # All done! | ||
| 434 | return; | ||
| 435 | } | ||
| 436 | |||
| 437 | # Registers interest in the client | ||
| 438 | sub register : State { | ||
| 439 | # Automatically add the sender session to listeners | ||
| 440 | if ( ! exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) { | ||
| 441 | $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } = 1; | ||
| 442 | } | ||
| 443 | |||
| 444 | # All done! | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | |||
| 448 | # Unregisters interest in the client | ||
| 449 | sub unregister : State { | ||
| 450 | # Automatically add the sender session to listeners | ||
| 451 | if ( exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) { | ||
| 452 | delete $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID }; | ||
| 453 | } | ||
| 454 | |||
| 455 | # All done! | ||
| 456 | return; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub RW_Error : State { | ||
| 460 | warn "ReadWrite wheel(" . $_[ARG3] . ") got error " . $_[ARG1] . " - " . $_[ARG2] . " doing " . $_[ARG0] if DEBUG; | ||
| 461 | |||
| 462 | # Disconnect now! | ||
| 463 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 464 | |||
| 465 | return; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub RW_GotPacket : State { | ||
| 469 | ## no critic (Bangs::ProhibitBitwiseOperators Bangs::ProhibitNumberedNames) | ||
| 470 | |||
| 471 | # we might get multiple packets per read | ||
| 472 | for my $packet ( @{ $_[ARG0] } ) { | ||
| 473 | my( $type, $sender, $groups, $mess_type, $endian, $message ) = @$packet; | ||
| 474 | |||
| 475 | # Check for disconnect | ||
| 476 | if ( ! defined $type ) { | ||
| 477 | # Disconnect now! | ||
| 478 | $_[KERNEL]->call( $_[SESSION], 'disconnect' ); | ||
| 479 | } else { | ||
| 480 | # Logic mostly taken from http://www.spread.org/docs/spread_docs_3/docs/sp_receive.html | ||
| 481 | # Check the type | ||
| 482 | if ( $type & REGULAR_MESS ) { | ||
| 483 | # Do we have an endian problem? | ||
| 484 | if ( defined $endian and $endian ) { | ||
| 485 | # FIXME Argh! | ||
| 486 | if ( DEBUG ) { | ||
| 487 | warn "endian mis-match detected!"; | ||
| 488 | } | ||
| 489 | } | ||
| 490 | |||
| 491 | # Regular message | ||
| 492 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 493 | $_[KERNEL]->post( $l, '_sp_message', $_[HEAP]->{'PRIV_NAME'}, $sender, $groups, $mess_type, $message ); | ||
| 494 | } | ||
| 495 | } else { | ||
| 496 | # Okay, figure out the type | ||
| 497 | if ( $type & TRANSITION_MESS ) { | ||
| 498 | # Transitional Message | ||
| 499 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 500 | $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'TRANSITIONAL', 'GROUP' => $sender } ); | ||
| 501 | } | ||
| 502 | } elsif ( $type & CAUSED_BY_LEAVE and ! ( $type & REG_MEMB_MESS ) ) { | ||
| 503 | # Self leave | ||
| 504 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 505 | $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'SELF_LEAVE', 'GROUP' => $sender } ); | ||
| 506 | } | ||
| 507 | } elsif ( $type & REG_MEMB_MESS ) { | ||
| 508 | # Parse the message! | ||
| 509 | my ( $gid1, $gid2, $gid3, $num_memb, $member ); | ||
| 510 | eval { | ||
| 511 | # Code copied from Spread::Message v0.21, thanks! | ||
| 512 | #($gid[0],$gid[1],$gid[2],$numg,$who) = unpack("IIIIa*",$msg); | ||
| 513 | #$who =~ s/[[:cntrl:]]+/ /go; # Just to clean it up | ||
| 514 | #$who =~ s/\s+$/ /go; # No space at end thanks | ||
| 515 | # changed from a to Z thanks RT#65795 | ||
| 516 | ( $gid1, $gid2, $gid3, $num_memb, $member ) = unpack( "IIIIZ*", $message ); | ||
| 517 | }; | ||
| 518 | if ( $@ ) { | ||
| 519 | # Inform our registered listeners | ||
| 520 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 521 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'RECEIVE', $@ ); | ||
| 522 | } | ||
| 523 | } else { | ||
| 524 | # Okay, what was it? | ||
| 525 | if ( $type & CAUSED_BY_JOIN ) { | ||
| 526 | # Inform our registered listeners | ||
| 527 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 528 | $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'JOIN', 'GROUP' => $sender, 'MEMBERS' => $groups, 'WHO' => $member, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type } ); | ||
| 529 | } | ||
| 530 | } elsif ( $type & CAUSED_BY_LEAVE ) { | ||
| 531 | # Inform our registered listeners | ||
| 532 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 533 | $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'LEAVE', 'GROUP' => $sender, 'MEMBERS' => $groups, 'WHO' => $member, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type } ); | ||
| 534 | } | ||
| 535 | } elsif ( $type & CAUSED_BY_DISCONNECT ) { | ||
| 536 | # Inform our registered listeners | ||
| 537 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 538 | $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'DISCONNECT', 'GROUP' => $sender, 'MEMBERS' => $groups, 'WHO' => $member, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type } ); | ||
| 539 | } | ||
| 540 | } elsif ( $type & CAUSED_BY_NETWORK ) { | ||
| 541 | # FIXME Unpack the full nodelist | ||
| 542 | #my @nodes = unpack( "a32" x ( length( $member ) / 32 + 1 ), $member ); | ||
| 543 | |||
| 544 | # Network failure | ||
| 545 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 546 | $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'NETWORK', 'GROUP' => $sender, 'MEMBERS' => $groups, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type, 'MESSAGE' => $message } ); | ||
| 547 | } | ||
| 548 | } else { | ||
| 549 | # Unknown? | ||
| 550 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 551 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'RECEIVE', 'UNKNOWN PACKET TYPE' ); | ||
| 552 | } | ||
| 553 | } | ||
| 554 | } | ||
| 555 | # TODO REJECT_MESS ??? | ||
| 556 | } else { | ||
| 557 | # Unknown? | ||
| 558 | foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) { | ||
| 559 | $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'RECEIVE', 'UNKNOWN PACKET TYPE' ); | ||
| 560 | } | ||
| 561 | } | ||
| 562 | } | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | # All done! | ||
| 567 | return; | ||
| 568 | } | ||
| 569 | |||
| 570 | 1; | ||
| 571 | |||
| 572 | __END__ | ||