-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.py
124 lines (95 loc) · 3.29 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
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
import struct
from math import floor, ceil
BUY_MESSAGE_CODE = 1
BUY_MESSAGE_RESPONSE_CODE = 2
BUY_SUCCESS = 1
BUY_FAIL = 0
REQUEST_MESSAGE_CODE = 3
TOTAL_KIOSKS = None
REPLY_MESSAGE_CODE = 4
RELEASE_MESSAGE_CODE = 5
class Message:
def __init__(self, data):
self.data = data
self.data_length = len(data)
def serialize(self):
pass
@staticmethod
def deserialize(data):
message_type = struct.unpack("!B",data[0:1])[0]
if message_type == BUY_MESSAGE_CODE:
return BuyMessage.deserialize(data)
elif message_type == BUY_MESSAGE_RESPONSE_CODE:
return BuyMessageResponse.deserialize(data)
elif message_type == REQUEST_MESSAGE_CODE:
return RequestMessage.deserialize(data)
elif message_type == REPLY_MESSAGE_CODE:
return ReplyMessage.deserialize(data)
elif message_type == RELEASE_MESSAGE_CODE:
return ReleaseMessage.deserialize(data)
else: #other message types
pass
class ReleaseMessage(Message):
def serialize(self):
return struct.pack("!BI", RELEASE_MESSAGE_CODE, self.num_tickets)
def __init__(self, num_tickets):
self.num_tickets = num_tickets
super(ReleaseMessage, self).__init__(self.serialize())
@staticmethod
def deserialize(data):
msg_code, num_tickets = struct.unpack("!BI", data)
assert msg_code == RELEASE_MESSAGE_CODE
return ReleaseMessage(num_tickets)
class ReplyMessage(Message):
def serialize(self):
return struct.pack("!BI", REPLY_MESSAGE_CODE, 0)
def __init__(self):
super(ReplyMessage, self).__init__(self.serialize())
@staticmethod
def deserialize(data):
msg_code, fpoint = struct.unpack("!BI", data)
assert msg_code == REPLY_MESSAGE_CODE
return ReplyMessage()
def get_rank(lclock, pid):
return (float(lclock) + (float(pid)/TOTAL_KIOSKS))
class RequestMessage(Message):
def serialize(self):
return struct.pack("!Bf", REQUEST_MESSAGE_CODE, (float(self.lamport_clock) + float(self.pid)/TOTAL_KIOSKS))
def __init__(self, lamport_clock, process_id):
self.lamport_clock = lamport_clock
self.pid = process_id
self.rank = get_rank(self.lamport_clock, self.pid)
super(RequestMessage, self).__init__(self.serialize())
@staticmethod
def deserialize(data):
msg_code, fpoint = struct.unpack("!Bf", data)
assert msg_code == REQUEST_MESSAGE_CODE
lamport_clock = floor(fpoint)
process_id = (float(fpoint)-float(lamport_clock))*float(TOTAL_KIOSKS)
process_id = int(round(process_id))
return RequestMessage(lamport_clock, process_id)
class BuyMessage(Message):
def serialize(self):
return struct.pack("!BI", BUY_MESSAGE_CODE, self.num_tickets)
def __init__(self, num_tickets):
self.num_tickets = num_tickets
super(BuyMessage, self).__init__(self.serialize())
@staticmethod
def deserialize(data):
msg_code, num_tickets = struct.unpack("!BI",data[0:5])
assert msg_code == BUY_MESSAGE_CODE
return BuyMessage(int(num_tickets))
class BuyMessageResponse(Message):
def serialize(self):
return struct.pack("!BI", BUY_MESSAGE_RESPONSE_CODE, self.success)
def __init__(self, successful):
if successful is True:
self.success = BUY_SUCCESS
else:
self.success = BUY_FAIL
super(BuyMessageResponse, self).__init__(self.serialize())
@staticmethod
def deserialize(data):
msg_code, success = struct.unpack("!BI", data)
assert msg_code == BUY_MESSAGE_RESPONSE_CODE
return BuyMessageResponse(success == BUY_SUCCESS)