]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/stack.py
added error message. re-tabbed to 4 spaces
[OpenSRF.git] / src / python / osrf / stack.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
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.
9
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 # -----------------------------------------------------------------------
15
16 from osrf.json import *
17 from osrf.log import *
18 from osrf.ex import *
19 from osrf.ses import osrfSession, osrfClientSession, osrfServerSession
20 from osrf.const import *
21 from time import time
22
23
24 def osrfPushStack(netMessage):
25     ses = osrfSession.findSession(netMessage.thread)
26
27     if not ses:
28         # This is an incoming request from a client, create a new server session
29         osrfLogErr("server-side sessions don't exist yet")
30         pass
31
32     ses.setRemoteId(netMessage.sender)
33
34     oMessages = osrfJSONToObject(netMessage.body)
35
36     osrfLogInternal("osrfPushStack(): received %d messages" % len(oMessages))
37
38     # Pass each bundled opensrf message to the message handler
39     t = time()
40     for m in oMessages:
41         osrfHandleMessage(ses, m)
42     t = time() - t
43
44     if isinstance(ses, osrfServerSession):
45         osrfLogInfo("Message processing duration %f" % t)
46
47 def osrfHandleMessage(session, message):
48
49     osrfLogInternal("osrfHandleMessage(): processing message of type %s" % message.type())
50
51     if isinstance(session, osrfClientSession):
52
53         if message.type() == OSRF_MESSAGE_TYPE_RESULT:
54             session.pushResponseQueue(message)
55             return
56
57         if message.type() == OSRF_MESSAGE_TYPE_STATUS:
58
59             statusCode = int(message.payload().statusCode())
60             statusText = message.payload().status()
61             osrfLogInternal("osrfHandleMessage(): processing STATUS,  statusCode =  %d" % statusCode)
62
63         if statusCode == OSRF_STATUS_COMPLETE:
64             # The server has informed us that this request is complete
65             req = session.findRequest(message.threadTrace())
66             if req: 
67                 osrfLogInternal("marking request as complete: %d" % req.id)
68                 req.setComplete()
69             return
70
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
75             return
76
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())
80             return;
81
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
85             return
86
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)
91
92         raise osrfProtocolException("Unknown message status: %d" % statusCode)
93
94
95
96