File Coverage

blib/lib/POE/Component/MessageQueue/Storage/Remote/Server.pm
Criterion Covered Total %
statement 15 26 57.6
branch 0 4 0.0
condition n/a
subroutine 5 7 71.4
pod 0 1 0.0
total 20 38 52.6


line stmt bran cond sub pod time code
1             #
2             # Copyright 2008 Paul Driver <frodwith@gmail.com>
3             #
4             # This program is free software: you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation, either version 2 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see <http://www.gnu.org/licenses/>.
16             #
17              
18             package POE::Component::MessageQueue::Storage::Remote::Server;
19 1     1   1928 use Moose;
  1         3  
  1         7  
20 1     1   6527 use POE;
  1         3  
  1         8  
21 1     1   348 use POE::Filter::Reference;
  1         2  
  1         20  
22 1     1   6 use POE::Component::Server::TCP;
  1         2  
  1         18  
23 1     1   5 use POE::Component::MessageQueue::Storage::BigMemory;
  1         2  
  1         267  
24              
25             has session_id => (
26             is => 'rw',
27             isa => 'Int',
28             init_arg => undef,
29             );
30              
31             has storage => (
32             is => 'ro',
33             does => 'POE::Component::MessageQueue::Storage',
34             default => sub { POE::Component::MessageQueue::Storage::BigMemory->new },
35             );
36              
37             has port => (
38             is => 'ro',
39             isa => 'Int',
40             required => 1,
41             );
42              
43             sub BUILD
44             {
45 0     0 0   my ($self, $args) = @_;
46              
47             $self->session_id(POE::Component::Server::TCP->new(
48             Port => $self->port,
49             ClientFilter => POE::Filter::Reference->new,
50              
51             ClientInput => sub {
52 0     0     my ($heap, $request) = @_[HEAP, ARG0];
53 0           my ($method, $args) = @{$request}{'method', 'args'};
  0            
54 0           my $callback_id = pop(@$args);
55 0           my $storage = $self->storage;
56              
57 0 0         if (my $method_ref = $storage->can($method))
58             {
59             $method_ref->($storage, @$args, sub {
60             $heap->{client}->put({
61 0           callback => $callback_id,
62             args => [@_]
63             });
64 0 0         $poe_kernel->post($self->session_id, 'shutdown')
65             if ($method eq 'storage_shutdown');
66 0           });
67             }
68             },
69 0           ));
70             }
71              
72             1;
73              
74             =pod
75              
76             =head1 NAME
77              
78             POE::Component::MessageQueue::Storage::Remote::Server -- Expose a storage
79             engine as a service over a TCP socket
80              
81             =head1 DESCRIPTION
82              
83             This module wraps any storage engine and exposes it as a service over a TCP
84             socket. It handles multiple clients. It takes requests in the form of a
85             Storable hashref of the form C<< { 'method_name' => [arg1, arg2, etc] } >>.
86              
87             =head1 CONSTRUCTOR PARAMETERS
88              
89             =over 2
90              
91             =item port
92              
93             The port number to listen on.
94              
95             =item storage
96              
97             A L<POE::Component::MessageQUeue::Storage> engine to wrap.
98              
99             =back
100              
101             =head1 SEE ALSO
102              
103             L<POE::Component::MessageQueue::Storage::Remote>
104              
105             =cut
106