got all general logging done, start of adding ids to objects and incorporating ids...
[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);
9031635d 5use Object::Remote::Logging qw ( :log );
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
14has connection => (
15 is => 'ro', required => 1,
16 coerce => sub {
17 blessed($_[0])
18 ? $_[0]
19 : use_module('Object::Remote::Connection')->new_from_spec($_[0])
20 },
21);
22
23has id => (is => 'rwp');
24
25has disarmed_free => (is => 'rwp');
26
27sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] }
28
29sub proxy {
30 bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy');
31}
32
33sub BUILD {
34 my ($self, $args) = @_;
9031635d 35 log_debug { "constructing remote handle" };
1d26d6f9 36 if ($self->id) {
9031635d 37 log_trace { "disaming free for this hanle" };
1d26d6f9 38 $self->disarm_free;
39 } else {
676438a1 40 die "No id supplied and no class either" unless $args->{class};
41 ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args};
9031635d 42 log_trace { "fetching id for handle and disarming free on remote side" };
676438a1 43 $self->_set_id(
44 await_future(
f7611866 45 $self->connection->send_class_call(
46 0, $args->{class},
676438a1 47 $args->{constructor}||'new', @{$args->{args}||[]}
48 )
49 )->{remote}->disarm_free->id
50 );
51 }
9031635d 52 log_trace { "finished constructing remote handle; registering it" . ref($self) };
676438a1 53 $self->connection->register_remote($self);
54}
55
56sub call {
57 my ($self, $method, @args) = @_;
58 my $w = wantarray;
9031635d 59 log_debug { my $def = defined $w; "call() has been invoked on a remote handle; wantarray: '$def'" };
676438a1 60 $method = "start::${method}" if (caller(0)||'') eq 'start';
61 future {
62 $self->connection->send(call => $self->id, $w, $method, @args)
63 };
64}
65
66sub call_discard {
67 my ($self, $method, @args) = @_;
9031635d 68 log_trace { "invoking send_discard() with 'call' for method '$method' on connection for remote handle" };
676438a1 69 $self->connection->send_discard(call => $self->id, $method, @args);
70}
71
72sub call_discard_free {
73 my ($self, $method, @args) = @_;
74 $self->disarm_free;
9031635d 75 log_trace { "invoking send_discard() with 'call_free' for method '$method' on connection for remote handle" };
676438a1 76 $self->connection->send_discard(call_free => $self->id, $method, @args);
77}
78
79sub DEMOLISH {
80 my ($self, $gd) = @_;
9031635d 81 log_trace { "Demolishing remote handle" };
676438a1 82 return if $gd or $self->disarmed_free;
83 $self->connection->send_free($self->id);
84}
85
861;