blib/lib/Net/EGTS/Simple.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1 use utf8;
2  
3 package Net::EGTS::Simple;
4 use Mouse;
5  
6 use Carp;
7 use IO::Socket::INET;
8  
9 use Net::EGTS::Util;
10 use Net::EGTS::Types;
11 use Net::EGTS::Codes;
12  
13 use Net::EGTS::Packet;
14 use Net::EGTS::Record;
15 use Net::EGTS::SubRecord;
16  
17 use Net::EGTS::Packet::Appdata;
18 use Net::EGTS::SubRecord::Auth::DispatcherIdentity;
19 use Net::EGTS::SubRecord::Teledata::PosData;
20  
21 =head1 NAME
22  
23 Net::EGTS::Simple - simple socket transport
24  
25 =cut
26  
27 # Timeout, sec. (0 .. 255)
28 use constant EGTS_SL_NOT_AUTH_TO => 6;
29  
30 # Response timeout
31 use constant EGTS_TL_RESPONSE_ТО => 5;
32 # Resend attempts if timeout TL_RESPONSE_ТО
33 use constant EGTS_TL_RESEND_ATTEMPTS => 3;
34 # Connection timeout
35 use constant EGTS_TL_RECONNECT_ТО => 30;
36  
37 has host => is => 'ro', isa => 'Str', required => 1;
38 has port => is => 'ro', isa => 'Int', required => 1;
39  
40 has timeout => is => 'ro', isa => 'Int', default => EGTS_TL_RECONNECT_ТО;
41 has attempt => is => 'ro', isa => 'Int', default => EGTS_TL_RESEND_ATTEMPTS;
42 has rtimeout => is => 'ro', isa => 'Int', default => EGTS_TL_RESPONSE_ТО;
43  
44 has did => is => 'ro', isa => 'Int', required => 1;
45 has type => is => 'ro', isa => 'Int', default => 0;
46 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 my ($self) = @_;
55 my $socket = IO::Socket::INET->new(
56 PeerAddr => $self->host,
57 PeerPort => $self->port,
58 Proto => 'tcp',
59 Timeout => $self->timeout,
60 );
61 die "Open socket error: $!\n" unless $socket;
62 return $socket;
63 }
64 ;
65  
66 =head2 reset
67  
68 Reset internal counters for new connection
69  
70 =cut
71  
72 sub reset {
73 $Net::EGTS::Packet::PID = 0;
74 $Net::EGTS::Record::RN = 0;
75 }
76  
77 =head2 connect
78  
79 =cut
80  
81 sub connect {
82 my ($self) = @_;
83 $self->disconnect if $self->socket;
84 $self->reset;
85 return $self;
86 }
87  
88 =head2 disconnect
89  
90 =cut
91  
92 sub disconnect {
93 my ($self) = @_;
94 $self->socket->shutdown(2);
95 $self->socket_drop;
96 $self->reset;
97 return $self;
98 }
99  
100 # Get packet response
101 sub _response {
102 my ($self, $packet) = @_;
103  
104 my $response;
105 my $start = time;
106 while (1) {
107 my $in = '';
108 $self->socket->recv($in, 65536);
109  
110 my ($p) = Net::EGTS::Packet->stream( \$in );
111 if( $p ) {
112 next unless $p->PT eq EGTS_PT_RESPONSE;
113 next unless $p->RPID eq $packet->PID;
114  
115 $response = $p;
116 last;
117 }
118  
119 last if time > $start + $self->rtimeout;
120 }
121 return 'Response timeout' unless $response;
122  
123 my $record = $response->records->[0];
124 return 'No records' unless $record;
125  
126 my $subrecord = $record->subrecords->[0];
127 return 'No subrecords' unless $subrecord;
128 return "Error on packet @{[ $subrecord->CRN ]}"
129 unless $subrecord->RST eq EGTS_PC_OK;
130  
131 return $self;
132 }
133  
134 =head2 auth
135  
136 Athorization
137  
138 =cut
139  
140 sub auth {
141 my ($self) = @_;
142 use bytes;
143  
144 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 $self->socket->send( $auth->encode );
157  
158 return $self->_response($auth);
159 }
160  
161 =head2 posdata $data
162  
163 Send telemetry data
164  
165 =cut
166  
167 sub posdata {
168 my ($self, $data) = @_;
169 use bytes;
170  
171 my $oid = delete $data->{id};
172 croak "id required" unless $oid;
173  
174 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 $self->socket->send( $pd->encode );
186  
187 return $self->_response($pd);
188 }
189  
190 __PACKAGE__->meta->make_immutable();