1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
16 from osrf.json import *
17 from osrf.log import *
19 from osrf.ses import osrfSession, osrfClientSession, osrfServerSession
20 from osrf.const import *
24 def osrfPushStack(netMessage):
25 ses = osrfSession.findSession(netMessage.thread)
28 # This is an incoming request from a client, create a new server session
31 ses.setRemoteId(netMessage.sender)
33 oMessages = osrfJSONToObject(netMessage.body)
35 osrfLogInternal("osrfPushStack(): received %d messages" % len(oMessages))
37 # Pass each bundled opensrf message to the message handler
40 osrfHandleMessage(ses, m)
43 if isinstance(ses, osrfServerSession):
44 osrfLogInfo("Message processing duration %f" % t)
46 def osrfHandleMessage(session, message):
48 osrfLogInternal("osrfHandleMessage(): processing message of type %s" % message.type())
50 if isinstance(session, osrfClientSession):
52 if message.type() == OSRF_MESSAGE_TYPE_RESULT:
53 session.pushResponseQueue(message)
56 if message.type() == OSRF_MESSAGE_TYPE_STATUS:
58 statusCode = int(message.payload().statusCode())
59 statusText = message.payload().status()
60 osrfLogInternal("osrfHandleMessage(): processing STATUS, statusCode = %d" % statusCode)
62 if statusCode == OSRF_STATUS_COMPLETE:
63 # The server has informed us that this request is complete
64 req = session.findRequest(message.threadTrace())
66 osrfLogInternal("marking request as complete: %d" % req.id)
70 if statusCode == OSRF_STATUS_OK:
71 # We have connected successfully
72 osrfLogDebug("Successfully connected to " + session.service)
73 session.state = OSRF_APP_SESSION_CONNECTED
76 if statusCode == OSRF_STATUS_CONTINUE:
77 # server is telling us to reset our wait timeout and keep waiting for a response
78 session.resetRequestTimeout(message.threadTrace())
81 if statusCode == OSRF_STATUS_TIMEOUT:
82 osrfLogDebug("The server did not receive a request from us in time...")
83 session.state = OSRF_APP_SESSION_DISCONNECTED
86 if statusCode == OSRF_STATUS_NOTFOUND:
87 osrfLogErr("Requested method was not found on the server: %s" % statusText)
88 session.state = OSRF_APP_SESSION_DISCONNECTED
89 raise osrfServiceException(statusText)
91 raise osrfProtocolException("Unknown message status: %d" % statusCode)