blib/lib/Net/Async/Tangence/Client.pm

filter: Statementstmt 39.6% 42 / 106 Branchbran 16.6% 6 / 36 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 45.8% 11 / 24 Podpod 44.4% 4 / 9 totaltotal 37.0% 66 / 178 CC 20 SCAR 33.5
CC
20
Cov
35%
connect_url
110.0
connect_exec
12.0
configure
6.3
CRAP 28.5
↑ Summary Build »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2015 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::Tangence::Client;
7  
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use base qw( Net::Async::Tangence::Protocol Tangence::Client );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '0.14';
14  
15 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 ▶use URI::Split qw( uri_split );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 C<Net::Async::Tangence::Client> - connect to a C<Tangence> server using
24 C<IO::Async>
25  
26 =head1 DESCRIPTION
27  
28 This subclass of L<Net::Async::Tangence::Protocol> connects to a L<Tangence>
29 server, allowing the client program to access exposed objects in the server.
30 It is a concrete implementation of the C<Tangence::Client> mixin.
31  
32 The following documentation concerns this specific implementation of the
33 client; for more general information on the C<Tangence>-specific parts of this
34 class, see instead the documentation for L<Tangence::Client>.
35  
36 =cut
37  
38 sub new
39 {
40 4 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
41 4 my %args = @_;
42  
43 4 my $self = $class->SUPER::new( %args );
44  
45 # It's possible a handle was passed in the constructor.
46 4 ▶ $self->tangence_connected( %args ) if defined $self->read_handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->read_handle 4 0 4
47  
48 4 return $self;
49 }
50  
51 =head1 PARAMETERS
52  
53 The following named parameters may be passed to C<new> or C<configure>:
54  
55 =over 8
56  
57 =item identity => STRING
58  
59 The identity string to send to the server.
60  
61 =item on_error => STRING or CODE
62  
63 Default error-handling policy for method calls. If set to either of the
64 strings C<carp> or C<croak> then a CODE ref will be created that invokes the
65 given function from C<Carp>; otherwise must be a CODE ref.
66  
67 =back
68  
69 =cut
70  
71 sub _init
72 {
73 4 ▶ my $self = shift;
Subroutine _init: called CC=2 SCAR=6.9
74 4 my ( $params ) = @_;
75  
76 4 $self->identity( delete $params->{identity} );
77  
78 4 $self->SUPER::_init( $params );
79  
80 4 ▶ $params->{on_error} ||= "croak";
Condition100%2/2
$params->{"on_error"} ||= "croak"
l!l
13
MC/DC100%1/1
$params->{"on_error"} ||= "croak"
$params->{"on_error"}
Truth table100%2/2
A
A= $params->{"on_error"}
Aresult
11
00
81 }
82  
83 sub configure
84 {
85 6 ▶ my $self = shift;
Subroutine configure: called CC=5 SCAR=18.3
Pod documented
86 6 my %params = @_;
87  
88 6 ▶ if( my $on_error = delete $params{on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_error = delete $params{"on_error"}) 4 2 6
89 4 ▶ if( ref $on_error eq "CODE" ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $on_error eq 'CODE') { } 1 3 4
elsif ($on_error eq 'croak') { } 3 0 3
elsif ($on_error eq 'carp') { } 0 0 0
90 # OK
91 }
92 elsif( $on_error eq "croak" ) {
93 3 ▶ $on_error = sub { croak "Received MSG_ERROR: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
94 }
95 elsif( $on_error eq "carp" ) {
96 0 ▶ $on_error = sub { carp "Received MSG_ERROR: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 }
98 else {
99 0 croak "Expected 'on_error' to be CODE reference or strings 'croak' or 'carp'";
100 }
101  
102 4 $self->on_error( $on_error );
103 }
104  
105 6 $self->SUPER::configure( %params );
106 }
107  
108 =head1 METHODS
109  
110 The following methods documented with a trailing call to C<< ->get >> return
111 L<Future> instances.
112  
113 =cut
114  
115 sub new_future
116 {
117 8 ▶ my $self = shift;
Subroutine new_future: called CC=1 SCAR=0.0
Pod documented
118 8 return $self->loop->new_future;
119 }
120  
121 =head2 connect_url
122  
123 $rootobj = $client->connect_url( $url, %args )->get
124  
125 Connects to a C<Tangence> server at the given URL. The returned L<Future> will
126 yield the root object proxy once it has been obtained.
127  
128 Takes the following named arguments:
129  
130 =over 8
131  
132 =item on_registry => CODE
133  
134 =item on_root => CODE
135  
136 Invoked once the registry and root object proxies have been obtained from the
137 server. See the documentation the L<Tangence::Client> C<tangence_connected>
138 method.
139  
140 =back
141  
142 The following URL schemes are recognised:
143  
144 =over 4
145  
146 =cut
147  
148 sub connect_url
149 {
150 0 ▶ my $self = shift;
Subroutine connect_url: not called CC=10 SCAR=47.0
Pod documented
151 0 my ( $url, %args ) = @_;
152  
153 0 my ( $scheme, $authority, $path, $query, $fragment ) = uri_split( $url );
154  
155 0 ▶ defined $scheme or croak "Invalid URL '$url'";
Branch: 1 branch
BranchTrueFalseTotal
defined $scheme or croak("Invalid URL '${url}'") 0 0 0
156  
157 0 ▶ if( $scheme =~ m/\+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme =~ /\+/) 0 0 0
158 0 ▶ $scheme =~ s/^circle\+// or croak "Found a + within URL scheme that is not 'circle+'";
Branch: 1 branch
BranchTrueFalseTotal
$scheme =~ s/^circle\+// or croak("Found a + within URL scheme that is not 'circle+'") 0 0 0
159 }
160  
161 # Legacy name
162 0 ▶ $scheme = "sshexec" if $scheme eq "ssh";
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "ssh" 0 0 0
163  
164 0 my $f;
165  
166 0 ▶ if( $scheme eq "exec" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($scheme eq 'exec') { } 0 0 0
elsif ($scheme eq 'sshexec') { } 0 0 0
elsif ($scheme eq 'tcp') { } 0 0 0
elsif ($scheme eq 'unix') { } 0 0 0
elsif ($scheme eq 'sshunix') { } 0 0 0
167 # Path will start with a leading /; we need to trim that
168 0 $path =~ s{^/}{};
169 # $query will contain args to exec - split them on +
170 0 my @argv = split( m/\+/, $query );
171 0 $f = $self->connect_exec( [ $path, @argv ], %args );
172 }
173 elsif( $scheme eq "sshexec" ) {
174 # Path will start with a leading /; we need to trim that
175 0 $path =~ s{^/}{};
176 # $query will contain args to exec - split them on +
177 0 my @argv = split( m/\+/, $query );
178 0 $f = $self->connect_sshexec( $authority, [ $path, @argv ], %args );
179 }
180 elsif( $scheme eq "tcp" ) {
181 0 $f = $self->connect_tcp( $authority, %args );
182 }
183 elsif( $scheme eq "unix" ) {
184 # Path will start with a leading /; we need to trim that
185 0 $path =~ s{^/}{};
186 0 $f = $self->connect_unix( $path, %args );
187 }
188 elsif( $scheme eq "sshunix" ) {
189 # Path will start with a leading /; we need to trim that
190 0 $path =~ s{^/}{};
191 0 $f = $self->connect_sshunix( $authority, $path, %args );
192 }
193 else {
194 0 croak "Unrecognised URL scheme name '$scheme'";
195 }
196  
197 return $f->then( sub {
198 0 ▶ my $on_root = $args{on_root};
Subroutine __ANON__: not called CC=1 SCAR=6.9
199  
200 0 my $root_f = $self->new_future;
201  
202 $self->tangence_connected( %args,
203 on_root => sub {
204 0 ▶ my ( $root ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
205  
206 0 ▶ $on_root->( $root ) if $on_root;
Branch: 1 branch
BranchTrueFalseTotal
if $on_root 0 0 0
207 0 $root_f->done( $root );
208 },
209 0 );
210  
211 0 $root_f;
212 0 });
213 }
214  
215 =item * exec
216  
217 Directly executes the server as a child process. This is largely provided for
218 testing purposes, as the server will only run for this one client; it will
219 exit when the client disconnects.
220  
221 exec:///path/to/command?with+arguments
222  
223 The URL's path should point to the required command, and the query string will
224 be split on C<+> signs and used as the arguments. The authority section of the
225 URL will be ignored, so may be left empty.
226  
227 =cut
228  
229 sub connect_exec
230 {
231 0 ▶ my $self = shift;
Subroutine connect_exec: not called CC=3 SCAR=24.8
Pod undocumented
232 0 my ( $command, %args ) = @_;
233  
234 0 my $loop = $self->get_loop;
235  
236 0 ▶ pipe( my $myread, my $childwrite ) or croak "Cannot pipe - $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $myread, my $childwrite or croak("Cannot pipe - $!") 0 0 0
237 0 ▶ pipe( my $childread, my $mywrite ) or croak "Cannoe pipe - $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $childread, my $mywrite or croak("Cannoe pipe - $!") 0 0 0
238  
239 $loop->spawn_child(
240 command => $command,
241  
242 setup => [
243 stdin => $childread,
244 stdout => $childwrite,
245 ],
246  
247 on_exit => sub {
248 0 ▶ print STDERR "Child exited unexpectedly\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 },
250 0 );
251  
252 0 $self->configure(
253 read_handle => $myread,
254 write_handle => $mywrite,
255 );
256  
257 0 Future->done;
258 }
259  
260 =item * sshexec
261  
262 A convenient wrapper around the C<exec> scheme, to connect to a server running
263 remotely via F<ssh>.
264  
265 sshexec://host/path/to/command?with+arguments
266  
267 The URL's authority section will give the SSH server (and optionally
268 username), and the path and query sections will be used as for C<exec>.
269  
270 (This scheme is also available as C<ssh>, though this name is now deprecated)
271  
272 =cut
273  
274 sub connect_sshexec
275 {
276 0 ▶ my $self = shift;
Subroutine connect_sshexec: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my ( $host, $argv, %args ) = @_;
278  
279 0 $self->connect_exec( [ "ssh", $host, @$argv ], %args );
280 }
281  
282 =item * tcp
283  
284 Connects to a server via a TCP socket.
285  
286 tcp://host:port/
287  
288 The URL's authority section will be used to give the server's hostname and
289 port number. The other sections of the URL will be ignored.
290  
291 =cut
292  
293 sub connect_tcp
294 {
295 0 ▶ my $self = shift;
Subroutine connect_tcp: not called CC=1 SCAR=6.9
Pod undocumented
296 0 my ( $authority, %args ) = @_;
297  
298 0 my ( $host, $port ) = $authority =~ m/^(.*):(.*)$/;
299  
300 0 $self->connect(
301 host => $host,
302 service => $port,
303 );
304 }
305  
306 =item * unix
307  
308 Connects to a server via a UNIX local socket.
309  
310 unix:///path/to/socket
311  
312 The URL's path section will give the path to the local socket. The other
313 sections of the URL will be ignored.
314  
315 =cut
316  
317 sub connect_unix
318 {
319 0 ▶ my $self = shift;
Subroutine connect_unix: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my ( $path, %args ) = @_;
321  
322 0 $self->connect(
323 addr => {
324 family => 'unix',
325 socktype => 'stream',
326 path => $path,
327 },
328 );
329 }
330  
331 =item * sshunix
332  
333 Connects to a server running remotely via a UNIX socket over F<ssh>.
334  
335 sshunix://host/path/to/socket
336  
337 (This is implemented by running F<perl> remotely and sending it a tiny
338 self-contained program that connects STDIN/STDOUT to the given UNIX socket
339 path. It requires that the server has F<perl> at least version 5.6 available
340 in the path simply as C<perl>)
341  
342 =cut
343  
344 # A tiny program we can run remotely to connect STDIN/STDOUT to a UNIX socket
345 # given as $ARGV[0]
346 4 ▶use constant _NC_MICRO => <<'EOPERL';
Subroutine BEGIN: called CC=1 SCAR=0.0
347 use Socket qw( AF_UNIX SOCK_STREAM pack_sockaddr_un );
348 use IO::Handle;
349 socket(my $socket, AF_UNIX, SOCK_STREAM, 0) or die "socket(AF_UNIX): $!\n";
350 connect($socket, pack_sockaddr_un($ARGV[0])) or die "connect $ARGV[0]: $!\n";
351 my $fd = fileno($socket);
352 $socket->blocking(0); $socket->autoflush(1);
353 STDIN->blocking(0); STDOUT->autoflush(1);
354 my $rin = "";
355 vec($rin, 0, 1) = 1;
356 vec($rin, $fd, 1) = 1;
357 print "READY";
358 while(1) {
359 select(my $rout = $rin, undef, undef, undef);
360 if(vec($rout, 0, 1)) {
361 sysread STDIN, my $buffer, 8192 or last;
362 print $socket $buffer;
363 }
364 if(vec($rout, $fd, 1)) {
365 sysread $socket, my $buffer, 8192 or last;
366 print $buffer;
367 }
368 }
369 EOPERL
370  
371 sub connect_sshunix
372 {
373 0 ▶ my $self = shift;
Subroutine connect_sshunix: not called CC=1 SCAR=6.9
Pod undocumented
374 0 my ( $host, $path, %args ) = @_;
375  
376 # Tell perl we're going to send it a program on STDIN
377 $self->connect_sshexec( $host, [ 'perl', '-', $path ], %args )
378 ->then( sub {
379 0 ▶ $self->write( _NC_MICRO . "\n__END__\n" );
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 0 my $f = $self->new_future;
381  
382 $self->configure( on_read => sub {
383 0 ▶ my ( $self, $buffref, $eof ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
384 0 ▶ return 0 unless $$buffref =~ s/READY//;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buffref =~ s/READY// 0 0 0
385 0 $self->configure( on_read => undef );
386 0 $f->done;
387 0 return 0;
388 0 } );
389  
390 0 return $f;
391 0 });
392 }
393  
394 =back
395  
396 =cut
397  
398 =head1 AUTHOR
399  
400 Paul Evans <leonerd@leonerd.org.uk>
401  
402 =cut
403  
404 4 0x55AA;
↑ Summary Build »