POE-Component-Client-TCPMulti

 view release on metacpan or  search on metacpan

lib/POE/Component/Client/TCPMulti.pm  view on Meta::CPAN

                        $user_code{Disconnected}->(@_);
                        
                        # Blow shit up
                        delete $_[CHEAP];
#                   }
    
                    # Its either gone and we're out of synch (shouldn't happen),
                    # or we want to wait for a clean shutdown.
                    return;
#               } 
#           }    
            # Our wheel is dead if we didn't return above.
            # This is kind of redundant, but much of this module is.
            $_[KERNEL]->alarm_remove ( delete $heap{$_[ARG0]}{-ALARM} );

            push @_, $heap{$_[ARG0]};
            $user_code{Disconnected}->(@_);
    
            delete $_[CHEAP];
            delete $heap{$_[ARG0]};
    
# Don't do this unless we're flushed...
# delete $heap{$_[ARG0]};
        },
    
        #   }}}
        #   die:            Gracefully close all sockets {{{
        # Shutdown quick, clean and gracefull. 

        die         => sub {
            $_[KERNEL]->call(shutdown => $_) for keys %heap;
            $_[KERNEL]->alias_remove($_) for $_[KERNEL]->alias_list;
            $_[KERNEL]->alarm_remove_all;
        },

        #   }}}
        # }}}
    }; 
    # }}}
    # Session Constructor {{{

    POE::Session->create
        ( inline_states => { %{ delete $user_code{inline_states} }, %$code },
          object_states     => delete $user_code{object_states},
          package_states    => delete $user_code{package_states},
          options           => delete $user_code{options},
          args              => delete $user_code{args},
        );

    # }}}
}

# }}}
# Connect Method {{{

sub connect {
    my %Options = @_[1..$#_];
    $Options{Heap} ||= {};

    printf STDERR "!!! -> connect method called from %s:%d\n",
           (caller)[1,2] if DEBUG;


    my $server = POE::Wheel::SocketFactory->new
        ( RemoteAddress => $Options{RemoteAddress},
          RemotePort    => $Options{RemotePort},
          BindAddress   => $Options{BindAddress},
          BindPort      => $Options{BindPort},
          SuccessEvent  => '-success',
          FailureEvent  => '-failure',
          Reuse         => 'yes',
        );
    
    my $id = $server->ID; 

    printf TRACE "->connect(count %d, id %d, host (%s:%d) %s:%d);\n",
           scalar keys %heap, $id, @Options{qw( RemoteAddress RemotePort )},
           (caller)[1,2] if TRACE_CONNECT;

    $heap{$id} = bless {
        %{ $Options{Heap} },
        -ID         => $server->ID,
        -ADDR       => $Options{RemoteAddress},
        -PORT       => $Options{RemotePort},
        -BINDA      => $Options{BindAddress},
        -BINDP      => $Options{BindPort},
        -RUNNING    => 1,
        -TIMEOUT    => $Options{Timeout},
        -SERVER     => $server,
        -STAMP      => time,
    }, __PACKAGE__ . "::CHEAP";
    
    if ($heap{$id}{-TIMEOUT}) {
        $heap{$id}{-ALARM}  = $poe_kernel->delay_set
            ( -timeout => $heap{$id}{-TIMEOUT}, $id, $heap{$id}{email});
    }
    else {
        $heap{$id}{-ALARM} = 0;
    }

    printf "%d ++ Connecting %s:%d \n", $id, @{ $heap{$id} }{qw( -ADDR -PORT )}
        if DEBUG;

    return $heap{$id};
}

# }}}
# CHEAP Package {{{

package POE::Component::Client::TCPMulti::CHEAP;
use POE::Kernel;

#   Attribute Accessors {{{
sub ID {
    shift->{-ID}
}
sub ADDR {
    shift->{-ADDR}
}
sub PORT {
    shift->{-PORT}
}
#   }}}
#   Filter Settings {{{

sub filter {
    shift->{-SERVER}->set_filter( shift->new(@_) );
}

sub input_filter {
    shift->{-SERVER}->set_input_filter( shift->new(@_) );
}

sub output_filter {
    shift->{-SERVER}->set_output_filter( shift->new(@_) );
}

# }}}



( run in 2.828 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )