forked from vanbuile/NetworkApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageProtocol.py
42 lines (31 loc) · 853 Bytes
/
messageProtocol.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
from enum import Enum
from json import loads
class Type(Enum):
REQUEST = 0
RESPONSE = 1
class Header(Enum):
PING = 0
DISCOVER = 1
PUBLISH = 2
FETCH = 3
REGISTER = 4
RETRIEVE = 5
LEAVE = 6
class Message:
def __init__(self, header, type, info, json_string=None):
if json_string:
__tmp__ = loads(json_string)
header = Header(__tmp__['header'])
type = Type(__tmp__['type'])
info = __tmp__['info']
self._header = header
self._type = type
self._info = info
def get_header(self):
return self._header
def get_type(self):
return self._type
def get_info(self):
return self._info
def get_packet(self):
return {'header': self._header.value, 'type': self._type.value, 'info': self._info}