]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/stack.py
added support for multi-threaded client interactions. much like the java lib, each...
[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       pass
30
31    ses.setRemoteId(netMessage.sender)
32
33    oMessages = osrfJSONToObject(netMessage.body)
34
35    osrfLogInternal("osrfPushStack(): received %d messages" % len(oMessages))
36
37    # Pass each bundled opensrf message to the message handler
38    t = time()
39    for m in oMessages:
40       osrfHandleMessage(ses, m)
41    t = time() - t
42
43    if isinstance(ses, osrfServerSession):
44       osrfLogInfo("Message processing duration %f" % t)
45
46 def osrfHandleMessage(session, message):
47
48    osrfLogInternal("osrfHandleMessage(): processing message of type %s" % message.type())
49
50    if isinstance(session, osrfClientSession):
51       
52       if message.type() == OSRF_MESSAGE_TYPE_RESULT:
53          session.pushResponseQueue(message)
54          return
55
56       if message.type() == OSRF_MESSAGE_TYPE_STATUS:
57
58          statusCode = int(message.payload().statusCode())
59          statusText = message.payload().status()
60          osrfLogInternal("osrfHandleMessage(): processing STATUS,  statusCode =  %d" % statusCode)
61
62          if statusCode == OSRF_STATUS_COMPLETE:
63             # The server has informed us that this request is complete
64             req = session.findRequest(message.threadTrace())
65             if req: 
66                osrfLogInternal("marking request as complete: %d" % req.id)
67                req.setComplete()
68             return
69
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
74             return
75
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())
79             return;
80
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
84             return
85
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)
90
91          raise osrfProtocolException("Unknown message status: %d" % statusCode)
92       
93          
94    
95