-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.py
111 lines (84 loc) · 3.05 KB
/
node.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
import os
import json
import crypto
from time import sleep
from helper import Helper
from network import ManufacturerNode, Network
from tcp_logger import Tcp_Logger
from file_logger import File_Logger
from raft import Raft
from http_server import HttpServer
class Node:
def __init__(self, internal_ip, external_ip, network : Network, force_leader = False):
self.node : ManufacturerNode = network.get_manufacturer_node_info(internal_ip, external_ip)
self.tcp_logger = Tcp_Logger(self.node.name)
if self.node.name == 'F7':
force_leader = True
self.raft = Raft(self.node.name, network, self.node.tcp_port, self.tcp_logger, force_leader)
self.http_sever = HttpServer(self.node.http_port, self.on_http_server_receive)
start_msg = '[INITIALIZED] - ' + self.node.get_str_info()
self.tcp_logger.save(start_msg)
def kill(self):
self.tcp_logger.save("[KILL]")
pid = str(os.getpid())
cmd = "kill " + pid
os.system(cmd)
def reset(self, time):
self.tcp_logger.save("[RESET]")
pid = str(os.getpid())
cmd = "kill " + pid
cmd += "; sleep " + time + ";"
cmd += "python3 node.py"
os.system(cmd)
def suspend(self, time):
self.tcp_logger.save("[SUSPEND]")
self.raft.suspend()
sleep(int(time))
self.raft.resume()
def start(self):
self.raft.start()
# TODO: implement unidirectional failures
def add_author(self, author_data_json, answer):
self.tcp_logger("[ADD_AUTHOR]")
author_data = json.loads(author_data_json)
(sk, pk) = crypto.ecdsa_gen_pair_keys()
answer['private_key'] = sk
author_data['public_key'] = pk
return self.raft.publish('add_author', author_data)
def on_http_server_receive(self, keys, values):
answer = {}
status = True
if keys[0] == "action":
if values[0] == "start":
self.start()
elif values[0] == "kill":
self.kill()
elif values[0] == "reset":
time = values[1]
self.reset(time)
elif values[0] == "suspend":
time = values[1]
self.suspend(time)
elif values[0] == "add_author":
author_data_json = values[1]
(status, msg) = self.add_author(author_data_json, answer)
else:
status = False
msg = 'action not recognitzed.'
if status:
answer['status'] = "success"
else:
answer = {} # clean answer
answer['status'] = "error"
answer['msg'] = msg
return json.dumps(answer)
if __name__ == "__main__":
while(True):
try:
internal_ip = Helper.get_internal_ip()
external_ip = Helper.get_external_ip()
break
except:
sleep(1)
network = Network('network_info.csv')
Node(internal_ip, external_ip, network, force_leader = False)