blib/lib/UR/Service/RPC/Server.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 75 SCAR 42.8
CC
8
Cov
0%
loop
20.0
create
12.0
add_executer
12.0
CRAP 72.0
1 package UR::Service::RPC::Server;
2  
3 use UR;
4 use IO::Select;
5  
6 use strict;
7 use warnings;
8 our $VERSION = "0.46"; # UR $VERSION;
9  
10 # We're going to be essentially reimplementing an Event queue here. :(
11  
12 class UR::Service::RPC::Server {
13 has => [
14 'select' => { is => 'IO::Select' },
15 timeout => { is => 'Float', default_value => undef },
16 executers => { is => 'HASH', doc => 'maps file handles to the UR::Service::RPC::Executer objects we are working with' },
17 ],
18 };
19  
20 sub create {
21 my($class, %args) = @_;
22  
23 unless ($args{'executers'}) {
24 $args{'executers'} = {};
25 }
26
27 unless ($args{'select'}) {
28 my @fh = map { $_->fh } values %{$args{'executers'}};
29 $args{'select'} = IO::Select->new(@fh);
30 }
31  
32 my $self = $class->SUPER::create(%args);
33  
34 return $self;
35 }
36  
37 sub add_executer {
38 my($self,$executer,$fh) = @_;
39  
40 unless ($fh) {
41 if ($executer->can('fh')) {
42 $fh = $executer->fh;
43 } else {
44 $self->error_message("Cannot determine file handle for RPC executer $executer");
45 return;
46 }
47 }
48  
49 $self->{'executers'}->{$fh} = $executer;
50 $self->select->add($fh);
51 }
52  
53 sub loop {
54 my $self = shift;
55  
56 my $timeout;
57 if (@_) {
58 $timeout = shift;
59 } else {
60 $timeout = $self->timeout;
61 }
62  
63 my @ready = $self->select->can_read($timeout);
64  
65 my $count = 0;
66 foreach my $fh ( @ready ) {
67 my $executer = $self->{'executers'}->{$fh};
68 unless ($executer) {
69 $self->error_message("Cannot determine RPC executer for file handle $fh fileno ",$fh->fileno);
70 return;
71 }
72  
73 $count++;
74 unless ($executer->execute($self) ) {
75 # they told us they were done
76 $self->select->remove($fh);
77 delete $self->{'executers'}->{$fh};
78 }
79 }
80  
81 return $count;
82 }
83  
84 1;
85  
86  
87 =pod
88  
89 =head1 NAME
90  
91 UR::Service::RPC::Server - Class for implementing RPC servers
92  
93 =head1 SYNOPSIS
94  
95 my $executer = Some::Exec::Class->create(fh => $fh);
96  
97 my $server = UR::Service::RPC::Server->create();
98 $server->add_executer($executer);
99  
100 $server->loop(5); # Process messages for 5 seconds
101  
102 =head1 DESCRIPTION
103  
104 The RPC server implementation isn't fleshed out very well yet, and may change
105 in the future.
106  
107 =head1 METHODS
108  
109 =over 4
110  
111 =item add_executer
112  
113 $server->add_executer($exec);
114  
115 Incorporate a new UR::Service::RPC::Executer instance to this server. It
116 adds the Executer's filehandle to its own internal IO::Select object.
117  
118 =item loop
119  
120 $server->loop();
121  
122 $server->loop(0);
123  
124 $server->loop($timeout);
125  
126 Enter the Server's event loop for the given number of seconds. If the timeout
127 is undef, it will stay in the loop forever. If the timeout is 0, it will make
128 a single pass though the readable filehandles and call C<execute> on their
129 Executer objects.
130  
131 If the return value of an Executer's C<execute> method is false, that Executer's
132 file handle is removed from the internal Select object.
133  
134 =back
135  
136 =head1 SEE ALSO
137  
138 UR::Service::RPC::Executer, UR::Service::RPC::Message
139  
140 =cut