This code comes from the TCP port redirection with components in the
poe.perl.org website cookbook (http://poe.perl.org/?POE_Cookbook/
TCP_Port_Redirection_With_Components).
I swapped the client and server roles.  Basically I want the client
component to connect to a remote server first, then start up the
server component to listen for incoming connections.  It all seems to
work fine except when the connection is lost to the remote server, the
client server never shuts down.  In other words...I can't figure out
why the server component does not recieve the "shutdown" command when
the client component sends it.  Any help would be greatly appreciated.

Thanks!

And now for the code:
###################################################
#!/usr/bin/perl

use warnings;
use strict;
use POE qw(Component::Server::TCP Component::Client::TCP);

# Spawn the forwarder server on port 1110.  When new connections
# arrive, spawn clients to connect them to their destination.

POE::Component::Client::TCP->new(
    RemoteAddress => 'localhost',

    RemotePort    => 7500,

        Alias => 'ClientTCP',

    Connected => sub {
        my ( $heap, $session ) = @_[ HEAP, SESSION ];
        logevent( 'client connected', $session );
                spawn_client_side();
    },

    ServerInput => sub {
        my ( $kernel, $heap, $session, $input ) = @_[ KERNEL, HEAP,
SESSION, ARG0 ];
        logevent( 'client got input', $session, $input );
        $kernel->post( $heap->{server_id} => send_stuff => $input );
    },

    Disconnected => sub {
        my ( $kernel, $heap, $session ) = @_[ KERNEL, HEAP, SESSION ];
        logevent( 'client disconnected', $session );
        $kernel->post( 'ServerTCP' => 'shutdown' );
    },

    InlineStates => {
        send_stuff => sub {
            my ( $heap, $stuff ) = @_[ HEAP, ARG0 ];
            logevent( "sending to client", $_[SESSION] );
            $heap->{server}->put($stuff);
        },
                _child => sub {
                        my ( $heap, $child_op, $child ) = @_[ HEAP, ARG0, ARG1 
];
                        if ( $child_op eq "create" ) {
                                $heap->{server_id} = $child->ID;
                        }
                },
    },
);


sub spawn_client_side {

POE::Component::Server::TCP->new(
    Port => 7400,

        Alias => 'ServerTCP',

    Started => sub {
        $_[HEAP]->{client_id} = $_[SENDER]->ID;
    },

    ClientConnected => sub {
        my ( $heap, $session ) = @_[ HEAP, SESSION ];
        logevent( 'server got connection', $session );
        spawn_client_side();
    },

    ClientInput => sub {
        my ( $kernel, $session, $heap, $input ) = @_[ KERNEL, SESSION,
HEAP, ARG0 ];
        logevent( 'server got input', $session, $input );
        $kernel->post( $heap->{client_id} => send_stuff => $input );
    },

    ClientDisconnected => sub {
        my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ];
        logevent( 'server got disconnect', $session );
        $kernel->post( $heap->{client_id} => "shutdown" );
    },

    InlineStates => {
        send_stuff => sub {
            my ( $heap, $stuff ) = @_[ HEAP, ARG0 ];
            logevent( "sending to server", $_[SESSION] );
            $heap->{client}->put($stuff);
        },
    },
);
}

sub logevent {
    my ( $state, $session, $arg ) = @_;
    my $id = $session->ID();

    print "session $id $state ";
    print ": $arg" if ( defined $arg );
    print "\n";
}

warn 'running';
$poe_kernel->run();
exit 0;
######################################################

Reply via email to