Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python
#coding:utf8
'''
This module contains the RpcConnection class, more informations about this
class are located in it docstring.
'''
import ssl
import json
import struct
import socket
import logging
import threading
from uuid import uuid1
from Queue import Queue
from sjrpc.utils import BytesBuffer
from sjrpc.core.callers import RpcCaller, ThreadedRpcCaller
from sjrpc.core.exceptions import RpcError
class RpcConnection(object):
'''
This class manage a single peer connection.
'''
RECV_BUF_SIZE = 1024
SEND_BUF_SIZE = 1024
REQUEST_MESSAGE = {'id': None, 'method': None, 'args': [], 'kwargs': {}}
RESPONSE_MESSAGE = {'id': None, 'return': None, 'error': None}
def __init__(self, sock, manager, handler=None):
# Sock of this connection:
self._sock = sock
# Inbound buffer containing currently readed message while it sending:
self._inbound_buffer = BytesBuffer()
# The size of the currently readed message, None value significate that
# the next message size have to be readed:
self._cur_msg_size = None
# Outbound buffer containing data to write on the socket:
self._outbound_buffer = BytesBuffer()
# Reference to the RPC handler, this attribute can be set to None if
# no handler is defined for this connection (for example, for a client
# connection).
self._handler = handler
# Store all calls sent to the peer. Key is the id of the call and value
# the event to raise when call is finished.
self._calls = {}
# The manager of this connection
self._manager = manager
@classmethod
def from_addr(cls, addr, port, manager, enable_ssl=False,
cert=None, handler=None):
'''
Construct the instance of :class:`RpcConnection` without providing
the :class:`socket` object. Socket is automatically created and passed
to the standard constructor before to return the new instance.
:param addr: the target ip address
:param port: the target port
:param enable_ssl: enable SSL
:param cert: is SSL is enabled, profile the filename of certificate to
check. If None, don't check certificate.
:param *args: parameters to pass to the standard constructor
:param **kwargs: keyword parameters to pass to the standard constructor
'''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if enable_ssl:
req = ssl.CERT_NONE if cert is None else ssl.CERT_REQUIRED
sock = ssl.wrap_socket(sock, certfile=None, cert_reqs=req,
ssl_version=ssl.PROTOCOL_TLSv1)
sock.connect((addr, port))
sock.setblocking(False)
return cls(sock, manager, handler)
def __repr__(self):
return '<RpcConnection fd:%s>' % self.get_fd()
def __hash__(self):
return self._sock.__hash__()
def send(self):
'''
Flush the outbound buffer by sending all it content through socket. This
method is usually called by the :class:`ConnectionManager` of
this :class:`RpcConnection`.
'''
data = self._outbound_buffer.pull(RpcConnection.SEND_BUF_SIZE)
while data:
sent = self._sock.send(data)
data = data[sent:]
if not len(self._outbound_buffer):
self._manager.nothing_to_write(self)
def receive(self):
'''
Receive data from socket into the inbound buffer. If data can be
decoded, do it and pass result to :meth:`dispatch` method.
'''
buf = self._sock.recv(RpcConnection.RECV_BUF_SIZE)
if not buf:
raise socket.error()
self._inbound_buffer.push(buf)
# Read the message length:
if self._cur_msg_size is None and len(self._inbound_buffer) >= 4:
length = struct.unpack('!L', self._inbound_buffer.pull(4))[0]
self._cur_msg_size = length
# Read the message payload:
if (self._cur_msg_size is not None
and len(self._inbound_buffer) >= self._cur_msg_size):
payload = self._inbound_buffer.pull(self._cur_msg_size)
self._cur_msg_size = None
message = json.loads(payload)
logging.debug('Received: %s' % message)
self.dispatch(message)
def _send(self, message):
'''
Low level method to encode a message in json, calculate it size, and
place result on outbound buffer.
.. warning::
Message must be a jsonisable structure.
'''
logging.debug('Sending: %s' % message)
json_msg = json.dumps(message)
size = struct.pack('!L', len(json_msg))
self._outbound_buffer.push(size + json_msg)
self._manager.data_to_write(self)
def _send_call(self, method_name, *args, **kwargs):
'''
Create the message for the call and push them to the outbound queue.
:param method_name: the name of the method to call on the peer
:param *args: arguments to pass to the remote method
:param **kwargs: keyword arguments to pass to the remote method
:return: the generated id for the request
:rtype: :class:`str` object
'''
msg = RpcConnection.REQUEST_MESSAGE.copy()
msg['method'] = method_name
msg['args'] = args
msg['kwargs'] = kwargs
msg['id'] = str(uuid1())
self._send(msg)
return msg['id']
def _send_response(self, msg_id, returned=None, error=None):
'''
Low level method to send a response message to the peer.
:param msg_id: the id of the replied message
:param returned: returned data
:type returned: returned data or None if errors have been raised
:param error: raised errors
:type error: raised error or None if no error have been raised
'''
msg = RpcConnection.RESPONSE_MESSAGE.copy()
msg['id'] = msg_id
msg['return'] = returned
msg['error'] = error
self._send(msg)
def response(self, msg_id, returned):
'''
Send an "return" response to the peer.
:param msg_id: the id of the replied message
:param returned: the value returned by the function
.. warning::
In case of raised error, use the :meth:`error` method instead of
this one.
'''
self._send_response(msg_id, returned=returned)
def error(self, msg_id, error, message):
'''
Send an error response to the peer.
:param msg_id: the id of the replied message
:param error: the name of the raised exception
:param message: human readable error for the exception
'''
err = {'exception': error, 'message': message}
self._send_response(msg_id, error=err)
def call(self, method_name, *args, **kwargs): #TODO: timeout
'''
Make a new remote call on the peer.
:param method_name: the method to call on the peer
:param \*args: the arguments for the call
:param \*\*kwargs: the keyword arguments for the call
:return: the data returned by the peer for the call
.. note::
This function will block until the peer response is received. You
can also specify a ``timeout`` argument to specify a number of
seconds before to raise an :exc:`CallTimeout` exception if the peer
didnt respond.
'''
# Send the call to the peer:
msg_id = self._send_call(method_name, *args, **kwargs)
# Create an item in calls dict with reference to the event to raise:
self._calls[msg_id] = {'return': None, 'error': None,
'event': threading.Event()}
# Wait for the response:
self._calls[msg_id]['event'].wait()
# Check if error occured while execution:
if self._calls[msg_id]['error'] is not None:
raise RpcError(self._calls[msg_id]['error']['exception'],
self._calls[msg_id]['error']['message'])
response = self._calls[msg_id]['return']
del self._calls[msg_id]
return response
def async_call(self, method_name, *args, **kwargs):
'''
Make a new asynchronous call on the peer.
:param method_name: the method to call on the peer
:param \*args: the arguments for the call
:param \*\*kwargs: the keyword arguments for the call
:return: the message id of the call
'''
# Send the call to the peer:
msg_id = self._send_call(method_name, *args, **kwargs)
self._calls[msg_id] = {'async': True}
return msg_id
def dispatch(self, message):
'''
Dispatch a received message according to it type.
:param message: the received message to dispatch
.. note::
When dispatching a call, the responsability of response is delegated
to the caller, except for the case where the method isn't found on
the handler.
'''
if set(RpcConnection.REQUEST_MESSAGE) <= set(message):
# Handle call requests from the peer:
if self._handler is not None:
try:
func = self._handler[message['method']]
except KeyError:
self.error(
message['id'],
'NameError',
"remote name '%s' is not defined" % message['method'],
)
else:
if getattr(func, '__threaded__', True):
ThreadedRpcCaller(message, self, func).start()
else:
RpcCaller(message, self, func).start()
elif set(RpcConnection.RESPONSE_MESSAGE) <= set(message):
# Handle response message from the peer:
call = self._calls.get(message['id'])
if call is not None:
# Call exists in call list
if message['error'] is None:
call['return'] = message['return']
else:
call['error'] = message['error']
if 'event' in call:
# Release the call if its synchronous:
call['event'].set()
else:
# Else, it's an asynchonous call, so, we need to notify
# manager that the response has been received:
self._manager.signal_arrival(message)
# Message will be deleted from the call list by the
# manager while cleanup.
else:
pass # Malformed messages are note processed
def shutdown(self, callback_name=None):
'''
Shutdown this connection.
:param callback_name: Name of the callback to call on the handler
:type callback_name: :class:`str` or None
'''
self._sock.close()
if callback_name is not None and self._handler is not None:
try:
func = self._handler[callback_name]
except KeyError:
pass
else:
func(self)
def clean_call(self, msg_id):
'''
Delete waiting call by it id in the call list.
:param msg_id: the id of the call to delete
:return: True if call have been deleted or False if it doesn't exists
'''
try:
del self._calls[msg_id]
except KeyError:
return False
else:
return True
def clean_all_calls(self, msg_ids):
'''
Delete all call by their ids in the call list.
:param msg_id: list of call id to delete
'''
for msg_id in msg_ids:
self.clean_call(msg_id)
def get_handler(self):
'''
Return the handler binded to the :class:`RpcConnection`.
:return: binded handler
'''
return self._handler
def set_handler(self, handler):
'''
Define a new handler for this connection.
:param handler: the new handler to define.
'''
self._handler = handler
def get_fd(self):
'''
Get the file descriptor of the socket managed by this connection.
:return: the file descriptor number of the socket
'''
return self._sock.fileno()