-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestination.py
76 lines (60 loc) · 2.41 KB
/
destination.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
import asyncio
import parameters
__author__ = 'Zakaria'
import node
import json
#destination or sink or last node of the network
class Destination(node.Node):
def __init__(self, name, host, port):
super().__init__(name, host, port)
self.nodes = []
self.node_index_r_join = 0
self.node_index_s_join = 0
self.r_join_results_queue = []
self.s_join_results_queue = []
self.is_merging_r = False
self.is_merging_s = False
@asyncio.coroutine
def merge_r(self, packet):
print("merge r")
if (packet.sender['host'], packet.sender['port']) == self.nodes[self.node_index_r_join]:
self.node_index_r_join += 1
self.node_index_r_join %= len(self.nodes)
# yield from self.print_packet(packet)
@asyncio.coroutine
def merge_s(self, packet):
print("merge s")
if (packet.sender['host'], packet.sender['port']) == self.nodes[self.node_index_s_join]:
self.node_index_s_join += 1
self.node_index_s_join %= len(self.nodes)
# yield from self.print_packet(packet)
@asyncio.coroutine
def print_packet(self, packet, filename="_result.txt"):
# print(packet.type + str(len(packet.data)))
# print(packet.type + str(len(packet.data)))
with open(filename, 'a') as f:
for p in packet.data:
print(packet.type + ' : ' + json.dumps(p), file=f)
print("----------------------------------------", file=f)
@asyncio.coroutine
def update_node_serial(self, packet):
self.nodes = packet.data[:]
print(self.nodes)
def do(self, packet):
"""
:param packet:
"""
if packet.type == parameters.DATATYPE_R_JOIN:
asyncio.async(self.merge_r(packet))
# asyncio.async(self.print_packet(packet, "_log_dest.txt"))
elif packet.type == parameters.DATATYPE_S_JOIN:
asyncio.async(self.merge_s(packet))
# asyncio.async(self.print_packet(packet, "_log_dest.txt"))
elif packet.type == parameters.DATATYPE_NODE_SERIAL:
asyncio.async(self.update_node_serial(packet))
# print(packet.type)
#Test run
from sys import argv
if __name__ == '__main__':
destination = Destination('destination', argv[1], argv[2])
destination.run_server()