Msmgr/Connection.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 221 CC 24 SCAR 35.5
CC
24
Cov
0%
connect
90.0
_deq_command
42.0
_send_message
42.0
CRAP 34.7
1 #
2 # $Id: Connection.pm,v 0.16 2003/08/07 00:01:59 lawrence Exp lawrence $
3 #
4  
5 package Net::Msmgr::Connection;
6 use strict;
7 use warnings;
8 use IO::Socket::INET;
9 use Digest::MD5;
10 use Carp;
11  
12 use Net::Msmgr qw(:debug);
13 use Net::Msmgr::Command;
14 use Net::Msmgr::Object;
15  
16 our @ISA = qw ( Net::Msmgr::Object );
17  
18 =pod
19  
20 =head1 NAME
21  
22 Net::Msmgr::Connection
23  
24 =head1 SYNOPSIS
25  
26 use Net::Msmgr::Connection;
27  
28 my $session = Net::Msmgr::Sesssion->new(....);
29 my $connection = new Net::Msmgr::Connection;
30  
31 $connection->nserver( ip-address or hostname );
32 $connection->nsport( 1863 ); # default port #
33 $connection->name('Descriptive Name'); # used in debugging output
34 $connection->session($session); # associate with a session
35 $connection->debug($debugflags); # lots of options here
36 $connection->connect; # open the connection
37
38 =head1 DESCRIPTION
39  
40 Net::Msmgr::Connection is the encapsulation for a connection to an Net::Msmgr
41 Dispatch Server, Notification Server, or Switchboard Server. It will
42 instantiate Event watchers to empty the transmit queue as messages are
43 sent through it, and dispatch messages as they are received from the
44 network, calling a list of per-message handlers.
45  
46 =head1 CONSTRUCTOR
47  
48 my $connection = new Net::Msmgr::Connection( ... );
49  
50 - or -
51  
52 my $connection = Net::Msmgr::Command->new( ... );
53  
54 Constructor parameters are:
55  
56 =over
57  
58 =item nserver (optional)
59  
60 The server to connect to. Defaults to messenger.hotmail.com, which is
61 the current Dispatch Server.
62  
63 =item nsport (optional)
64  
65 The port to connect to. All transfer messages from the network will
66 include a port, and you ought to honor them, but they appear to
67 currently always be 1863, which is the default.
68  
69 =item name (optional)
70  
71 A descriptive name. It will show up from time to time in some
72 debugging output. It defaults to 'Dispatch Server', which correlates
73 to the default IP address.
74  
75 =item session (mandatory)
76  
77 Correlates the connection to the Net::Msmgr::Session in which it lives.
78  
79 =item debug (optional)
80  
81 See the manpage for Net::Msmgr.pm for the full list of debug flags and their meanings.
82  
83  
84 =back
85  
86 =head1 INSTANCE METHODS
87  
88 =over
89  
90 =cut
91  
92 sub _fields
93 {
94 return shift->SUPER::_fields, ( send_queue => undef,
95 send_queue_flag => 0,
96 recv_buffer => '',
97 socket => undef,
98 nserver => 'messenger.hotmail.com',
99 nsport => 1863,
100 open => 0,
101 handlers => undef,
102 name => 'Dispatch Server',
103 _closeflag => 0,
104 session => undef,
105 debug => 256 );
106 }
107  
108 =pod
109  
110 =item $connection->shutdown;
111  
112 Sends an 'OUT' message to the associated server, and marks the
113 connection for closure.
114  
115 =cut
116  
117 sub shutdown
118 {
119 my $self = shift;
120 my $cmd = new Net::Msmgr::Command(cmd => 'OUT',
121 type => Net::Msmgr::Command::Async);
122 $cmd->send($self);
123 $self->close;
124 }
125  
126 =pod
127  
128 =item $connection->close;
129  
130 Immediately close this connection.
131  
132 =cut
133  
134 sub close
135 {
136 my $self = shift;
137  
138 &{$self->session->disconnect_handler}($self) if $self->session->disconnect_handler;
139 $self->{socket}->close;
140
141 print STDERR "Connection to $self->{nserver}:$self->{nsport} ($self->{name}) closed\n"
142 if $self->{debug} & DEBUG_CLOSE;
143 $self->{open} = 0;
144 undef $self->{socket};
145 }
146  
147 sub send($$)
148 {
149 my $self = shift;
150 my $message = shift;
151  
152 push @{$self->{send_queue}}, $message;
153 $self->{send_queue_flag}++; # trigger watcher
154 $self->_send_message;
155 }
156  
157 sub _deq_command
158 {
159 my $self = shift;
160 return unless $self->{recv_buffer} =~ m/\r\n/m;
161 my $joy = 1;
162 do
163 {
164 (my $command, $self->{recv_buffer}) = split("\r\n",$self->{recv_buffer},2);
165
166 #
167 # Parse NS commands
168 #
169
170 my ($cmd, @parms) = split(' ',$command);
171 my $object = new Net::Msmgr::Command( cmd => $cmd, connection => $self, params => \@parms );
172 if ($cmd eq 'MSG')
173 {
174 $object->type(Net::Msmgr::Command::Payload);
175 my $have = length($self->{recv_buffer});
176 if ($have < $parms[2] )
177 { # waiting for more input -- Put The Candle Back
178 substr($self->{recv_buffer},0,0,$command . "\r\n");
179 undef $object;
180 $joy = 0; # and we're going to have to wait
181 }
182 else
183 { # dequeue entire message
184 $object->body(substr($self->{recv_buffer},0,$parms[2],''));
185 }
186 }
187 else
188 {
189 $object->type(Net::Msmgr::Command::Normal);
190 }
191
192 #
193 # now, with the object, call all the handlers registered for it in order
194 #
195
196 if ($object) # might get thrown away
197 {
198 print STDERR "<-- " . $object->as_text if $self->debug & DEBUG_COMMAND_RECV;
199 $self->session->dispatch_all($self, $object);
200 }
201 } while ($joy && $self->{recv_buffer} =~ m/\r\n/m); # keep going while there is content in the buffer
202 }
203  
204 =pod
205  
206 =item $connection->add_handler( $handler, @classes )
207  
208 For each message in any of @classes, call the handler associated with $handler.
209  
210 Message Handlers can be registered for each of the inbound messages.
211 All message handlers are called with at least one parameter, the
212 Net::Msmgr::Command object encapsulating the message. Optionally, you can at
213 registration time add extra parameters to that list.
214  
215 Handlers look like 'methodname' which will turn into a
216 $session->methodname($command); If handler is an array ref, the first
217 element is a session handler, and the following elements will be
218 passed as the second through nth parameters to that handler. A
219 handler 'nonmethod' exists to allow you to call arbitrary code.
220  
221 An example:
222  
223 my $code = sub { my ($c,$t) = @_; print STDERR $t , $c->as_text } ;
224 $ns->add_handler( [ 'nonmethod', $code, 'test_handler' ] , 'QNG' );
225  
226 =cut
227  
228 sub add_handler($$@)
229 {
230 my $self = shift;
231 my $handler = shift;
232 my @command_classes = @_;
233  
234 $handler = [ $handler ] unless ref($handler) eq 'ARRAY';
235  
236 foreach my $cc (@command_classes)
237 {
238 push @{$self->{handlers}->{$cc}}, $handler;
239 }
240 }
241  
242 sub _send_message($)
243 {
244 my $self = shift;
245 my $sock = $self->{socket};
246  
247 unless ($sock && $sock->connected)
248 {
249 print STDERR "Not connected" if $self->{debug} & DEBUG_CONFUSED;
250 return;
251 }
252  
253 return unless $#{$self->{send_queue}} >= 0;
254  
255 my $message = shift @{$self->{send_queue}};
256 $sock->syswrite($message);
257  
258 print STDERR "$self->{name} >>>$message" if $self->{debug} & DEBUG_PACKET_SEND;
259  
260 if ($self->{_closeflag} && ($#{$self->{send_queue}} < 0))
261 {
262 $self->close;
263 }
264 }
265  
266 sub _close
267 {
268 my $self = shift;
269 my $event = shift;
270  
271 # generate pseudo command so session can close things off
272  
273 my $command = new Net::Msmgr::Command;
274 $command->type(Net::Msmgr::Command::Pseudo);
275 $command->cmd('close');
276 $self->session->dispatch_all($self, $command);
277
278 }
279  
280 sub _recv_message
281 {
282 my $self = shift;
283 # my $event = shift;
284  
285 my $buf;
286 my $socket = $self->{socket};
287 if ($socket->connected)
288 {
289 while (my $count = sysread($socket, $buf, 80))
290 {
291 $self->{recv_buffer} .= $buf;
292 print STDERR "$self->{name} <<<$buf" if $self->{debug} & DEBUG_PACKET_RECV;
293 }
294 $self->_deq_command;
295 }
296 else
297 {
298 $self->_close;
299 }
300 }
301  
302 =pod
303  
304 =item $connection->connect;
305  
306 Opens the connection, and sets up the Event watchers.
307  
308 =cut
309  
310 sub connect
311 {
312 my $self = shift;
313  
314 carp "Somebody has been sleeping in my bead ... " if $self->{socket};
315 carp "And he is still here!" if $self->{socket} && $self->{socket}->connected;
316  
317 $self->open(0);
318  
319 unless ($self->nserver && $self->nsport)
320 {
321 carp 'missing server or port';
322 return undef;
323 }
324  
325 print STDERR $self->name . ' connecting to: ' , $self->nserver , ':', $self->nsport ,"\n"
326 if $self->debug & DEBUG_OPEN;
327  
328 $self->{socket} = new IO::Socket::INET ( PeerAddr => $self->nserver,
329 PeerPort => $self->nsport,
330 Proto => 'tcp');
331  
332 carp 'No socket open' unless $self->{socket};
333  
334 return unless $self->{socket};
335  
336 $self->open(1) if $self->{socket}->connected;
337  
338 print STDERR "$self->{name} connected\n"
339 if ($self->{socket}->connected && ($self->debug & DEBUG_OPEN ));
340 $self->{socket}->autoflush(1);
341 $self->{socket}->blocking(0);
342  
343 # if ($self->session->domain eq 'Event')
344 # {
345 # $self->{recv_watcher} = Event->io(fd => $self->{socket},
346 # cb => [ $self, '_recv_message' ],
347 # poll => 're',
348 # desc => 'recv_watcher',
349 # repeat => 1);
350 # }
351 # elsif ($self->session->domain eq 'Perl::Tk')
352 # {
353 # $main::mw->fileevent($self->{socket},
354 # 'readable',
355 # sub { $self->_recv_message } );
356 # }
357  
358 return $self;
359 }
360  
361 1;
362  
363 #
364 # $Log: Connection.pm,v $
365 # Revision 0.16 2003/08/07 00:01:59 lawrence
366 # Initial Release
367 #
368 #