-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.py
97 lines (75 loc) · 2.41 KB
/
message.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Hao Luo
"""This module implements message passing functionality.
classes:
MessageEncoder: encode a Message object to json
MessageDecoder: decode a Message object from json_string
Message: media of nodes communication
"""
import json
from enum_type import MSG_TYPE
class MessageEncoder(json.JSONEncoder):
def encode(self, obj):
obj_dict = dict()
obj_dict['msg_type'] = int(obj.msg_type)
obj_dict['src'] = obj.src
obj_dict['dest'] = obj.dest
obj_dict['ts'] = obj.ts
obj_dict['data'] = obj.data
return super(MessageEncoder, self).encode(obj_dict)
class MessageDecoder(json.JSONDecoder):
def decode(self, json_string):
parsed_dict = super(MessageDecoder, self).decode(json_string)
return Message(MSG_TYPE(parsed_dict['msg_type']),
parsed_dict['src'],
parsed_dict['dest'],
parsed_dict['ts'],
parsed_dict['data'])
class Message(object):
"""Class that implements the media of nodes communication.
Attributes:
msg_type (MSG_TYPE): type of message
src (int): source of message
dest (int): destination of message
ts (int): Lamport timestamp of message
data (str): other information of message
"""
def __init__(self,
msg_type=None,
src=None,
dest=None,
ts=None,
data=None,
):
self.msg_type = msg_type
self.src = src
self.dest = dest
self.ts = ts
self.data = data
def __json__(self):
return dict(msg_type=self.msg_type,
src=self.src,
dest=self.dest,
ts=self.ts,
data=self.data)
def __cmp__(self, other):
if self.ts != other.ts:
return cmp(self.ts, other.ts)
else:
return cmp(self.src, other.src)
def set_type(self, msg_type):
self.msg_type = msg_type
def set_src(self, src):
self.src = src
def set_dest(self, dest):
self.dest = dest
def set_ts(self, ts):
self.ts = ts
def set_data(self, data):
self.data = data
def to_json(self):
return json.dumps(self, cls=MessageEncoder)
@staticmethod
def to_message(json_str):
return json.loads(json_str, cls=MessageDecoder)