blib/lib/Net/HL7/Daemon.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 25 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 176 CC 10 SCAR 18.1
CC
10
Cov
0%
accept
12.0
getNextRequest
12.0
sendAck
12.0
CRAP 6.1
1 package Net::HL7::Daemon;
2  
3 use IO::Socket qw(AF_INET INADDR_ANY inet_ntoa);
4 use base qw(IO::Socket::INET);
5 use strict;
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 my($class, %args) = @_;
43 $args{Listen} ||= 10;
44 $args{Proto} ||= 'tcp';
45 $args{Reuse} ||= 1;
46 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 my $self = shift;
67
68 my ($sock, $peer) = $self->SUPER::accept("Net::HL7::Daemon::Client");
69 if ($sock) {
70 ${*$sock}{'hl7d'} = $self;
71 return wantarray ? ($sock, $peer) : $sock;
72 } else {
73 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 my $self = shift;
88
89 my $addr = $self->sockaddr;
90 if (!$addr || $addr eq INADDR_ANY) {
91 require Sys::Hostname;
92 return lc(Sys::Hostname::hostname());
93 }
94 else {
95 return gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
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 my $self = shift;
112  
113 return $self->sockport;
114 }
115  
116  
117 package Net::HL7::Daemon::Client;
118  
119 use IO::Socket;
120 use IO::Socket::Timeout;
121 use base qw(IO::Socket::INET);
122 use Net::HL7::Request;
123 use Net::HL7::Messages::ACK;
124 use Net::HL7::Connection;
125 use strict;
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 my($class, %args) = @_;
154 $args{Timeout} ||= 10;
155  
156 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 my $self = shift;
181  
182 ${*$self}{'REQ'} && return ${*$self}{'REQ'};
183  
184 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 my $self = shift;
204  
205 IO::Socket::Timeout->enable_timeouts_on($self);
206  
207 $self->read_timeout(${*$self}{'io_socket_timeout'});
208 $self->write_timeout(${*$self}{'io_socket_timeout'});
209  
210 {
211 local $/ = $Net::HL7::Connection::MESSAGE_SUFFIX;
212  
213 use Errno qw(ETIMEDOUT EWOULDBLOCK);
214  
215 # slurrrp
216 my $buff = <$self>;
217
218 if (!$buff && ( 0+$! == ETIMEDOUT || 0+$! == EWOULDBLOCK )) {
219 return undef;
220 }
221  
222 if (not defined $buff) {
223 return undef;
224 }
225
226 # Remove HL7 pre- and suffix
227 #
228 $buff =~ s/^$Net::HL7::Connection::MESSAGE_PREFIX//;
229 $buff =~ s/$Net::HL7::Connection::MESSAGE_SUFFIX$//;
230
231 ${*$self}{'REQ'} = new Net::HL7::Request($buff);
232 }
233
234 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 my ($self, $res) = @_;
251  
252 # If this is true, we didn't get the incoming message yet!
253 if (! ${*$self}{'REQ'}) {
254 $self->getRequest() || return undef;
255 }
256  
257 if (! ref $res) {
258 $res = new Net::HL7::Messages::ACK(${*$self}{'REQ'});
259 }
260  
261 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 my ($self, $errMsg, $res) = @_;
278  
279 # If this is true, we didn't get the incoming message yet!
280 if (! ${*$self}{'REQ'}) {
281 $self->getRequest() || return undef;
282 }
283
284 if (! ref $res) {
285 $res = new Net::HL7::Messages::ACK(${*$self}{'REQ'});
286 }
287
288 $res->setAckCode("E", $errMsg);
289
290 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 my ($self, $res) = @_;
308  
309 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 1;