Tangence/Server.pm

filter: Statementstmt 96.4% 244 / 253 Branchbran 60.3% 35 / 58 Conditioncond 60.0% 12 / 20 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 42 / 42 Podpod 19.0% 4 / 21 totaltotal 84.3% 339 / 402 CC 42 SCAR 13.2
CC
42
Cov
88%
tangence_closed
8.1
object_destroyed
7.1
handle_request_UNSUBSCRIBE
6.8
CRAP 3.7
« Tangence/Object.pm ↑ Summary Tangence/Message.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, 2011-2021 -- leonerd@leonerd.org.uk
5  
6 package Tangence::Server 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 Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # TODO: since we're using meta 0.007 we could use set_subname from there but
18 # it's more awkward to use it this way
19 9 ▶use Sub::Util 1.40 qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ▶use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use Tangence::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use Tangence::Server::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 9 ▶use Struct::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 struct CursorObject => [qw( cursor obj )];
28  
29 # We will accept any version back to 3
30 9 ▶use constant VERSION_MINOR_MIN => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =head1 NAME
33  
34 C<Tangence::Server> - mixin class for building a C<Tangence> server
35  
36 =head1 SYNOPSIS
37  
38 This class is a mixin, it cannot be directly constructed
39  
40 package Example::Server;
41 use base qw( Base::Server Tangence::Server );
42  
43 sub new
44 {
45 my $class = shift;
46 my %args = @_;
47  
48 my $registry = delete $args{registry};
49  
50 my $self = $class->SUPER::new( %args );
51  
52 $self->registry( $registry );
53  
54 return $self;
55 }
56  
57 sub tangence_write
58 {
59 my $self = shift;
60 $self->write( $_[0] );
61 }
62  
63 sub on_read
64 {
65 my $self = shift;
66 $self->tangence_readfrom( $_[0] );
67 }
68  
69 =head1 DESCRIPTION
70  
71 This module provides mixin to implement a C<Tangence> server connection. It
72 should be mixed in to an object used to represent a single connection from a
73 client. It provides a location for the objects in server to store information
74 about the client connection, and coordinates passing messages between the
75 client and the objects in the server.
76  
77 This is a subclass of L<Tangence::Stream> which provides implementations of
78 the required C<handle_request_> methods. A class mixing in C<Tangence::Server>
79 must still provide the C<write> method required for sending data to the
80 client.
81  
82 For an example of a class that uses this mixin, see
83 L<Net::Async::Tangence::ServerProtocol>.
84  
85 =cut
86  
87 =head1 PROVIDED METHODS
88  
89 The following methods are provided by this mixin.
90  
91 =cut
92  
93 9 ▶sub subscriptions { shift->{subscriptions} ||= [] }
Subroutine subscriptions: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
+(shift())->{"subscriptions"} ||= []
l!l
63
MC/DC100%1/1
+(shift())->{"subscriptions"} ||= []
+(shift())->{"subscriptions"}
Truth table100%2/2
A
A= +(shift())->{"subscriptions"}
Aresult
11
00
94 49 ▶sub watches { shift->{watches} ||= [] }
Subroutine watches: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
+(shift())->{"watches"} ||= []
l!l
409
MC/DC100%1/1
+(shift())->{"watches"} ||= []
+(shift())->{"watches"}
Truth table100%2/2
A
A= +(shift())->{"watches"}
Aresult
11
00
95  
96 =head2 registry
97  
98 $server->registry( $registry )
99  
100 $registry = $server->registry
101  
102 Accessor to set or obtain the L<Tangence::Registry> object for the server.
103  
104 =cut
105  
106 sub registry
107 75 ▶{
Subroutine registry: called CC=2 SCAR=6.9
Pod documented
108 75 my $self = shift;
109 75 ▶ $self->{registry} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 66 75
110 75 return $self->{registry};
111 }
112  
113 sub tangence_closed
114 1 ▶{
Subroutine tangence_closed: called CC=7 SCAR=21.0
Pod documented
115 1 my $self = shift;
116 1 $self->SUPER::tangence_closed;
117  
118 1 ▶ if( my $subscriptions = $self->subscriptions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subscriptions = $self->subscriptions) 1 0 1
119 1 foreach my $s ( @$subscriptions ) {
120 0 my ( $object, $event, $id ) = @$s;
121 0 $object->unsubscribe_event( $event, $id );
122 }
123  
124 1 undef @$subscriptions;
125 }
126  
127 1 ▶ if( my $watches = $self->watches ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $watches = $self->watches) 1 0 1
128 1 foreach my $w ( @$watches ) {
129 4 my ( $object, $prop, $id ) = @$w;
130 4 $object->unwatch_property( $prop, $id );
131 }
132  
133 1 undef @$watches;
134 }
135  
136 1 ▶ if( my $cursors = $self->peer_hascursor ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cursors = $self->peer_hascursor) 1 0 1
137 1 foreach my $cursorobj ( values %$cursors ) {
138 0 $self->drop_cursorobj( $cursorobj );
139 }
140 }
141 }
142  
143 54 ▶sub get_by_id ( $self, $id )
Subroutine get_by_id: called CC=3 SCAR=11.1
Pod undocumented
144 {
145 # Only permit the client to interact with objects they've already been
146 # sent, so they cannot gain access by inventing object IDs
147 54 ▶ $self->peer_hasobj->{$id} or
Branch: 1 branch
BranchTrueFalseTotal
$self->peer_hasobj->{$id} or die "Access not allowed to object with id $id\n" 48 6 54
148 die "Access not allowed to object with id $id\n";
149  
150 48 ▶ my $obj = $self->registry->get_by_id( $id ) or
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $self->registry->get_by_id($id) or die "No such object with id $id\n" 48 0 48
151 die "No such object with id $id\n";
152  
153 48 return $obj;
154 }
155  
156 4 ▶sub handle_request_CALL ( $self, $token, $message )
Subroutine handle_request_CALL: called CC=1 SCAR=0.0
Pod undocumented
157 {
158 4 my $ctx = Tangence::Server::Context->new( $self, $token );
159  
160 4 my $response;
161 4 try {
162 4 my $objid = $message->unpack_int();
163  
164 4 my $object = $self->get_by_id( $objid );
165  
166 3 $response = $object->handle_request_CALL( $ctx, $message );
167 }
168 catch ( $e ) {
169 1 return $ctx->responderr( $e );
170 }
171  
172 3 $ctx->respond( $response );
173 }
174  
175 3 ▶sub handle_request_SUBSCRIBE ( $self, $token, $message )
Subroutine handle_request_SUBSCRIBE: called CC=1 SCAR=0.0
Pod undocumented
176 {
177 3 my $ctx = Tangence::Server::Context->new( $self, $token );
178  
179 3 my $response;
180 3 try {
181 3 my $objid = $message->unpack_int();
182 3 my $event = $message->unpack_str();
183  
184 3 my $object = $self->get_by_id( $objid );
185  
186 2 weaken( my $weakself = $self );
187  
188 my $id = $object->subscribe_event( $event,
189 2 ▶ set_subname "__SUBSCRIBE($event)__" => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
190 2 ▶ $weakself or return;
Branch: 1 branch
BranchTrueFalseTotal
$weakself or return 2 0 2
191 2 my $object = shift;
192  
193 2 my $message = $object->generate_message_EVENT( $weakself, $event, @_ );
194 $weakself->request(
195 request => $message,
196 2 ▶ on_response => sub { "IGNORE" },
Subroutine __ANON__: called CC=1 SCAR=0.0
197 2 );
198 }
199 2 );
200  
201 2 push @{ $self->subscriptions }, [ $object, $event, $id ];
202  
203 2 $response = Tangence::Message->new( $self, MSG_SUBSCRIBED )
204 }
205 catch ( $e ) {
206 1 return $ctx->responderr( $e );
207 }
208  
209 2 $ctx->respond( $response );
210 }
211  
212 2 ▶sub handle_request_UNSUBSCRIBE ( $self, $token, $message )
Subroutine handle_request_UNSUBSCRIBE: called CC=6 SCAR=19.1
Pod undocumented
213 {
214 2 my $ctx = Tangence::Server::Context->new( $self, $token );
215  
216 2 my $response;
217 2 try {
218 2 my $objid = $message->unpack_int();
219 2 my $event = $message->unpack_str();
220  
221 2 my $object = $self->get_by_id( $objid );
222  
223 2 ▶ my $edef = $object->can_event( $event ) or
Branch: 1 branch
BranchTrueFalseTotal
my $edef = $object->can_event($event) or die "Object cannot respond to event $event\n" 2 0 2
224 die "Object cannot respond to event $event\n";
225  
226 # Delete from subscriptions and obtain id
227 2 my $id;
228 2 ▶ @{ $self->subscriptions } = grep { $_->[0] == $object and $_->[1] eq $event and ( $id = $_->[2], 0 ) or 1 }
Condition37%3/8
$_->[0] == $object && $_->[1] eq $event && ($id = $_->[2], 0) || 1
l!l
02
$_->[0] == $object and $_->[1] eq $event and $id = $_->[2], 0
!ll&&!rl&&r
020
$_->[0] == $object and $_->[1] eq $event
!ll&&!rl&&r
002
MC/DC0%0/3
$_->[0] == $object && $_->[1] eq $event && ($id = $_->[2], 0) || 1
$_->[0] == $object $_->[1] eq $event $id = $_->[2], 0
Truth table25%1/4
A and B and C
A= $_->[0] == $objectB= $_->[1] eq $eventC= $id = $_->[2], 0
ABCresult
1111
0XX0
10X0
1100
229 2 @{ $self->subscriptions };
230 2 ▶ defined $id or
Branch: 1 branch
BranchTrueFalseTotal
defined $id or die "Not subscribed to $event\n" 2 0 2
231 die "Not subscribed to $event\n";
232  
233 2 $object->unsubscribe_event( $event, $id );
234  
235 2 $response = Tangence::Message->new( $self, MSG_OK )
236 }
237 catch ( $e ) {
238 0 return $ctx->responderr( $e );
239 }
240  
241 2 $ctx->respond( $response );
242 }
243  
244 5 ▶sub handle_request_GETPROP ( $self, $token, $message )
Subroutine handle_request_GETPROP: called CC=1 SCAR=0.0
Pod undocumented
245 {
246 5 my $ctx = Tangence::Server::Context->new( $self, $token );
247  
248 5 my $response;
249 5 try {
250 5 my $objid = $message->unpack_int();
251  
252 5 my $object = $self->get_by_id( $objid );
253  
254 4 $response = $object->handle_request_GETPROP( $ctx, $message )
255 }
256 catch ( $e ) {
257 1 return $ctx->responderr( $e );
258 }
259  
260 4 $ctx->respond( $response );
261 }
262  
263 4 ▶sub handle_request_GETPROPELEM ( $self, $token, $message )
Subroutine handle_request_GETPROPELEM: called CC=1 SCAR=0.0
Pod undocumented
264 {
265 4 my $ctx = Tangence::Server::Context->new( $self, $token );
266  
267 4 my $response;
268 4 try {
269 4 my $objid = $message->unpack_int();
270  
271 4 my $object = $self->get_by_id( $objid );
272  
273 4 $response = $object->handle_request_GETPROPELEM( $ctx, $message )
274 }
275 catch ( $e ) {
276 0 return $ctx->responderr( $e );
277 }
278  
279 4 $ctx->respond( $response );
280 }
281  
282 9 ▶sub handle_request_SETPROP ( $self, $token, $message )
Subroutine handle_request_SETPROP: called CC=1 SCAR=0.0
Pod undocumented
283 {
284 9 my $ctx = Tangence::Server::Context->new( $self, $token );
285  
286 9 my $response;
287 9 try {
288 9 my $objid = $message->unpack_int();
289  
290 9 my $object = $self->get_by_id( $objid );
291  
292 8 $response = $object->handle_request_SETPROP( $ctx, $message )
293 }
294 catch ( $e ) {
295 2 return $ctx->responderr( $e );
296 }
297  
298 7 $ctx->respond( $response );
299 }
300  
301 9 *handle_request_WATCH = \&_handle_request_WATCHany;
302 9 *handle_request_WATCH_CUSR = \&_handle_request_WATCHany;
303 19 ▶sub _handle_request_WATCHany ( $self, $token, $message )
Subroutine _handle_request_WATCHany: called CC=5 SCAR=16.1
304 {
305 19 my $ctx = Tangence::Server::Context->new( $self, $token );
306  
307 19 my ( $want_initial, $object, $prop );
308  
309 19 my $response;
310 19 try {
311 19 my $objid = $message->unpack_int();
312 19 $prop = $message->unpack_str();
313  
314 19 $object = $self->get_by_id( $objid );
315  
316 18 ▶ my $pdef = $object->can_property( $prop ) or
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $object->can_property($prop) or die "Object does not have property $prop\n" 18 0 18
317 die "Object does not have property $prop\n";
318  
319 18 $self->_install_watch( $object, $prop );
320  
321 18 ▶ if( $message->code == MSG_WATCH ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($message->code == 7) { } 12 6 18
elsif ($message->code == 12) { } 6 0 6
322 12 $want_initial = $message->unpack_bool();
323  
324 12 $response = Tangence::Message->new( $self, MSG_WATCHING )
325 }
326 elsif( $message->code == MSG_WATCH_CUSR ) {
327 6 my $from = $message->unpack_int();
328  
329 6 my $m = "cursor_prop_$prop";
330 6 my $cursor = $object->$m( $from );
331 6 my $id = $self->message_state->{next_cursorid}++;
332  
333 6 $self->peer_hascursor->{$id} = CursorObject( $cursor, $object );
334 $response = Tangence::Message->new( $self, MSG_WATCHING_CUSR )
335 ->pack_int( $id )
336 ->pack_int( 0 ) # first index
337 6 ->pack_int( $#{ $object->${\"get_prop_$prop"} } ) # last index
338 }
339 }
340 catch ( $e ) {
341 1 return $ctx->responderr( $e );
342 }
343  
344 18 $ctx->respond( $response );
345  
346 18 ▶ $self->_send_initial( $object, $prop ) if $want_initial;
Branch: 1 branch
BranchTrueFalseTotal
if $want_initial 8 10 18
347 }
348  
349 8 ▶sub _send_initial ( $self, $object, $prop )
Subroutine _send_initial: called CC=2 SCAR=7.1
350 {
351 8 my $m = "get_prop_$prop";
352 8 ▶ return unless( $object->can( $m ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $object->can($m) 0 8 8
353  
354 8 try {
355 8 my $value = $object->$m();
356 8 my $message = $object->generate_message_UPDATE( $self, $prop, CHANGE_SET, $value );
357 $self->request(
358 request => $message,
359 8 ▶ on_response => sub { "IGNORE" },
Subroutine __ANON__: called CC=1 SCAR=0.0
360 8 );
361 }
362 catch ( $e ) {
363 0 warn "$e during initial property fetch";
364 }
365 }
366  
367 4 ▶sub handle_request_UNWATCH ( $self, $token, $message )
Subroutine handle_request_UNWATCH: called CC=6 SCAR=18.4
Pod undocumented
368 {
369 4 my $ctx = Tangence::Server::Context->new( $self, $token );
370  
371 4 my $response;
372 4 try {
373 4 my $objid = $message->unpack_int();
374 4 my $prop = $message->unpack_str();
375  
376 4 my $object = $self->get_by_id( $objid );
377  
378 4 ▶ my $pdef = $object->can_property( $prop ) or
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $object->can_property($prop) or die "Object does not have property $prop\n" 4 0 4
379 die "Object does not have property $prop\n";
380  
381 # Delete from watches and obtain id
382 4 my $id;
383 13 ▶ @{ $self->watches } = grep { $_->[0] == $object and $_->[1] eq $prop and ( $id = $_->[2], 0 ) or 1 }
Condition62%5/8
$_->[0] == $object && $_->[1] eq $prop && ($id = $_->[2], 0) || 1
l!l
013
$_->[0] == $object and $_->[1] eq $prop and $id = $_->[2], 0
!ll&&!rl&&r
850
$_->[0] == $object and $_->[1] eq $prop
!ll&&!rl&&r
085
MC/DC0%0/3
$_->[0] == $object && $_->[1] eq $prop && ($id = $_->[2], 0) || 1
$_->[0] == $object $_->[1] eq $prop $id = $_->[2], 0
Truth table50%2/4
A and B and C
A= $_->[0] == $objectB= $_->[1] eq $propC= $id = $_->[2], 0
ABCresult
1111
0XX0
10X0
1100
384 4 @{ $self->watches };
385 4 ▶ defined $id or
Branch: 1 branch
BranchTrueFalseTotal
defined $id or die "Not watching $prop\n" 4 0 4
386 die "Not watching $prop\n";
387  
388 4 $object->unwatch_property( $prop, $id );
389  
390 4 $response = Tangence::Message->new( $self, MSG_OK );
391 }
392 catch ( $e ) {
393 0 return $ctx->responderr( $e );
394 }
395  
396 4 $ctx->respond( $response );
397 }
398  
399 12 ▶sub handle_request_CUSR_NEXT ( $self, $token, $message )
Subroutine handle_request_CUSR_NEXT: called CC=2 SCAR=7.0
Pod undocumented
400 {
401 12 my $cursor_id = $message->unpack_int();
402  
403 12 my $ctx = Tangence::Server::Context->new( $self, $token );
404  
405 12 ▶ my $cursorobj = $self->peer_hascursor->{$cursor_id} or
Branch: 1 branch
BranchTrueFalseTotal
my $cursorobj = $self->peer_hascursor->{$cursor_id} or return $ctx->responderr("No such cursor with id $cursor_id") 12 0 12
406 return $ctx->responderr( "No such cursor with id $cursor_id" );
407  
408 12 $cursorobj->cursor->handle_request_CUSR_NEXT( $ctx, $message );
409 }
410  
411 6 ▶sub handle_request_CUSR_DESTROY ( $self, $token, $message )
Subroutine handle_request_CUSR_DESTROY: called CC=1 SCAR=0.0
Pod undocumented
412 {
413 6 my $cursor_id = $message->unpack_int();
414  
415 6 my $ctx = Tangence::Server::Context->new( $self, $token );
416  
417 6 my $cursorobj = delete $self->peer_hascursor->{$cursor_id};
418 6 $self->drop_cursorobj( $cursorobj );
419  
420 6 $ctx->respond( Tangence::Message->new( $self, MSG_OK ) );
421 }
422  
423 6 ▶sub drop_cursorobj ( $self, $cursorobj )
Subroutine drop_cursorobj: called CC=1 SCAR=0.0
Pod undocumented
424 {
425 6 my $m = "uncursor_prop_" . $cursorobj->cursor->prop->name;
426 6 $cursorobj->obj->$m( $cursorobj->cursor );
427 }
428  
429 9 ▶sub handle_request_INIT ( $self, $token, $message )
Subroutine handle_request_INIT: called CC=5 SCAR=17.2
Pod undocumented
430 {
431 9 my $major = $message->unpack_int();
432 9 my $minor_max = $message->unpack_int();
433 9 my $minor_min = $message->unpack_int();
434  
435 9 my $ctx = Tangence::Server::Context->new( $self, $token );
436  
437 9 ▶ if( $major != VERSION_MAJOR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major != 0) 0 9 9
438 0 return $ctx->responderr( "Major version $major not available" );
439 }
440  
441 # Don't accept higher than the minor version we recognise
442 9 ▶ $minor_max = VERSION_MINOR if $minor_max > VERSION_MINOR;
Branch: 1 branch
BranchTrueFalseTotal
if $minor_max > 4 0 9 9
443 9 ▶ $minor_min = VERSION_MINOR_MIN if $minor_min < VERSION_MINOR_MIN;
Branch: 1 branch
BranchTrueFalseTotal
if $minor_min < 3 0 9 9
444  
445 9 ▶ if( $minor_max < $minor_min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minor_max < $minor_min) 0 9 9
446 0 return $ctx->responderr( "No suitable minor version available" );
447 }
448  
449 # For unit tests or other synchronous cases, we need to set the version
450 # -before- we send the message. But we'd better construct the response
451 # message before setting the version, in case it makes a difference.
452 9 my $response = Tangence::Message->new( $self, MSG_INITED )
453 ->pack_int( $major )
454 ->pack_int( $minor_max );
455  
456 9 $self->minor_version( $minor_max );
457  
458 9 $ctx->respond( $response );
459 }
460  
461 9 ▶sub handle_request_GETROOT ( $self, $token, $message )
Subroutine handle_request_GETROOT: called CC=1 SCAR=0.0
Pod undocumented
462 {
463 9 my $identity = TYPE_ANY->unpack_value( $message );
464  
465 9 my $ctx = Tangence::Server::Context->new( $self, $token );
466  
467 9 $self->identity( $identity );
468  
469 9 my $root = $self->rootobj( $identity );
470  
471 9 my $response = Tangence::Message->new( $self, MSG_RESULT );
472 9 TYPE_OBJ->pack_value( $response, $root );
473  
474 9 $ctx->respond( $response );
475 }
476  
477 9 ▶sub handle_request_GETREGISTRY ( $self, $token, $message )
Subroutine handle_request_GETREGISTRY: called CC=2 SCAR=7.0
Pod undocumented
478 {
479 9 my $ctx = Tangence::Server::Context->new( $self, $token );
480  
481 9 ▶ $self->permit_registry or
Branch: 1 branch
BranchTrueFalseTotal
$self->permit_registry or return $ctx->responderr("This client is not permitted access to the registry") 9 0 9
482 return $ctx->responderr( "This client is not permitted access to the registry" );
483  
484 9 my $response = Tangence::Message->new( $self, MSG_RESULT );
485 9 TYPE_OBJ->pack_value( $response, $self->registry );
486  
487 9 $ctx->respond( $response );
488 }
489  
490 9 my %change_values = (
491 on_set => CHANGE_SET,
492 on_add => CHANGE_ADD,
493 on_del => CHANGE_DEL,
494 on_push => CHANGE_PUSH,
495 on_shift => CHANGE_SHIFT,
496 on_splice => CHANGE_SPLICE,
497 on_move => CHANGE_MOVE,
498 );
499  
500 38 ▶sub _install_watch ( $self, $object, $prop )
Subroutine _install_watch: called CC=2 SCAR=6.9
501 {
502 38 my $pdef = $object->can_property( $prop );
503 38 my $dim = $pdef->dimension;
504  
505 38 weaken( my $weakself = $self );
506  
507 38 my %callbacks;
508 38 foreach my $name ( @{ CHANGETYPES->{$dim} } ) {
509 106 my $how = $change_values{$name};
510 37 ▶ $callbacks{$name} = set_subname "__WATCH($prop:$name)__" => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
511 37 ▶ $weakself or return;
Branch: 1 branch
BranchTrueFalseTotal
$weakself or return 37 0 37
512 37 my $object = shift;
513  
514 37 my $message = $object->generate_message_UPDATE( $weakself, $prop, $how, @_ );
515 $weakself->request(
516 request => $message,
517 37 ▶ on_response => sub { "IGNORE" },
Subroutine __ANON__: called CC=1 SCAR=0.0
518 37 );
519 106 };
520 }
521  
522 38 my $id = $object->watch_property( $prop, %callbacks );
523  
524 38 push @{ $self->watches }, [ $object, $prop, $id ];
525 }
526  
527 2 ▶sub object_destroyed ( $self, $obj, @rest )
Subroutine object_destroyed: called CC=7 SCAR=19.6
Pod undocumented
528 {
529 2 ▶ if( my $subs = $self->subscriptions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subs = $self->subscriptions) 2 0 2
530 2 my $i = 0;
531 2 ▶ while( $i < @$subs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$subs) 2 2 4
532 2 my $s = $subs->[$i];
533  
534 2 ▶ $i++, next unless $s->[0] == $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->[0] == $obj 0 2 2
535  
536 2 my ( undef, $event, $id ) = @$s;
537 2 $obj->unsubscribe_event( $event, $id );
538  
539 2 splice @$subs, $i, 1;
540 # No $i++
541 }
542 }
543  
544 2 ▶ if( my $watches = $self->watches ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $watches = $self->watches) 2 0 2
545 2 my $i = 0;
546 2 ▶ while( $i < @$watches ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$watches) 8 2 10
547 8 my $w = $watches->[$i];
548  
549 8 ▶ $i++, next unless $w->[0] == $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->[0] == $obj 0 8 8
550  
551 8 my ( undef, $prop, $id ) = @$w;
552 8 $obj->unwatch_property( $prop, $id );
553  
554 8 splice @$watches, $i, 1;
555 # No $i++
556 }
557 }
558  
559 2 $self->SUPER::object_destroyed( $obj, @rest );
560 }
561  
562 =head1 OVERRIDEABLE METHODS
563  
564 The following methods are provided but intended to be overridden if the
565 implementing class wishes to provide different behaviour from the default.
566  
567 =cut
568  
569 =head2 rootobj
570  
571 $rootobj = $server->rootobj( $identity )
572  
573 Invoked when a C<GETROOT> message is received from the client, this method
574 should return a L<Tangence::Object> as root object for the connection.
575  
576 The default implementation will return the object with ID 1; i.e. the first
577 object created in the registry.
578  
579 =cut
580  
581 sub rootobj
582 9 ▶{
Subroutine rootobj: called CC=1 SCAR=0.0
Pod documented
583 9 my $self = shift;
584  
585 9 return $self->registry->get_by_id( 1 );
586 }
587  
588 =head2 permit_registry
589  
590 $allow = $server->permit_registry
591  
592 Invoked when a C<GETREGISTRY> message is received from the client, this method
593 should return a boolean to indicate whether the client is allowed to access
594 the object registry.
595  
596 The default implementation always permits this, but an overridden method may
597 decide to disallow it in some situations. When disabled, a client will not be
598 able to gain access to any serverside objects other than the root object, and
599 (recursively) any other objects returned by methods, events or properties on
600 objects already known. This can be used as a security mechanism.
601  
602 =cut
603  
604 9 ▶sub permit_registry { 1; }
Subroutine permit_registry: called CC=1 SCAR=0.0
Pod documented
605  
606 =head1 AUTHOR
607  
608 Paul Evans <leonerd@leonerd.org.uk>
609  
610 =cut
611  
612 9 0x55AA;
« Tangence/Object.pm ↑ Summary Tangence/Message.pm »