found location of hang; make annotations; added more log lines
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connection.pm
index 21bcec4..ba68d86 100644 (file)
@@ -6,6 +6,7 @@ use Object::Remote::Handle;
 use Object::Remote::CodeContainer;
 use Object::Remote::GlobProxy;
 use Object::Remote::GlobContainer;
+use Object::Remote::Logging qw (:log :dlog);
 use Object::Remote;
 use Symbol;
 use IO::Handle;
@@ -104,6 +105,10 @@ sub _build__json {
       tie *$handle, 'Object::Remote::GlobProxy', $glob_container;
       return $handle;
     }
+  )->filter_json_single_key_object(
+    __local_object__ => sub {
+      $self->local_objects_by_id->{$_[0]}
+    }
   );
 }
 
@@ -119,7 +124,9 @@ sub new_from_spec {
   my ($class, $spec) = @_;
   return $spec if blessed $spec;
   foreach my $poss (do { our @Guess }) {
-    if (my $obj = $poss->($spec)) { return $obj }
+    if (my $conn = $poss->($spec)) {
+      return $conn->maybe::start::connect;
+    }
   }
   die "Couldn't figure out what to do with ${spec}";
 }
@@ -133,6 +140,7 @@ sub remote_object {
 
 sub connect {
   my ($self, $to) = @_;
+  Dlog_debug { "Creating connection to remote node $_" } $to;
   return await_future(
     $self->send_class_call(0, 'Object::Remote', connect => $to)
   );
@@ -141,11 +149,13 @@ sub connect {
 sub remote_sub {
   my ($self, $sub) = @_;
   my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/;
+  log_debug { "Invoking remote sub '$sub'" };
   return await_future($self->send_class_call(0, $pkg, can => $name));
 }
 
 sub send_class_call {
   my ($self, $ctx, @call) = @_;
+  log_trace { "Sending a non-blocking class call" };
   $self->send(call => class_call_handler => $ctx => call => @call);
 }
 
@@ -169,12 +179,14 @@ sub new_class_call_handler {
 
 sub register_remote {
   my ($self, $remote) = @_;
+  log_trace { my $i = $remote->id; "Registered a remote object with id of '$i'" };
   weaken($self->remote_objects_by_id->{$remote->id} = $remote);
   return $remote;
 }
 
 sub send_free {
   my ($self, $id) = @_;
+  log_debug { "sending request to free object '$id'" };
   delete $self->remote_objects_by_id->{$id};
   $self->_send([ free => $id ]);
 }
@@ -209,8 +221,16 @@ sub send_discard {
 
 sub _send {
   my ($self, $to_send) = @_;
-
-  print { $self->send_to_fh } $self->_serialize($to_send)."\n";
+  my $fh = $self->send_to_fh;
+  my $serialized = $self->_serialize($to_send)."\n";
+  Dlog_debug { my $l = length($serialized); "Sending '$l' characters of serialized data to $_" } $fh;
+  #TODO this is very risky for deadlocks unless it's set to non-blocking and then with out extra
+  #logic it could easily do short-writes to the remote side
+  my $ret = print $fh $serialized;
+  Dlog_trace { my $r = defined $ret ? $ret : 'undef'; "print() returned $r with $_" } $fh;
+  #TODO hrm reason print's return value was ignored?
+  die "could not write to filehandle: $!" unless $ret;
+  return $ret; 
 }
 
 sub _serialize {
@@ -241,7 +261,14 @@ sub _local_object_to_id {
 sub _deobjectify {
   my ($self, $data) = @_;
   if (blessed($data)) {
-    return +{ __remote_object__ => $self->_local_object_to_id($data) };
+    if (
+      $data->isa('Object::Remote::Proxy')
+      and $data->{remote}->connection == $self
+    ) {
+      return +{ __local_object__ => $data->{remote}->id };
+    } else {
+      return +{ __remote_object__ => $self->_local_object_to_id($data) };
+    }
   } elsif (my $ref = ref($data)) {
     if ($ref eq 'HASH') {
       return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data };