| 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 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 76 | |||||||||||||
| 77 | 1 | my $class = shift; | |||||||||||
| 78 | 1 | bless my $self = {}, $class; | |||||||||||
| 79 | |||||||||||||
| 80 | 1 | ▶ | $self->_init(@_) || return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | |||||||||||||
| 82 | 1 | return $self; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | |||||||||||||
| 86 | 1 | ▶ | sub _init { | ||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.1
| |||||||||||||
| 87 | |||||||||||||
| 88 | 1 | my ($self, $host, $port, %arg) = @_; | |||||||||||
| 89 | |||||||||||||
| 90 | 1 | ▶ | $self->{Timeout} = $arg{Timeout} || 10; | ||||||||||
|
Condition50%1/2
$arg{'Timeout'} || 10
MC/DC0%0/1
$arg{'Timeout'} || 10
$arg{'Timeout'}
Truth table50%1/2
A
A= $arg{'Timeout'}
| |||||||||||||
| 91 | 1 | $self->{HANDLE} = $self->_connect($host, $port); | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | 1 | ▶ | sub _connect { | ||||||||||
|
Subroutine
_connect: called CC=2 SCAR=7.1
| |||||||||||||
| 96 | |||||||||||||
| 97 | 1 | 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 | 1 | ▶ | || | ||||||||||
|
Condition50%1/2
'IO::Socket::INET'->new('Proto', 'tcp', 'PeerAddr', $host, 'PeerPort', $port, 'Timeout', $self->{'Timeout'}) || (return undef)
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'})
| |||||||||||||
| 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 | 1 | ▶ | sub send { | ||||||||||
|
Subroutine
send: called CC=6 SCAR=21.4
Pod
documented
| |||||||||||||
| 123 | |||||||||||||
| 124 | 1 | my ($self, $req) = @_; | |||||||||||
| 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 | 0 | ▶ | 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
| |||||||||||||
| 147 | 0 | ▶ | if ($@ eq "alarm\n") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 0 | $! = ETIMEDOUT(); | |||||||||||
| 149 | 0 | return undef; | |||||||||||
| 150 | } | ||||||||||||
| 151 | 0 | die $@; | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | # Read response in slurp mode | ||||||||||||
| 155 | 1 | eval { | |||||||||||
| 156 | 0 | ▶ | 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
| |||||||||||||
| 162 | 0 | ▶ | if ($@ eq "alarm\n") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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 | 1 | ▶ | sub close { | ||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 190 | |||||||||||||
| 191 | 1 | my $self = shift; | |||||||||||
| 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 | ||||||||||||