blib/lib/XAS/Lib/Net/Client.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 59 CC 6 SCAR 20.4
CC
6
Cov
0%
get
12.0
put
12.0
disconnect
6.0
CRAP 7.7
1 package XAS::Lib::Net::Client;
2  
3 our $VERSION = '0.02';
4  
5 use IO::Socket;
6  
7 use XAS::Class
8 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 utils => 'trim',
12 accessors => 'handle',
13 mutators => 'timeout',
14 vars => {
15 PARAMS => {
16 -port => 1,
17 -host => 1,
18 -timeout => { optional => 1, default => 60 },
19 }
20 }
21 ;
22  
23 #use Data::Hexdumper;
24  
25 # ----------------------------------------------------------------------
26 # Public Methods
27 # ----------------------------------------------------------------------
28  
29 sub connect {
30 my $self = shift;
31  
32 $self->{handle} = IO::Socket::INET->new(
33 Proto => 'tcp',
34 PeerPort => $self->port,
35 PeerAddr => $self->host,
36 ) or $self->throw_msg(
37 'xas.lib.net.client.connect.noconnect',
38 'connection',
39 $self->host,
40 $self->port
41 );
42  
43 }
44  
45 sub disconnect {
46 my $self = shift;
47  
48 if ($self->handle->connected) {
49  
50 $self->handle->close();
51  
52 }
53  
54 }
55  
56 sub get {
57 my $self = shift;
58  
59 my $packet;
60 my $timeout = $self->handle->timeout;
61  
62 $self->handle->timeout($self->timeout) if ($self->timeout);
63  
64 # temporarily set the INPUT_RECORD_SEPERATOR
65  
66 local $/ = "\012\015";
67  
68 $self->handle->clearerr;
69 $packet = $self->handle->getline();
70 chomp($packet);
71  
72 # $self->log->debug(hexdump($packet));
73  
74 $self->throw_msg(
75 'xas.lib.net.client.get',
76 'network',
77 $!
78 ) if ($self->handle->error);
79  
80 $self->handle->timeout($timeout);
81  
82 return $packet;
83  
84 }
85  
86 sub put {
87 my $self = shift;
88
89 my ($packet) = $self->validate_params(\@_, [1]);
90 my $timeout = $self->handle->timeout;
91  
92 $self->handle->timeout($self->timeout) if ($self->timeout);
93 $self->handle->clearerr;
94 $self->handle->printf("%s\012\015", trim($packet));
95  
96 $self->throw_msg(
97 'xas.lib.net.client.put',
98 'network',
99 $!
100 ) if ($self->handle->error);
101  
102 $self->handle->timeout($timeout);
103  
104 }
105  
106 sub setup {
107 my $self = shift;
108  
109 warn "setup() needs to be overridden\n";
110  
111 }
112  
113 # ----------------------------------------------------------------------
114 # Private Methods
115 # ----------------------------------------------------------------------
116  
117 1;
118  
119 __END__
120  
121 =head1 NAME
122  
123 XAS::Lib::Net::Client - The network client interface for the XAS environment
124  
125 =head1 SYNOPSIS
126  
127 my $rpc = XAS::Lib::Net::Client->new(
128 -port => 9505,
129 -host => 'localhost',
130 };
131  
132 =head1 DESCRIPTION
133  
134 This module implements a simple text orientated network protocol. All "packets"
135 will have an explicit "\012\015" appended. This delineates the "packets" and is
136 network neutral. No attempt is made to decipher these "packets".
137  
138 =head1 METHODS
139  
140 =head2 new
141  
142 This initializes the module and can take three parameters. It doesn't actually
143 make a network connection.
144  
145 =over 4
146  
147 =item B<-port>
148  
149 The port number to attach too.
150  
151 =item B<-host>
152  
153 The host to use for the connection. This can be an IP address or
154 a host name.
155  
156 =item B<-timeout>
157  
158 An optional timeout, it defaults to 60 seconds.
159  
160 =back
161  
162 =head2 connect
163  
164 Connect to the defined socket.
165  
166 =head2 disconnect
167  
168 Disconnect from the defined socket.
169  
170 =head2 put($packet)
171  
172 This writes a "packet" to the socket.
173  
174 =over 4
175  
176 =item B<$packet>
177  
178 The "packet" to send over the socket.
179  
180 =back
181  
182 =head2 get
183  
184 This reads a "packet" from the socket.
185  
186 =head1 SEE ALSO
187  
188 =over 4
189  
190 =item L<XAS|XAS>
191  
192 =back
193  
194 =head1 AUTHOR
195  
196 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
197  
198 =head1 COPYRIGHT AND LICENSE
199  
200 Copyright (C) 2014 Kevin L. Esteb
201  
202 This library is free software; you can redistribute it and/or modify
203 it under the same terms as Perl itself, either Perl version 5.8.8 or,
204 at your option, any later version of Perl 5 you may have available.
205  
206 See L<http://dev.perl.org/licenses/> for more information.
207  
208 =cut