fix warning if handle is DEMOLISHED after the backing connection is gone
[scpubgit/Object-Remote.git] / lib / Object / Remote / Handle.pm
CommitLineData
676438a1 1package Object::Remote::Handle;
2
3use Object::Remote::Proxy;
4use Scalar::Util qw(weaken blessed);
998ff9a4 5use Object::Remote::Logging qw ( :log :dlog router );
676438a1 6use Object::Remote::Future;
4a9fa1a5 7#must find way to exclude certain log events
8#from being forwarded - log events generated in
9#response to log events cause exploding
10#use Object::Remote::Logging qw(:log);
676438a1 11use Module::Runtime qw(use_module);
12use Moo;
13
f4a85080 14BEGIN { router()->exclude_forwarding }
4e446335 15
676438a1 16has connection => (
5add5e29 17 is => 'ro', required => 1, handles => ['is_valid'],
676438a1 18 coerce => sub {
19 blessed($_[0])
20 ? $_[0]
21 : use_module('Object::Remote::Connection')->new_from_spec($_[0])
22 },
23);
24
25has id => (is => 'rwp');
26
27has disarmed_free => (is => 'rwp');
28
29sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] }
30
31sub proxy {
32 bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy');
33}
34
35sub BUILD {
36 my ($self, $args) = @_;
7790ca36 37 log_trace { "constructing remote handle" };
1d26d6f9 38 if ($self->id) {
90115979 39 log_trace { "disarming free for this handle" };
1d26d6f9 40 $self->disarm_free;
41 } else {
676438a1 42 die "No id supplied and no class either" unless $args->{class};
43 ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args};
9031635d 44 log_trace { "fetching id for handle and disarming free on remote side" };
676438a1 45 $self->_set_id(
46 await_future(
f7611866 47 $self->connection->send_class_call(
48 0, $args->{class},
676438a1 49 $args->{constructor}||'new', @{$args->{args}||[]}
50 )
51 )->{remote}->disarm_free->id
52 );
53 }
998ff9a4 54 Dlog_trace { "finished constructing remote handle; id is $_" } $self->id;
676438a1 55 $self->connection->register_remote($self);
56}
57
58sub call {
59 my ($self, $method, @args) = @_;
60 my $w = wantarray;
5add5e29 61 my $id = $self->id;
d2eadebb 62
676438a1 63 $method = "start::${method}" if (caller(0)||'') eq 'start';
5add5e29 64 log_trace { "call('$method') has been invoked on remote handle '$id'; creating future" };
65
676438a1 66 future {
5add5e29 67 log_debug { "Invoking send on connection for handle '$id' method $method" };
68 $self->connection->send(call => $id, $w, $method, @args)
676438a1 69 };
70}
71
72sub call_discard {
73 my ($self, $method, @args) = @_;
9031635d 74 log_trace { "invoking send_discard() with 'call' for method '$method' on connection for remote handle" };
676438a1 75 $self->connection->send_discard(call => $self->id, $method, @args);
76}
77
78sub call_discard_free {
79 my ($self, $method, @args) = @_;
80 $self->disarm_free;
9031635d 81 log_trace { "invoking send_discard() with 'call_free' for method '$method' on connection for remote handle" };
676438a1 82 $self->connection->send_discard(call_free => $self->id, $method, @args);
83}
84
85sub DEMOLISH {
86 my ($self, $gd) = @_;
998ff9a4 87 Dlog_trace { "Demolishing remote handle $_" } $self->id;
676438a1 88 return if $gd or $self->disarmed_free;
82ef4e4b 89 #this could happen after the connection has gone away
90 eval { $self->connection->send_free($self->id) };
91 if ($@ && $@ !~ m/^Attempt to invoke _send on a connection that is not valid/) {
92 die "Could not invoke send_free on connection for handle " . $self->id;
93 }
676438a1 94}
95
961;