#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper::Simple;
use File::Basename;
use Module::Load;
use POE;
use POE::Component::Client::TCP;
use POE::Filter::Stream;
use SM::Constants;
# Recursively load all Packet:: modules
# Get a list of modules that we need loaded
my $modules = load_modules("SM::Server::Packet");
# Start them
foreach my $module (@$modules) {
load $module;
}
my $port = 9500;
my $host = "localhost"; # The host to test.
POE::Component::Client::TCP->new(
RemoteAddress => $host,
RemotePort => $port,
Filter => "POE::Filter::Stream",
# The client has connected. Display some status and prepare to
# gather information. Start a timer that will send ENTER if the
# server does not talk to us for a while.
Connected => sub {
print "connected to $host:$port ...\n";
my $heap = $_[HEAP];
$_[KERNEL]->delay("send", 1);
},
# The connection failed.
ConnectError => sub {
print "could not connect to $host:$port ...\n";
},
# The server has sent us something. Save the information. Stop
# the ENTER timer, and begin (or refresh) an input timer. The
# input timer will go off if the server becomes idle.
ServerInput => sub {
my ( $kernel, $heap, $input ) = @_[ KERNEL, HEAP, ARG0 ];
print "got input from $host:$port ...\n";
my ($data_type, $sub_type) = unpack "nn", $input;
#print Dumper($data_type, $sub_type);
#print Dumper(AVATAR, AVATAR_LOCATION);
if ( ( $data_type == AVATAR ) && ( $sub_type == AVATAR_INITIAL ) ) {
my @packet = unpack "nnaa", $input;
print Dumper(@packet);
}
else {
my @packet = unpack "nnnaa", $input;
print Dumper(@packet);
}
},
# These are handlers for additional events not included in the
# default Server::TCP module. In this example, they handle
# timers that have gone off.
InlineStates =>
{ # The server has not sent us anything yet. Send an ENTER
# keystroke (really a network newline, \x0D\x0A), and wait
# some more.
send_packet => sub {
print "sending enter on $host:$port ...\n";
my $out_packet = pack "nnnaa", AVATAR, AVATAR_LOCATION, 3, 1, 1;
$_[HEAP]->{server}->put($out_packet);
},
}
);
# Run the clients until the last one has shut down.
$poe_kernel->run();
exit 0;
# Dynamic module loader
sub load_modules {
my @modules_to_load = @_;
my @modules;
my @module_paths;
my @already_done;
foreach my $module_name (@modules_to_load) {
push @already_done, ($module_name);
foreach my $inc_path (@INC) {
my $path = $module_name;
my @counter = split /::/, $path;
my $count = scalar @counter;
$path =~ s/::/\//g;
my $full_path = $inc_path . "/" . $path . "/";
if ( -e $full_path ) {
foreach my $module ( glob "$full_path*.pm" ) {
# print $module_name;
#require $module;
my $full_module_name = $module_name . "::"
. ( fileparse( $module, ".pm" ) );
push @modules_to_load, ($full_module_name);
}
}
}
}
push @modules, @already_done;
return \@modules;
}