blib/lib/XAS/Lib/SSH/Server.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 130 CC 4 SCAR 11.8
CC
4
Cov
0%
_client_output
6.0
_client_error
6.0
init
6.0
CRAP 3.3
1 package XAS::Lib::SSH::Server;
2  
3 our $VERSION = '0.01';
4  
5 use POE;
6 use POE::Filter::Line;
7 use POE::Wheel::ReadWrite;
8  
9 use XAS::Class
10 debug => 0,
11 version => $VERSION,
12 base => 'XAS::Lib::Service',
13 accessors =>'client filter host port',
14 vars => {
15 PARAMS => {
16 -filter => { optional => 1, default => undef },
17 }
18 }
19 ;
20  
21 #use Data::Dumper;
22  
23 # ----------------------------------------------------------------------
24 # Public Methods
25 # ----------------------------------------------------------------------
26  
27 sub session_initialize {
28 my $self = shift;
29  
30 my $alias = $self->alias;
31  
32 $self->log->debug("$alias: entering session_intialize()");
33  
34 # public events
35  
36 $poe_kernel->state('process_errors', $self);
37 $poe_kernel->state('process_request', $self);
38 $poe_kernel->state('process_response', $self);
39  
40 # private events
41  
42 $poe_kernel->state('client_error', $self, '_client_error');
43 $poe_kernel->state('client_input', $self, '_client_input');
44 $poe_kernel->state('client_output', $self, '_client_output');
45  
46 # Find the remote host and port.
47  
48 my ($rhost, $rport, $lhost, $lport) = split(' ', $ENV{SSH_CONNECTION});
49  
50 $self->{host} = $rhost;
51 $self->{port} = $rport;
52  
53 # walk the chain
54  
55 $self->SUPER::session_initialize();
56  
57 $self->log->debug("$alias: leaving session_intialize()");
58  
59 }
60  
61 sub session_startup {
62 my $self = shift;
63  
64 my $alias = $self->alias;
65  
66 # start listening for connections
67  
68 $self->log->debug("$alias: entering session_startup()");
69  
70 $poe_kernel->post($alias, 'client_connection');
71  
72 # walk the chain
73  
74 $self->SUPER::session_startup();
75  
76 $self->log->debug("$alias: leaving session_startup()");
77  
78 }
79  
80 sub session_pause {
81 my $self = shift;
82  
83 my $alias = $self->alias;
84  
85 $self->log->debug("$alias: entering session_pause()");
86  
87 $self->client->pause_input();
88  
89 # walk the chain
90  
91 $self->SUPER::session_pause();
92  
93 $self->log->debug("$alias: entering session_pause()");
94  
95 }
96  
97 sub session_resume {
98 my $self = shift;
99  
100 my $alias = $self->alias;
101  
102 $self->log->debug("$alias: entering session_resume()");
103  
104 $self->client->resume_input();
105  
106 # walk the chain
107  
108 $self->SUPER::session_resume();
109  
110 $self->log->debug("$alias: leaving session_resume()");
111  
112 }
113  
114 # ----------------------------------------------------------------------
115 # Public Events
116 # ----------------------------------------------------------------------
117  
118 sub process_request {
119 my ($self, $input, $ctx) = @_[OBJECT,ARG0,ARG1];
120  
121 my $output = $input;
122 my $alias = $self->alias;
123  
124 $self->log->debug("$alias: process_request()");
125  
126 $poe_kernel->post($alias, 'process_response', $output, $ctx);
127  
128 }
129  
130 sub process_response {
131 my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1];
132  
133 my $alias = $self->alias;
134  
135 $self->log->debug("$alias: process_response()");
136  
137 $poe_kernel->post($alias, 'client_output', $output, $ctx);
138  
139 }
140  
141 sub process_errors {
142 my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1];
143  
144 my $alias = $self->alias;
145  
146 $self->log->debug("$alias: process_errors()");
147  
148 $poe_kernel->post($alias, 'client_output', $output, $ctx);
149  
150 }
151  
152 # ----------------------------------------------------------------------
153 # Private Events
154 # ----------------------------------------------------------------------
155  
156 sub _client_connection {
157 my ($self) = $_[OBJECT];
158  
159 my $alias = $self->alias;
160  
161 $self->log->debug("$alias: _client_connection()");
162  
163 # Start listening on stdin.
164  
165 $self->{client} = POE::Wheel::ReadWrite->new(
166 InputHandle => \*STDIN,
167 OutputHandle => \*STDOUT,
168 Filter => $self->filter,
169 InputEvent => 'client_input',
170 ErrorEvent => 'client_error'
171 );
172  
173 }
174  
175 sub _client_input {
176 my ($self, $input, $wheel) = @_[OBJECT,ARG0,ARG1];
177  
178 my $alias = $self->alias;
179 my $ctx = {
180 wheel => $wheel
181 };
182  
183 $self->log->debug("$alias: _client_input()");
184  
185 $poe_kernel->post($alias, 'process_request', $input, $ctx);
186  
187 }
188  
189 sub _client_output {
190 my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1];
191  
192 my @packet;
193 my $alias = $self->alias;
194  
195 push(@packet, $output);
196  
197 $self->log->debug("$alias: _client_output()");
198  
199 if (my $wheel = $ctx->{wheel})) {
200  
201 $wheel->put(@packet);
202  
203 } else {
204  
205 $self->log->error_msg('nowheel', $alias);
206  
207 }
208  
209 }
210  
211 sub _client_error {
212 my ($self, $syscall, $errnum, $errstr, $wheel) = @_[OBJECT,ARG0 .. ARG3];
213  
214 my $alias = $self->alias;
215  
216 $self->log->debug("$alias: _client_error()");
217  
218 if ($errnum == 0) {
219  
220 $self->log->info_msg('client_disconnect', $alias, $self->host(), $self->port()));
221  
222 } else {
223  
224 $self->log->error_msg('client_error', $alias, $errnum, $errstr);
225  
226 }
227  
228 }
229  
230 # ----------------------------------------------------------------------
231 # Private Methods
232 # ----------------------------------------------------------------------
233  
234 sub init {
235 my $class = shift;
236  
237 my $self = $class->SUPER::init(@_);
238  
239 unless (defined($self->filter)) {
240  
241 $self->{filter} = POE::Filter::Line->new(
242 InputLiteral => "\012\015",
243 OutputLiteral => "\012\015"
244 );
245  
246 }
247  
248 return $self;
249  
250 }
251  
252 1;
253  
254 __END__
255  
256 =head1 NAME
257  
258 XAS::Lib::SSH::Server - A SSH Subsystem based server
259  
260 =head1 SYNOPSIS
261  
262 use XAS::Lib::SSH::Server;
263  
264 my $sub = XAS::Lib::SSH::Server->new();
265  
266 $sub->run();
267  
268 =head1 DESCRIPTION
269  
270 The module provides basic I/O for a SSH subsystem. A SSH subsystem reads from
271 stdin, writes to stdout and stderr.
272  
273 =head1 METHODS
274  
275 =head2 new
276  
277 This initializes the object.
278  
279 =head2 connect
280  
281 This method redirects the stdin, stdout and stderr file streams
282 to the SSH server.
283  
284 =head2 get
285  
286 This method reads data from stdin. It uses blocking reads. It
287 will attempt to read all pending data up to EOL.
288  
289 =head2 put($buffer)
290  
291 This method will write data to stdout. It uses blocking writes.
292 It will attempt to write all the data in the buffer.
293  
294 =over 4
295  
296 =item B<$buffer>
297  
298 The buffer to be written.
299  
300 =back
301  
302 =head2 disconnect
303  
304 This method closes the connection.
305  
306 =head1 MUTATORS
307  
308 =head2 eol
309  
310 This method sets the EOL for reads. It defaults to LF - "\012".
311  
312 =head1 SEE ALSO
313  
314 =over 4
315  
316 =item L<XAS|XAS>
317  
318 =back
319  
320 =head1 AUTHOR
321  
322 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
323  
324 =head1 COPYRIGHT AND LICENSE
325  
326 Copyright (C) 2014 Kevin L. Esteb
327  
328 This library is free software; you can redistribute it and/or modify
329 it under the same terms as Perl itself, either Perl version 5.8.8 or,
330 at your option, any later version of Perl 5 you may have available.
331  
332 See L<http://dev.perl.org/licenses/> for more information.
333  
334 =cut