Tangence/Client.pm

filter: Statementstmt 91.1% 93 / 102 Branchbran 52.6% 20 / 38 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 96.5% 28 / 29 Podpod 38.4% 5 / 13 totaltotal 79.7% 150 / 188 CC 22 SCAR 9.8
CC
22
Cov
81%
__ANON__
6.0
__ANON__
6.0
make_proxy
5.3
CRAP 2.7
« Tangence/Class.pm ↑ Summary Tangence/Struct.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 Tangence::Client 0.33;
7  
8 9 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use base qw( Tangence::Stream );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Tangence::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Tangence::ObjectProxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use Future 0.36; # ->retain
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 9 ▶use constant VERSION_MINOR_MIN => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 C<Tangence::Client> - mixin class for building a C<Tangence> client
29  
30 =head1 SYNOPSIS
31  
32 This class is a mixin, it cannot be directly constructed
33  
34 package Example::Client;
35 use base qw( Base::Client Tangence::Client );
36  
37 sub connect
38 {
39 my $self = shift;
40 $self->SUPER::connect( @_ );
41  
42 $self->tangence_connected;
43  
44 wait_for { defined $self->rootobj };
45 }
46  
47 sub tangence_write
48 {
49 my $self = shift;
50 $self->write( $_[0] );
51 }
52  
53 sub on_read
54 {
55 my $self = shift;
56 $self->tangence_readfrom( $_[0] );
57 }
58  
59 package main;
60  
61 my $client = Example::Client->new;
62 $client->connect( "server.location.here" );
63  
64 my $rootobj = $client->rootobj;
65  
66 =head1 DESCRIPTION
67  
68 This module provides mixin to implement a C<Tangence> client connection. It
69 should be mixed in to an object used to represent a single connection to a
70 server. It provides a central location in the client to store object proxies,
71 including to the root object and the registry, and coordinates passing
72 messages between the server and the object proxies it contains.
73  
74 This is a subclass of L<Tangence::Stream> which provides implementations of
75 the required C<handle_request_> methods. A class mixing in C<Tangence::Client>
76 must still provide the C<tangence_write> method required for sending data to
77 the server.
78  
79 For an example of a class that uses this mixin, see
80 L<Net::Async::Tangence::Client>.
81  
82 =cut
83  
84 =head1 PROVIDED METHODS
85  
86 The following methods are provided by this mixin.
87  
88 =cut
89  
90 # Accessors for Tangence::Message decoupling
91 131 ▶sub objectproxies { shift->{objectproxies} ||= {} }
Subroutine objectproxies: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
+(shift())->{"objectproxies"} ||= {}
l!l
1229
MC/DC100%1/1
+(shift())->{"objectproxies"} ||= {}
+(shift())->{"objectproxies"}
Truth table100%2/2
A
A= +(shift())->{"objectproxies"}
Aresult
11
00
92  
93 =head2 rootobj
94  
95 $rootobj = $client->rootobj;
96  
97 Returns a L<Tangence::ObjectProxy> to the server's root object
98  
99 =cut
100  
101 sub rootobj
102 19 ▶{
Subroutine rootobj: called CC=2 SCAR=6.9
Pod documented
103 19 my $self = shift;
104 19 ▶ $self->{rootobj} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 10 19
105 19 return $self->{rootobj};
106 }
107  
108 =head2 registry
109  
110 $registry = $client->registry;
111  
112 Returns a L<Tangence::ObjectProxy> to the server's object registry if one has
113 been received, or C<undef> if not.
114  
115 This method is now deprecated in favour of L</get_registry>. Additionally note
116 that currently the client will attempt to request the registry at connection
117 time, but a later version of this module will stop doing that, so users who
118 need access to it should call C<get_registry>.
119  
120 =cut
121  
122 sub registry
123 19 ▶{
Subroutine registry: called CC=2 SCAR=6.9
Pod documented
124 19 my $self = shift;
125 19 ▶ $self->{registry} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 10 19
126 19 return $self->{registry};
127 }
128  
129 =head2 get_registry
130  
131 $registry = await $client->get_registry;
132  
133 Returns a L<Future> that will yield a L<Tangence::ObjectProxy> to the server's
134 registry object.
135  
136 Note that not all servers may permit access to the registry.
137  
138 =cut
139  
140 sub get_registry
141 9 ▶{
Subroutine get_registry: called CC=1 SCAR=0.0
Pod documented
142 9 my $self = shift;
143  
144 $self->request(
145 request => Tangence::Message->new( $self, MSG_GETREGISTRY ),
146 9 ▶ )->then( sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
147 9 my ( $message ) = @_;
148 9 my $code = $message->code;
149  
150 9 ▶ $code == MSG_RESULT or
Branch: 1 branch
BranchTrueFalseTotal
$code == 130 or return "Future"->fail("Cannot get registry - code $code", "tangence", $message) 9 0 9
151 return Future->fail( "Cannot get registry - code $code", tangence => $message );
152  
153 9 $self->registry( TYPE_OBJ->unpack_value( $message ) );
154 9 return Future->done( $self->registry );
155 9 });
156 }
157  
158 sub on_error
159 28 ▶{
Subroutine on_error: called CC=2 SCAR=6.9
Pod undocumented
160 28 my $self = shift;
161 28 ▶ $self->{on_error} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 19 28
162 28 return $self->{on_error};
163 }
164  
165 =head2 tangence_connected
166  
167 $client->tangence_connected( %args );
168  
169 Once the base connection to the server has been established, this method
170 should be called to perform the initial work of requesting the root object and
171 the registry.
172  
173 It takes the following named arguments:
174  
175 =over 8
176  
177 =item do_init => BOOL
178  
179 Ignored. Maintained for compatibility with previous version that allowed this
180 to be disabled.
181  
182 =item on_root => CODE
183  
184 Optional callback to be invoked once the root object has been returned. It
185 will be passed a L<Tangence::ObjectProxy> to the root object.
186  
187 $on_root->( $rootobj );
188  
189 =item on_registry => CODE
190  
191 Optional callback to be invoked once the registry has been returned. It will
192 be passed a L<Tangence::ObjectProxy> to the registry.
193  
194 $on_registry->( $registry );
195  
196 Note that in the case that the server does not permit access to the registry
197 or an error occurs while requesting it, this is invoked with an empty list.
198  
199 $on_registry->();
200  
201 =item version_minor_min => INT
202  
203 Optional minimum minor version to negotiate with the server. This can be used
204 to require a higher minimum version than the client module itself supports, in
205 case the application requires features in a newer version than that.
206  
207 =back
208  
209 =cut
210  
211 9 ▶sub tangence_connected ( $self, %args )
Subroutine tangence_connected: called CC=2 SCAR=8.0
Pod documented
212 {
213 9 ▶ my $version_minor_min = max( VERSION_MINOR_MIN, $args{version_minor_min} || 0 );
Condition50%1/2
$args{'version_minor_min'} || 0
l!l
09
MC/DC0%0/1
$args{'version_minor_min'} || 0
$args{'version_minor_min'}
Truth table50%1/2
A
A= $args{'version_minor_min'}
Aresult
11
00
214  
215 $self->request(
216 request => Tangence::Message->new( $self, MSG_INIT )
217 ->pack_int( VERSION_MAJOR )
218 ->pack_int( VERSION_MINOR )
219 ->pack_int( $version_minor_min ),
220  
221 9 ▶ on_response => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
222 9 my ( $message ) = @_;
223 9 my $code = $message->code;
224  
225 9 ▶ if( $code == MSG_INITED ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code == 255) { } 9 0 9
elsif ($code == 129) { } 0 0 0
226 9 my $major = $message->unpack_int();
227 9 my $minor = $message->unpack_int();
228  
229 9 $self->minor_version( $minor );
230 9 $self->tangence_initialised( %args );
231 }
232 elsif( $code == MSG_ERROR ) {
233 0 my $msg = $message->unpack_str();
234 0 print STDERR "Cannot initialise stream - error $msg";
235 }
236 else {
237 0 print STDERR "Cannot initialise stream - code $code\n";
238 }
239 },
240 9 );
241 }
242  
243 9 ▶sub tangence_initialised ( $self, %args )
Subroutine tangence_initialised: called CC=1 SCAR=0.8
Pod undocumented
244 {
245 9 my $request = Tangence::Message->new( $self, MSG_GETROOT );
246 9 TYPE_ANY->pack_value( $request, $self->identity );
247  
248 $self->request(
249 request => $request,
250  
251 9 ▶ on_response => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
252 9 my ( $message ) = @_;
253 9 my $code = $message->code;
254  
255 9 ▶ if( $code == MSG_RESULT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code == 130) { } 9 0 9
elsif ($code == 129) { } 0 0 0
256 9 $self->rootobj( TYPE_OBJ->unpack_value( $message ) );
257 9 ▶ $args{on_root}->( $self->rootobj ) if $args{on_root};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_root"} 0 9 9
258 }
259 elsif( $code == MSG_ERROR ) {
260 0 my $msg = $message->unpack_str();
261 0 print STDERR "Cannot get root object - error $msg";
262 }
263 else {
264 0 print STDERR "Cannot get root object - code $code\n";
265 }
266 }
267 9 );
268  
269 $self->get_registry->then(
270 9 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
271 9 my ( $registry ) = @_;
272 9 ▶ $args{on_registry}->( $registry ) if $args{on_registry};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_registry"} 0 9 9
273 },
274 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
275 0 ▶ $args{on_registry}->() if $args{on_registry};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_registry"} 0 0 0
276 }
277 9 )->retain;
278 }
279  
280 2 ▶sub handle_request_EVENT ( $self, $token, $message )
Subroutine handle_request_EVENT: called CC=2 SCAR=7.0
Pod undocumented
281 {
282 2 my $objid = $message->unpack_int();
283  
284 2 $self->respond( $token, Tangence::Message->new( $self, MSG_OK ) );
285  
286 2 ▶ if( my $obj = $self->objectproxies->{$objid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->objectproxies->{$objid}) 2 0 2
287 2 $obj->handle_request_EVENT( $message );
288 }
289 }
290  
291 47 ▶sub handle_request_UPDATE ( $self, $token, $message )
Subroutine handle_request_UPDATE: called CC=2 SCAR=7.0
Pod undocumented
292 {
293 47 my $objid = $message->unpack_int();
294  
295 47 $self->respond( $token, Tangence::Message->new( $self, MSG_OK ) );
296  
297 47 ▶ if( my $obj = $self->objectproxies->{$objid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->objectproxies->{$objid}) 47 0 47
298 47 $obj->handle_request_UPDATE( $message );
299 }
300 }
301  
302 2 ▶sub handle_request_DESTROY ( $self, $token, $message )
Subroutine handle_request_DESTROY: called CC=2 SCAR=7.0
Pod undocumented
303 {
304 2 my $objid = $message->unpack_int();
305  
306 2 ▶ if( my $obj = $self->objectproxies->{$objid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->objectproxies->{$objid}) 2 0 2
307 2 $obj->destroy;
308 2 delete $self->objectproxies->{$objid};
309 }
310  
311 2 $self->respond( $token, Tangence::Message->new( $self, MSG_OK ) );
312 }
313  
314 20 ▶sub get_by_id ( $self, $id )
Subroutine get_by_id: called CC=2 SCAR=9.2
Pod undocumented
315 {
316 20 ▶ return $self->objectproxies->{$id} if exists $self->objectproxies->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->objectproxies->{$id} 20 0 20
317  
318 0 croak "Have no proxy of object id $id";
319 }
320  
321 19 ▶sub make_proxy ( $self, $id, $classname, $smashdata )
Subroutine make_proxy: called CC=5 SCAR=16.7
Pod undocumented
322 {
323 19 ▶ if( exists $self->objectproxies->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->objectproxies->{$id}) 0 19 19
324 0 croak "Already have an object id $id";
325 }
326  
327 19 my $class;
328 19 ▶ if( defined $classname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $classname) 19 0 19
329 19 $class = $self->peer_hasclass->{$classname}->[0];
330 19 ▶ defined $class or croak "Cannot construct a proxy for class $classname as no meta exists";
Branch: 1 branch
BranchTrueFalseTotal
defined $class or croak("Cannot construct a proxy for class $classname as no meta exists") 19 0 19
331 }
332  
333 19 my $obj = $self->objectproxies->{$id} =
334 Tangence::ObjectProxy->new(
335 client => $self,
336 id => $id,
337  
338 class => $class,
339  
340 on_error => $self->on_error,
341 );
342  
343 19 ▶ $obj->grab( $smashdata ) if defined $smashdata;
Branch: 1 branch
BranchTrueFalseTotal
if defined $smashdata 10 9 19
344  
345 19 return $obj;
346 }
347  
348 =head1 SUBCLASSING METHODS
349  
350 These methods are intended for implementation classes to override.
351  
352 =cut
353  
354 =head2 new_future
355  
356 $f = $client->new_future;
357  
358 Returns a new L<Future> instance for basing asynchronous operations on.
359  
360 =cut
361  
362 sub new_future
363 66 ▶{
Subroutine new_future: called CC=1 SCAR=0.0
Pod documented
364 66 return Future->new;
365 }
366  
367 =head1 AUTHOR
368  
369 Paul Evans <leonerd@leonerd.org.uk>
370  
371 =cut
372  
373 9 0x55AA;
« Tangence/Class.pm ↑ Summary Tangence/Struct.pm »