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 # -----------------------------------------------------------------------
17 import osrf.json, osrf.log, osrf.ex, osrf.ses, osrf.const, osrf.app
21 ses = osrf.ses.Session.find_or_create(net_msg.thread)
22 ses.set_remote_id(net_msg.sender)
24 ses.service = osrf.app.Application.name
26 omessages = osrf.json.to_object(net_msg.body)
28 osrf.log.log_internal("stack.push(): received %d messages" % len(omessages))
30 # Pass each bundled opensrf message to the message handler
33 handle_message(ses, msg)
34 duration = time.time() - start
36 if isinstance(ses, osrf.ses.ServerSession):
37 osrf.log.log_info("Message processing duration %f" % duration)
39 def handle_message(session, message):
41 osrf.log.log_internal("handle_message(): processing message of "
42 "type %s" % message.type())
44 if isinstance(session, osrf.ses.ClientSession):
45 handle_client(session, message)
47 handle_server(session, message)
50 def handle_client(session, message):
52 if message.type() == osrf.const.OSRF_MESSAGE_TYPE_RESULT:
53 session.push_response_queue(message)
56 if message.type() == osrf.const.OSRF_MESSAGE_TYPE_STATUS:
58 status_code = int(message.payload().statusCode())
59 status_text = message.payload().status()
60 osrf.log.log_internal("handle_message(): processing STATUS, "
61 "status_code = %d" % status_code)
63 if status_code == osrf.const.OSRF_STATUS_COMPLETE:
64 # The server has informed us that this request is complete
65 req = session.find_request(message.threadTrace())
67 osrf.log.log_internal("marking request as complete: %d" % req.rid)
71 if status_code == osrf.const.OSRF_STATUS_OK:
72 # We have connected successfully
73 osrf.log.log_debug("Successfully connected to " + session.service)
74 session.state = OSRF_APP_SESSION_CONNECTED
77 if status_code == osrf.const.OSRF_STATUS_CONTINUE:
78 # server is telling us to reset our wait timeout and keep waiting for a response
79 session.reset_request_timeout(message.threadTrace())
82 if status_code == osrf.const.OSRF_STATUS_TIMEOUT:
83 osrf.log.log_debug("The server did not receive a request from us in time...")
84 session.state = OSRF_APP_SESSION_DISCONNECTED
87 if status_code == osrf.const.OSRF_STATUS_NOTFOUND:
88 osrf.log.log_error("Requested method was not found on the server: %s" % status_text)
89 session.state = OSRF_APP_SESSION_DISCONNECTED
90 raise osrf.ex.OSRFServiceException(status_text)
92 if status_code == osrf.const.OSRF_STATUS_INTERNALSERVERERROR:
93 raise osrf.ex.OSRFServiceException("Server error %d : %s" % (status_code, status_text))
95 raise osrf.ex.OSRFProtocolException("Unknown message status: %d" % status_code)
98 def handle_server(session, message):
100 if message.type() == osrf.const.OSRF_MESSAGE_TYPE_REQUEST:
101 osrf.log.log_debug("server received REQUEST from %s" % session.remote_id)
102 osrf.app.Application.handle_request(session, message)
105 if message.type() == osrf.const.OSRF_MESSAGE_TYPE_CONNECT:
106 osrf.log.log_debug("server received CONNECT from %s" % session.remote_id)
107 session.state == osrf.const.OSRF_APP_SESSION_CONNECTED
108 session.send_connect_ok(message.threadTrace())
111 if message.type() == osrf.const.OSRF_MESSAGE_TYPE_DISCONNECT:
112 osrf.log.log_debug("server received DISCONNECT from %s" % session.remote_id)
113 session.state = osrf.const.OSRF_APP_SESSION_DISCONNECTED
116 if message.type() == osrf.const.OSRF_MESSAGE_TYPE_STATUS:
117 # Should never get here
118 osrf.log.log_warn("server received STATUS from %s" % session.remote_id)