lib/Net/EGTS/Simple.pm

filter: Statementstmt 84.9% 96 / 113 Branchbran 36.3% 8 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 22 / 26 Podpod 100% 5 / 5 totaltotal 78.9% 131 / 166 CC 12 SCAR 14.1
CC
12
Cov
77%
_response
11.1
__ANON__
6.0
connect
6.0
CRAP 4.1
« lib/Net/EGTS/Record.pm ↑ Summary lib/Net/EGTS/Types.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Net::EGTS::Simple;
4 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Net::EGTS::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::EGTS::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::EGTS::Codes;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Net::EGTS::Packet;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Net::EGTS::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Net::EGTS::SubRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Net::EGTS::Packet::Appdata;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Net::EGTS::SubRecord::Auth::DispatcherIdentity;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Net::EGTS::SubRecord::Teledata::PosData;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 Net::EGTS::Simple - simple socket transport
24  
25 =cut
26  
27 # Timeout, sec. (0 .. 255)
28 2 ▶use constant EGTS_SL_NOT_AUTH_TO => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Response timeout
31 2 ▶use constant EGTS_TL_RESPONSE_ТО => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 # Resend attempts if timeout TL_RESPONSE_ТО
33 2 ▶use constant EGTS_TL_RESEND_ATTEMPTS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 # Connection timeout
35 2 ▶use constant EGTS_TL_RECONNECT_ТО => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 has host => is => 'ro', isa => 'Str', required => 1;
38 2 has port => is => 'ro', isa => 'Int', required => 1;
39  
40 2 has timeout => is => 'ro', isa => 'Int', default => EGTS_TL_RECONNECT_ТО;
41 2 has attempt => is => 'ro', isa => 'Int', default => EGTS_TL_RESEND_ATTEMPTS;
42 2 has rtimeout => is => 'ro', isa => 'Int', default => EGTS_TL_RESPONSE_ТО;
43  
44 2 has did => is => 'ro', isa => 'Int', required => 1;
45 2 has type => is => 'ro', isa => 'Int', default => 0;
46 2 has description => is => 'ro', isa => 'Maybe[Str]';
47  
48 has socket =>
49 is => 'ro',
50 isa => 'Object',
51 lazy => 1,
52 clearer => 'socket_drop',
53 builder => sub {
54 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
55 0 my $socket = IO::Socket::INET->new(
56 PeerAddr => $self->host,
57 PeerPort => $self->port,
58 Proto => 'tcp',
59 Timeout => $self->timeout,
60 );
61 0 ▶ die "Open socket error: $!\n" unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
62 0 return $socket;
63 }
64 2 ;
65  
66 =head2 reset
67  
68 Reset internal counters for new connection
69  
70 =cut
71  
72 sub reset {
73 0 ▶ $Net::EGTS::Packet::PID = 0;
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
74 0 $Net::EGTS::Record::RN = 0;
75 }
76  
77 =head2 connect
78  
79 =cut
80  
81 sub connect {
82 0 ▶ my ($self) = @_;
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
83 0 ▶ $self->disconnect if $self->socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->socket 0 0 0
84 0 $self->reset;
85 0 return $self;
86 }
87  
88 =head2 disconnect
89  
90 =cut
91  
92 sub disconnect {
93 0 ▶ my ($self) = @_;
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
94 0 $self->socket->shutdown(2);
95 0 $self->socket_drop;
96 0 $self->reset;
97 0 return $self;
98 }
99  
100 # Get packet response
101 sub _response {
102 3 ▶ my ($self, $packet) = @_;
Subroutine _response: called CC=9 SCAR=24.1
103  
104 3 my $response;
105 3 my $start = time;
106 3 while (1) {
107 3 my $in = '';
108 3 $self->socket->recv($in, 65536);
109  
110 3 my ($p) = Net::EGTS::Packet->stream( \$in );
111 3 ▶ if( $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 3 0 3
112 3 ▶ next unless $p->PT eq EGTS_PT_RESPONSE;
Branch: 1 branch
BranchTrueFalseTotal
unless $p->PT eq 0 0 3 3
113 3 ▶ next unless $p->RPID eq $packet->PID;
Branch: 1 branch
BranchTrueFalseTotal
unless $p->RPID eq $packet->PID 0 3 3
114  
115 3 $response = $p;
116 3 last;
117 }
118  
119 0 ▶ last if time > $start + $self->rtimeout;
Branch: 1 branch
BranchTrueFalseTotal
if time > $start + $self->rtimeout 0 0 0
120 }
121 3 ▶ return 'Response timeout' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 3 3
122  
123 3 my $record = $response->records->[0];
124 3 ▶ return 'No records' unless $record;
Branch: 1 branch
BranchTrueFalseTotal
unless $record 0 3 3
125  
126 3 my $subrecord = $record->subrecords->[0];
127 3 ▶ return 'No subrecords' unless $subrecord;
Branch: 1 branch
BranchTrueFalseTotal
unless $subrecord 0 3 3
128 3 ▶ return "Error on packet @{[ $subrecord->CRN ]}"
Branch: 1 branch
BranchTrueFalseTotal
unless $subrecord->RST eq 0 0 3 3
129 unless $subrecord->RST eq EGTS_PC_OK;
130  
131 3 return $self;
132 }
133  
134 =head2 auth
135  
136 Athorization
137  
138 =cut
139  
140 sub auth {
141 1 ▶ my ($self) = @_;
Subroutine auth: called CC=1 SCAR=0.0
Pod documented
142 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 1 my $auth = Net::EGTS::Packet::Appdata->new(
145 PRIORITY => 0b11,
146 SDR => Net::EGTS::Record->new(
147 SST => EGTS_AUTH_SERVICE,
148 RST => EGTS_AUTH_SERVICE,
149 RD => Net::EGTS::SubRecord::Auth::DispatcherIdentity->new(
150 DT => $self->type,
151 DID => $self->did,
152 DSCR => $self->description,
153 )->encode,
154 )->encode,
155 );
156 1 $self->socket->send( $auth->encode );
157  
158 1 return $self->_response($auth);
159 }
160  
161 =head2 posdata $data
162  
163 Send telemetry data
164  
165 =cut
166  
167 sub posdata {
168 2 ▶ my ($self, $data) = @_;
Subroutine posdata: called CC=2 SCAR=6.9
Pod documented
169 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 2 my $oid = delete $data->{id};
172 2 ▶ croak "id required" unless $oid;
Branch: 1 branch
BranchTrueFalseTotal
unless $oid 0 2 2
173  
174 2 my $pd = Net::EGTS::Packet::Appdata->new(
175 PRIORITY => 0b11,
176 SDR => Net::EGTS::Record->new(
177 OID => $oid,
178 SST => EGTS_TELEDATA_SERVICE,
179 RST => EGTS_TELEDATA_SERVICE,
180 RD => Net::EGTS::SubRecord::Teledata::PosData->new(
181 %$data,
182 )->encode,
183 )->encode,
184 );
185 2 $self->socket->send( $pd->encode );
186  
187 2 return $self->_response($pd);
188 }
189  
190 2 __PACKAGE__->meta->make_immutable();
« lib/Net/EGTS/Record.pm ↑ Summary lib/Net/EGTS/Types.pm »