Tangence/Client.pm

filter: Statementstmt 86.7% 85 / 98 Branchbran 57.8% 22 / 38 Conditioncond 35.7% 5 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 85.7% 18 / 21 Podpod 57.1% 4 / 7 totaltotal 72.1% 135 / 187 CC 25 SCAR 19.9
CC
25
Cov
75%
connect_url
19.2
configure
6.3
connect_tcp
6.1
CRAP 7.3
« Tangence/Client/via/sshunix.pm ↑ Summary Tangence/Protocol.pm »
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-2021 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::Tangence::Client 0.16;
7  
8 7 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use base qw( Net::Async::Tangence::Protocol Tangence::Client );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 ▶use URI;
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 7 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
40 7 my $class = shift;
41 7 my %args = @_;
42  
43 7 my $self = $class->SUPER::new( %args );
44  
45 # It's possible a handle was passed in the constructor.
46 7 ▶ $self->tangence_connected( %args ) if defined $self->read_handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->read_handle 4 3 7
47  
48 7 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 7 ▶{
Subroutine _init: called CC=2 SCAR=6.9
73 7 my $self = shift;
74 7 my ( $params ) = @_;
75  
76 7 $self->identity( delete $params->{identity} );
77  
78 7 $self->SUPER::_init( $params );
79  
80 7 ▶ $params->{on_error} ||= "croak";
Condition100%2/2
$params->{"on_error"} ||= "croak"
l!l
16
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 12 ▶{
Subroutine configure: called CC=5 SCAR=18.3
Pod documented
85 12 my $self = shift;
86 12 my %params = @_;
87  
88 12 ▶ if( my $on_error = delete $params{on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_error = delete $params{"on_error"}) 7 5 12
89 7 ▶ if( ref $on_error eq "CODE" ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $on_error eq 'CODE') { } 1 6 7
elsif ($on_error eq 'croak') { } 6 0 6
elsif ($on_error eq 'carp') { } 0 0 0
90 # OK
91 }
92 elsif( $on_error eq "croak" ) {
93 0 ▶ $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 7 $self->on_error( $on_error );
103 }
104  
105 12 $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 14 ▶{
Subroutine new_future: called CC=1 SCAR=0.0
Pod documented
117 14 my $self = shift;
118 14 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 =item family => STRING
141  
142 Optional. May be set to C<inet4> or C<inet6> to force IPv4 or IPv6 if
143 relevant. Ignored by C<exec:> and C<unix:> schemes.
144  
145 =back
146  
147 The following URL schemes are recognised:
148  
149 =over 4
150  
151 =cut
152  
153 sub connect_url
154 3 ▶{
Subroutine connect_url: called CC=12 SCAR=29.5
Pod documented
155 3 my $self = shift;
156 3 my ( $url, %args ) = @_;
157  
158 3 ▶ my $uri = ( blessed $url && $url->isa( "URI" ) ) ? $url : URI->new( $url );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($url) && $url->isa('URI') ? : 0 3 3
Condition33%1/3
builtin::blessed($url) and $url->isa("URI")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($url) and $url->isa("URI")
builtin::blessed($url) $url->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($url)B= $url->isa("URI")
ABresult
0X0
100
111
159  
160 3 my $scheme = $uri->scheme;
161  
162 3 ▶ if( $scheme =~ m/\+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme =~ /\+/u) 0 3 3
163 0 ▶ $scheme =~ s/^circle\+// or croak "Found a + within URL scheme that is not 'circle+'";
Branch: 1 branch
BranchTrueFalseTotal
$scheme =~ s/^circle\+//u or croak("Found a + within URL scheme that is not 'circle+'") 0 0 0
164 }
165  
166 # Legacy name
167 3 ▶ $scheme = "sshexec" if $scheme eq "ssh";
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "ssh" 0 3 3
168  
169 3 my $authority = $uri->authority;
170  
171 3 my $path = $uri->path;
172 # Path will start with a leading /; we need to trim that
173 3 $path =~ s{^/}{};
174  
175 3 my $query = $uri->query;
176 3 ▶ defined $query or $query = "";
Branch: 1 branch
BranchTrueFalseTotal
defined $query or $query = "" 1 2 3
177  
178 3 my $f;
179  
180 3 ▶ if( $scheme eq "exec" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($scheme eq 'exec') { } 1 2 3
elsif ($scheme eq 'tcp') { } 1 1 2
elsif ($scheme eq 'unix') { } 1 0 1
181 # $query will contain args to exec - split them on +
182 1 $f = $self->connect_exec( [ $path, split m/\+/, $query ], %args );
183 }
184 elsif( $scheme eq "tcp" ) {
185 1 $f = $self->connect_tcp( $authority, %args );
186 }
187 elsif( $scheme eq "unix" ) {
188 1 $f = $self->connect_unix( $path, %args );
189 }
190 else {
191 0 my $connectorpkg = "Net::Async::Tangence::Client::via::$scheme";
192 0 ( my $connectorfile = "$connectorpkg.pm" ) =~ s{::}{/}g;
193 0 ▶ if( eval { require $connectorfile } and
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require $connectorfile } } and my $code = $connectorpkg->can('connect')) { } 0 0 0
Condition0%0/3
eval { do { require $connectorfile } } and my $code = $connectorpkg->can("connect")
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require $connectorfile } } and my $code = $connectorpkg->can("connect")
eval { do { require $connectorfile } } my $code = $connectorpkg->can("connect")
Truth table0%0/3
A and B
A= eval { do { require $connectorfile } }B= my $code = $connectorpkg->can("connect")
ABresult
0X0
100
111
194 my $code = $connectorpkg->can( 'connect' ) ) {
195 0 $f = $code->( $self, $uri, %args );
196 }
197 else {
198 0 croak "Unrecognised URL scheme name '$scheme'";
199 }
200 }
201  
202 3 ▶ return $f->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
203 3 my $on_root = $args{on_root};
204  
205 3 my $root_f = $self->new_future;
206  
207 $self->tangence_connected( %args,
208 3 ▶ on_root => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
209 3 my ( $root ) = @_;
210  
211 3 ▶ $on_root->( $root ) if $on_root;
Branch: 1 branch
BranchTrueFalseTotal
if $on_root 0 3 3
212 3 $root_f->done( $root );
213 },
214 3 );
215  
216 3 $root_f;
217 3 });
218 }
219  
220 =item * exec
221  
222 Directly executes the server as a child process. This is largely provided for
223 testing purposes, as the server will only run for this one client; it will
224 exit when the client disconnects.
225  
226 exec:///path/to/command?with+arguments
227  
228 The URL's path should point to the required command, and the query string will
229 be split on C<+> signs and used as the arguments. The authority section of the
230 URL will be ignored, so may be left empty.
231  
232 =cut
233  
234 sub connect_exec
235 1 ▶{
Subroutine connect_exec: called CC=3 SCAR=11.7
Pod undocumented
236 1 my $self = shift;
237 1 my ( $command ) = @_;
238  
239 1 my $loop = $self->get_loop;
240  
241 1 ▶ pipe( my $myread, my $childwrite ) or croak "Cannot pipe - $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $myread, my $childwrite or croak("Cannot pipe - $!") 1 0 1
242 1 ▶ pipe( my $childread, my $mywrite ) or croak "Cannoe pipe - $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $childread, my $mywrite or croak("Cannoe pipe - $!") 1 0 1
243  
244 $loop->spawn_child(
245 command => $command,
246  
247 setup => [
248 stdin => $childread,
249 stdout => $childwrite,
250 ],
251  
252 0 ▶ on_exit => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 my ( undef, $exitcode, $dollarbang ) = @_;
254 0 print STDERR "Child exited unexpectedly (status=$exitcode, \$!=$dollarbang)\n";
255 },
256 1 );
257  
258 1 $self->configure(
259 read_handle => $myread,
260 write_handle => $mywrite,
261 );
262  
263 1 Future->done;
264 }
265  
266 =item * sshexec
267  
268 A convenient wrapper around the C<exec> scheme, to connect to a server running
269 remotely via F<ssh>.
270  
271 sshexec://host/path/to/command?with+arguments
272  
273 The URL's authority section will give the SSH server (and optionally
274 username), and the path and query sections will be used as for C<exec>.
275  
276 (This scheme is also available as C<ssh>, though this name is now deprecated)
277  
278 =cut
279  
280 =item * tcp
281  
282 Connects to a server via a TCP socket.
283  
284 tcp://host:port/
285  
286 The URL's authority section will be used to give the server's hostname and
287 port number. The other sections of the URL will be ignored.
288  
289 =cut
290  
291 sub connect_tcp
292 1 ▶{
Subroutine connect_tcp: called CC=5 SCAR=18.1
Pod undocumented
293 1 my $self = shift;
294 1 my ( $authority, %args ) = @_;
295  
296 1 my $family;
297 1 ▶ $family = Socket::PF_INET() if $args{family} and $args{family} eq "inet4";
Branch: 1 branch
BranchTrueFalseTotal
if $args{"family"} and $args{"family"} eq "inet4" 0 1 1
Condition33%1/3
$args{"family"} and $args{"family"} eq "inet4"
!ll&&!rl&&r
100
MC/DC0%0/2
$args{"family"} and $args{"family"} eq "inet4"
$args{"family"} $args{"family"} eq "inet4"
Truth table33%1/3
A and B
A= $args{"family"}B= $args{"family"} eq "inet4"
ABresult
0X0
100
111
298 1 ▶ $family = Socket::PF_INET6() if $args{family} and $args{family} eq "inet6";
Branch: 1 branch
BranchTrueFalseTotal
if $args{"family"} and $args{"family"} eq "inet6" 0 1 1
Condition33%1/3
$args{"family"} and $args{"family"} eq "inet6"
!ll&&!rl&&r
100
MC/DC0%0/2
$args{"family"} and $args{"family"} eq "inet6"
$args{"family"} $args{"family"} eq "inet6"
Truth table33%1/3
A and B
A= $args{"family"}B= $args{"family"} eq "inet6"
ABresult
0X0
100
111
299  
300 1 my ( $host, $port ) = $authority =~ m/^(.*):(.*)$/;
301  
302 1 $self->connect(
303 host => $host,
304 service => $port,
305 );
306 }
307  
308 =item * unix
309  
310 Connects to a server via a UNIX local socket.
311  
312 unix:///path/to/socket
313  
314 The URL's path section will give the path to the local socket. The other
315 sections of the URL will be ignored.
316  
317 =cut
318  
319 sub connect_unix
320 1 ▶{
Subroutine connect_unix: called CC=1 SCAR=0.0
Pod undocumented
321 1 my $self = shift;
322 1 my ( $path ) = @_;
323  
324 1 $self->connect(
325 addr => {
326 family => 'unix',
327 socktype => 'stream',
328 path => $path,
329 },
330 );
331 }
332  
333 =item * sshunix
334  
335 Connects to a server running remotely via a UNIX socket over F<ssh>.
336  
337 sshunix://host/path/to/socket
338  
339 (This is implemented by running F<perl> remotely and sending it a tiny
340 self-contained program that connects STDIN/STDOUT to the given UNIX socket
341 path. It requires that the server has F<perl> at least version 5.6 available
342 in the path simply as C<perl>)
343  
344 =cut
345  
346 =back
347  
348 =cut
349  
350 =head1 AUTHOR
351  
352 Paul Evans <leonerd@leonerd.org.uk>
353  
354 =cut
355  
356 7 0x55AA;
« Tangence/Client/via/sshunix.pm ↑ Summary Tangence/Protocol.pm »