-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
83 lines (62 loc) · 2.63 KB
/
general.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
import time
import random
from node import Node
from state import State, Election
actions = ['attack', 'retreat']
message = []
class MyTestNode (Node):
def __init__(self, host, port, id):
self.ownMessage = []
self.timestamp = 0
self.state = State.NF
self.majority = ""
self.election_status = Election.secondary
self.local_count = 0
self.queues = []
super(MyTestNode, self).__init__(host, port, id)
global message
message.append("mytestnode started")
def outbound_node_connected(self, node):
global message
message.append("outbound_node_connected: " + node.id)
def inbound_node_connected(self, node):
global message
message.append("inbound_node_connected: " + node.id)
def inbound_node_disconnected(self, node):
global message
message.append("inbound_node_disconnected: " + node.id)
def outbound_node_disconnected(self, node):
global message
message.append("outbound_node_disconnected: " + node.id)
def node_message(self, node, data):
global message
# When we receive data from the primary with new action, we reset our local storage
get_received = str(data.split(",")[2])
received_from = get_received.strip()
if received_from == "primary":
self.ownMessage = []
if self.state.name == 'NF':
action = str(data.split(",")[0])
self.ownMessage.append((action, node.id))
# Send the new action to other generals except the "Primary General"
forward_action = action + ", from, " + str(self.id)
self.send_to_nodes(forward_action, exclude=[node])
time.sleep(2)
else:
action = str(data.split(",")[0])
random_action = random.choice(actions)
self.ownMessage.append((action, node.id))
# Send the new action to other generals except the "Primary General"
forward_action = random_action + ", from, " + str(self.id)
self.send_to_nodes(forward_action, exclude=[node])
time.sleep(2)
else:
action = str(data.split(",")[0])
self.ownMessage.append((action, node.id))
def node_disconnect_with_outbound_node(self, node):
global message
message.append(
"node wants to disconnect with oher outbound node: " + node.id)
def node_request_to_stop(self):
global message
message.append("node is requested to stop!")