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
29 osrfLogErr("server-side sessions don't exist yet")
32 ses.setRemoteId(netMessage.sender)
34 oMessages = osrfJSONToObject(netMessage.body)
36 osrfLogInternal("osrfPushStack(): received %d messages" % len(oMessages))
38 # Pass each bundled opensrf message to the message handler
41 osrfHandleMessage(ses, m)
44 if isinstance(ses, osrfServerSession):
45 osrfLogInfo("Message processing duration %f" % t)
47 def osrfHandleMessage(session, message):
49 osrfLogInternal("osrfHandleMessage(): processing message of type %s" % message.type())
51 if isinstance(session, osrfClientSession):
53 if message.type() == OSRF_MESSAGE_TYPE_RESULT:
54 session.pushResponseQueue(message)
57 if message.type() == OSRF_MESSAGE_TYPE_STATUS:
59 statusCode = int(message.payload().statusCode())
60 statusText = message.payload().status()
61 osrfLogInternal("osrfHandleMessage(): processing STATUS, statusCode = %d" % statusCode)
63 if statusCode == OSRF_STATUS_COMPLETE:
64 # The server has informed us that this request is complete
65 req = session.findRequest(message.threadTrace())
67 osrfLogInternal("marking request as complete: %d" % req.id)
71 if statusCode == OSRF_STATUS_OK:
72 # We have connected successfully
73 osrfLogDebug("Successfully connected to " + session.service)
74 session.state = OSRF_APP_SESSION_CONNECTED
77 if statusCode == OSRF_STATUS_CONTINUE:
78 # server is telling us to reset our wait timeout and keep waiting for a response
79 session.resetRequestTimeout(message.threadTrace())
82 if statusCode == OSRF_STATUS_TIMEOUT:
83 osrfLogDebug("The server did not receive a request from us in time...")
84 session.state = OSRF_APP_SESSION_DISCONNECTED
87 if statusCode == OSRF_STATUS_NOTFOUND:
88 osrfLogErr("Requested method was not found on the server: %s" % statusText)
89 session.state = OSRF_APP_SESSION_DISCONNECTED
90 raise osrfServiceException(statusText)
92 raise osrfProtocolException("Unknown message status: %d" % statusCode)