[Opensrf-commits] r1297 - in trunk/src/python: . osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Apr 7 16:36:09 EDT 2008


Author: erickson
Date: 2008-04-07 15:59:07 -0400 (Mon, 07 Apr 2008)
New Revision: 1297

Modified:
   trunk/src/python/osrf/ex.py
   trunk/src/python/osrf/http_translator.py
   trunk/src/python/osrf/net.py
   trunk/src/python/srfsh.py
Log:
raise an exception when the xmpp recipient is not found.  http_translator turns this into a 404; srfsh prints a user error

Modified: trunk/src/python/osrf/ex.py
===================================================================
--- trunk/src/python/osrf/ex.py	2008-03-31 20:47:41 UTC (rev 1296)
+++ trunk/src/python/osrf/ex.py	2008-04-07 19:59:07 UTC (rev 1297)
@@ -48,5 +48,3 @@
     """Raised when a JSON parsing error occurs."""
     pass
 
-
-

Modified: trunk/src/python/osrf/http_translator.py
===================================================================
--- trunk/src/python/osrf/http_translator.py	2008-03-31 20:47:41 UTC (rev 1296)
+++ trunk/src/python/osrf/http_translator.py	2008-04-07 19:59:07 UTC (rev 1297)
@@ -98,8 +98,11 @@
 
         try:
             post = util.parse_qsl(apreq.read(int(apreq.headers_in['Content-length'])))
+            osrf.log.log_debug('post = ' + str(post))
             self.body = [d for d in post if d[0] == 'osrf-msg'][0][1]
-        except: 
+            osrf.log.log_debug(self.body)
+        except Exception, e: 
+            osrf.log.log_warn("error parsing osrf message: %s" % unicode(e))
             self.body = None
             return
 
@@ -153,7 +156,12 @@
         first_write = True
         while not self.complete:
 
-            net_msg = self.handle.recv(self.timeout)
+            net_msg = None
+            try:
+                net_msg = self.handle.recv(self.timeout)
+            except osrf.net.XMPPNoRecipient:
+                return apache.HTTP_NOT_FOUND 
+
             if not net_msg: 
                 return apache.GATEWAY_TIME_OUT
 

Modified: trunk/src/python/osrf/net.py
===================================================================
--- trunk/src/python/osrf/net.py	2008-03-31 20:47:41 UTC (rev 1296)
+++ trunk/src/python/osrf/net.py	2008-04-07 19:59:07 UTC (rev 1297)
@@ -20,7 +20,7 @@
 from pyxmpp.jid import JID
 from socket import gethostname
 import libxml2
-import osrf.log
+import osrf.log, osrf.ex
 
 THREAD_SESSIONS = {}
 
@@ -31,6 +31,16 @@
 #logger.addHandler(logging.FileHandler('j.log'))
 #logger.setLevel(logging.DEBUG)
 
+
+
+class XMPPNoRecipient(osrf.ex.OSRFException):
+    ''' Raised when a message was sent to a non-existent recipient 
+        The recipient is stored in the 'recipient' field on this object
+    '''
+    def __init__(self, recipient):
+        osrf.ex.OSRFException.__init__(self, 'Error communicating with %s' % recipient)
+        self.recipient = recipient
+
 def set_network_handle(handle):
     """ Sets the thread-specific network handle"""
     THREAD_SESSIONS[threading.currentThread().getName()] = handle
@@ -120,6 +130,7 @@
         self.queue = []
 
         self.receive_callback = None
+        self.transport_error_msg = None
 
     def connect(self):
         JabberClient.connect(self)
@@ -139,6 +150,7 @@
         osrf.log.log_info("Successfully connected to the opensrf network")
         self.authenticated()
         self.stream.set_message_handler("normal", self.message_received)
+        self.stream.set_message_handler("error", self.error_received)
         self.isconnected = True
 
     def send(self, message):
@@ -147,6 +159,10 @@
         message.sender = self.jid.as_utf8()
         msg = message.make_xmpp_msg()
         self.stream.send(msg)
+
+    def error_received(self, stanza):
+        self.transport_error_msg = NetworkMessage(stanza)
+        osrf.log.log_error("XMPP error message received from %s" % self.transport_error_msg.sender)
     
     def message_received(self, stanza):
         """Handler for received messages."""
@@ -181,6 +197,12 @@
                     timeout -= endtime
                 osrf.log.log_internal("exiting stream loop after %s seconds. "
                     "act=%s, queue size=%d" % (str(endtime), act, len(self.queue)))
+
+                if self.transport_error_msg:
+                    msg = self.transport_error_msg
+                    self.transport_error_msg = None
+                    raise XMPPNoRecipient(msg.sender)
+
                 if not act:
                     self.idle()
 

Modified: trunk/src/python/srfsh.py
===================================================================
--- trunk/src/python/srfsh.py	2008-03-31 20:47:41 UTC (rev 1296)
+++ trunk/src/python/srfsh.py	2008-04-07 19:59:07 UTC (rev 1297)
@@ -23,11 +23,7 @@
 """
 
 import os, sys, time, readline, atexit, re
-import osrf.json
-import osrf.system
-import osrf.ses
-import osrf.conf
-import osrf.log
+import osrf.json, osrf.system, osrf.ses, osrf.conf, osrf.log, osrf.net
 
 # -------------------------------------------------------------------
 # main listen loop
@@ -126,7 +122,14 @@
 
 
     while True:
-        resp = req.recv(timeout=120)
+        resp = None
+        try:
+            resp = req.recv(timeout=120)
+        except osrf.net.XMPPNoRecipient:
+            print "Unable to communicate with %s" % service
+            total = 0
+            break
+
         osrf.log.log_internal("Looping through receive request")
         if not resp:
             break



More information about the opensrf-commits mailing list