blob: a4b4b5e56eb6853df98dfe6eadd7ceb54612f399 (
plain) (
blame)
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
|
"""JSONPackage"""
import json
class JSONPackageError(Exception):
"""Error raised by JSONPackage."""
pass
class JSONPackage(object):
"""Send/receive json object by gived function.
Attributes:
content: Content of the package body.
Static attributes:
_ENCODING: Encoding of the package.
_HEADER_LENGTH: Length of the header.
"""
_ENCODING = 'utf-8'
_HEADER_LENGTH = 10
def __init__(self, content=None, recv_func=None):
"""Constructor.
If the receive_func is not None, it will grap the default content by
calling that function instead of by the argument "content".
The detail of arguments/return values format see the method "recv_from".
Args:
content: The default content of this package.
recv_func: A function for receive the default content.
"""
self.content = content
if recv_func is not None:
self.recv(recv_func)
def send(self, send_func):
"""Sends by calling the gived sending function.
Args:
send_func: A function which will send the whole data gived.
Function format:
send_func(bytes_data): None
"""
try:
body = bytes(json.dumps(self.content), JSONPackage._ENCODING)
header_str = ('%%0%dd' % JSONPackage._HEADER_LENGTH) % len(body)
send_func(bytes(header_str, JSONPackage._ENCODING) + body)
except TypeError as e:
raise JSONPackageError('json: %r' % e)
except UnicodeError as e:
raise JSONPackageError('Cannot encode the string: %r.' % e)
def recv(self, recv_func):
"""Receives a json object from a gived function.
It will calls the give function like this:
recv_func(<num_of_bytes>) => bytes with length <num_of_bytes>
Args:
recv_func: A function to be called to get the serialize data.
"""
try:
header_str = str(recv_func(JSONPackage._HEADER_LENGTH),
JSONPackage._ENCODING)
body_str = str(recv_func(int(header_str)), JSONPackage._ENCODING)
except UnicodeError as e:
raise JSONPackageError('Cannot decode the bytes: %r.' % e)
except ValueError as e:
raise JSONPackageError('Cannot get the body length %r' % e)
try:
self.content = json.loads(body_str)
except ValueError as e:
raise JSONPackageError('Cannot loads to the json object: %r' % e)
|