-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacket.py
42 lines (37 loc) · 1.18 KB
/
Packet.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
#/usr/bin/python
import json
class Packet:
def __init__(self, type, node_id=None, from_ip = None, to_ip = None, gateway_id = None, route_table = None, target_id = None, link = None, keyword = None, sender_id = None, response = None, word = None):
self.type = type
if type == "JOINING_NETWORK_SIMPLIFIED":
self.node_id = node_id
self.target_id = target_id
self.from_ip = from_ip
elif type == "JOINING_NETWORK_RELAY_SIMPLIFIED":
self.node_id = node_id
self.target_id = target_id
self.gateway_id = gateway_id
elif type == "ROUTING_INFO":
self.gateway_id = gateway_id
self.node_id = node_id
self.ip_address = from_ip
self.table = route_table
elif type == "LEAVING_NETWORK":
self.node_id = node_id
elif type == "INDEX":
self.target_id = target_id
self.sender_id = sender_id
self.keyword = keyword
self.link = link
elif type == "SEARCH":
self.word = word
self.node_id = node_id
self.sender_id = sender_id
elif type == "SEARCH_RESPONSE":
self.word = word
self.node_id = node_id
self.sender_id = sender_id
self.response = response
def send(self, sock, ip):
print "sending"
sock.sendto(json.dumps(self.__dict__), (ip, 8767))