LP#1703411 XMPP opensrf sub-element repairs
[opensrf-equinox.git] / src / perl / lib / OpenSRF / Transport / SlimJabber / XMPPReader.pm
index b8a5da1..0a84ae1 100644 (file)
@@ -166,6 +166,11 @@ sub tcp_connected {
 # -----------------------------------------------------------
 sub send {
     my($self, $xml) = @_;
+        
+    local $SIG{'PIPE'} = sub {
+        $logger->error("Disconnected from Jabber server, exiting immediately");
+        exit(99);
+    };
     $self->{socket}->print($xml);
 }
 
@@ -205,7 +210,31 @@ sub wait {
     my $infile = '';
     vec($infile, $socket->fileno, 1) = 1;
 
-    my $nfound = select($infile, undef, undef, $timeout);
+    my $nfound;
+    if (!OpenSRF->OSRF_APACHE_REQUEST_OBJ || $timeout <= 1.0) {
+        $nfound = select($infile, undef, undef, $timeout);
+    } else {
+        $timeout -= 1.0;
+        for (
+            my $sleep = 1.0;
+            $timeout >= 0.0;
+            do {
+                $sleep = $timeout < 1.0 ? $timeout : 1.0;
+                $timeout -= 1.0;
+            }
+        ) {
+            $nfound = select($infile, undef, undef, $sleep);
+            last if $nfound;
+            if (
+                OpenSRF->OSRF_APACHE_REQUEST_OBJ &&
+                OpenSRF->OSRF_APACHE_REQUEST_OBJ->connection->aborted
+            ) {
+                # Should this be more severe? Die or throw error?
+                $logger->warn("Upstream Apache client disconnected, aborting.");
+                last;
+            };
+        }
+    }
     return undef if !$nfound or $nfound == -1;
 
     # now slurp the data off the socket
@@ -226,9 +255,10 @@ sub wait {
         $first_read = 0;
     }
 
-    if ($nbytes == 0 and $first_read) {
+    if ($first_read and defined $nbytes and $nbytes == 0) {
         # if the first read on an active socket is 0 bytes, 
         # the socket has been disconnected from the remote end. 
+        $self->{stream_state} = DISCONNECTED;
         $logger->error("Disconnected from Jabber server");
         throw OpenSRF::EX::Jabber("Disconnected from Jabber server");
     }
@@ -276,10 +306,15 @@ sub start_element {
 
         my $msg = $self->{message};
         $msg->{to} = $attrs{'to'};
+        $msg->{from} = $attrs{from};
+        $msg->{type} = $attrs{type};
+
+    } elsif($name eq 'opensrf') {
+
+        # These will be authoritative if they exist
+        my $msg = $self->{message};
         $msg->{from} = $attrs{router_from} if $attrs{router_from};
-        $msg->{from} = $attrs{from} unless $msg->{from};
         $msg->{osrf_xid} = $attrs{'osrf_xid'};
-        $msg->{type} = $attrs{type};
 
     } elsif($name eq 'body') {
         $self->{xml_state} = IN_BODY;