File Coverage

blib/lib/SSH/RPC/Shell.pm
Criterion Covered Total %
statement 12 31 38.7
branch 1 8 12.5
condition n/a
subroutine 4 5 80.0
pod 3 3 100.0
total 20 47 42.5


line stmt bran cond sub pod time code
1             package SSH::RPC::Shell;
2             $SSH::RPC::Shell::VERSION = '1.203';
3 1     1   1114 use strict;
  1         2  
  1         25  
4 1     1   328 use JSON;
  1         5611  
  1         4  
5              
6             =head1 NAME
7              
8             SSH::RPC::Shell - The shell, or server side, of an RPC call over SSH.
9              
10             =head1 VERSION
11              
12             version 1.203
13              
14             =head1 SYNOPSIS
15              
16             To make your own shell with it's own methods:
17              
18             package MyShell;
19              
20             use base 'SSH::RPC::Shell';
21              
22             sub run_time {
23             my ($class, $args) = @_;
24             return {
25             status => 200,
26             response => time(),
27             };
28             }
29              
30             1;
31              
32              
33             To create a usuable shell:
34              
35             #!/usr/bin/perl
36              
37             use strict;
38             use MyShell;
39              
40             MyShell->run();
41              
42              
43             =head1 DESCRIPTION
44              
45             SSH::RPC::Shell allows you to quickly implement your own shells that are remotely callable with L.
46              
47             =head1 METHODS
48              
49             The following methods are available from this class.
50              
51             =cut
52              
53              
54              
55             #-------------------------------------------------------------------
56              
57             =head2 processRequest ( request )
58              
59              
60             =cut
61              
62             sub processRequest {
63 1     1 1 2 my ($class, $request) = @_;
64 1         3 my $command = 'run_'.$request->{command};
65 1         2 my $args = $request->{args};
66 1 50       7 if (my $sub = $class->can($command)) {
67 1         2 return $sub->($args);
68             }
69 0         0 return { "error" => "Method not allowed.", "status" => "405" };
70             }
71              
72             #-------------------------------------------------------------------
73              
74             =head2 run ()
75              
76             Class method. This method is executed to invoke the shell.
77              
78             =cut
79              
80             sub run {
81 0     0 1 0 my $class = shift;
82 0         0 my $json = JSON->new->utf8;
83 0         0 my $request;
84 0         0 while (my $line = ) {
85 0         0 $request = eval {$json->incr_parse($line)};
  0         0  
86 0 0       0 if ($@) {
87 0         0 warn $@;
88 0         0 print '{ "error" : "Malformed request.", "status" : "400" }';
89 0         0 return;
90             }
91 0 0       0 last if defined $request;
92             }
93 0         0 my $result = $class->processRequest($request);
94 0         0 $result->{version} = $SSH::RPC::Shell::VERSION;
95 0         0 my $encodedResult = eval{JSON->new->pretty->utf8->encode($result)};
  0         0  
96 0 0       0 if ($@) {
97 0         0 print { "error" => "Malformed response.", "status" => "511" };
98             }
99             else {
100 0         0 print $encodedResult."\n";
101             }
102             }
103              
104              
105             #-------------------------------------------------------------------
106              
107             =head2 run_noop ()
108              
109             Class method. This subroutine just returns a successful status so you know that communication is working.
110              
111             =cut
112              
113             sub run_noop {
114 3     3 1 317 return {status=>200};
115             }
116              
117             =head1 LEGAL
118              
119             -------------------------------------------------------------------
120             SSH::RPC::Client is Copyright 2008-2009 Plain Black Corporation
121             and is licensed under the same terms as Perl itself.
122             -------------------------------------------------------------------
123             http://www.plainblack.com info@plainblack.com
124             -------------------------------------------------------------------
125              
126             =cut
127              
128             1;