-
Notifications
You must be signed in to change notification settings - Fork 2
/
NameScheduler.py
138 lines (114 loc) · 4.28 KB
/
NameScheduler.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import random
import Node
import asyncio
import websockets
import json
import threading
import socket
import struct
from utils import boolean
#for quicker testing
import argparse
parser = argparse.ArgumentParser(description='To get same results without a lot of effort.')
parser.add_argument('--OPTIMIZE', type=boolean, help='Do NOT use this for the final result', default=False)
args = parser.parse_args()
from communication import *
from report import Report
import sign
class NameScheduler(object):
"""docstring for NameScheduler"""
def __init__(self):
self.MaxNodes = 100
# Ids start from 1 till MaxNodes
self.ListOfIds = [str(i) for i in range(self.MaxNodes)]
self.ConnectedClients = {}
# self.Uri = 'ws://localhost:8765'
self.node = Node.Node(8765)
self.node.NodeId = -1
self.flag = True
def register(self, id, IpAddr, port, uri, allocate, public_key, private_key):
self.ConnectedClients[id] = { 'IpAddr': IpAddr,
'port': port,
'Uri': uri,
'allocate': allocate,
'public_key': public_key,
'private_key': private_key}
def generateId(self, IpAddr, port, uri, allocate):
# id_ = random.choice(self.ListOfIds)
id_ = self.ListOfIds[0]
if args.OPTIMIZE:
import pickle
print("Unpickling keys for id:", id_)
pub = pickle.load(open("keys/pub{}.pickle".format(id_), 'rb'))
priv = pickle.load(open("keys/priv{}.pickle".format(id_), 'rb'))
else:
pub, priv = sign.GenerateKeys(2048)
pub, priv = pub.exportKey('PEM').decode('utf-8'), priv.exportKey('PEM').decode('utf-8')
self.ListOfIds.remove(id_)
self.register(id_, IpAddr, port, uri, allocate, pub, priv)
return id_, pub
# Update Broadcast
# async def BroadCastNewNode(self, message, msg):
# for _, client in self.ConnectedClients.items():
# if client['IpAddr'] != message['IpAddr'] or client['port'] != message['port']:
# await SendMsgRoutine(client['Uri'], msg)
async def IdRoutine(self, websocket, path):
async for message in websocket:
server = 'http://0.0.0.0:4003/'
message = json.loads(message)
if message['type'].upper() == 'HANDSHAKE':
# if self.flag:
# reply = Report(server, 'new_client')
exists = False
client = [(i,x) for i,x in self.ConnectedClients.items() if x['IpAddr']==message['IpAddr'] and x['port']==message['port']]
# print("GODDAMN", client)
if len(client):
client = client[0]
exists = True
msg = {'id': client[0], 'LoN': self.ConnectedClients}
msg = json.dumps(msg)
await websocket.send(msg)
# server = 'http:/' + node.NodeIPAddr + ':5000'
if not exists:
client_id ,client_pub = self.generateId(message['IpAddr'], message['port'], message['Uri'], message['allocate'])
print("New Client {}:{} added with ID = {}".format(message['IpAddr'], message['port'], client_id))
a = {'id': client_id, 'LoN': self.ConnectedClients}
# print(self.ConnectedClients)
try:
a = json.dumps(a)
except TypeError as e:
print('Cleaning Client data')
for client in self.ConnectedClients:
if 'websocket' in client:
del client['websocket']
a = json.dumps(a)
await websocket.send(a)
del self.ConnectedClients[client_id]['private_key']
msg = {'type': 'NewNode', 'id': client_id, 'Uri': message['Uri'], 'info': self.ConnectedClients[client_id]}
m = {'total_clients': len(self.ConnectedClients), 'id': client_id, 'clients_info': self.ConnectedClients[client_id]}
# print(m)
Report(server, 'client', m)
# if len(self.ConnectedClients) > 2:
# Report(server, 'check_clients', {'lol':'lol'})
# await self.BroadCastNewNode(message, msg)
# await BroadCast(message['IpAddr'], message['port'], self.ConnectedClients, msg)
Multicast('224.1.1.1', 8766, msg)
def SocketServer(self):
MulticastServer('224.1.1.1', 8766, self.node)
def Id_websocket(self):
asyncio.get_event_loop().run_until_complete(websockets.serve(self.IdRoutine, port=8765, close_timeout=10000))
asyncio.get_event_loop().run_forever()
def Id(self):
# t1 = threading.Thread(target=self.Id_websocket)
t2 = threading.Thread(target=self.SocketServer)
# print('a')
# t1.start()
t2.start()
# print('b')
self.Id_websocket()
# t1.join()
t2.join()
# print('c')
if __name__ == '__main__':
ns = NameScheduler()
ns.Id()