/usr/share/perl5/POE/Component/IKC/Channel.pm is in libpoe-component-ikc-perl 0.2302-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 | package POE::Component::IKC::Channel;
############################################################
# $Id: Channel.pm 819 2011-08-27 01:57:36Z fil $
# Based on tests/refserver.perl
# Contributed by Artur Bergman <artur@vogon-solutions.com>
# Revised for 0.06 by Rocco Caputo <troc@netrus.net>
# Turned into a module by Philp Gwyn <fil@pied.nu>
#
# Copyright 1999-2011 Philip Gwyn. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# Contributed portions of IKC may be copyright by their respective
# contributors.
use strict;
use Socket;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
use POE qw(Wheel::ListenAccept Wheel::ReadWrite Wheel::SocketFactory
Driver::SysRW Filter::Reference Filter::Line
);
use POE::Component::IKC::Responder;
use POE::Component::IKC::Protocol;
use Data::Dumper;
# use Net::Gen ();
use Time::HiRes qw( gettimeofday tv_interval );
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(create_ikc_channel);
$VERSION = '0.2302';
sub DEBUG () { 0 }
BEGIN {
no strict 'refs';
unless( defined &TIMING ) {
if( $ENV{IKC_TIMING} ) { *TIMING = sub () { 1 } }
else { *TIMING = sub () { 0 } }
}
unless( defined &PROTOCOL ) {
if( $ENV{IKC_PROTOCOL} ) { *PROTOCOL = sub () { 1 } }
else { *PROTOCOL = sub () { 0 } }
}
}
###############################################################################
# Channel instances are created by the listening session to handle
# connections. They receive one or more thawed references, and pass
# them to the running Responder session for processing.
#----------------------------------------------------
# This is just a convenient way to create channels.
sub create_ikc_channel
{
my %p;
@p{qw(handle name on_connect subscribe rname unix aliases serializers protocol)}
= @_;
return __PACKAGE__->spawn(%p);
}
sub spawn
{
my $package=shift;
my %params=@_;
return POE::Session->create(
inline_states => {
_start => \&channel_start,
_stop => \&channel_stop,
_default => \&channel_default,
error => \&channel_error,
shutdown =>\&channel_close,
receive => \&channel_receive,
'send' => \&channel_send,
'flushed' => \&channel_flushed,
'done' => \&channel_done,
'close' => \&channel_close,
server_000 => \&server_000,
server_001 => \&negociate_001,
server_002 => \&server_002,
server_003 => \&server_003,
server_010 => \&server_010,
client_000 => \&client_000,
client_001 => \&negociate_001,
client_002 => \&client_002,
client_003 => \&client_003,
client_010 => \&client_010,
'sig_INT' => \&sig_INT
},
args => [\%params]
)->ID;
}
#----------------------------------------------------
# Accept POE's standard _start event, and begin processing data.
sub channel_start
{
my ($kernel, $heap, $session, $p) = @_[KERNEL, HEAP, SESSION, ARG0];
if( TIMING ) {
$heap->{start_time} = [ gettimeofday ];
delete $heap->{last_time};
}
my @names;
push @names, $p->{name} if $p->{name};
push @names, @{$p->{aliases}} if $p->{aliases};
# $name is blank if create_ikc_{server,client} wasn't called with a name
# OR if we are a kernel that was connected to (2001/05 huh?)
# +GC
my $alias = 0+$session;
$alias = "Channel $alias";
$kernel->alias_set($alias);
$heap->{session_alias} = $alias;
# all clients have $on_connect defined, even if sub {}
$heap->{is_server} = not $p->{client};
DEBUG and
warn "$$: We are a ".($heap->{is_server} ? 'server' : 'client')."\n";
if($p->{unix}) {
$p->{unix}=~s/[^-:.\w]+/_/g;
push @names, "unix:$p->{unix}";
unless($heap->{is_server}) {
$names[-1].=":$$-".fileno($p->{handle});
}
}
else {
my @name=unpack_sockaddr_in(getsockname($p->{handle}));
$name[1]=inet_ntoa($name[1]);
push @names, join ':', @name[1,0];
}
DEBUG and warn "$$: Names: ", join ',', @names;
$heap->{kernel_name}=shift @names;
$heap->{kernel_aliases}=\@names;
# remote_kernel is only needed for DEBUG messages only
# remote_aliases, however, is important
# remote_ID is set when negociations are finished
# it should be cannonical name according to remote side
# temp_remote_kernel is a local sanity alias. (ie, if we connect to
# something:port, it should have that name as an alias)
if($p->{rname}) { # we are a server
$heap->{remote_kernel}=$p->{rname};
$heap->{temp_remote_kernel}=$p->{rname};
}
elsif($p->{unix}) { # we are a client
my $n=$p->{unix};
$n=~tr(/\\)(--);
$heap->{remote_kernel}="unix:$n:$$:".
fileno($p->{handle});
$heap->{temp_remote_kernel}="unix:n" unless $heap->{is_server};
# we need to have unique aliases for remote kernels
# so, only the server gets a default name, clients don't
}
else {
my @name=unpack_sockaddr_in(getpeername($p->{handle}));
$name[1]=inet_ntoa($name[1]);
$heap->{temp_remote_kernel}=
$heap->{remote_kernel}=join ':', @name[1,0];
}
DEBUG && warn "Channel session $heap->{kernel_name}<->$heap->{remote_kernel} created.\n";
# start reading and writing
$heap->{wheel_client} = new POE::Wheel::ReadWrite
( Handle => $p->{handle}, # on this handle
Driver => new POE::Driver::SysRW, # using sysread and syswrite
InputEvent => 'none',
Filter => POE::Filter::Line->new(), # use a line filter for negociations
ErrorEvent => 'error', # generate this event on error
);
$session->option(default=>1);
$heap->{on_connect}=$p->{on_connect} if ref($p->{on_connect});
$heap->{subscribe}=$p->{subscribe}
if ref($p->{subscribe}) and @{$p->{subscribe}};
unless($heap->{is_server}) {
if(ref($p->{serializers}) and @{$p->{serializers}}) {
$heap->{serializers}=$p->{serializers};
# } else {
# $heap->{serializers}=$p->{serializers};
}
DEBUG and
warn __PACKAGE__, " Serializers: ",
join(', ', @{$heap->{serializers}||[]}), "\n";
}
# Setup negociation
$p->{protocol} ||= 'IKC';
if( $p->{protocol} eq 'IKC0' ) {
PROTOCOL and warn "$$: Using protocol IKC0\n";
_set_phase($kernel, $heap, '010');
}
else {
PROTOCOL and warn "$$: Using protocol IKC\n";
_set_phase($kernel, $heap, '000');
}
# Register this channel
my $ikc = eval { $kernel->alias_resolve( 'IKC' ) };
if( $ikc ) {
$kernel->call( $ikc, 'register_channel' );
}
else {
warn __PACKAGE__, " has no IKC responder.";
$kernel->yield( 'shutdown' );
}
return "channel-$session";
}
#----------------------------------------------------
sub _negociation_done
{
my($kernel, $heap)=@_;
DEBUG and
warn "$$: Negociation done ($heap->{kernel_name}<->$heap->{remote_kernel}).\n";
# generate this event on input
$heap->{'wheel_client'}->event(InputEvent => 'receive',
FlushedEvent => 'flushed');
unless($heap->{filter}) {
DEBUG and warn "$$: We didn't negociate a freezer, using defaults\n";
$heap->{filter}=POE::Filter::Reference->new();
}
# parsing I/O as references
my $ft = $heap->{filter};
DEBUG and warn "$$: Filter is now $ft";
$heap->{wheel_client}->set_filter($ft);
delete $heap->{filter};
create_ikc_responder();
# Register the foreign kernel with the responder
my $aliases=delete $heap->{remote_aliases};
push @$aliases, $heap->{temp_remote_kernel}
if $heap->{temp_remote_kernel} and
not grep {$_ eq $heap->{temp_remote_kernel}} @$aliases;
DEBUG and
warn "$$: Register remote as ", join ', ', @$aliases;
# we need a globaly unique ID
$heap->{remote_ID}=shift @$aliases;
# delete $heap->{remote_kernel};
$kernel->call('IKC', 'register', $heap->{remote_ID}, $aliases);
TIMING and channel_log( $heap, "negociated" );
T->point( 'IKC', 'nego done' );
# Now that we're set up properly
if($heap->{subscribe}) { # subscribe to wanted sessions
$kernel->call('IKC', 'subscribe', $heap->{subscribe}, 'done');
}
else {
# "fake" a completed subscription
$kernel->yield('done');
}
return;
}
#----------------------------------------------------
# This is the subscription callback
sub channel_done
{
my($heap, $subscribed)=@_[HEAP, ARG0];
if($heap->{subscribe})
{
my %count;
foreach my $spec (@$subscribed, @{$heap->{subscribe}})
{ $count{$spec}++;
}
my @missing=grep { $count{$_} != 2 } keys %count;
if(@missing)
{
die "Unable to subscribe to ".join(', ', @missing)."\n";
}
delete $heap->{subscribe};
DEBUG and warn "$$: Subscriptions are completed\n";
}
if($heap->{on_connect}) # or call the on_connect
{
DEBUG and warn "$$: On connect\n";
$heap->{on_connect}->();
delete $heap->{on_connect};
}
# Detach from parent session
unless( $heap->{is_server} ) {
# Only if we are a client. Server uses 'lose' to detect disconnects
# for concurrency.
$_[KERNEL]->detach_myself;
}
TIMING and channel_log( $heap, "subscribed" );
# wait until everything is sane before registering this
# $kernel->signal(INT=>'sig_INT'); # sig_INT() is in fact empty
}
#----------------------------------------------------
#### DEAL WITH NEGOCIATION PHASE
sub _set_phase
{
my($kernel, $heap, $phase, $line)=@_;
if($phase eq 'ZZZ')
{
_negociation_done($kernel, $heap);
return;
}
my $neg = $heap->{is_server} ? 'server_' : 'client_';
# generate this event on input
$heap->{'wheel_client'}->event(InputEvent => $neg.$phase);
DEBUG && warn "Negociation phase $neg$phase.\n";
$kernel->yield($neg.$phase, $line); # Start the negociation phase
return;
}
# First server state is
sub server_000
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
# wait for client to send HELLO
}
elsif( $line =~ /^HELLO IKC\d$/ ) { # compatible with IKC1
$heap->{'wheel_client'}->put( 'NOT' );
}
elsif( $line =~ /^SETUP/ ) { # compatible with IKC0
$heap->{'wheel_client'}->put( 'NOT' );
}
elsif( $line eq 'HELLO' ) {
$heap->{'wheel_client'}->put('IAM '.$kernel->ID());
# put other server aliases here
$heap->{aliases001}=[$heap->{kernel_name},
@{$heap->{kernel_aliases}}];
DEBUG and warn "$$: Server we are going to tell remote that aliases001=", join ',', @{$heap->{aliases001}};
_set_phase($kernel, $heap, '001');
}
else {
# wait for client to say something coherrent :)
warn "Client sent '$line' during phase 000\n";
}
return;
}
# We tell who we are
sub negociate_001
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
# far side must talk now (we sent "IAM kernel")
}
elsif($line eq 'OK') {
my $a=pop @{$heap->{aliases001}};
if($a) {
$heap->{'wheel_client'}->put("IAM $a");
}
else {
delete $heap->{aliases001};
$heap->{'wheel_client'}->put('DONE');
_set_phase($kernel, $heap, '002');
}
}
else {
warn "Received '$line' during phase 001\n";
# prod far side into saying something coherrent
$heap->{wheel_client}->put('NOT') unless $line eq 'NOT';
}
return;
}
# We find out who the client is
sub server_002
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
# far side must respond to the "DONE"
}
elsif($line eq 'DONE') {
_set_phase($kernel, $heap, '003');
}
elsif($line =~ /^IAM\s+([-:.\w]+)$/) {
# Register this kernel alias with the responder
push @{$heap->{remote_aliases}}, $1;
$heap->{'wheel_client'}->put('OK');
}
else {
warn "Client sent '$line' during phase 002\n";
# prod far side into saying something coherrent
$heap->{wheel_client}->put('NOT') unless $line eq 'NOT';
}
return;
}
# We find out what type of serialisation the client wants
sub server_003
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
# wait for client to send FREEZER after last IAM
}
elsif($line =~ /^FREEZER\s+([-:\w]+)$/) {
my $package=$1;
eval {
DEBUG and warn "Going to use $package as serializer\n";
$heap->{filter}=POE::Filter::Reference->new($package);
};
if($heap->{filter}) {
DEBUG &&
warn "$$: Using $package\n";
$heap->{wheel_client}->put('OK');
} else {
DEBUG && warn "Client wanted $package, but we can't : $@";
$heap->{wheel_client}->put('NOT');
}
}
elsif($line =~ /^FREEZER\s+(.+)$/) {
warn "Client sent invalid package $1 as a serializer, refused\n";
$heap->{wheel_client}->put('NOT');
}
elsif($line eq 'WORLD') {
# last bit of the dialog has to come from us :(
$heap->{wheel_client}->put('UP');
_set_phase($kernel, $heap, 'ZZZ');
}
else {
warn "Client sent '$line' during phase 003\n";
$heap->{wheel_client}->put('NOT') unless $line eq 'NOT';
}
return;
}
#----------------------------------------------------
# These states is invoked for each line during the negociation phase on
# the client's side
## Start negociation and listen to who the server is
sub client_000
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
$heap->{wheel_client}->put('HELLO');
}
elsif($line =~ /^IAM\s+([-:.\w]+)$/) {
# Register this kernel alias with the responder
DEBUG and warn "$$: Remote server is called $1\n";
push @{$heap->{remote_aliases}}, $1;
$heap->{wheel_client}->put('OK');
}
elsif($line eq 'DONE') {
$heap->{'wheel_client'}->put('IAM '.$poe_kernel->ID());
$heap->{aliases001}=[$heap->{kernel_name},
@{$heap->{kernel_aliases}}];
_set_phase($kernel, $heap, '001');
}
else {
warn "Server sent '$line' during negociation phase 000\n";
# prod far side into saying something coherrent
$heap->{wheel_client}->put('NOT') unless $line eq 'NOT';
}
return;
}
# try to negociate a serialization method
sub client_002
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
$heap->{serial002}=$heap->{serializers};
$line=$heap->{serial002} ? 'NOT' : 'OK';
# NOT= pretend that we already sent a FREEZER
# OK= use default freezers
}
if($line eq 'NOT') {
delete $heap->{filter};
my $ft;
while(@{$heap->{serial002}}) {
$ft=shift @{$heap->{serial002}};
DEBUG and
warn "$$: Trying serializer $ft\n";
$heap->{filter}=eval {
POE::Filter::Reference->new($ft);
};
last if $heap->{filter};
DEBUG and warn $@;
}
if($ft) {
$heap->{'wheel_client'}->put('FREEZER '.$ft);
}
else {
DEBUG and
warn "Server doesn't like our list of serializers ",
join ', ', @{$heap->{serializers}};
delete $heap->{serial002};
_set_phase($kernel, $heap, '003');
}
}
elsif($line eq 'OK') {
delete $heap->{serial002};
_set_phase($kernel, $heap, '003');
}
else {
warn "Server sent '$line' during negociation phase 002\n";
# prod far side into saying something coherrent
$heap->{wheel_client}->put('NOT') unless $line eq 'NOT';
}
}
# Game over
sub client_003
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
unless(defined $line) {
$heap->{'wheel_client'}->put('WORLD');
}
elsif($line eq 'UP') {
_set_phase($kernel, $heap, 'ZZZ');
}
else {
warn "Server sent '$line' during phase 003\n";
# prod far side into saying something coherrent
$heap->{wheel_client}->put('NOT') unless $line eq 'NOT';
}
return;
}
##############################################################################
sub client_010
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
DEBUG and $line and warn "Client010: $line";
unless(defined $line) {
# TODO : make sure all serializers load
# T->point( 'IKC', 'first line' );
my $setup = __build_setup( $heap, $heap->{serializers} );
# T->point( 'IKC', 'build_setup' );
DEBUG and warn "Client010: sending $setup";
$heap->{wheel_client}->put( $setup );
}
elsif( $line eq 'NOT' ) {
PROTOCOL and warn "$$: Using protocol IKC (fallback)\n";
_set_phase( $kernel, $heap, '000' );
}
elsif($line =~ /^SETUP (.+)$/) {
# T->point( IKC => 'got SETUP' );
DEBUG and warn "$$: Remote server setup as $1\n";
my( $K, $freezer, $bad) = __neg_setup( $1 );
unless( 1==@$freezer ) {
warn "Server didn't send one freezer in $line\n";
$bad++;
}
if( $bad ) {
$heap->{wheel_client}->put( 'NOT' );
return;
}
# Register these kernel alias with the responder
$heap->{remote_aliases} = $K;
# Build the filter we shall use later
$heap->{filter} = eval { POE::Filter::Reference->new( $freezer->[0] ) };
die "Unable to build filter: $@" if $@;
die "Unable to build filter $freezer->[0]" unless $heap->{filter};
# T->point( IKC => 'got SETUP' );
_set_phase( $kernel, $heap, 'ZZZ' );
}
else {
warn "Server sent '$line' during negociation phase 002\n";
$heap->{wheel_client}->put('NOT');
}
}
sub server_010
{
my ($heap, $kernel, $line)=@_[HEAP, KERNEL, ARG0];
DEBUG and $line and warn "Server010: $line";
unless(defined $line) {
# wait for client
}
elsif( $line =~ /^HELLO IKC\d$/ ) { # compatible with IKC1
$heap->{'wheel_client'}->put( 'NOT' );
}
elsif( $line eq 'HELLO' ) {
PROTOCOL and warn "$$: Using protocol IKC (fallback)\n";
_set_phase( $kernel, $heap, '000', $line );
return;
}
elsif( $line =~ /^SETUP (.+)$/ ) {
DEBUG and warn "$$: Remote client setup as $1\n";
my( $K, $freezer, $bad) = __neg_setup( $1 );
my $filter;
if( not $bad ) {
# Build the filter we shall use later
foreach my $ft ( @$freezer ) {
$filter = $ft;
$heap->{filter} = eval { POE::Filter::Reference->new( $ft ) };
last if $heap->{filter};
DEBUG and warn "Client wanted $ft, but we can't: $@";
}
}
unless( $heap->{filter} ) {
warn "None of the filters the client wants are OK: ", join ', ', @$freezer;
$bad++;
}
if( $bad ) {
$heap->{wheel_client}->put( 'NOT' );
return;
}
# Register these kernel alias with the responder
$heap->{remote_aliases} = $K;
# Send our SETUP back
my @freezers = ( $filter );
my $setup = __build_setup( $heap, [$filter] );
DEBUG and warn "Server010: sending $setup";
$heap->{wheel_client}->put( $setup );
# Move to next phase
_set_phase( $kernel, $heap, 'ZZZ' );
}
}
sub __build_setup
{
my( $heap, $freezers ) = @_;
my $aliases = [ $poe_kernel->ID,
$heap->{kernel_name},
@{$heap->{kernel_aliases}}
];
return POE::Component::IKC::Protocol::__build_setup( $aliases, $freezers );
}
sub __neg_setup
{
return POE::Component::IKC::Protocol::__neg_setup( $_[0] );
}
#----------------------------------------------------
# This state is invoked for each error encountered by the session's
# ReadWrite wheel.
sub channel_error
{
my ($heap, $kernel, $operation, $errnum, $errstr) =
@_[HEAP, KERNEL, ARG0, ARG1, ARG2];
if ($errnum) {
DEBUG &&
warn "$$: Channel encountered $operation error $errnum: $errstr\n";
}
else {
DEBUG &&
warn "$$: The channel's client closed its connection ($heap->{kernel_name}<->$heap->{remote_kernel})\n";
}
# warn "ERROR $heap->{remote_ID}";
_close_channel($heap, 1); # either way, shut down
}
#----------------------------------------------------
sub _channel_unregister
{
my($heap)=@_;
if($heap->{remote_ID}) {
DEBUG and warn <<WARN;
------------------------------------------
UNREGISTER $$ $heap->{remote_ID}
------------------------------------------
WARN
# 2005/06 Tell IKC we closed the connection
my $ikc = eval { $poe_kernel->alias_resolve( 'IKC' ) };
if( $ikc ) {
$poe_kernel->call( $ikc, 'unregister', $heap->{remote_ID});
}
delete $heap->{remote_ID};
}
# either way, shut down
}
#----------------------------------------------------
sub _close_channel
{
my($heap, $force)=@_;
# tell responder right away that this channel isn't to be used
_channel_unregister($heap);
return unless $heap->{wheel_client};
if(not $force and $heap->{wheel_client}->get_driver_out_octets) {
DEBUG and
warn "************ Defering wheel close";
$heap->{go_away}=1; # wait until next Flushed
return;
}
DEBUG and
warn "Deleting wheel session = ", $poe_kernel->get_active_session->ID;
my $x=delete $heap->{wheel_client};
# WORK AROUND
# $x->DESTROY;
# sig_INT is empty
# $kernel->sig( 'INT' );
if( $heap->{session_alias} ) {
$poe_kernel->alias_remove( delete $heap->{session_alias} );
}
if( TIMING ) {
channel_log( $heap, "close" );
delete $heap->{start_time};
delete $heap->{last_time};
}
T->point( 'IKC', 'close' );
return;
}
#----------------------------------------------------
#
sub channel_default
{
my($event)=$_[STATE];
DEBUG && warn "Unknown event $event posted to IKC::Channel\n"
if $event !~ /^_/;
return;
}
#----------------------------------------------------
# Process POE's standard _stop event by shutting down.
sub channel_stop
{
my $heap = $_[HEAP];
DEBUG &&
warn "$$: *** Channel will shut down.\n";
_close_channel($heap);
T->end( 'IKC' );
return "channel-$_[SESSION]";
}
###########################################################################
## Next two events forward messages between Wheel::ReadWrite and the
## Responder
## Because the Responder know which foreign kernel sent a request,
## these events fill in some of the details.
#----------------------------------------------------
# Foreign kernel sent us a request
sub channel_receive
{
my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
T->point( 'IKC', 'receive' );
TIMING and channel_log( $heap, "receive" );
DEBUG &&
warn "$$: Received data...\n";
return if $heap->{shutdown};
# we won't trust the other end to set this properly
$request->{errors_to}={ kernel=>$heap->{remote_ID},
session=>'IKC',
state=>'remote_error',
};
# just in case
$request->{call}->{kernel}||=$heap->{kernel_name};
# call the Responder channel to process
# hmmm.... i wonder if this could be stream-lined into a direct call
$kernel->call('IKC', 'request', $request);
return;
}
#----------------------------------------------------
# Local kernel is sending a request to a foreign kernel
sub channel_send
{
my ($heap, $request)=@_[HEAP, ARG0];
TIMING and channel_log( $heap, "send" );
DEBUG &&
warn "$$: Sending data...\n";
# add our name so the foreign channel can find us
# TODO should we do this? or should the other end do this?
$request->{rsvp}->{kernel}||=$heap->{kernel_name}
if ref($request) and $request->{rsvp};
if($heap->{'wheel_client'}) {
# use Data::Dumper;
# warn "Sending ", Dumper $request;
$heap->{'wheel_client'}->put($request);
}
else {
my $what={event => $request->{event},
from => $request->{from}};
$what->{action} = $request->{params}[0]
if $what->{event}{state} eq 'IKC:proxy' and
'ARRAY' eq ref $request->{params};
my $type = "missing";
$type = "shutdown" if $heap->{shutdown};
warn "$$: Attempting to put to a $type channel! ". Dumper $what;
}
T->point( 'IKC', 'send' );
return 1;
}
#----------------------------------------------------
sub channel_flushed
{
my($heap, $wheel)=@_[HEAP, ARG0];
DEBUG &&
warn "$$: Flushed data...\n";
if($heap->{go_away}) {
_close_channel($heap);
}
return;
}
#----------------------------------------------------
# Local kernel thinks it's time to close down the channel
sub channel_close
{
my ($heap, $sender)=@_[HEAP, SENDER];
unless( $heap->{shutdown} ) {
DEBUG &&
warn "$$: channel_close *****************************************\n";
$heap->{shutdown}=1;
}
_close_channel( $heap );
}
#----------------------------------------------------
# User wants to kill process / kernel
sub sig_INT
{
my ($heap, $kernel)=@_[HEAP, KERNEL];
DEBUG && warn "$$: Channel::sig_INT\n";
$kernel->sig_handled();
return;
}
#----------------------------------------------------
sub channel_log
{
my( $heap, $when ) = @_;
return unless $heap->{start_time};
my $now = [ gettimeofday ];
my $el = tv_interval( $heap->{start_time}, $now );
my $time = _delta_time( $el );
if( $heap->{last_time} ) {
$el = tv_interval( $heap->{last_time}, $now );
$time .= " +"._delta_time( $el );
}
$heap->{last_time} = $now;
print STDERR "$$: CHANNEL $time $when\n";
}
sub _delta_time
{
my( $el ) = @_;
if( $el > 1 ) {
return sprintf( "%.3fs", $el);
}
$el *= 1000; # microseconds -> milliseconds
if( $el > 10 ) {
return sprintf( "%ims", int $el);
}
return sprintf( "%.1gms", $el);
}
###########################################################################
1;
__END__
=head1 NAME
POE::Component::IKC::Channel - POE Inter-Kernel Communication I/O session
=head1 SYNOPSIS
use POE;
use POE::Component::IKC::Channel;
create_ikc_channel($handle, $name, $on_connect, $subscribe,
$rname, $unix);
=head1 DESCRIPTION
This module implements an POE IKC I/O.
When a new connection
is established, C<IKC::Server> and C<IKC::Client> create an
C<IKC::Channel> to handle the I/O.
IKC communication happens in 2 phases : negociation phase and normal phase.
The negociation phase uses C<Filter::Line> and is used to exchange various
parameters between kernels (example : kernel names, what type of freeze/thaw
to use, etc). After negociation, C<IKC::Channel> switches to a
C<Filter::Reference> and creates a C<IKC::Responder>, if needed. After
this, the channel forwards reads and writes between C<Wheel::ReadWrite> and
the Responder.
C<IKC::Channel> is also in charge of cleaning up kernel names when
the foreign kernel disconnects.
=head1 EXPORTED FUNCTIONS
=head2 create_ikc_channel
This function initiates all the work of connecting to a IKC connection
channel. It is a wrapper around C<spawn>.
=head1 METHODS
=head2 spawn
POE::Component::IKC::Channel->spawn(%param);
Creates a new IKC channel to handle the negociations then the actual data.
Parameters are keyed as follows:
=over 4
=item handle
The perl handle we should hand to C<Wheel::ReadWrite::new>.
=item kernel_name
The name of the local kernel. B<This is a stop-gap until event naming
has been resolved>.
=item on_connect
Code ref that is called when the negociation phase has terminated. Normaly,
you would use this to start the sessions that post events to foreign
kernels.
=item subscribe
Array ref of specifiers (either foreign sessions, or foreign states) that
you want to subscribe to. $on_connect will only be called if you can
subscribe to all those specifiers. If it can't, it will die().
=item unix
A flag indicating that the handle is a Unix domain socket or not.
=item aliases
Arrayref of aliases for the local kernel.
=item serializers
Arrayref or scalar of the packages that you want to use for data
serialization. A serializer package requires 2 functions : freeze (or
nfreeze) and thaw. See C<POE::Component::IKC::Client>.
=item C<protocol>
Which IKC negociation protocol to use. The original protocol (C<IKC>) was
synchronous and slow. The new protocol (C<IKC0>) sends all information at
once. IKC0 will degrade gracefully to IKC, if the client and server don't
match.
Default currently IKC but will move to IKC0 when I'm confident in the new
protocol.
=back
=head1 EVENTS
=head2 shutdown
This event causes the server to close it's socket and skiddadle on down the
road. Normally it is only posted from IKC::Responder.
If you want to post this event yourself, you can get the channel's
session ID from IKC::Client's on_connect:
POE::Component::IKC::Client->spawn(
....
on_connect=>sub {
$heap->{channel} = $poe_kernel->get_active_session()->ID;
},
....
);
Then, when it becomes time to disconnect:
$poe_kernel->call($heap->{channel} => 'shutdown');
Yes, this is a hack. A cleaner machanism needs to be provided.
=head1 BUGS
=head1 AUTHOR
Philip Gwyn, <perl-ikc at pied.nu>
=head1 COPYRIGHT AND LICENSE
Copyright 1999-2011 by Philip Gwyn. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
See L<http://www.perl.com/language/misc/Artistic.html>
=head1 SEE ALSO
L<POE>, L<POE::Component::IKC::Server>, L<POE::Component::IKC::Client>,
L<POE::Component::IKC::Responder>
=cut
|