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 # -----------------------------------------------------------------------
20 from osrf.const import OSRF_APP_SESSION_CONNECTED, \
21 OSRF_APP_SESSION_DISCONNECTED, OSRF_MESSAGE_TYPE_RESULT, \
22 OSRF_MESSAGE_TYPE_STATUS, OSRF_STATUS_COMPLETE, OSRF_STATUS_CONTINUE, \
23 OSRF_STATUS_NOTFOUND, OSRF_STATUS_OK, OSRF_STATUS_TIMEOUT
28 ses = osrf.ses.Session.find_session(net_msg.thread)
31 # This is an incoming request from a client, create a new server session
32 osrf.log.log_error("server-side sessions don't exist yet")
34 ses.set_remote_id(net_msg.sender)
36 omessages = osrf.json.to_object(net_msg.body)
38 osrf.log.log_internal("push(): received %d messages" \
41 # Pass each bundled opensrf message to the message handler
44 handle_message(ses, msg)
45 duration = time.time() - start
47 if isinstance(ses, osrf.ses.ServerSession):
48 osrf.log.log_info("Message processing duration %f" % duration)
50 def handle_message(session, message):
52 osrf.log.log_internal("handle_message(): processing message of "
53 "type %s" % message.type())
55 if isinstance(session, osrf.ses.ClientSession):
57 if message.type() == OSRF_MESSAGE_TYPE_RESULT:
58 session.push_response_queue(message)
61 if message.type() == OSRF_MESSAGE_TYPE_STATUS:
63 status_code = int(message.payload().statusCode())
64 status_text = message.payload().status()
65 osrf.log.log_internal("handle_message(): processing STATUS, "
66 "status_code = %d" % status_code)
68 if status_code == OSRF_STATUS_COMPLETE:
69 # The server has informed us that this request is complete
70 req = session.find_request(message.threadTrace())
72 osrf.log.log_internal("marking request as complete: %d" % req.rid)
76 if status_code == OSRF_STATUS_OK:
77 # We have connected successfully
78 osrf.log.log_debug("Successfully connected to " + session.service)
79 session.state = OSRF_APP_SESSION_CONNECTED
82 if status_code == OSRF_STATUS_CONTINUE:
83 # server is telling us to reset our wait timeout and keep waiting for a response
84 session.reset_request_timeout(message.threadTrace())
87 if status_code == OSRF_STATUS_TIMEOUT:
88 osrf.log.log_debug("The server did not receive a request from us in time...")
89 session.state = OSRF_APP_SESSION_DISCONNECTED
92 if status_code == OSRF_STATUS_NOTFOUND:
93 osrf.log.log_error("Requested method was not found on the server: %s" % status_text)
94 session.state = OSRF_APP_SESSION_DISCONNECTED
95 raise osrf.ex.OSRFServiceException(status_text)
97 raise osrf.ex.OSRFProtocolException("Unknown message status: %d" % status_code)