Remote/ConnectionServer.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 112 CC 3 SCAR 14.6
CC
3
Cov
0%
DEMOLISH
12.0
BUILD
2.0
run
2.0
CRAP 4.3
1 package Object::Remote::ConnectionServer;
2  
3 use Scalar::Util qw(blessed weaken);
4 use Module::Runtime qw(use_module);
5 use Object::Remote;
6 use Object::Remote::Logging qw( :log :dlog );
7 use Future;
8 use IO::Socket::UNIX;
9 use Moo;
10  
11 has listen_on => (
12 is => 'ro',
13 coerce => sub {
14 return $_[0] if blessed($_[0]);
15 unlink($_[0]);
16 IO::Socket::UNIX->new(
17 Local => $_[0],
18 Listen => 1
19 ) or die "Couldn't liten to $_[0]: $!";
20 },
21 trigger => sub {
22 my ($self, $fh) = @_;
23 log_debug { "adding connection server to run loop because the trigger has executed" };
24 weaken($self);
25 Object::Remote->current_loop
26 ->watch_io(
27 handle => $fh,
28 on_read_ready => sub { $self->_listen_ready($fh) }
29 );
30 },
31 );
32  
33 has connection_args => (
34 is => 'ro', default => sub { [] }
35 );
36  
37 has connection_callback => (
38 is => 'ro', default => sub { sub { shift } }
39 );
40  
41 sub BUILD {
42 log_debug { "A connection server has been built; calling want_run on run loop" };
43 Object::Remote->current_loop->want_run;
44 }
45  
46 sub run {
47 log_debug { "Connection server is calling run_while_wanted on the run loop" };
48 Object::Remote->current_loop->run_while_wanted;
49 }
50  
51 sub _listen_ready {
52 my ($self, $fh) = @_;
53 log_debug { "Got a connection, calling accept on the file handle" };
54 my $new = $fh->accept or die "Couldn't accept: $!";
55 log_trace { "Setting file handle non-blocking" };
56 $new->blocking(0);
57 my $f = Object::Remote->current_loop->new_future;
58 log_trace { "Creating a new connection with the remote node" };
59 my $c = use_module('Object::Remote::Connection')->new(
60 receive_from_fh => $new,
61 send_to_fh => $new,
62 on_close => $f, # and so will die $c
63 @{$self->connection_args}
64 )->${\$self->connection_callback};
65 $f->on_ready(sub { undef($c) });
66 log_trace { "marking the future as done" };
67 $c->ready_future->done;
68 Dlog_trace { "Sending 'Shere' to socket $_" } $new;
69 print $new "Shere\n" or die "Couldn't send to new socket: $!";
70 log_debug { "Connection has been fully handled" };
71 return $c;
72 }
73  
74 sub DEMOLISH {
75 my ($self, $gd) = @_;
76 log_debug { "A connection server is being destroyed; global destruction: '$gd'" };
77 return if $gd;
78 log_trace { "Removing the connection server IO watcher from run loop" };
79 Object::Remote->current_loop
80 ->unwatch_io(
81 handle => $self->listen_on,
82 on_read_ready => 1
83 );
84 if ($self->listen_on->can('hostpath')) {
85 log_debug { my $p = $self->listen_on->hostpath; "Removing '$p' from the filesystem" };
86 unlink($self->listen_on->hostpath);
87 }
88 log_trace { "calling want_stop on the run loop" };
89 Object::Remote->current_loop->want_stop;
90 }
91  
92 1;
93  
94 =head1 NAME
95  
96 Object::Remote::ConnectionServer - Listen for incoming Object::Remote connections
97  
98 =head1 SYNOPSIS
99  
100 use Object::Remote::ConnectionServer;
101  
102 my $server = Object::Remote::ConnectionServer->new(
103 listen_on => '/tmp/object-remote.sock',
104 );
105  
106 $server->run;
107  
108 =head1 DESCRIPTION
109  
110 C<Object::Remote::ConnectionServer> creates a server-side listener for
111 incoming L<Object::Remote> connections from L<Object::Remote::Connector::UNIX>.
112  
113 The C<listen_on> attribute is coerced into a listening
114 L<IO::Socket::UNIX> handle if it is given a pathname. When a connection
115 arrives, it is accepted and passed to C<Object::Remote::Connection> using
116 the same handle for both input and output.
117  
118 The server registers itself with the current L<Object::Remote> event loop
119 during construction and remains active until the object is destroyed or the
120 loop is stopped.
121  
122 =head1 ATTRIBUTES
123  
124 =head2 listen_on
125  
126 A listening socket or a pathname for a Unix domain socket.
127  
128 If a pathname is provided, any existing socket at that path is removed and
129 a new C<IO::Socket::UNIX> listener is created.
130  
131 =head2 connection_args
132  
133 Array reference of additional arguments passed to
134 L<Object::Remote::Connection> when a client connects.
135  
136 Defaults to an empty array reference.
137  
138 =head2 connection_callback
139  
140 Code reference invoked on the newly created connection object after it is
141 constructed.
142  
143 Defaults to an identity-style callback that returns the connection object
144 unchanged.
145  
146 =head1 METHODS
147  
148 =head2 run
149  
150 Runs the current L<Object::Remote> event loop while the server is wanted.
151  
152 =head1 SEE ALSO
153  
154 L<Object::Remote>, L<Object::Remote::Connection>, L<IO::Socket::UNIX>
155  
156 =cut