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

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 94 CC 6 SCAR 28.9
CC
6
Cov
0%
send
42.0
new
2.0
_init
2.0
CRAP 18.0
1 package Net::HL7::Connection;
2  
3 use 5.004;
4 use strict;
5 use Net::HL7::Response;
6 use IO::Socket;
7 use Errno qw(:POSIX);
8  
9 =head1 NAME
10  
11 Net::HL7::Connection - An HL7 connection
12  
13 =head1 SYNOPSIS
14  
15  
16 use Net::HL7::Connection;
17 use Net::HL7::Request;
18  
19 my $conn = new Net::HL7::Connection('localhost', 8089);
20  
21 my $req = new Net::HL7::Request();
22  
23 ... set some request attributes
24  
25 my $res = $conn->send($req);
26  
27 $conn->close();
28  
29  
30 =head1 DESCRIPTION
31  
32 The Net::HL7::Connection object represents the tcp connection to the
33 HL7 message broker. The Connection has only two useful methods (apart
34 from the constructor), send and close. The 'send' method takes a
35 L<Net::HL7::Request|Net::HL7::Request> as argument, and returns a
36 L<Net::HL7::Response|Net::HL7::Response>. The send method can be used
37 more than once, before the connection is closed.
38  
39 =head1 FIELDS
40  
41 The Connection object holds the following fields:
42  
43 =over 4
44  
45 =item MESSAGE_PREFIX
46  
47 The prefix to be sent to the HL7 server to initiate the
48 message. Defaults to \013.
49  
50 =item MESSAGE_SUFFIX
51  
52 End of message signal for HL7 server. Defaults to \034\015.
53  
54 =back
55  
56 =cut
57  
58 our $MESSAGE_PREFIX = "\013";
59 our $MESSAGE_SUFFIX = "\034\015";
60  
61  
62 =head1 METHODS
63  
64 The following methods are available:
65  
66 =over 4
67  
68 =item B<$c = new Net::HL7::Connection( $host, $port[, Timeout => timeout] )>
69 Creates a connection to a HL7 server, or returns undef when a
70 connection could not be established. timeout is optional, and will
71 default to 10 seconds.
72  
73 =cut
74  
75 sub new {
76
77 my $class = shift;
78 bless my $self = {}, $class;
79
80 $self->_init(@_) || return undef;
81
82 return $self;
83 }
84  
85  
86 sub _init {
87  
88 my ($self, $host, $port, %arg) = @_;
89
90 $self->{Timeout} = $arg{Timeout} || 10;
91 $self->{HANDLE} = $self->_connect($host, $port);
92 }
93  
94  
95 sub _connect {
96  
97 my ($self, $host, $port) = @_;
98  
99 my $remote = IO::Socket::INET->new
100 (
101 Proto => "tcp",
102 PeerAddr => $host,
103 PeerPort => $port,
104 Timeout => $self->{Timeout}
105 )
106 ||
107 return undef;
108
109 return $remote;
110 }
111  
112  
113 =pod
114  
115 =item B<send($request)>
116  
117 Sends a L<Net::HL7::Request|Net::HL7::Request> object over this
118 connection.
119  
120 =cut
121  
122 sub send {
123  
124 my ($self, $req) = @_;
125  
126 my $buff;
127 my $handle = $self->{HANDLE};
128 my $hl7Msg = $req->toString();
129  
130 # Setting separators to HL7 defaults, so print and read operations
131 # will do the whole message at once.
132 #
133 {
134 local $/;
135  
136 $/ = $MESSAGE_SUFFIX;
137  
138 # Send message, prefixed with HL7 message start symbol(s)
139 # Use an alarm in for the timeout.
140 eval {
141 local $SIG{ALRM} = sub { die "alarm\n" };
142 alarm $self->{Timeout};
143 print $handle $MESSAGE_PREFIX . $hl7Msg . $MESSAGE_SUFFIX;
144 alarm 0;
145 };
146 if ($@) {
147 if ($@ eq "alarm\n") {
148 $! = ETIMEDOUT();
149 return undef;
150 }
151 die $@;
152 }
153  
154 # Read response in slurp mode
155 eval {
156 local $SIG{ALRM} = sub { die "alarm\n" };
157 alarm $self->{Timeout};
158 $buff = <$handle>;
159 alarm 0;
160 };
161 if ($@) {
162 if ($@ eq "alarm\n") {
163 $! = ETIMEDOUT();
164 return undef;
165 }
166 die $@;
167 }
168 }
169  
170 # Remove message prefix and suffix
171 if (defined($buff)) {
172 $buff =~ s/^$MESSAGE_PREFIX//;
173 $buff =~ s/$MESSAGE_SUFFIX$//;
174
175 return new Net::HL7::Response($buff);
176 } else {
177 return $buff;
178 }
179 }
180  
181 =pod
182  
183 =item B<close()>
184  
185 Close the connection.
186  
187 =cut
188  
189 sub close {
190  
191 my $self = shift;
192  
193 $self->{HANDLE}->close();
194 }
195  
196 1;
197  
198  
199 =pod
200  
201 =back
202  
203 =head1 AUTHOR
204  
205 D.A.Dokter <dokter@wyldebeast-wunderliebe.com>
206  
207 =head1 LICENSE
208  
209 Copyright (c) 2002 D.A.Dokter. All rights reserved. This program is
210 free software; you can redistribute it and/or modify it under the same
211 terms as Perl itself.
212  
213 =cut