allow scalar refs to be transferred
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connection.pm
index 4490e44..4d593b3 100644 (file)
@@ -81,6 +81,11 @@ sub _build__json {
       my $code_container = $self->_id_to_remote_object(@_);
       sub { $code_container->call(@_) };
     }
+  )->filter_json_single_key_object(
+    __scalar_ref__ => sub {
+      my $value = shift;
+      return \$value;
+    }
   );
 }
 
@@ -128,13 +133,20 @@ sub send_class_call {
 
 sub register_class_call_handler {
   my ($self) = @_;
-  $self->local_objects_by_id->{'class_call_handler'}
-    = Object::Remote::CodeContainer->new(
-        code => sub {
-          my ($class, $method) = splice @_, 0, 2;
-          use_module($class)->$method(@_);
-        }
-      );
+  $self->local_objects_by_id->{'class_call_handler'} ||= do {
+    my $o = $self->new_class_call_handler;
+    $self->_local_object_to_id($o);
+    $o;
+  };
+}
+
+sub new_class_call_handler {
+  Object::Remote::CodeContainer->new(
+    code => sub {
+      my ($class, $method) = (shift, shift);
+      use_module($class)->$method(@_);
+    }
+  );
 }
 
 sub register_remote {
@@ -158,12 +170,16 @@ sub send {
   my ($self, $type, @call) = @_;
 
   my $future = CPS::Future->new;
+  my $remote = $self->remote_objects_by_id->{$call[0]};
 
   unshift @call, $type => $self->_local_object_to_id($future);
 
   my $outstanding = $self->outstanding_futures;
   $outstanding->{$future} = $future;
-  $future->on_ready(sub { delete $outstanding->{$future} });
+  $future->on_ready(sub {
+    undef($remote);
+    delete $outstanding->{$future}
+  });
 
   $self->_send(\@call);
 
@@ -188,7 +204,7 @@ sub _send {
 
 sub _serialize {
   my ($self, $data) = @_;
-  local our @New_Ids;
+  local our @New_Ids = (-1);
   return eval {
     my $flat = $self->_encode($self->_deobjectify($data));
     warn "$$ >>> ${flat}\n" if $DEBUG;
@@ -205,7 +221,7 @@ sub _local_object_to_id {
   my ($self, $object) = @_;
   my $id = refaddr($object);
   $self->local_objects_by_id->{$id} ||= do {
-    push our(@New_Ids), $id;
+    push our(@New_Ids), $id if @New_Ids;
     $object;
   };
   return $id;
@@ -225,6 +241,8 @@ sub _deobjectify {
                  Object::Remote::CodeContainer->new(code => $data)
                );
       return +{ __remote_code__ => $id };
+    } elsif ($ref eq 'SCALAR') {
+      return +{ __scalar_ref__ => $$data };
     } else {
       die "Can't collapse reftype $ref";
     }
@@ -294,15 +312,6 @@ sub receive_call_free {
   $self->receive_free($id);
 }
 
-sub receive_class_call {
-  my ($self, $future_id, $class, @rest) = @_;
-  my $future = $self->_id_to_remote_object($future_id);
-  $future->{method} = 'call_discard_free';
-  eval { use_module($class) }
-    or do { $future->fail("Error loading ${class}: $@"); return };
-  $self->_invoke($future, $class, @rest);
-}
-
 sub _invoke {
   my ($self, $future, $local, $ctx, $method, @args) = @_;
   if ($method =~ /^start::/) {
@@ -335,3 +344,13 @@ sub DEMOLISH {
 }
 
 1;
+
+=head1 NAME
+
+Object::Remote::Connection - An underlying connection for L<Object::Remote>
+
+=head1 LAME
+
+Shipping prioritised over writing this part up. Blame mst.
+
+=cut