lib/Net/HL7/Connection.pm

filter: Statementstmt 82.5% 52 / 63 Branchbran 33.3% 4 / 12 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 73.9% 71 / 96 CC 9 SCAR 12.7
CC
9
Cov
73%
send
8.5
_init
2.0
_connect
2.0
CRAP 3.6
« lib/Net/HL7/Segment.pm ↑ Summary lib/Net/HL7/Segments/MSH.pm »
1 package Net::HL7::Connection;
2  
3 3 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Net::HL7::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 3 our $MESSAGE_PREFIX = "\013";
59 3 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 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
78 1 bless my $self = {}, $class;
79
80 1 ▶ $self->_init(@_) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init(@_) or return undef 0 1 1
81
82 1 return $self;
83 }
84  
85  
86 sub _init {
87  
88 1 ▶ my ($self, $host, $port, %arg) = @_;
Subroutine _init: called CC=2 SCAR=7.1
89
90 1 ▶ $self->{Timeout} = $arg{Timeout} || 10;
Condition50%1/2
$arg{'Timeout'} || 10
l!l
01
MC/DC0%0/1
$arg{'Timeout'} || 10
$arg{'Timeout'}
Truth table50%1/2
A
A= $arg{'Timeout'}
Aresult
11
00
91 1 $self->{HANDLE} = $self->_connect($host, $port);
92 }
93  
94  
95 sub _connect {
96  
97 1 ▶ my ($self, $host, $port) = @_;
Subroutine _connect: called CC=2 SCAR=7.1
98  
99 my $remote = IO::Socket::INET->new
100 (
101 Proto => "tcp",
102 PeerAddr => $host,
103 PeerPort => $port,
104 Timeout => $self->{Timeout}
105 )
106 1 ▶ ||
Condition50%1/2
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $host, 'PeerPort', $port, 'Timeout', $self->{'Timeout'}) || (return undef)
l!l
10
MC/DC0%0/1
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $host, 'PeerPort', $port, 'Timeout', $self->{'Timeout'}) || (return undef)
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $host, 'PeerPort', $port, 'Timeout', $self->{'Timeout'})
Truth table50%1/2
A
A= 'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $host, 'PeerPort', $port, 'Timeout', $self->{'Timeout'})
Aresult
11
00
107 return undef;
108
109 1 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 1 ▶ my ($self, $req) = @_;
Subroutine send: called CC=6 SCAR=21.4
Pod documented
125  
126 1 my $buff;
127 1 my $handle = $self->{HANDLE};
128 1 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 1 local $/;
135  
136 1 $/ = $MESSAGE_SUFFIX;
137  
138 # Send message, prefixed with HL7 message start symbol(s)
139 # Use an alarm in for the timeout.
140 1 eval {
141 1 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
142 1 alarm $self->{Timeout};
143 1 print $handle $MESSAGE_PREFIX . $hl7Msg . $MESSAGE_SUFFIX;
144 1 alarm 0;
145 };
146 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
147 0 ▶ if ($@ eq "alarm\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "alarm\n") 0 0 0
148 0 $! = ETIMEDOUT();
149 0 return undef;
150 }
151 0 die $@;
152 }
153  
154 # Read response in slurp mode
155 1 eval {
156 1 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
157 1 alarm $self->{Timeout};
158 1 $buff = <$handle>;
159 1 alarm 0;
160 };
161 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
162 0 ▶ if ($@ eq "alarm\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "alarm\n") 0 0 0
163 0 $! = ETIMEDOUT();
164 0 return undef;
165 }
166 0 die $@;
167 }
168 }
169  
170 # Remove message prefix and suffix
171 1 ▶ if (defined($buff)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buff) { } 1 0 1
172 1 $buff =~ s/^$MESSAGE_PREFIX//;
173 1 $buff =~ s/$MESSAGE_SUFFIX$//;
174
175 1 return new Net::HL7::Response($buff);
176 } else {
177 0 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 1 ▶ my $self = shift;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
192  
193 1 $self->{HANDLE}->close();
194 }
195  
196 3 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
« lib/Net/HL7/Segment.pm ↑ Summary lib/Net/HL7/Segments/MSH.pm »