[Opensrf-commits] r1223 - trunk/src/python/osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sat Jan 19 19:26:16 EST 2008


Author: erickson
Date: 2008-01-19 19:00:15 -0500 (Sat, 19 Jan 2008)
New Revision: 1223

Modified:
   trunk/src/python/osrf/net.py
   trunk/src/python/osrf/ses.py
   trunk/src/python/osrf/stack.py
Log:
added the ability to wait forever by passing <0 to recv.  explicitly setting sender address in messages to be correct.  added ability to create a message from raw xml

Modified: trunk/src/python/osrf/net.py
===================================================================
--- trunk/src/python/osrf/net.py	2008-01-17 16:39:23 UTC (rev 1222)
+++ trunk/src/python/osrf/net.py	2008-01-20 00:00:15 UTC (rev 1223)
@@ -14,12 +14,13 @@
 # -----------------------------------------------------------------------
 
 
+import os, time, threading
 from pyxmpp.jabber.client import JabberClient
 from pyxmpp.message import Message
 from pyxmpp.jid import JID
 from socket import gethostname
+import libxml2
 import osrf.log
-import os, time, threading
 
 THREAD_SESSIONS = {}
 
@@ -62,6 +63,7 @@
             self.body = message.get_body()
             self.thread = message.get_thread()
             self.recipient = message.get_to()
+            self.router_command = None
             if message.xmlnode.hasProp('router_from') and \
                 message.xmlnode.prop('router_from') != '':
                 self.sender = message.xmlnode.prop('router_from')
@@ -74,10 +76,17 @@
             self.thread = args.get('thread')
             self.router_command = args.get('router_command')
 
+    @staticmethod
+    def from_xml(xml):
+        doc=libxml2.parseDoc(xml)
+        msg = Message(doc.getRootElement())
+        return NetworkMessage(msg)
+        
+
     def make_xmpp_msg(self):
         ''' Creates a pyxmpp.message.Message and adds custom attributes '''
 
-        msg = Message(None, None, self.recipient, None, None, None, \
+        msg = Message(None, self.sender, self.recipient, None, None, None, \
             self.body, self.thread)
         if self.router_command:
             msg.xmlnode.newProp('router_command', self.router_command)
@@ -131,6 +140,7 @@
     def send(self, message):
         """Sends the provided network message."""
         osrf.log.log_internal("jabber sending to %s: %s" % (message.recipient, message.body))
+        message.sender = self.jid.as_utf8()
         msg = message.make_xmpp_msg()
         self.stream.send(msg)
     
@@ -153,12 +163,18 @@
         returned.
         """
 
+        forever = False
+        if timeout < 0:
+            forever = True
+            timeout = None
+
         if len(self.queue) == 0:
-            while timeout >= 0 and len(self.queue) == 0:
+            while (forever or timeout >= 0) and len(self.queue) == 0:
                 starttime = time.time()
                 act = self.get_stream().loop_iter(timeout)
                 endtime = time.time() - starttime
-                timeout -= endtime
+                if not forever:
+                    timeout -= endtime
                 osrf.log.log_internal("exiting stream loop after %s seconds. "
                     "act=%s, queue size=%d" % (str(endtime), act, len(self.queue)))
                 if not act:

Modified: trunk/src/python/osrf/ses.py
===================================================================
--- trunk/src/python/osrf/ses.py	2008-01-17 16:39:23 UTC (rev 1222)
+++ trunk/src/python/osrf/ses.py	2008-01-20 00:00:15 UTC (rev 1223)
@@ -257,7 +257,7 @@
         self.session.send(message)
 
     def recv(self, timeout=120):
-        """Waits up to <timeout> seconds for a response to this request.
+        """ Waits up to <timeout> seconds for a response to this request.
         
             If a message is received in time, the response message is returned.
             Returns None otherwise."""
@@ -265,10 +265,11 @@
         self.session.wait(0)
 
         orig_timeout = timeout
-        while not self.complete and timeout >= 0 and len(self.queue) == 0:
+        while not self.complete and (timeout >= 0 or orig_timeout < 0) and len(self.queue) == 0:
             s = time.time()
             self.session.wait(timeout)
-            timeout -= time.time() - s
+            if orig_timeout >= 0:
+                timeout -= time.time() - s
             if self.reset_timeout:
                 self.reset_timeout = False
                 timeout = orig_timeout

Modified: trunk/src/python/osrf/stack.py
===================================================================
--- trunk/src/python/osrf/stack.py	2008-01-17 16:39:23 UTC (rev 1222)
+++ trunk/src/python/osrf/stack.py	2008-01-20 00:00:15 UTC (rev 1223)
@@ -30,6 +30,7 @@
     if not ses:
         # This is an incoming request from a client, create a new server session
         osrf.log.log_error("server-side sessions don't exist yet")
+        return
 
     ses.set_remote_id(net_msg.sender)
 



More information about the opensrf-commits mailing list