File Coverage

blib/lib/Lim/Component.pm
Criterion Covered Total %
statement 36 123 29.2
branch 2 52 3.8
condition 0 3 0.0
subroutine 11 19 57.8
pod 7 7 100.0
total 56 204 27.4


line stmt bran cond sub pod time code
1             package Lim::Component;
2              
3 7     7   52 use common::sense;
  7         17  
  7         47  
4 7     7   725 use Carp;
  7         20  
  7         544  
5              
6 7     7   1410 use Log::Log4perl ();
  7         64080  
  7         244  
7 7     7   56 use Scalar::Util qw(blessed);
  7         13  
  7         414  
8              
9 7     7   108 use Lim ();
  7         14  
  7         131  
10 7     7   1428 use Lim::RPC::Value ();
  7         21  
  7         131  
11 7     7   1428 use Lim::RPC::Value::Collection ();
  7         18  
  7         132  
12 7     7   4124 use Lim::RPC::Call ();
  7         25  
  7         3242  
13              
14             =encoding utf8
15              
16             =head1 NAME
17              
18             Lim::Component - Base class for plugins
19              
20             =head1 VERSION
21              
22             See L for version.
23              
24             =cut
25              
26             our $VERSION = $Lim::VERSION;
27              
28             =head1 SYNOPSIS
29              
30             =over 4
31              
32             package Lim::Plugin::MyPlugin;
33              
34             use base qw(Lim::Component);
35              
36             sub Module {
37             'MyPlugin';
38             }
39              
40             sub Calls {
41             {
42             ReadVersion => {
43             out => {
44             version => 'string'
45             }
46             }
47             };
48             }
49              
50             sub Commands {
51             {
52             version => 1
53             };
54             }
55              
56             =back
57              
58             =head1 DESCRIPTION
59              
60             This is the base class of all plugins in Lim. It defines the name, RPC calls and
61             CLI commands. It must be present for any plugin to work but the different plugin
62             parts does not have to exist everywhere. For example the CLI part does not have
63             to have the Server and Client but it will most likly have the Client part if you
64             want to communicate with the Server.
65              
66             =head1 METHODS
67              
68             =over 4
69              
70             =item $plugin_name = Lim::Plugin::MyPlugin->Name
71              
72             Returns the plugin's name.
73              
74             This function must be overloaded or it will L.
75              
76             =cut
77              
78             sub Name {
79 0     0 1 0 confess 'Name not overloaded';
80             }
81              
82             =item $plugin_description = Lim::Plugin::MyPlugin->Description
83              
84             Returns the plugin's description.
85              
86             =cut
87              
88             sub Description {
89 0     0 1 0 'No description for this plugin';
90             }
91              
92             =item $call_hash_ref = Lim::Plugin::MyPlugin->Calls
93              
94             Returns a hash reference to the calls that can be made to this plugin, used both
95             in Server and Client to verify input and output arguments.
96              
97             Read more about this in L.
98              
99             This function must be overloaded or it will L.
100              
101             =cut
102              
103             sub Calls {
104 0     0 1 0 confess 'Calls not overloaded';
105             }
106              
107             =item $command_hash_ref = Lim::Plugin::MyPlugin->Commands
108              
109             Returns a hash reference to the CLI commands that can be made by this plugin.
110              
111             This function must be overloaded or it will L.
112              
113             =cut
114              
115             sub Commands {
116 0     0 1 0 confess 'Commands not overloaded';
117             }
118              
119             =item $cli = Lim::Plugin::MyPlugin->CLI
120              
121             Create a CLI object of the plugin, read more about this in
122             L.
123              
124             =cut
125              
126             sub CLI {
127 0     0 1 0 my $self = shift;
128            
129 0 0       0 if (ref($self)) {
130 0         0 confess __PACKAGE__, ': Should not be called with refered/blessed argument';
131             }
132 0         0 $self .= '::CLI';
133            
134 0         0 eval 'use '.$self.' ();';
135 0 0       0 die $self.' : '.$@ if $@;
136 0         0 $self->new(@_);
137             }
138              
139             =item $client = Lim::Plugin::MyPlugin->Client
140              
141             Create a Client object of the plugin, read more about this in
142             L.
143              
144             =cut
145              
146             sub Client {
147 0     0 1 0 my $self = shift;
148            
149 0 0       0 if (ref($self)) {
150 0         0 confess __PACKAGE__, ': Should not be called with refered/blessed argument';
151             }
152 0         0 my $calls = $self->Calls;
153 0         0 my $plugin = $self->Name;
154 0         0 $self .= '::Client';
155            
156 0         0 eval 'use '.$self.' ();';
157 0 0       0 die $self.' : '.$@ if $@;
158              
159 0 0       0 if ($self->can('__lim_bootstrapped')) {
160 0         0 return $self->new(@_);
161             }
162            
163 7     7   98 no strict 'refs';
  7         19  
  7         17961  
164 0         0 foreach my $call (keys %$calls) {
165 0 0       0 unless ($self->can($call)) {
166 0         0 my $sub = $self.'::'.$call;
167 0         0 my $call_def = $calls->{$call};
168              
169 0 0       0 unless (ref($call_def) eq 'HASH') {
170 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid definition';
171             }
172            
173 0 0       0 if (exists $call_def->{in}) {
174 0 0       0 unless (ref($call_def->{in}) eq 'HASH') {
175 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid in parameter definition';
176             }
177            
178 0         0 my @keys = keys %{$call_def->{in}};
  0         0  
179 0 0       0 unless (scalar @keys) {
180 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid in parameter definition';
181             }
182            
183 0         0 my @values = ($call_def->{in});
184 0         0 while (defined (my $value = shift(@values))) {
185 0         0 foreach my $key (keys %$value) {
186 0 0       0 if (ref($value->{$key}) eq 'HASH') {
    0          
187 0 0       0 if (exists $value->{$key}->{''}) {
188 0         0 my $collection = Lim::RPC::Value::Collection->new($value->{$key}->{''});
189 0         0 delete $value->{$key}->{''};
190 0         0 $value->{$key} = $collection->set_children($value->{$key});
191 0         0 push(@values, $value->{$key}->children);
192             }
193             else {
194 0         0 push(@values, $value->{$key});
195             }
196 0         0 next;
197             }
198             elsif (blessed $value->{$key}) {
199 0 0       0 if ($value->{$key}->isa('Lim::RPC::Value')) {
200 0         0 next;
201             }
202 0 0       0 if ($value->{$key}->isa('Lim::RPC::Value::Collection')) {
203 0         0 push(@values, $value->{$key}->children);
204 0         0 next;
205             }
206             }
207             else {
208 0         0 $value->{$key} = Lim::RPC::Value->new($value->{$key});
209 0         0 next;
210             }
211              
212 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid in parameter definition';
213             }
214             }
215             }
216            
217 0 0       0 if (exists $call_def->{out}) {
218 0 0       0 unless (ref($call_def->{out}) eq 'HASH') {
219 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid out parameter definition';
220             }
221            
222 0         0 my @keys = keys %{$call_def->{out}};
  0         0  
223 0 0       0 unless (scalar @keys) {
224 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid out parameter definition';
225             }
226              
227 0         0 my @values = ($call_def->{out});
228 0   0     0 while (defined $calls and (my $value = shift(@values))) {
229 0         0 foreach my $key (keys %$value) {
230 0 0       0 if (ref($value->{$key}) eq 'HASH') {
    0          
231 0 0       0 if (exists $value->{$key}->{''}) {
232 0         0 my $collection = Lim::RPC::Value::Collection->new($value->{$key}->{''});
233 0         0 delete $value->{$key}->{''};
234 0         0 $value->{$key} = $collection->set_children($value->{$key});
235 0         0 push(@values, $value->{$key}->children);
236             }
237             else {
238 0         0 push(@values, $value->{$key});
239             }
240 0         0 next;
241             }
242             elsif (blessed $value->{$key}) {
243 0 0       0 if ($value->{$key}->isa('Lim::RPC::Value')) {
244 0         0 next;
245             }
246 0 0       0 if ($value->{$key}->isa('Lim::RPC::Value::Collection')) {
247 0         0 push(@values, $value->{$key}->children);
248 0         0 next;
249             }
250             }
251             else {
252 0         0 $value->{$key} = Lim::RPC::Value->new($value->{$key});
253 0         0 next;
254             }
255              
256 0         0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid out parameter definition';
257             }
258             }
259             }
260            
261             *$sub = sub {
262 0 0   0   0 unless (Lim::RPC::Call->new($plugin, $call, $call_def, @_)) {
263 0         0 confess __PACKAGE__, ': Unable to create Lim::RPC::Call for ', $sub;
264             }
265 0         0 };
266             }
267             }
268            
269 0         0 my $sub = $self.'::__lim_bootstrapped';
270             *$sub = sub {
271 0     0   0 1;
272 0         0 };
273            
274 0         0 $self->new(@_);
275             }
276              
277             =item $client = Lim::Plugin::MyPlugin->Server
278              
279             Create a Server object of the plugin, read more about this in
280             L.
281              
282             =cut
283              
284             sub Server {
285 2     2 1 19 my $self = shift;
286            
287 2 50       11 if (ref($self)) {
288 0         0 confess __PACKAGE__, ': Should not be called with refered/blessed argument';
289             }
290 2         16 $self .= '::Server';
291            
292 2     2   11324 eval 'use '.$self.' ();';
  2         2270  
  2         8  
  2         40  
293 2 50       23 die $self.' : '.$@ if $@;
294 2         32 $self->new(@_);
295             }
296              
297             =back
298              
299             =head1 AUTHOR
300              
301             Jerry Lundström, C<< >>
302              
303             =head1 BUGS
304              
305             Please report any bugs or feature requests to L.
306              
307             =head1 SUPPORT
308              
309             You can find documentation for this module with the perldoc command.
310              
311             perldoc Lim::Component
312              
313             You can also look for information at:
314              
315             =over 4
316              
317             =item * Lim issue tracker (report bugs here)
318              
319             L
320              
321             =back
322              
323             =head1 ACKNOWLEDGEMENTS
324              
325             =head1 LICENSE AND COPYRIGHT
326              
327             Copyright 2012-2013 Jerry Lundström.
328              
329             This program is free software; you can redistribute it and/or modify it
330             under the terms of either: the GNU General Public License as published
331             by the Free Software Foundation; or the Artistic License.
332              
333             See http://dev.perl.org/licenses/ for more information.
334              
335              
336             =cut
337              
338             1; # End of Lim::Component