From a21c70bb736ce86b6feb3cef958a5387eaedc8cd Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 7 Apr 2008 19:59:07 +0000 Subject: [PATCH] raise an exception when the xmpp recipient is not found. http_translator turns this into a 404; srfsh prints a user error git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1297 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/osrf/ex.py | 2 -- src/python/osrf/http_translator.py | 12 ++++++++++-- src/python/osrf/net.py | 24 +++++++++++++++++++++++- src/python/srfsh.py | 15 +++++++++------ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/python/osrf/ex.py b/src/python/osrf/ex.py index f62aca8..d7395d5 100644 --- a/src/python/osrf/ex.py +++ b/src/python/osrf/ex.py @@ -48,5 +48,3 @@ class OSRFJSONParseException(OSRFException): """Raised when a JSON parsing error occurs.""" pass - - diff --git a/src/python/osrf/http_translator.py b/src/python/osrf/http_translator.py index d265cfb..6439db1 100644 --- a/src/python/osrf/http_translator.py +++ b/src/python/osrf/http_translator.py @@ -98,8 +98,11 @@ class HTTPTranslator(object): 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 @@ class HTTPTranslator(object): 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 diff --git a/src/python/osrf/net.py b/src/python/osrf/net.py index a48bdaa..a13d1ce 100644 --- a/src/python/osrf/net.py +++ b/src/python/osrf/net.py @@ -20,7 +20,7 @@ from pyxmpp.message import Message 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 @@ THREAD_SESSIONS = {} #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 @@ class Network(JabberClient): self.queue = [] self.receive_callback = None + self.transport_error_msg = None def connect(self): JabberClient.connect(self) @@ -139,6 +150,7 @@ class Network(JabberClient): 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 @@ class Network(JabberClient): 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 @@ class Network(JabberClient): 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() diff --git a/src/python/srfsh.py b/src/python/srfsh.py index 82daee2..74af0aa 100755 --- a/src/python/srfsh.py +++ b/src/python/srfsh.py @@ -23,11 +23,7 @@ srfsh.py - provides a basic shell for issuing OpenSRF requests """ 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 @@ def handle_request(parts): 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 -- 2.43.2