Message.pm

filter: Statementstmt 30.0% 69 / 230 Branchbran 13.2% 17 / 128 Conditioncond 7.6% 5 / 65 MC/DCmcdc 2.5% 1 / 40 Subroutinesub 48.0% 24 / 50 Podpod 54.7% 23 / 42 totaltotal 25.0% 139 / 555 CC 90 SCAR 44.0
CC
90
Cov
22%
_autoRouting
342.0
read
182.0
_postOrCall
110.0
CRAP 81.2
↑ Summary Message/Header.pm »
1 # -*- Perl -*-
2 #
3 # File: POE/Event/Message.pm
4 # Desc: A generic messaging protocol - to use as a starting point
5 # Date: Mon Oct 10 10:35:59 2005
6 # Stat: Prototype, Experimental
7 # Note: Fix to remove message length limitation contributed
8 # by Martin Holste, Sun 21 Feb 2010
9 #
10 package POE::Event::Message;
11 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $PACK = __PACKAGE__;
16 3 our $VERSION = '0.11';
17 ### @ISA = qw( );
18  
19 # WARN: Don't use POE or the POE::Kernel classes here. This is a
20 # generic messaging class that can also be used outside of
21 # the POE environment. Support is included for sending and
22 # receiving messages across sockets (such as between a POE-
23 # based server process and a non-POE client process), and
24 # when sending messages across file handles (as in the case
25 # of a POE-based parent process communicating with a non-POE
26 # child process).
27 #
28  
29 ### POE::Kernel; # Don't use POE here!
30 3 ▶use POE::Event::Message::Header; # Header class wraps attributes
Subroutine BEGIN: called CC=1 SCAR=0.0
31 ### POE::Event::Message::Body; # ((not yet created))
32 3 ▶use POE::Filter::Reference; # Perl object filtering
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use POE::Driver::SysRW; # sysread/syswrite driver
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 my $HeaderClass = "POE::Event::Message::Header";
36 ## $BodyClass = "POE::Event::Message::Body"; # ((not yet created))
37 3 our $Driver = new POE::Driver::SysRW;
38 3 our $Filter = new POE::Filter::Reference;
39  
40 #-------------------------------------------------------------------------
41 # Message Creation (new message and reply message)
42 # Envelope method to create new message with body data
43  
44 sub new
45 3 ▶{ my($class,$header,$body) = @_;
Subroutine new: called CC=5 SCAR=16.7
Pod documented
46  
47 # This method allows for the following usage:
48 # . Creating a new message
49 # $message = $msgClass->new();
50 # $message = $msgClass->new( undef, $msgBody ); # see 'package' method
51 #
52 # . Creating a reply to an existing message
53 # $response = $msgClass->new( $message->header() ); # header obj
54 # $response = $msgClass->new( $message ); # message obj
55 #
56 3 ▶ if (ref($header) and $header =~ /=HASH/ and $header->can("new") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $header and $header =~ /=HASH/ and $header->can('new')) { } 1 2 3
Condition66%4/6
ref $header and $header =~ /=HASH/ and $header->can("new")
!ll&&!rl&&r
201
ref $header and $header =~ /=HASH/
!ll&&!rl&&r
201
MC/DC33%1/3
ref $header and $header =~ /=HASH/ and $header->can("new")
ref $header $header =~ /=HASH/ $header->can("new")
Truth table50%2/4
A and B and C
A= ref $headerB= $header =~ /=HASH/C= $header->can("new")
ABCresult
0XX0
10X0
1100
1111
57 1 my $msg = $header;
58 1 $header = $HeaderClass->new( $msg->header() ),
59  
60 } else {
61 2 $header = $HeaderClass->new( $header ),
62 }
63  
64 3 ▶ return( bless my $self = {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
65 header => $header,
66 body => $body,
67 ## body => $BodyClass->new( $body ), # ((no BodyClass yet))
68 }, ref($class)||$class
69 );
70 }
71  
72 3 *envelope = \&package;
73  
74 sub package
75 0 ▶{ my($class,$msgBody) = @_;
Subroutine package: not called CC=1 SCAR=6.9
Pod documented
76 0 return $class->new("", $msgBody); # create new message "envelope"
77 }
78  
79 #-----------------------------------------------------------------------
80 # Pass through methods for message header object
81 # and a method to get/set the message body
82  
83 0 ▶sub set { $_[0]->{header} and $_[0]->{header}->set ( $_[1], $_[2] ) }
Subroutine set: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->set($_[1], $_[2]) 0 0 0
84 0 ▶sub get { $_[0]->{header} and $_[0]->{header}->get ( $_[1] ) }
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->get($_[1]) 0 0 0
85 0 ▶sub del { $_[0]->{header} and $_[0]->{header}->del ( $_[1] ) }
Subroutine del: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->del($_[1]) 0 0 0
86 0 ▶sub param { $_[0]->{header} and $_[0]->{header}->param ( $_[1], $_[2] ) }
Subroutine param: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->param($_[1], $_[2]) 0 0 0
87 0 ▶sub setErr { $_[0]->{header} and $_[0]->{header}->setErr( $_[1], $_[2] ) }
Subroutine setErr: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->setErr($_[1], $_[2]) 0 0 0
88 0 ▶sub status { $_[0]->{header} and $_[0]->{header}->status() }
Subroutine status: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->status 0 0 0
89 0 ▶sub stat { $_[0]->{header} and $_[0]->{header}->stat() }
Subroutine stat: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->stat 0 0 0
90 0 ▶sub err { $_[0]->{header} and $_[0]->{header}->err() }
Subroutine err: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->err 0 0 0
91  
92 0 ▶sub getMode { $_[0]->{header} and $_[0]->{header}->mode() }
Subroutine getMode: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->mode 0 0 0
93 0 ▶sub setMode { $_[0]->{header} and $_[0]->{header}->mode( $_[1] ) }
Subroutine setMode: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->mode($_[1]) 0 0 0
94 2 ▶sub delRouteBack { $_[0]->{header} and $_[0]->{header}->delRouteBack() }
Subroutine delRouteBack: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->delRouteBack 2 0 2
95 2 ▶sub delRouteTo { $_[0]->{header} and $_[0]->{header}->delRouteTo() }
Subroutine delRouteTo: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->delRouteTo 2 0 2
96  
97 3 *delete = \&del;
98 3 *reset = \&del;
99 3 *inResponseToId = \&r2id;
100  
101 1 ▶sub hasRouting { $_[0]->{header} and $_[0]->{header}->hasRouting() }
Subroutine hasRouting: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->hasRouting 1 0 1
102 2 ▶sub hasRouteTo { $_[0]->{header} and $_[0]->{header}->hasRouteTo() }
Subroutine hasRouteTo: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->hasRouteTo 2 0 2
103 2 ▶sub hasRouteBack { $_[0]->{header} and $_[0]->{header}->hasRouteBack() }
Subroutine hasRouteBack: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"header"} and $_[0]{"header"}->hasRouteBack 2 0 2
104  
105 3 *getRouting = \&hasRouting;
106 3 *getRouteTo = \&hasRouteTo;
107 3 *getRouteBack = \&hasRouteBack;
108  
109 0 ▶sub nextRouteType { $_[0]->{header}->nextRouteType() if $_[0]->{header}}
Subroutine nextRouteType: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"header"} 0 0 0
110 2 ▶sub nextRouteIsRemote { $_[0]->{header}->nextRouteIsRemote() if $_[0]->{header}}
Subroutine nextRouteIsRemote: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"header"} 2 0 2
111 2 ▶sub nextRouteIsLocal { $_[0]->{header}->nextRouteIsLocal() if $_[0]->{header}}
Subroutine nextRouteIsLocal: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"header"} 2 0 2
112 2 ▶sub nextRouteIsPost { $_[0]->{header}->nextRouteIsPost() if $_[0]->{header}}
Subroutine nextRouteIsPost: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"header"} 2 0 2
113 2 ▶sub nextRouteIsCall { $_[0]->{header}->nextRouteIsCall() if $_[0]->{header}}
Subroutine nextRouteIsCall: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"header"} 2 0 2
114  
115 2 ▶sub id { $_[0]->{header}->get('id') } # unique message ID
Subroutine id: called CC=1 SCAR=0.0
Pod documented
116 1 ▶sub r2id { $_[0]->{header}->get('r2id')} # orig. unique message ID
Subroutine r2id: called CC=1 SCAR=0.0
Pod documented
117 1 ▶sub header { $_[0]->{header} } # get only
Subroutine header: called CC=1 SCAR=0.0
Pod undocumented
118 2 ▶sub body { $_[1] ? $_[0]->{body} = $_[1] : $_[0]->{body} } # get or set
Subroutine body: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 1 2
119  
120 sub addRouteTo
121 1 ▶{ my($self, @args) = @_;
Subroutine addRouteTo: called CC=2 SCAR=7.1
Pod documented
122 1 ▶ return undef unless $self->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"header"} 0 1 1
123 1 return $self->{header}->addRouteTo( @args );
124 }
125  
126 sub addRouteBack
127 1 ▶{ my($self, @args) = @_;
Subroutine addRouteBack: called CC=2 SCAR=7.1
Pod documented
128 1 ▶ return undef unless $self->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"header"} 0 1 1
129 1 return $self->{header}->addRouteBack( @args );
130 }
131  
132 sub addRemoteRouteTo
133 1 ▶{ my($self, @args) = @_;
Subroutine addRemoteRouteTo: called CC=2 SCAR=7.1
Pod documented
134 1 ▶ return undef unless $self->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"header"} 0 1 1
135 1 return $self->{header}->addRemoteRouteTo( @args );
136 }
137  
138 sub addRemoteRouteBack
139 1 ▶{ my($self, @args) = @_;
Subroutine addRemoteRouteBack: called CC=2 SCAR=7.1
Pod documented
140 1 ▶ return undef unless $self->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"header"} 0 1 1
141 1 return $self->{header}->addRemoteRouteBack( @args );
142 }
143  
144 #-----------------------------------------------------------------------
145 # Message Routing and Auto-Replies
146 # This is an experimental interface to provide a flexible,
147 # semi-automatic message routing mechanism. It provides
148 # the following features as an alternative to "postbacks"
149 # and extends the concept somewhat.
150 #
151 # . immediate routing via "post" (queued, asynchronous) - post method
152 # . immediate routing via "call" (direct, synchronous) - call method
153 #
154 # . forward routing, delayed (via "post" or "call) - addRouteTo
155 # . return routing, delayed (via "post" or "call) - addRouteBack
156 # . combinations of these two
157 #
158 # . forward routing, delayed remote (via socket "write") -addRemoteRouteTo
159 # . return routing, delayed remote (via socket "write") -addRemoteRouteBack
160 # . combinations of these two
161 #
162 # In addition, this allows the "stacking" of both forward and
163 # return destinations. This is the basis of the "semi-automatic
164 # routing" feature. The "route" method DOES need to be called
165 # on a message, but the caller DOESN'T need to know anything
166 # about the next destination(s).
167 #
168 # Some experimentation and experience will determine whether
169 # these are useful as designed, or if changes are needed.
170 # The INTENT is that intermediate destinations can be added
171 # to interrupt the original routing, TEMPORARIALY redirecting
172 # a message, while STILL allowing it to return as intended.
173  
174 #---------------------------------------------
175 # Direct message routing, via "post" or "call"
176  
177 0 ▶sub post { shift->_postOrCall( "post", @_ ) }
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
178 0 ▶sub call { shift->_postOrCall( "call", @_ ) }
Subroutine call: not called CC=1 SCAR=6.9
Pod documented
179  
180 sub _postOrCall # For immediate routing
181 0 ▶{ my($self, $mode, $session, $event, @args) = @_;
Subroutine _postOrCall: not called CC=10 SCAR=47.0
182  
183 0 ▶ if (! defined $INC{'POE/Kernel.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $INC{"POE/Kernel.pm"}) 0 0 0
184 0 return $self->setErr(-1, "'POE::Kernel' module is not loaded in '_postOrCall' method of '$PACK'");
185 }
186  
187 0 ▶ $session ||= POE::Kernel->get_active_session()->ID();
Condition0%0/3
$session ||= "POE::Kernel"->get_active_session->ID
l!l&&r!l&&!r
000
MC/DC0%0/2
$session ||= "POE::Kernel"->get_active_session->ID
$session "POE::Kernel"->get_active_session->ID
Truth table0%0/3
A ||= B
A= $sessionB= "POE::Kernel"->get_active_session->ID
ABresult
1X1
011
000
188  
189 0 ▶ return unless ($session and $event);
Branch: 1 branch
BranchTrueFalseTotal
unless $session and $event 0 0 0
Condition0%0/3
$session and $event
!ll&&!rl&&r
000
MC/DC0%0/2
$session and $event
$session $event
Truth table0%0/3
A and B
A= $sessionB= $event
ABresult
0X0
100
111
190 0 $mode = lc $mode; # a mode arg?
191 0 ▶ $mode ||= lc $self->getMode(); # use prior setting?
Condition0%0/3
$mode ||= lc $self->getMode
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode ||= lc $self->getMode
$mode lc $self->getMode
Truth table0%0/3
A ||= B
A= $modeB= lc $self->getMode
ABresult
1X1
011
000
192 0 ▶ $mode = "post" unless ($mode eq "call"); # default value!
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "call" 0 0 0
193  
194 # NOTE that this changes the default behavior of POE's post/call.
195 # Here, arguments are bundled into list references so the result
196 # is "more compatible" with POE's postback/callback mechanism.
197 #
198 # Here, we ensure that the arguments passed here are sent to the
199 # target event handler as a list reference to be received in ARG0.
200 # The "$self" var (current message) is sent as the only element in
201 # a list reference to be received in ARG1. This allows "$self" to
202 # be "received" as if it was sent by the method where post/call was
203 # invoked. In addition, this maintains a compatible result with
204 # the "addRouteTo()/addRouteBack()" method, below. Clear as mud?
205 # This is either a good idea or really cludgy. Time will tell.
206 #
207 # All message events sent via "post()", "call()" or "route()" will
208 # result in ONLY two parameters received by the target event handler.
209  
210 # issue one of post/call
211  
212 0 my $argRef;
213  
214 0 ▶ if ( ($#args == 0) and (ref($args[0]) eq "ARRAY") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#args == 0 and ref $args[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
$#args == 0 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$#args == 0 and ref $args[0] eq "ARRAY"
$#args == 0 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= $#args == 0B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
215 0 $argRef = $args[0];
216 } else {
217 0 $argRef = [ @args ];
218 }
219  
220 # FIX: handle 'call' return here as done in sub '_autoRouting'
221  
222 0 ▶ if (! POE::Kernel->$mode( $session, $event, $argRef, [ $self ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("POE::Kernel"->$mode($session, $event, $argRef, [$self])) 0 0 0
223  
224 0 my $msg = "Error(1): POE_Kernel->$mode( $session, $event, [ $argRef ], [ $self ] )";
225  
226 0 $self->setErr(-1, "$msg: $! in '$PACK'");
227 ## warn $msg;
228 }
229  
230 # warn "DEBUG: Route: POE_Kernel->$mode( $session, $event, @args )\n";
231  
232 0 return;
233 }
234  
235 #---------------------------------------------
236 # Automatic message routing, via "post" or "call"
237  
238 3 *autoroute = \&route;
239 3 *autoRoute = \&route;
240  
241 sub route # forward, if possible, or reply
242 0 ▶{ my($self,@newArgs) = @_;
Subroutine route: not called CC=3 SCAR=24.8
Pod documented
243  
244 # routeTo() until empty, then routeBack()
245 # 'post' returns: 1=routed, 0=nowhere to route
246 # 'call' returns: whatever 'called' method returns
247  
248 0 my(@list) = ();
249  
250 0 ▶ if ( $self->hasRouteTo() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->hasRouteTo) { } 0 0 0
elsif ($self->hasRouteBack) { } 0 0 0
251 0 (@list) = $self->routeTo( @newArgs );
252 } elsif ( $self->hasRouteBack() ) {
253 0 (@list) = $self->routeBack( @newArgs );
254 }
255  
256 0 return( @list );
257 }
258  
259 #---------------------------------------------
260 # Forward message routing, via "post" or "call"
261  
262 3 *forward = \&routeTo;
263 3 *routeto = \&routeTo;
264  
265 sub routeTo # activate "auto-forward", if any
266 0 ▶{ my($self,@newArgs) = @_;
Subroutine routeTo: not called CC=1 SCAR=6.9
Pod undocumented
267  
268 0 my(@list) = $self->_autoRouting( "delRouteTo", @newArgs );
269 0 return( @list );
270 }
271  
272 #---------------------------------------------
273 # Return message routing, via "post" or "call"
274  
275 3 *reply = \&routeBack;
276 3 *routeback = \&routeBack;
277  
278 sub routeBack # activate "auto-reply", if any
279 0 ▶{ my($self,@newArgs) = @_;
Subroutine routeBack: not called CC=1 SCAR=6.9
Pod undocumented
280  
281 0 my(@list) = $self->_autoRouting( "delRouteBack", @newArgs );
282 0 return( @list );
283 }
284  
285 #---------------------------------------------
286 # Generic message routing method
287  
288 sub _autoRouting # activate "auto-routing," if any
289 0 ▶{ my($self,$type,@newArgs) = @_;
Subroutine _autoRouting: not called CC=18 SCAR=58.3
290  
291 # This "generic" method is intended to be called by
292 # either the "routeTo" or "routeBack" method, which
293 # in turn might be invoked by the "route" method.
294  
295 0 ▶ return undef unless $self->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"header"} 0 0 0
296  
297 #---------------------------------------------------------------
298 # Depending on the "$type" collect a "RouteTo" or a "RouteBack"
299 # Note: "@origArgs" is now included for 'delRouteTo' type.
300  
301 0 ▶ $type = "delRouteBack" unless $type eq "delRouteTo";
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "delRouteTo" 0 0 0
302  
303 0 my($host, $port, $mode, $session, $event, @origArgs) = $self->$type();
304  
305 ##warn "($host, $port, $mode, $session, $event, @origArgs)\n";
306  
307 # Must have something to post/call and somewhere to send it!
308 # FIX: differentiate between call when "no routing"
309 # and call when "incomplete routing" attributes.
310 #
311 0 ▶ if (! ($session and $event) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session and $event) 0 0 0
Condition0%0/3
$session and $event
!ll&&!rl&&r
000
MC/DC0%0/2
$session and $event
$session $event
Truth table0%0/3
A and B
A= $sessionB= $event
ABresult
0X0
100
111
312 ## warn "DEBUG: session='$session' event='$event'";
313 0 return;
314 }
315  
316 #---------------------------------------------------------------
317 # FIX: ToDo: complete remote routing via '$host', '$port'
318 # using the "write" method, below. First, fix up message such
319 # that, upon being received, a "route()" will forward it on.
320 #
321 ###if ($host or $port) {
322  
323 0 ▶ if ($port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 0 0 0
324 0 ▶ $host ||= 'localhost';
Condition0%0/2
$host ||= "localhost"
l!l
00
MC/DC0%0/1
$host ||= "localhost"
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
325  
326 0 my $socket = $self->createSocket( $host, $port );
327 0 ▶ if (! $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 0 0 0
328 0 my $err = "Error: connection to '${host}:$port' refused in '$PACK'";
329  
330 # FIX: Do we overwrite the original error message here? or not??
331 # YES. This is a better (clearer) message here for the user.
332 #
333 # $self->setErr(-1, $err) unless $self->status();
334 0 $self->setErr(-1, $err);
335  
336 0 return $self;
337 }
338  
339 0 my($stat,$err) = $self->write( $socket );
340 0 ▶ if ($stat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat) 0 0 0
341 0 $self->setErr( $stat, $err );
342 0 return $self;
343 }
344  
345 0 my $response;
346  
347 # With a "call" or "sync" mode we expect an immediate
348 # response message from the server. With any other
349 # mode, such as "async", only a "successful send"
350 # response is generated here--this assumes that if
351 # an actual response message will be generated, an
352 # appropriate "addRemoteRouteBack" was added earlier.
353  
354 0 ▶ if (($mode eq "call") or ($mode eq "sync")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'call' or $mode eq 'sync') { } 0 0 0
Condition0%0/3
$mode eq "call" or $mode eq "sync"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "call" or $mode eq "sync"
$mode eq "call" $mode eq "sync"
Truth table0%0/3
A or B
A= $mode eq "call"B= $mode eq "sync"
ABresult
1X1
011
000
355 0 $response = $self->read( $socket );
356 } else {
357 # FIX: Should this be handled differently??
358 0 $response = $self->new( $self );
359 0 $response->setErr( 0, "message sent successfully" );
360 }
361  
362 0 $self->shutdownSocket();
363  
364 0 return $response;
365 }
366  
367 #---------------------------------------------------------------
368 # Determine whether we "post" (asych) or "call" (synch)
369  
370 0 ▶ $mode ||= lc $self->mode(); # use prior setting?
Condition0%0/3
$mode ||= lc $self->mode
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode ||= lc $self->mode
$mode lc $self->mode
Truth table0%0/3
A ||= B
A= $modeB= lc $self->mode
ABresult
1X1
011
000
371 0 ▶ $mode = "post" unless ($mode eq "call"); # default value!
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "call" 0 0 0
372  
373 #---------------------------------------------------------------
374 # If we're going to rely on POE, make sure it's available.
375  
376 0 ▶ if (! defined $INC{'POE/Kernel.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $INC{"POE/Kernel.pm"}) 0 0 0
377 0 $self->setErr(-1, "'POE::Kernel' module is not loaded in '_autoRouting' method of '$PACK'");
378 0 return undef;
379 }
380  
381 #---------------------------------------------------------------
382 # This now works as does 'post/call', when using 'routeTo'.
383 # STRANGE SYNTAX here, but it allows for reasonably compatible
384 # syntax (and a plug-compatible result) to POE's "postback"
385 # mechanism.
386 #
387 # NOTE that "$self" is prepended to the "@args" list here! This
388 # allows "$self" (current message) to be "received" as if it was
389 # sent by the method where the 'post/call/postback' was invoked.
390 # --- This obviously needs some well documented examples. ---
391  
392 0 my (@args);
393 0 ▶ if ($type eq "delRouteTo") { # issue a post/call
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'delRouteTo') { } 0 0 0
394 # This is kind of funky usage but, at the moment, it
395 # seems to be "the right thing." Feedback is welcome.
396 # Currently we PREPEND "$self" (current message) to
397 # any "new args" when "route()" method is called.
398 #
399 # Adding "$self" AFTER the orig args and BEFORE added args
400 # is either really good or really cludgy. Time will tell.
401 #
402 0 (@args) = ( [ @origArgs ], [ $self, @newArgs ] );
403  
404 } else { # emulate postback
405 # Currently, these are the same. Should the above change?
406 #
407 0 (@args) = ( [ @origArgs ], [ $self, @newArgs ] );
408 }
409  
410 0 my(@list) = POE::Kernel->$mode( $session, $event, @args );
411  
412 0 ▶ if (! @list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@list) 0 0 0
413  
414 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
415 0 my $err = "Error(2): POE_Kernel->$mode( $session, $event, @args )";
416  
417 0 $self->setErr(-1, "$err: $! in '$PACK'");
418 ## warn $err;
419 }
420 }
421  
422 ## warn "DEBUG: Route: POE_Kernel->$mode( $session, $event, @args )\n";
423  
424 # Here we return the same result as the POE::Kernel
425 # . for 'post' return a boolean indicating success
426 # . for 'call' return what the called method returns
427 #
428 0 ▶ return( @list ) if ($mode eq 'call');
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "call" 0 0 0
429 0 return 1;
430 }
431  
432 #-----------------------------------------------------------------------
433 # Reading and Writing messages, via POE::Filter::Reference,
434 # for use with file/socket handles. Note that the "$message"
435 # argument defaults to "$self" -- this allows for nice clean
436 # syntax in most situations: $message->write( $fh );
437  
438 3 *send = \&write;
439 3 *recv = \&read;
440  
441 sub write
442 0 ▶{ my($self,$fh,$message) = @_;
Subroutine write: not called CC=10 SCAR=47.0
Pod documented
443  
444 0 ▶ (defined($fh) and length($fh)) or return (1,"nowhere to write");
Branch: 1 branch
BranchTrueFalseTotal
defined $fh and length $fh or return 1, "nowhere to write" 0 0 0
Condition0%0/3
defined $fh and length $fh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fh and length $fh
defined $fh length $fh
Truth table0%0/3
A and B
A= defined $fhB= length $fh
ABresult
0X0
100
111
445  
446 # Support class OR object method
447 0 ▶ $message = $self if ((! $message) and ref($self));
Branch: 1 branch
BranchTrueFalseTotal
if not $message and ref $self 0 0 0
Condition0%0/3
not $message and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
not $message and ref $self
not $message ref $self
Truth table0%0/3
not(A) and B
A= not $messageB= ref $self
ABresult
0X0
100
111
448 0 ▶ return (1,"nothing to write") unless $message;
Branch: 1 branch
BranchTrueFalseTotal
unless $message 0 0 0
449  
450 ## warn "DEBUG: write message='$message'\n";
451  
452 0 ▶ my $tmp = delete $self->{_socket_} if (ref $self);
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
453 ## warn "DEBUG fh='$fh' in 'write()'";
454  
455 0 $Driver->put( $Filter->put( [ $message ] ) ); # queue the message
456 0 $Driver->flush( $fh ); # send the message
457  
458 0 ▶ $! and warn "OUCH: Driver/flush failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$! and warn "OUCH: Driver/flush failed: $!" 0 0 0
459 ## $! or warn "OKAY: Driver/flush succeeded";
460  
461 0 ▶ $self->{_socket_} = $tmp if ($tmp and ref $self);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp and ref $self 0 0 0
Condition0%0/3
$tmp and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
$tmp and ref $self
$tmp ref $self
Truth table0%0/3
A and B
A= $tmpB= ref $self
ABresult
0X0
100
111
462 0 return(0,""); # return success
463 }
464  
465 sub read
466 0 ▶{ my($self,$fh) = @_;
Subroutine read: not called CC=13 SCAR=52.0
Pod documented
467  
468 ## warn "DEBUG: fh='$fh' in 'read()'";
469  
470 0 ▶ (defined($fh) and length($fh)) or return (1,"nowhere to read");
Branch: 1 branch
BranchTrueFalseTotal
defined $fh and length $fh or return 1, "nowhere to read" 0 0 0
Condition0%0/3
defined $fh and length $fh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fh and length $fh
defined $fh length $fh
Truth table0%0/3
A and B
A= defined $fhB= length $fh
ABresult
0X0
100
111
471  
472 0 my $response = $Driver->get( $fh ); # fetch a reply
473  
474 0 ▶ if (! $response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
475 0 $response = $self->new();
476 0 $response->setErr(-1, "no response from server");
477 0 return $response;
478 }
479  
480 #-------------------------------------------------------------------
481 # Fix to remove message length limitation contributed
482 # by Martin Holste, Sun 21 Feb 2010:
483 # Parse expected length from the beginning of the first payload
484 0 ▶ unless (ref($response) eq 'ARRAY' and $response->[0] =~ /^(\d+)\0/){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $response eq "ARRAY" and $response->[0] =~ /^(\d+)\0/) 0 0 0
Condition0%0/3
ref $response eq "ARRAY" and $response->[0] =~ /^(\d+)\0/
!ll&&!rl&&r
000
MC/DC0%0/2
ref $response eq "ARRAY" and $response->[0] =~ /^(\d+)\0/
ref $response eq "ARRAY" $response->[0] =~ /^(\d+)\0/
Truth table0%0/3
A and B
A= ref $response eq "ARRAY"B= $response->[0] =~ /^(\d+)\0/
ABresult
0X0
100
111
485 0 $response = $self->new();
486 0 $response->setErr(-1, 'Error: Did not receive expected payload length at beginning of payload');
487 0 return $response;
488 }
489 0 my $expected_length = $1 + length($1) + 1;
490 0 my $received = length($response->[0]);
491 0 ▶ READ_LOOP: while ($received < $expected_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($received < $expected_length) 0 0 0
492 0 my $buf = $Driver->get($fh);
493 0 foreach (@$buf){
494 0 push @$response, $_;
495 0 $received += length($_);
496 }
497 }
498 #-------------------------------------------------------------------
499 # filter the reply, or report error at this line:
500 0 ($response) = @{ $Filter->get( $response ) }; my $line = __LINE__;
501  
502 0 ▶ if (($response) and ($response =~ /=HASH/) and ($response->can( "body" ))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response =~ /=HASH/ and $response->can('body')) { } 0 0 0
Condition0%0/6
$response and $response =~ /=HASH/ and $response->can("body")
!ll&&!rl&&r
000
$response and $response =~ /=HASH/
!ll&&!rl&&r
000
MC/DC0%0/3
$response and $response =~ /=HASH/ and $response->can("body")
$response $response =~ /=HASH/ $response->can("body")
Truth table0%0/4
A and B and C
A= $responseB= $response =~ /=HASH/C= $response->can("body")
ABCresult
0XX0
10X0
1100
1111
503 # response is okay
504 } else {
505 0 ▶ my $body = $response ||"";
Condition0%0/2
$response || ''
l!l
00
MC/DC0%0/1
$response || ''
$response
Truth table0%0/2
A
A= $response
Aresult
11
00
506 0 $response = $self->new();
507 0 $response->body( $body );
508 0 my $err = "Error: unknown message type in 'read' in '$PACK' at line $line";
509 0 $err .= "\nmessage = '$body'";
510 0 ▶ $err .= " (was message too big to send?)" if (! $body);
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 0 0
511 0 $response->setErr(-1, $err );
512 }
513  
514 0 return $response; # return the reply
515 }
516  
517 sub createSocket
518 0 ▶{ my($self,$host,$port) = @_;
Subroutine createSocket: not called CC=5 SCAR=34.0
Pod undocumented
519  
520 0 ▶ $host ||= 'localhost';
Condition0%0/2
$host ||= "localhost"
l!l
00
MC/DC0%0/1
$host ||= "localhost"
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
521 0 ▶ if (! $port) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $port) { } 0 0 0
elsif (not defined $INC{'IO/Socket.pm'}) { } 0 0 0
522 0 $self->setErr(-1, "Error: port is undefined in 'socket' in '$PACK'" );
523 0 return undef;
524  
525 } elsif (! defined $INC{'IO/Socket.pm'} ) {
526 0 $self->setErr(-1, "'IO::Socket' module is not loaded in 'socket' method of '$PACK'");
527 0 return undef;
528 }
529  
530 0 my $socket = IO::Socket::INET->new( 'PeerAddr' => $host,
531 'PeerPort' => $port,
532 'Proto' => 'tcp', );
533 0 ▶ if (! $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 0 0 0
534 0 $self->setErr(-1, "Error: IO_Socket_INET failed in '$PACK': $!" );
535 0 return undef;
536 }
537  
538 0 $self->{_socket_} = $socket;
539 0 return $socket;
540 }
541  
542 sub getOpenSocket
543 0 ▶{ my($self) = @_;
Subroutine getOpenSocket: not called CC=2 SCAR=17.9
Pod undocumented
544 0 ▶ return $self->{_socket_} || undef;
Condition0%0/2
$self->{'_socket_'} || undef
l!l
00
MC/DC0%0/1
$self->{'_socket_'} || undef
$self->{'_socket_'}
Truth table0%0/2
A
A= $self->{'_socket_'}
Aresult
11
00
545 }
546  
547 sub shutdownSocket
548 0 ▶{ my($self, $socket) = @_;
Subroutine shutdownSocket: not called CC=2 SCAR=17.9
Pod undocumented
549  
550 0 ▶ $socket ||= $self->getOpenSocket();
Condition0%0/3
$socket ||= $self->getOpenSocket
l!l&&r!l&&!r
000
MC/DC0%0/2
$socket ||= $self->getOpenSocket
$socket $self->getOpenSocket
Truth table0%0/3
A ||= B
A= $socketB= $self->getOpenSocket
ABresult
1X1
011
000
551  
552 0 $socket->shutdown(2); # proper socket etiquette:
553 0 $socket->close(); # a shutdown then a close.
554  
555 0 return;
556 }
557  
558 #-----------------------------------------------------------------------
559 # Somewhat useful for debugging ... replace with a better dumper.
560  
561 0 ▶sub dump {
Subroutine dump: not called CC=3 SCAR=24.8
Pod documented
562 0 my($self)= @_;
563 0 my($pack,$file,$line)=caller();
564 0 my $text = "DEBUG: ($PACK\:\:dump)\n self='$self'\n";
565 0 $text .= "CALLER $pack at line $line\n ($file)\n";
566 0 my $value;
567 0 foreach my $param (sort keys %$self) {
568 0 $value = $self->{$param};
569 0 $value = $self->zeroStr( $value, "" ); # handles value of "0"
570 0 $text .= " $param = $value\n";
571 }
572 0 $text .= "_" x 25 ."\n";
573  
574 0 ▶ if (ref($self->{header})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"header"}) 0 0 0
575 0 $text .= "header:\n";
576 0 $text .= $self->{header}->dump("nohead");
577 }
578 0 return($text);
579 }
580  
581 sub zeroStr
582 0 ▶{ my($self,$value,$undef) = @_;
Subroutine zeroStr: not called CC=4 SCAR=30.0
Pod undocumented
583 0 ▶ return $undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
584 0 ▶ return "0" if (length($value) and ! $value);
Branch: 1 branch
BranchTrueFalseTotal
if length $value and not $value 0 0 0
Condition0%0/3
length $value and not $value
!ll&&!rl&&r
000
MC/DC0%0/2
length $value and not $value
length $value not $value
Truth table0%0/3
A and not(B)
A= length $valueB= not $value
ABresult
0X0
100
111
585 0 return $value;
586 }
587 #_________________________
588 3 1; # Required by require()
589  
590 __END__
↑ Summary Message/Header.pm »