lib/Net/HL7/Daemon.pm

filter: Statementstmt 83.0% 83 / 100 Branchbran 33.3% 8 / 24 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 90.4% 19 / 21 Podpod 100% 4 / 4 totaltotal 66.6% 120 / 180 CC 21 SCAR 15.8
CC
21
Cov
67%
sendNack
20.0
getHost
6.0
getNextRequest
5.6
CRAP 4.9
« lib/Net/HL7/Messages/ACK.pm ↑ Summary lib/Net/HL7/Segment.pm »
1 package Net::HL7::Daemon;
2  
3 3 ▶use IO::Socket qw(AF_INET INADDR_ANY inet_ntoa);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw(IO::Socket::INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =pod
8  
9 =head1 NAME
10  
11 Net::HL7::Daemon
12  
13 =head1 SYNOPSIS
14  
15 my $d = new Net::HL7::Daemon( LocalPort => 12002, Listen => 5 );
16  
17 =head1 DESCRIPTION
18  
19 The Net::HL7::Daemon class provides a Daemon, roughly based on the
20 well known HTTP::Daemon class. The daemon wraps
21 L<IO::Socket::INET|IO::Socket::INET> so that incoming connections are
22 returned as Net::HL7::Daemon::Client objects. Other than that the
23 Daemon class doesn't do all that much. However, this allows you to use
24 the Daemon class as a base for more elaborate servers, like preforking
25 or multi-threaded servers. See the Perl Cookbook for examples on this,
26 and think Net::HL7::Daemon where you see IO::Socket.
27  
28 =head1 METHODS
29  
30 =over 4
31  
32 =item B<$d = new Net::HL7::Daemon()>
33  
34 Create a new instance of the Daemon class. Arguments are the same as
35 for the IO::Socket::INET. Default settings are: Listen = SOMAXCONN,
36 Proto = tcp, Reuse = 1.
37  
38 =cut
39  
40 sub new
41 {
42 3 ▶ my($class, %args) = @_;
Subroutine new: called CC=4 SCAR=14.6
Pod documented
43 3 ▶ $args{Listen} ||= 10;
Condition50%1/2
$args{"Listen"} ||= 10
l!l
03
MC/DC0%0/1
$args{"Listen"} ||= 10
$args{"Listen"}
Truth table50%1/2
A
A= $args{"Listen"}
Aresult
11
00
44 3 ▶ $args{Proto} ||= 'tcp';
Condition50%1/2
$args{"Proto"} ||= "tcp"
l!l
03
MC/DC0%0/1
$args{"Proto"} ||= "tcp"
$args{"Proto"}
Truth table50%1/2
A
A= $args{"Proto"}
Aresult
11
00
45 3 ▶ $args{Reuse} ||= 1;
Condition50%1/2
$args{"Reuse"} ||= 1
l!l
03
MC/DC0%0/1
$args{"Reuse"} ||= 1
$args{"Reuse"}
Truth table50%1/2
A
A= $args{"Reuse"}
Aresult
11
00
46 3 return $class->SUPER::new(%args);
47 }
48  
49 =pod
50  
51 =item B<accept([$pkg])>
52  
53 This method is the same as I<IO::Socket::accept> but returns an
54 I<Net::HL7::Daemon::Client> reference. It returns undef if
55 you specify a timeout and no connection is made within that time. In
56 a scalar context the returned value will be a reference to a object of
57 the I<Net::HL7::Daemon::Client> class which is another
58 I<IO::Socket::INET> subclass. In a list context a two-element array
59 is returned containing the new I<Net::HL7::Daemon::Client> reference
60 and the peer address; the list will be empty upon failure.
61  
62 =cut
63  
64 sub accept
65 {
66 1 ▶ my $self = shift;
Subroutine accept: called CC=3 SCAR=11.6
Pod documented
67
68 1 my ($sock, $peer) = $self->SUPER::accept("Net::HL7::Daemon::Client");
69 1 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) { } 1 0 1
70 1 ${*$sock}{'hl7d'} = $self;
71 1 ▶ return wantarray ? ($sock, $peer) : $sock;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
72 } else {
73 0 return;
74 }
75 }
76  
77 =pod
78  
79 =item B<getHost()>
80  
81 Returns the host where this daemon can be reached.
82  
83 =cut
84  
85 sub getHost
86 {
87 1 ▶ my $self = shift;
Subroutine getHost: called CC=4 SCAR=17.9
Pod documented
88
89 1 my $addr = $self->sockaddr;
90 1 ▶ if (!$addr || $addr eq INADDR_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $addr or $addr eq "\000\000\000\000") { } 1 0 1
Condition33%1/3
not $addr or $addr eq "\0\0\0\0"
l!l&&r!l&&!r
010
MC/DC0%0/2
not $addr or $addr eq "\0\0\0\0"
not $addr $addr eq "\0\0\0\0"
Truth table33%1/3
not(A) or B
A= not $addrB= $addr eq "\0\0\0\0"
ABresult
1X1
011
000
91 1 require Sys::Hostname;
92 1 return lc(Sys::Hostname::hostname());
93 }
94 else {
95 0 ▶ return gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
Condition0%0/3
gethostbyaddr($addr, 2) || inet_ntoa($addr)
l!l&&r!l&&!r
000
MC/DC0%0/2
gethostbyaddr($addr, 2) || inet_ntoa($addr)
gethostbyaddr($addr, 2) inet_ntoa($addr)
Truth table0%0/3
A || B
A= gethostbyaddr($addr, 2)B= inet_ntoa($addr)
ABresult
1X1
011
000
96 }
97 }
98  
99 =pod
100  
101 =item B<getPort()>
102  
103 Returns the port on which this daemon is listening.
104  
105 =back
106  
107 =cut
108  
109 sub getPort {
110  
111 1 ▶ my $self = shift;
Subroutine getPort: called CC=1 SCAR=0.0
Pod documented
112  
113 1 return $self->sockport;
114 }
115  
116  
117 package Net::HL7::Daemon::Client;
118  
119 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 3 ▶use IO::Socket::Timeout;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 3 ▶use base qw(IO::Socket::INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
122 3 ▶use Net::HL7::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 3 ▶use Net::HL7::Messages::ACK;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 3 ▶use Net::HL7::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127  
128 =pod
129  
130 =head1 NAME
131  
132 Net::HL7::Daemon::Client
133  
134 =head1 DESCRIPTION
135  
136 The I<Net::HL7::Daemon::Client> is also a I<IO::Socket::INET>
137 subclass. Instances of this class are returned by the accept() method
138 of I<Net::HL7::Daemon>.
139  
140 =head1 METHODS
141  
142 =over 4
143  
144 =item B<$d = new Net::HL7::Daemon::Client()>
145  
146 Create a new instance of the Client class. Arguments are the same as
147 for the IO::Socket::INET. Normally, you shouldn't do this...
148  
149 =cut
150  
151 sub new
152 {
153 1 ▶ my($class, %args) = @_;
Subroutine new: called CC=2 SCAR=7.1
154 1 ▶ $args{Timeout} ||= 10;
Condition50%1/2
$args{"Timeout"} ||= 10
l!l
01
MC/DC0%0/1
$args{"Timeout"} ||= 10
$args{"Timeout"}
Truth table50%1/2
A
A= $args{"Timeout"}
Aresult
11
00
155  
156 1 return $class->SUPER::new(%args);
157 }
158  
159 =pod
160  
161 =item B<getRequest()>
162  
163 Get the current request on this client. The current request is either
164 the request that has been read by the getNextRequest() method, or if
165 that hasn't been called yet, the request read from the socket. The
166 latter is implemented by calling getNextRequest. If both fail,
167 C<undef> is returned.
168 In case of failure, then the I<Net::HL7::Daemon::Client>
169 object ($c) should be discarded, and you should not call this method
170 again.
171  
172 Potentially, a HL7 client can receive more than one
173 message. So discard the client only when there's no more requests
174 pending, or the delivering service might experience timeouts.
175  
176 =cut
177  
178 sub getRequest
179 {
180 1 ▶ my $self = shift;
Subroutine getRequest: called CC=2 SCAR=7.4
181  
182 1 ▶ ${*$self}{'REQ'} && return ${*$self}{'REQ'};
Branch: 1 branch
BranchTrueFalseTotal
do { *$self }->{"REQ"} and return do { *$self }->{"REQ"} 0 1 1
183  
184 1 return $self->getNextRequest();
185 }
186  
187 =pod
188  
189 =item B<getNextRequest()>
190  
191 Read data from the socket and turn it into an I<Net::HL7::Request>
192 object which is then returned. It returns C<undef> if reading of the
193 request fails. If it fails, then the I<Net::HL7::Daemon::Client>
194 object ($c) should be discarded, and you should not call this method
195 again. Potentially, a HL7 client can receive more than one
196 message. So discard the client only when there's no more requests
197 pending, or the delivering service might experience timeouts.
198  
199 =cut
200  
201 sub getNextRequest
202 {
203 1 ▶ my $self = shift;
Subroutine getNextRequest: called CC=5 SCAR=17.1
204  
205 1 IO::Socket::Timeout->enable_timeouts_on($self);
206  
207 1 $self->read_timeout(${*$self}{'io_socket_timeout'});
208 1 $self->write_timeout(${*$self}{'io_socket_timeout'});
209  
210 {
211 1 local $/ = $Net::HL7::Connection::MESSAGE_SUFFIX;
212  
213 3 ▶ use Errno qw(ETIMEDOUT EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 # slurrrp
216 1 my $buff = <$self>;
217
218 1 ▶ if (!$buff && ( 0+$! == ETIMEDOUT || 0+$! == EWOULDBLOCK )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $buff and 0 + $! == 110 || 0 + $! == 11) 0 1 1
Condition16%1/6
not $buff and 0 + $! == 110 || 0 + $! == 11
!ll&&!rl&&r
100
0 + $! == 110 or 0 + $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/3
not $buff and 0 + $! == 110 || 0 + $! == 11
not $buff 0 + $! == 110 0 + $! == 11
Truth table25%1/4
not(A) and B or C
A= not $buffB= 0 + $! == 110C= 0 + $! == 11
ABCresult
0XX0
1000
11X1
1011
219 0 return undef;
220 }
221  
222 1 ▶ if (not defined $buff) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $buff) 0 1 1
223 0 return undef;
224 }
225
226 # Remove HL7 pre- and suffix
227 #
228 1 $buff =~ s/^$Net::HL7::Connection::MESSAGE_PREFIX//;
229 1 $buff =~ s/$Net::HL7::Connection::MESSAGE_SUFFIX$//;
230
231 1 ${*$self}{'REQ'} = new Net::HL7::Request($buff);
232 }
233
234 1 return ${*$self}{'REQ'};
235 }
236  
237 =pod
238  
239 =item B<sendAck($res)>
240  
241 Write a I<Net::HL7::Messages::ACK> message to the client as a
242 response, to signal success. You may provide your own
243 Net::HL7::Response, but it is better to rely on the ACK that is
244 generated internally.
245  
246 =cut
247  
248 sub sendAck {
249  
250 1 ▶ my ($self, $res) = @_;
Subroutine sendAck: called CC=4 SCAR=15.5
251  
252 # If this is true, we didn't get the incoming message yet!
253 1 ▶ if (! ${*$self}{'REQ'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do { *$self }->{"REQ"}) 0 1 1
254 0 ▶ $self->getRequest() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->getRequest or return undef 0 0 0
255 }
256  
257 1 ▶ if (! ref $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res) 1 0 1
258 1 $res = new Net::HL7::Messages::ACK(${*$self}{'REQ'});
259 }
260  
261 1 print $self $Net::HL7::Connection::MESSAGE_PREFIX . $res->toString() .
262 $Net::HL7::Connection::MESSAGE_SUFFIX;
263 }
264  
265 =pod
266  
267 =item B<sendNack($errMsg, $res)>
268  
269 Write a I<Net::HL7::Messages::ACK> message to the client as a
270 response, with the Acknowledge Code (MSA(1)) set to CE or AE,
271 depending on the original request, to signal an error.
272  
273 =cut
274  
275 sub sendNack {
276  
277 0 ▶ my ($self, $errMsg, $res) = @_;
Subroutine sendNack: not called CC=4 SCAR=30.0
278  
279 # If this is true, we didn't get the incoming message yet!
280 0 ▶ if (! ${*$self}{'REQ'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do { *$self }->{"REQ"}) 0 0 0
281 0 ▶ $self->getRequest() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->getRequest or return undef 0 0 0
282 }
283
284 0 ▶ if (! ref $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res) 0 0 0
285 0 $res = new Net::HL7::Messages::ACK(${*$self}{'REQ'});
286 }
287
288 0 $res->setAckCode("E", $errMsg);
289
290 0 print $self $Net::HL7::Connection::MESSAGE_PREFIX . $res->toString() .
291 $Net::HL7::Connection::MESSAGE_SUFFIX;
292 }
293  
294 =pod
295  
296 =item B<sendResponse($res)>
297  
298 Write a I<Net::HL7::Reponse> object to the client as a response. This
299 can hold an arbitrary HL7 message.
300  
301 =back
302  
303 =cut
304  
305 sub sendResponse {
306  
307 0 ▶ my ($self, $res) = @_;
Subroutine sendResponse: not called CC=1 SCAR=6.9
308  
309 0 print $self $Net::HL7::Connection::MESSAGE_PREFIX . $res->toString() .
310 $Net::HL7::Connection::MESSAGE_SUFFIX;
311 }
312  
313 =pod
314  
315 =head1 SEE ALSO
316  
317 RFC 2068
318  
319 L<IO::Socket::INET>
320  
321 =head1 COPYRIGHT
322  
323 Copyright 2003, D.A.Dokter
324  
325 This library is free software; you can redistribute it and/or
326 modify it under the same terms as Perl itself.
327  
328 =cut
329  
330 3 1;
« lib/Net/HL7/Messages/ACK.pm ↑ Summary lib/Net/HL7/Segment.pm »