]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/ses.py
added support for log transaction IDs
[OpenSRF.git] / src / python / osrf / ses.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 import osrf.json, osrf.conf, osrf.log, osrf.net, osrf.net_obj, osrf.const
17 from osrf.const import OSRF_APP_SESSION_CONNECTED, \
18     OSRF_APP_SESSION_CONNECTING, OSRF_APP_SESSION_DISCONNECTED, \
19     OSRF_MESSAGE_TYPE_CONNECT, OSRF_MESSAGE_TYPE_DISCONNECT, \
20     OSRF_MESSAGE_TYPE_REQUEST, OSRF_MESSAGE_TYPE_RESULT, OSRF_MESSAGE_TYPE_STATUS
21 import osrf.ex
22 import random, os, time, threading
23
24
25 # -----------------------------------------------------------------------
26 # Go ahead and register the common network objects
27 # -----------------------------------------------------------------------
28 osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash')
29 osrf.net_obj.register_hint('osrfMethod', ['method', 'params'], 'hash')
30 osrf.net_obj.register_hint('osrfResult', ['status', 'statusCode', 'content'], 'hash')
31 osrf.net_obj.register_hint('osrfConnectStatus', ['status', 'statusCode'], 'hash')
32 osrf.net_obj.register_hint('osrfMethodException', ['status', 'statusCode'], 'hash')
33
34
35 class Session(object):
36     """Abstract session superclass."""
37
38     ''' Global cache of in-service sessions '''
39     session_cache = {}
40
41     def __init__(self):
42         # by default, we're connected to no one
43         self.state = OSRF_APP_SESSION_DISCONNECTED
44         self.remote_id = None
45         self.locale = None
46         self.thread = None
47         self.service = None
48
49     @staticmethod
50     def find_or_create(thread):
51         if thread in Session.session_cache:
52             return Session.session_cache[thread]
53         return ServerSession(thread)
54
55     def set_remote_id(self, remoteid):
56         self.remote_id = remoteid
57         osrf.log.log_internal("Setting request remote ID to %s" % self.remote_id)
58
59     def wait(self, timeout=120):
60         """Wait up to <timeout> seconds for data to arrive on the network"""
61         osrf.log.log_internal("Session.wait(%d)" % timeout)
62         handle = osrf.net.get_network_handle()
63         handle.recv(timeout)
64
65     def send(self, omessages):
66         """Sends an OpenSRF message"""
67         if not isinstance(omessages, list):
68             omessages = [omessages]
69             
70         net_msg = osrf.net.NetworkMessage(
71             recipient      = self.remote_id,
72             body    = osrf.json.to_json(omessages),
73             thread = self.thread,
74             locale = self.locale,
75         )
76
77         handle = osrf.net.get_network_handle()
78         handle.send(net_msg)
79
80     def cleanup(self):
81         """Removes the session from the global session cache."""
82         del Session.session_cache[self.thread]
83
84 class ClientSession(Session):
85     """Client session object.  Use this to make server requests."""
86
87     def __init__(self, service, locale='en_US'):
88         
89         # call superclass constructor
90         Session.__init__(self)
91
92         # the service we are sending requests to
93         self.service = service
94
95         # the locale we want requests to be returned in
96         self.locale = locale
97
98         # find the remote service handle <router>@<domain>/<service>
99         domain = osrf.conf.get('domain', 0)
100         router = osrf.conf.get('router_name')
101         self.remote_id = "%s@%s/%s" % (router, domain, service)
102         self.orig_remote_id = self.remote_id
103
104         # generate a random message thread
105         self.thread = "%s%s%s%s" % (os.getpid(), 
106             str(random.randint(100,100000)), str(time.time()),threading.currentThread().getName().lower())
107
108         # how many requests this session has taken part in
109         self.next_id = 0 
110
111         # cache of request objects 
112         self.requests = {}
113
114         # cache this session in the global session cache
115         Session.session_cache[self.thread] = self
116
117     def reset_request_timeout(self, rid):
118         req = self.find_request(rid)
119         if req:
120             req.reset_timeout = True
121             
122
123     def request2(self, method, arr):
124         """Creates a new request and sends the request to the server using a python array as the params."""
125         return self.__request(method, arr)
126
127     def request(self, method, *args):
128         """Creates a new request and sends the request to the server using a variable argument list as params"""
129         arr = list(args)
130         return self.__request(method, arr)
131
132     def __request(self, method, arr):
133         """Builds the request object and sends it."""
134         if self.state != OSRF_APP_SESSION_CONNECTED:
135             self.reset_remote_id()
136
137         osrf.log.make_xid()
138
139         osrf.log.log_debug("Sending request %s -> %s " % (self.service, method))
140         req = ClientRequest(self, self.next_id, method, arr, self.locale)
141         self.requests[str(self.next_id)] = req
142         self.next_id += 1
143         req.send()
144         return req
145
146
147     def connect(self, timeout=10):
148         """Connects to a remote service"""
149
150         if self.state == OSRF_APP_SESSION_CONNECTED:
151             return True
152         self.state = OSRF_APP_SESSION_CONNECTING
153
154         # construct and send a CONNECT message
155         self.send(
156             osrf.net_obj.NetworkObject.osrfMessage( 
157                 {   'threadTrace' : 0,
158                     'type' : OSRF_MESSAGE_TYPE_CONNECT
159                 } 
160             )
161         )
162
163         while timeout >= 0 and not self.state == OSRF_APP_SESSION_CONNECTED:
164             start = time.time()
165             self.wait(timeout)
166             timeout -= time.time() - start
167         
168         if self.state != OSRF_APP_SESSION_CONNECTED:
169             raise osrf.ex.OSRFServiceException("Unable to connect to " + self.service)
170         
171         return True
172
173     def disconnect(self):
174         """Disconnects from a remote service"""
175
176         if self.state == OSRF_APP_SESSION_DISCONNECTED:
177             return True
178
179         self.send(
180             osrf.net_obj.NetworkObject.osrfMessage( 
181                 {   'threadTrace' : 0,
182                     'type' : OSRF_MESSAGE_TYPE_DISCONNECT
183                 } 
184             )
185         )
186
187         self.state = OSRF_APP_SESSION_DISCONNECTED
188
189     
190
191     def reset_remote_id(self):
192         """Recovers the original remote id"""
193         self.remote_id = self.orig_remote_id
194         osrf.log.log_internal("Resetting remote ID to %s" % self.remote_id)
195
196     def push_response_queue(self, message):
197         """Pushes the message payload onto the response queue 
198             for the request associated with the message's ID."""
199         osrf.log.log_debug("pushing %s" % message.payload())
200         try:
201             self.find_request(message.threadTrace()).push_response(message.payload())
202         except Exception, e: 
203             osrf.log.log_warn("pushing respond to non-existent request %s : %s" % (message.threadTrace(), e))
204
205     def find_request(self, rid):
206         """Returns the original request matching this message's threadTrace."""
207         try:
208             return self.requests[str(rid)]
209         except KeyError:
210             osrf.log.log_debug('find_request(): non-existent request %s' % str(rid))
211             return None
212
213     @staticmethod
214     def atomic_request(service, method, *args):
215         ses = ClientSession(service)
216         req = ses.request2(method, list(args))
217         resp = req.recv()
218         data = None
219         if resp:
220             data = resp.content()
221         req.cleanup()
222         ses.cleanup()
223         return data
224
225
226
227
228 class Request(object):
229     def __init__(self, session, rid, method=None, params=[], locale='en-US'):
230         self.session = session # my session handle
231         self.rid     = rid # my unique request ID
232         self.method = method # method name
233         self.params = params # my method params
234         self.locale = locale
235         self.complete = False # is this request done?
236         self.complete_time =  0 # time at which the request was completed
237
238
239 class ClientRequest(Request):
240     """Represents a single OpenSRF request.
241         A request is made and any resulting respones are 
242         collected for the client."""
243
244     def __init__(self, session, rid, method=None, params=[], locale='en-US'):
245         Request.__init__(self, session, rid, method, params, locale)
246         self.queue  = [] # response queue
247         self.reset_timeout = False # resets the recv timeout?
248         self.send_time = 0 # local time the request was put on the wire
249         self.first_response_time = 0 # time it took for our first reponse to be received
250
251     def send(self):
252         """Sends a request message"""
253
254         # construct the method object message with params and method name
255         method = osrf.net_obj.NetworkObject.osrfMethod( {
256             'method' : self.method,
257             'params' : self.params
258         } )
259
260         # construct the osrf message with our method message embedded
261         message = osrf.net_obj.NetworkObject.osrfMessage( {
262             'threadTrace' : self.rid,
263             'type' : OSRF_MESSAGE_TYPE_REQUEST,
264             'payload' : method,
265             'locale' : self.locale
266         } )
267
268         self.send_time = time.time()
269         self.session.send(message)
270
271     def recv(self, timeout=120):
272         """ Waits up to <timeout> seconds for a response to this request.
273         
274             If a message is received in time, the response message is returned.
275             Returns None otherwise."""
276
277         self.session.wait(0)
278
279         orig_timeout = timeout
280         while not self.complete and (timeout >= 0 or orig_timeout < 0) and len(self.queue) == 0:
281
282             s = time.time()
283             self.session.wait(timeout)
284
285             if self.reset_timeout:
286                 self.reset_timeout = False
287                 timeout = orig_timeout
288
289             elif orig_timeout >= 0:
290                 timeout -= time.time() - s
291
292         now = time.time()
293
294         # -----------------------------------------------------------------
295         # log some statistics 
296         if len(self.queue) > 0:
297             if not self.first_response_time:
298                 self.first_response_time = now
299                 osrf.log.log_debug("time elapsed before first response: %f" \
300                     % (self.first_response_time - self.send_time))
301
302         if self.complete:
303             if not self.complete_time:
304                 self.complete_time = now
305                 osrf.log.log_debug("time elapsed before complete: %f" \
306                     % (self.complete_time - self.send_time))
307         # -----------------------------------------------------------------
308
309
310         if len(self.queue) > 0:
311             # we have a reponse, return it
312             return self.queue.pop(0)
313
314         return None
315
316     def push_response(self, content):
317         """Pushes a method response onto this requests response queue."""
318         self.queue.append(content)
319
320     def cleanup(self):
321         """Cleans up request data from the cache. 
322
323             Do this when you are done with a request to prevent "leaked" cache memory."""
324         del self.session.requests[str(self.rid)]
325
326     def set_complete(self):
327         """Sets me as complete.  This means the server has sent a 'request complete' message"""
328         self.complete = True
329
330
331 class ServerSession(Session):
332     """Implements a server-side session"""
333
334     def __init__(self, thread):
335         Session.__init__(self)
336         self.thread = thread
337
338     def send_status(self, thread_trace, payload):
339         self.send(
340             osrf.net_obj.NetworkObject.osrfMessage( 
341                 {   'threadTrace' : thread_trace,
342                     'type' : osrf.const.OSRF_MESSAGE_TYPE_STATUS,
343                     'payload' : payload,
344                     'locale' : self.locale
345                 } 
346             )
347         )
348
349     def send_connect_ok(self, thread_trace):
350         status_msg = osrf.net_obj.NetworkObject.osrfConnectStatus({   
351             'status' : 'Connection Successful',
352             'statusCode': osrf.const.OSRF_STATUS_OK
353         })
354         self.send_status(thread_trace, status_msg)
355
356
357 class ServerRequest(Request):
358
359     def __init__(self, session, rid, method, params=[]):
360         Request.__init__(self, session, rid, method, params, session.locale)
361         self.response_list = []
362
363     def _build_response_msg(self, data):
364         result = osrf.net_obj.NetworkObject.osrfResult({
365             'content' :  data,
366             'statusCode' : osrf.const.OSRF_STATUS_OK,
367             'status' : 'OK'
368         })
369
370         return osrf.net_obj.NetworkObject.osrfMessage({
371             'threadTrace' : self.rid,
372             'type' : OSRF_MESSAGE_TYPE_RESULT,
373             'payload' : result,
374             'locale' : self.locale
375         })
376
377     def _build_complete_msg(self):
378
379         status = osrf.net_obj.NetworkObject.osrfConnectStatus({   
380             'threadTrace' : self.rid,
381             'status' : 'Request Complete',
382             'statusCode': osrf.const.OSRF_STATUS_COMPLETE
383         })
384
385         return osrf.net_obj.NetworkObject.osrfMessage({
386             'threadTrace' : self.rid,
387             'type' : OSRF_MESSAGE_TYPE_STATUS,
388             'payload' : status,
389             'locale' : self.locale
390         })
391
392     def respond(self, data):
393         ''' For non-atomic calls, this sends a response directly back
394             to the client.  For atomic calls, this pushes the response
395             onto the response list '''
396         osrf.log.log_internal("responding with %s" % str(data))
397         if self.method.atomic:
398             self.response_list.append(data)
399         else:
400             self.session.send(self._build_response_msg(data))
401
402     def respond_complete(self, data):
403         ''' Sends a complete message accompanied by the final result if applicable '''
404
405         if self.complete: 
406             return
407         self.complete = True
408         self.complete_time = time.time()
409
410         if self.method.atomic:
411             if data is not None:
412                 self.response_list.append(data) 
413             self.session.send([
414                 self._build_response_msg(self.response_list),
415                 self._build_complete_msg(),
416             ])
417
418         elif data is not None:
419             self.session.send([
420                 self._build_response_msg(data),
421                 self._build_complete_msg(),
422             ])
423
424         else:
425             self.session.send(self._build_complete_msg())
426             
427
428 class MultiSession(object):
429     ''' Manages multiple requests.  With the current implementation, a 1 second 
430         lag time before the first response is practically guaranteed.  Use 
431         only for long running requests.
432
433         Another approach would be a threaded version, but that would require
434         build-up and breakdown of thread-specific xmpp connections somewhere.
435         conection pooling? 
436     '''
437     class Container(object):
438         def __init__(self, req):
439             self.req = req
440             self.id = None
441
442     def __init__(self):
443         self.complete = False
444         self.reqs = []
445
446     def request(self, service, method, *args):
447         ses = ClientSession(service)
448         cont = MultiSession.Container(ses.request(method, *args))
449         cont.id = len(self.reqs)
450         self.reqs.append(cont)
451
452     def recv(self, timeout=120):
453         ''' Returns a tuple of req_id, response '''
454         duration = 0
455         block_time = 1
456         while True:
457             for i in range(0, len(self.reqs)):
458                 cont = self.reqs[i]
459                 req = cont.req
460
461                 res = req.recv(0)
462                 if i == 0 and not res:
463                     res = req.recv(block_time)
464
465                 if res: break
466
467             if res: break
468
469             duration += block_time
470             if duration >= timeout:
471                 return None
472
473         if req.complete:
474             self.reqs.pop(self.reqs.index(cont))
475
476         if len(self.reqs) == 0:
477             self.complete = True
478
479         return cont.id, res.content()
480