1 # File: Stem/Test/PacketIO.pm
3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
6 # Stem is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Stem is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Stem; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # For a license to use the Stem under conditions other than those
21 # described here, to purchase support for this software, or to purchase a
22 # commercial warranty contract, please contact Stem Systems at:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
29 package Stem::Test::PacketIO ;
33 use Stem::Route qw( register_cell unregister_cell ) ;
36 use base 'Stem::Cell' ;
43 This is the name under which this Cell was registered.
51 The port to use for the SockMsg cells.
55 'name' => 'write_addr',
57 The Cell address of a sending port
61 'name' => 'cell_attr',
62 'class' => 'Stem::Cell',
64 Argument list passed to Stem::Cell for this Cell
75 bless( { abc => 1 }, 'PIO_class' ),
76 { bar => 'xyz', qwert => 3 },
79 hash => { qwert => 3 },
83 my @codecs = qw( YAML Storable Data::Dumper SimpleHash ) ;
84 #my @codecs = qw( SimpleHash ) ;
85 @codecs = grep { eval "require Stem::Codec::$_" } @codecs ;
87 plan tests => @msg_data * @codecs ;
91 my( $class ) = shift ;
93 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
94 return $self unless ref $self ;
96 my $flow_text = <<FLOW ;
100 create_sock_msg_pair ;
108 $self->cell_flow_init( 'test', $flow_text ) ;
110 $self->cell_flow_go_in() ;
119 my $codec = $self->{'codec'} ;
121 # we send to the client hence to the server, on to echo, back to the
122 # server and through the client all the way to here
124 foreach my $data ( @msg_data ) {
126 if ( $codec eq 'SimpleHash' ) {
128 if ( ref $data ne 'HASH' ) {
131 'skip SimpleHash only allows hash refs for data') ;
135 if ( grep ref $_, values %{$data} ) {
138 'skip SimpleHash only allows single level hashes for data') ;
143 my $msg = Stem::Msg->new(
144 'to' => "client_$codec",
145 'from' => $self->{'reg_name'},
150 #print $msg->dump("MSG OUT") ;
153 push( @{$self->{'sent_data'}}, $data ) ;
161 my( $self, $msg ) = @_ ;
163 #print $msg->dump( 'PACKET IN' ) ;
165 my $recv_data = $msg->data() ;
167 my $sent_data = shift @{$self->{'sent_data'}} ;
169 #print "SENT [$sent_data]\nGOT[$recv_data]\n" ;
171 my $data_type = ref $sent_data || 'scalar' ;
173 is_deeply( $recv_data, $sent_data, "$self->{'codec'} - $data_type " ) ;
175 unless ( @{$self->{'sent_data'}} ) {
177 $self->destroy_sock_msg_pair() ;
178 $self->cell_flow_go_in() ;
191 exit unless @codecs ;
195 return( $self->{codec} = shift @codecs ) ;
198 sub create_sock_msg_pair {
202 my $codec = $self->{'codec'} ;
204 #print "CREATE [$codec]\n" ;
206 my $server_name = "server_$codec" ;
208 my $server_sock = Stem::SockMsg->new(
209 reg_name => $server_name,
210 port => ++$self->{port},
213 'data_addr' => 'echo',
218 #print "SERVER [$server_sock]\n" ;
219 die $server_sock unless ref $server_sock ;
220 my $err = register_cell( $server_sock, $server_name ) ;
221 $err and die "register error: $err" ;
223 $self->{server_cell} = $server_sock ;
224 $self->{server_name} = $server_name ;
226 my $client_name = "client_$codec" ;
228 my $client_sock = Stem::SockMsg->new(
229 reg_name => $client_name,
230 port => $self->{port},
234 'data_addr' => 'packet_io',
238 #print "CLIENT [$client_sock]\n" ;
240 die $client_sock unless ref $client_sock ;
241 register_cell( $client_sock, $client_name ) ;
242 $self->{client_cell} = $client_sock ;
243 $self->{client_name} = $client_name ;
248 sub destroy_sock_msg_pair {
252 my $codec = $self->{'codec'} ;
254 #print "DESTROY [$codec]\n" ;
256 foreach my $type ( qw( server client ) ) {
258 my $sock_msg = delete $self->{"${type}_cell"} ;
259 # my $sock_msg = delete $self->{"${type}_$codec"} ;
260 unregister_cell( $sock_msg ) ;
261 $sock_msg->shut_down() ;