1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2010-2021 -- leonerd@leonerd.org.uk
package Tangence::Object 0.33;
use v5.26;
use warnings;
use experimental 'signatures';
use Carp;
use Syntax::Keyword::Match;
use Tangence::Constants;
use Tangence::Types;
use Tangence::Class;
Tangence::Class->declare(
__PACKAGE__,
events => {
destroy => {
args => [],
},
},
);
=head1 NAME
C<Tangence::Object> - base class for accessible objects in a C<Tangence> server
=head1 DESCRIPTION
This class acts as a base class for the accessible objects in a L<Tangence>
server. All the objects actually created and made accessible to clients will
be subclasses of this one, including internally-created objects such as
L<Tangence::Registry>.
These objects are not directly constructed by calling the C<new> class method;
instead the C<Tangence::Registry> should be used to construct one.
=cut
sub new ( $class, %args )
{
defined( my $id = delete $args{id} ) or croak "Need a id";
my $registry = delete $args{registry} or croak "Need a registry";
my $self = bless {
id => $id,
registry => $registry,
meta => $args{meta} || Tangence::Class->for_perlname( $class ),
event_subs => {}, # {$event} => [ @cbs ]
properties => {}, # {$prop} => T:P::Instance struct
}, $class;
my $properties = $self->class->properties;
foreach my $prop ( keys %$properties ) {
my $meth = "new_prop_$prop";
$self->$meth();
}
return $self;
}
=head1 METHODS
=cut
=head2 destroy
$obj->destroy;
Requests that the object destroy itself, informing all clients that are aware
of it. Once they all report that they have dropped the object, the object is
deconstructed for real.
Not to be confused with Perl's own C<DESTROY> method.
=cut
sub destroy ( $self, %args )
{
$self->{destroying} = 1;
my $outstanding = 1;
my $on_destroyed = $args{on_destroyed};
my $incsub = sub {
$outstanding++
};
my $decsub = sub {
--$outstanding and return;
$self->_destroy_really;
$on_destroyed->() if $on_destroyed;
};
foreach my $cb ( @{ $self->{event_subs}->{destroy} } ) {
$cb->( $self, $incsub, $decsub );
}
$decsub->();
}
sub _destroy_really
{
my $self = shift;
$self->registry->destroy_object( $self );
undef %$self; # Now I am dead
$self->{destroyed} = 1;
}
=head2 id
$id = $obj->id;
Returns the object's C<Tangence> ID number
=cut
sub id
{
my $self = shift;
return $self->{id};
}
=head2 describe
$description = $obj->describe;
Returns a textual description of the object, for internal debugging purposes.
Subclasses are encouraged to override this method to return something more
descriptive within their domain of interest
=cut
sub describe
{
my $self = shift;
return ref $self;
}
=head2 registry
$registry = $obj->registry;
Returns the L<Tangence::Registry> that constructed this object.
=cut
sub registry
{
my $self = shift;
return $self->{registry};
}
sub smash ( $self, $smashkeys )
{
return undef unless $smashkeys and @$smashkeys;
my @keys;
if( ref $smashkeys eq "HASH" ) {
@keys = keys %$smashkeys;
}
else {
@keys = @$smashkeys;
}
return { map {
my $m = "get_prop_$_";
$_ => $self->$m()
} @keys };
}
=head2 class
$class = $obj->class;
Returns the L<Tangence::Meta::Class> object representing the class of this
object.
=cut
sub class
{
my $self = shift;
return ref $self ? $self->{meta} : Tangence::Class->for_perlname( $self );
}
=head2 can_method
$method = $obj->can_method( $name );
Returns the L<Tangence::Meta::Method> object representing the named method, or
C<undef> if no such method exists.
=cut
sub can_method
{
my $self = shift;
return $self->class->method( @_ );
}
=head2 can_event
$event = $obj->can_event( $name );
Returns the L<Tangence::Meta::Event> object representing the named event, or
C<undef> if no such event exists.
=cut
sub can_event
{
my $self = shift;
return $self->class->event( @_ );
}
=head2 can_property
$property = $obj->can_property( $name );
Returns the L<Tangence::Meta::Property> object representing the named
property, or C<undef> if no such property exists.
=cut
sub can_property
{
my $self = shift;
return $self->class->property( @_ );
}
sub smashkeys
{
my $self = shift;
return $self->class->smashkeys;
}
=head2 fire_event
$obj->fire_event( $event, @args );
Fires the named event on the object. Each event subscription function will be
invoked with the given arguments.
=cut
sub fire_event ( $self, $event, @args )
{
$event eq "destroy" and croak "$self cannot fire destroy event directly";
$self->can_event( $event ) or croak "$self has no event $event";
my $sublist = $self->{event_subs}->{$event} or return;
foreach my $cb ( @$sublist ) {
$cb->( $self, @args );
}
}
=head2 subscribe_event
$id = $obj->subscribe_event( $event, $callback );
Subscribes an event-handling callback CODE ref to the named event. When the
event is fired by C<fire_event> this callback will be invoked, being passed
the object reference and the event's arguments.
$callback->( $obj, @args );
Returns an opaque ID value that can be used to remove this subscription by
calling C<unsubscribe_event>.
=cut
sub subscribe_event ( $self, $event, $callback )
{
$self->can_event( $event ) or croak "$self has no event $event";
my $sublist = ( $self->{event_subs}->{$event} ||= [] );
push @$sublist, $callback;
my $ref = \@{$sublist}[$#$sublist]; # reference to last element
return $ref + 0; # force numeric context
}
=head2 unsubscribe_event
$obj->unsubscribe_event( $event, $id );
Removes an event-handling callback previously registered with
C<subscribe_event>.
=cut
sub unsubscribe_event ( $self, $event, $id )
{
my $sublist = $self->{event_subs}->{$event} or return;
my $index;
for( $index = 0; $index < @$sublist; $index++ ) {
last if \@{$sublist}[$index] + 0 == $id;
}
splice @$sublist, $index, 1, ();
}
=head2 watch_property
$id = $obj->watch_property( $prop, %callbacks );
Watches a named property for changes, registering a set of callback functions
to be invoked when the property changes in certain ways. The set of callbacks
required depends on the dimension of the property being watched.
For all property types:
$on_set->( $obj, $value );
For hash properties:
$on_add->( $obj, $key, $value );
$on_del->( $obj, $key );
For queue properties:
$on_push->( $obj, @values );
$on_shift->( $obj, $count );
For array properties:
$on_push->( $obj, @values );
$on_shift->( $obj, $count );
$on_splice->( $obj, $index, $count, @values );
$on_move->( $obj, $index, $delta );
For objset properties:
$on_add->( $obj, $added_object );
$on_del->( $obj, $deleted_object_id );
Alternatively, a single callback may be installed that is invoked after any
change of the property, being passed the new value entirely:
$on_updated->( $obj, $value );
Returns an opaque ID value that can be used to remove this watch by calling
C<unwatch_property>.
=cut
sub watch_property ( $self, $prop, %callbacks )
{
my $pdef = $self->can_property( $prop ) or croak "$self has no property $prop";
my $callbacks = {};
my $on_updated;
if( $callbacks{on_updated} ) {
$on_updated = delete $callbacks{on_updated};
ref $on_updated eq "CODE" or croak "Expected 'on_updated' to be a CODE ref";
keys %callbacks and croak "Expected no key other than 'on_updated'";
$callbacks->{on_updated} = $on_updated;
}
else {
foreach my $name ( @{ CHANGETYPES->{$pdef->dimension} } ) {
ref( $callbacks->{$name} = delete $callbacks{$name} ) eq "CODE"
or croak "Expected '$name' as a CODE ref";
}
}
my $watchlist = $self->{properties}->{$prop}->callbacks;
push @$watchlist, $callbacks;
$on_updated->( $self, $self->{properties}->{$prop}->value ) if $on_updated;
my $ref = \@{$watchlist}[$#$watchlist]; # reference to last element
return $ref + 0; # force numeric context
}
=head2 unwatch_property
$obj->unwatch_property( $prop, $id );
Removes the set of callback functions previously registered with
C<watch_property>.
=cut
sub unwatch_property ( $self, $prop, $id )
{
my $watchlist = $self->{properties}->{$prop}->callbacks or return;
my $index;
for( $index = 0; $index < @$watchlist; $index++ ) {
last if \@{$watchlist}[$index] + 0 == $id;
}
splice @$watchlist, $index, 1, ();
}
### Message handling
sub handle_request_CALL ( $self, $ctx, $message )
{
my $method = $message->unpack_str();
my $mdef = $self->can_method( $method ) or die "Object cannot respond to method $method\n";
my $m = "method_$method";
$self->can( $m ) or die "Object cannot run method $method\n";
my @args = map { $_->unpack_value( $message ) } $mdef->argtypes;
my $result = $self->$m( $ctx, @args );
my $response = Tangence::Message->new( $ctx->stream, MSG_RESULT );
$mdef->ret->pack_value( $response, $result ) if $mdef->ret;
return $response;
}
sub generate_message_EVENT ( $self, $conn, $event, @args )
{
my $edef = $self->can_event( $event ) or die "Object cannot respond to event $event";
my $response = Tangence::Message->new( $conn, MSG_EVENT )
->pack_int( $self->id )
->pack_str( $event );
my @argtypes = $edef->argtypes;
$argtypes[$_]->pack_value( $response, $args[$_] ) for 0..$#argtypes;
return $response;
}
sub handle_request_GETPROP ( $self, $ctx, $message )
{
my $prop = $message->unpack_str();
my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop";
my $m = "get_prop_$prop";
$self->can( $m ) or die "Object cannot get property $prop\n";
my $result = $self->$m();
my $response = Tangence::Message->new( $ctx->stream, MSG_RESULT );
$pdef->overall_type->pack_value( $response, $result );
return $response;
}
sub handle_request_GETPROPELEM ( $self, $ctx, $message )
{
my $prop = $message->unpack_str();
my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop";
my $dim = $pdef->dimension;
my $m = "get_prop_$prop";
$self->can( $m ) or die "Object cannot get property $prop\n";
my $result;
match( $dim : == ) {
case( DIM_QUEUE ), case( DIM_ARRAY ) {
my $idx = $message->unpack_int();
$result = $self->$m()->[$idx];
}
case( DIM_HASH ) {
my $key = $message->unpack_str();
$result = $self->$m()->{$key};
}
default {
die "Property $prop cannot fetch elements";
}
}
my $response = Tangence::Message->new( $ctx->stream, MSG_RESULT );
$pdef->type->pack_value( $response, $result );
return $response;
}
sub handle_request_SETPROP ( $self, $ctx, $message )
{
my $prop = $message->unpack_str();
my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop\n";
my $value = $pdef->overall_type->unpack_value( $message );
my $m = "set_prop_$prop";
$self->can( $m ) or die "Object cannot set property $prop\n";
$self->$m( $value );
return Tangence::Message->new( $self, MSG_OK );
}
sub generate_message_UPDATE ( $self, $conn, $prop, $how, @args )
{
my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop\n";
my $dim = $pdef->dimension;
my $message = Tangence::Message->new( $conn, MSG_UPDATE )
->pack_int( $self->id )
->pack_str( $prop );
TYPE_U8->pack_value( $message, $how );
my $dimname = DIMNAMES->[$dim];
if( $how == CHANGE_SET ) {
my ( $value ) = @args;
$pdef->overall_type->pack_value( $message, $value );
}
elsif( my $code = $self->can( "_generate_message_UPDATE_$dimname" ) ) {
$code->( $self, $message, $how, $pdef, @args );
}
else {
croak "Unrecognised property dimension $dim for $prop";
}
return $message;
}
sub _generate_message_UPDATE_scalar ( $self, $message, $how, $pdef, @args )
{
croak "Change type $how is not valid for a scalar property";
}
sub _generate_message_UPDATE_hash ( $self, $message, $how, $pdef, @args )
{
match( $how : == ) {
case( CHANGE_ADD ) {
my ( $key, $value ) = @args;
$message->pack_str( $key );
$pdef->type->pack_value( $message, $value );
}
case( CHANGE_DEL ) {
my ( $key ) = @args;
$message->pack_str( $key );
}
default {
croak "Change type $how is not valid for a hash property";
}
}
}
sub _generate_message_UPDATE_queue ( $self, $message, $how, $pdef, @args )
{
match( $how : == ) {
case( CHANGE_PUSH ) {
$message->pack_all_sametype( $pdef->type, @args );
}
case( CHANGE_SHIFT ) {
my ( $count ) = @args;
$message->pack_int( $count );
}
default {
croak "Change type $how is not valid for a queue property";
}
}
}
sub _generate_message_UPDATE_array ( $self, $message, $how, $pdef, @args )
{
match( $how : == ) {
case( CHANGE_PUSH ) {
$message->pack_all_sametype( $pdef->type, @args );
}
case( CHANGE_SHIFT ) {
my ( $count ) = @args;
$message->pack_int( $count );
}
case( CHANGE_SPLICE ) {
my ( $start, $count, @values ) = @args;
$message->pack_int( $start );
$message->pack_int( $count );
$message->pack_all_sametype( $pdef->type, @values );
}
case( CHANGE_MOVE ) {
my ( $index, $delta ) = @args;
$message->pack_int( $index );
$message->pack_int( $delta );
}
default {
croak "Change type $how is not valid for an array property";
}
}
}
sub _generate_message_UPDATE_objset ( $self, $message, $how, $pdef, @args )
{
match( $how : == ) {
case( CHANGE_ADD ) {
my ( $value ) = @args;
$pdef->type->pack_value( $message, $value );
}
case( CHANGE_DEL ) {
my ( $id ) = @args;
$message->pack_int( $id );
}
default {
croak "Change type $how is not valid for an objset property";
}
}
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|