1 # File: Stem/Test/UDP.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::UDP ;
31 use Test::More tests => 7 ;
38 This is the name under which this Cell was registered.
43 'name' => 'send_addr',
45 The Cell address of a sending port
49 'name' => 'send_host',
51 The UDP packet is sent to this host if the send message has no host
55 'name' => 'send_port',
57 The UDP packet is sent to this port if the send message has no port
65 my( $class ) = shift ;
67 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
68 return $self unless ref $self ;
71 $self->{'udp_send_obj'} = Stem::UDPMsg->new() ;
73 #print $self->{'udp_send_obj'}->status_cmd() ;
75 # create a private udp server object and save it.
77 $self->{'udp_recv_obj'} = Stem::UDPMsg->new(
85 #print $self->{'udp_recv_obj'}->status_cmd() ;
87 my $err = $self->{'udp_send_obj'}->send( "LOCAL send",
88 send_host => 'local_host',
92 ok( $err, 'bad host lookup' ) ;
94 $err = $self->{'udp_send_obj'}->send( \"LOCAL send",
95 send_host => 'localhost',
99 ok( !$err, 'good host lookup' ) ;
106 my( $self, $udp_data, $from_port, $from_host ) = @_ ;
108 my $ok = ${$udp_data} =~ /LOCAL send/ ;
110 ok( $ok, 'udp received') ;
112 #print "UDP [${$udp_data}]\n" ;
114 # now send out a bad and a good message to the udp send cell
116 my $udp_msg = Stem::Msg->new(
117 'to' => $self->{'send_addr'},
118 'from' => $self->{'reg_name'},
122 'send_port' => $self->{'send_port'},
126 $udp_msg->dispatch() ;
128 $udp_msg = Stem::Msg->new(
129 'to' => $self->{'send_addr'},
130 'from' => $self->{'reg_name'},
133 'data' => \"REMOTE foo",
134 'send_port' => $self->{'send_port'},
135 'send_host' => 'localhost',
139 #print $udp_msg->dump( 'UDP msg' ) ;
141 $udp_msg->dispatch() ;
148 ok(1, 'udp timeout') ;
150 # kill the receiver object so we can exit eventually
152 $self->{'udp_recv_obj'}->shut_down() ;
153 delete $self->{'udp_recv_obj'} ;
160 my( $self, $msg ) = @_ ;
162 ok(1, 'udp data in called') ;
164 my $udp_data = $msg->data()->{data} ;
166 my $ok = ${$udp_data} =~ /REMOTE/ ;
168 #print $msg->dump( 'UDP IN' ) ;
170 ok( $ok, 'udp data in') ;
172 # send a shutdown message to the udp receiver cell. with no more
173 # events it will cause the event loop to fall through and exit the
176 my $udp_msg = Stem::Msg->new(
177 'to' => $msg->from(),
178 'from' => $self->{'reg_name'},
179 'cmd' => 'shut_down',
182 $udp_msg->dispatch() ;
187 my( $self, $msg ) = @_ ;
189 ok(1, 'udp timeout in') ;
191 #print $msg->dump( 'UDP timeout IN' ) ;
198 my( $self, $msg ) = @_ ;
200 #print $msg->dump( 'UDP DATA' ) ;
202 my $data = $msg->data() ;
204 my $ok = ${$data} =~ /Missing send_host/ ;
206 ok($ok, 'udp error response') ;