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
|
use strict;
{
package TPSTParseWords;
use base qw(POE::Filter);
use Text::ParseWords;
my $VERSION = '1.02';
sub new {
my $class = shift;
my %opts = @_;
$opts{lc $_} = delete $opts{$_} for keys %opts;
$opts{keep} = 0 unless $opts{keep};
$opts{delim} = '\s+' unless $opts{delim};
$opts{BUFFER} = [];
bless \%opts, $class;
}
sub get {
my ($self, $raw) = @_;
my $events = [];
push @$events, [ parse_line( $self->{delim}, $self->{keep}, $_ ) ] for @$raw;
return $events;
}
sub get_one_start {
my ($self, $raw) = @_;
push @{ $self->{BUFFER} }, $_ for @$raw;
}
sub get_one {
my $self = shift;
my $events = [];
my $event = shift @{ $self->{BUFFER} };
push @$events, [ parse_line( $self->{delim}, $self->{keep}, $event ) ] if defined $event;
return $events;
}
sub put {
warn "PUT is unimplemented\n";
return;
}
sub clone {
my $self = shift;
my $nself = { };
$nself->{$_} = $self->{$_} for keys %{ $self };
$nself->{BUFFER} = [ ];
return bless $nself, ref $self;
}
}
use Socket;
use Test::More;
use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line);
use Test::POE::Server::TCP;
plan tests => 11;
my %data = (
tests => [
[ 'Howdy!' => '"This is just a test" "line" "so there"' ],
[ 'bleh' => 'quit' ],
],
);
POE::Session->create(
package_states => [
'main' => [qw(
_start
_sock_up
_sock_fail
_sock_in
_sock_err
testd_registered
testd_connected
testd_disconnected
testd_client_input
)],
],
heap => \%data,
);
$poe_kernel->run();
exit 0;
sub _start {
$_[HEAP]->{testd} = Test::POE::Server::TCP->spawn(
address => '127.0.0.1',
port => 0,
options => { trace => 0 },
inputfilter => TPSTParseWords->new(),
outputfilter => POE::Filter::Line->new(),
);
isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' );
return;
}
sub testd_registered {
my ($heap,$object) = @_[HEAP,ARG0];
isa_ok( $object, 'Test::POE::Server::TCP' );
$heap->{port} = $object->port();
$heap->{factory} = POE::Wheel::SocketFactory->new(
RemoteAddress => '127.0.0.1',
RemotePort => $heap->{port},
SuccessEvent => '_sock_up',
FailureEvent => '_sock_fail',
);
return;
}
sub _sock_up {
my ($heap,$socket) = @_[HEAP,ARG0];
delete $heap->{factory};
$heap->{socket} = POE::Wheel::ReadWrite->new(
Handle => $socket,
InputEvent => '_sock_in',
ErrorEvent => '_sock_err',
);
return;
}
sub _sock_fail {
my $heap = $_[HEAP];
delete $heap->{factory};
$heap->{testd}->shutdown();
return;
}
sub _sock_in {
my ($heap,$input) = @_[HEAP,ARG0];
my @parms = split /\s+/, $input;
my $test = shift @{ $heap->{tests} };
if ( $test and $test->[0] eq $parms[0] ) {
pass($input);
$heap->{socket}->put( $test->[1] );
return;
}
pass($input);
return;
}
sub _sock_err {
delete $_[HEAP]->{socket};
pass("Disconnected");
$_[HEAP]->{testd}->shutdown();
return;
}
sub testd_connected {
my ($heap,$state,$id) = @_[HEAP,STATE,ARG0];
$heap->{testd}->send_to_client( $id, 'Howdy!' );
pass($state);
return;
}
sub testd_disconnected {
pass($_[STATE]);
return;
}
sub testd_client_input {
my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1];
my $testd = $_[SENDER]->get_heap();
pass($_[STATE]);
if ( $input->[0] eq 'quit' ) {
$testd->disconnect( $id );
$testd->send_to_client( $id, 'Buh Bye!' );
return;
}
ok( ( $input->[0] eq 'This is just a test' and $input->[1] eq 'line' and $input->[2] eq 'so there' ) , 'Test Get' );
$testd->send_to_client( $id, 'bleh' );
return;
}
|