forked from vnknt/p2p_network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.py
312 lines (138 loc) · 5.25 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import socket
import time
import threading
import commands
import random
import json
import hashlib
from settings.terminal_set import bcolors
from helpers.terminal_helper import print_colored
from Message import Message
import os
os.system("color")
class Node:
SERVER_IP=socket.gethostbyname(socket.gethostname())
SERVER_PORT=None
SERVER_ADDR=None
GENESIS_NODE_ADDR="192.168.56.1" #boot node address
GENESIS_NODE_PORT=5050 #boot node port
nodes=list() #connections
connections=list() #connection addresses
incomingConnections=list()
connections_json=list()
message_logs=list()
nodes_in_network=list()
isJoinedNetwork=False
def __init__(self,ip=SERVER_IP,port=SERVER_PORT):
self.SERVER_IP=ip
self.SERVER_PORT=port
self.SERVER_ADDR=(ip,port)
def totalConection(self):
return len(self.connections)
def find_addr_index(self,ip,port):
index=0
result=-1
for node in self.connections:
if(node[0] == ip and node[1] == port):
result=index
break
index+=1
return result
def find_connection_index(self,ip,port):
index=0
result=-1
for node in self.nodes:
peername=node.getpeername()
if(peername[0] == ip and peername[1] == port):
result=index
break
index+=1
return result
def find_json_index(self,ip,port):
index=0
result=-1
for node in self.connections_json:
if(node["ip_addr"] == ip and node["port"] == port):
result=index
break
index+=1
return result
def getRandomNode(self):
json_temp=self.connections_json.copy() #all active connections are copied
total_node=self.totalConection() #calculate total connected node
if total_node==0:
return None #if there is no any connection, return self ip and port
rnd=random.randint(0,total_node-1) #random index
return json_temp[rnd]
def remove_connection(self,conn,ip,port):
try:
conn_index = self.find_connection_index(ip,port)
except:
conn_index=-1
if(conn_index!=-1):
self.nodes.pop(conn_index)
try:
node_index=self.find_addr_index(ip, port)
except:
node_index=-1
if(node_index!=-1):
self.connections.pop(node_index)
try:
node_index=self.find_json_index(ip, port)(ip, port)
except:
node_index=-1
if(node_index!=-1):
self.connections_json.pop(node_index)
print(self.nodes)
print(self.connections)
def getSelfOrAdjacent(self):
total_adj = self.totalConection()
server_json={"ip_addr":self.SERVER_IP,"port":self.SERVER_PORT}
if total_adj == 0 :
return self.merge_command(commands.NODE_CON_ADDR, f"({self.SERVER_IP},{self.SERVER_PORT})")
if total_adj == 1:
temp_json_list=self.connections_json.copy()
temp_json_list.append(server_json)
return temp_json_list
#If total_adjacent>=2 , return 2 or more node
if(total_adj>=2):
copy_json = self.connections_json.copy()
copy_json.append(server_json)
total_return = random.randint(2, total_adj+1) #+1 is for self address
arr=list(range(0,total_adj+1))
random.shuffle(arr)
arr=arr[0:total_return]
print(f"{total_return} , {arr}")
temp_json_list = list()
for i in arr:
temp_json_list.append(copy_json[i])
print(temp_json_list)
return temp_json_list
@classmethod
def set_node(cls,ip,port):
cls.SERVER_PORT=port
cls.SERVER_IP=ip
cls.SERVER_ADDR=(ip,port)
@staticmethod
def calculateMsgLen(self,msg):
message = msg.encode(self.FORMAT)
msg_len = len(message)
send_len = str(msg_len).encode(self.FORMAT)
send_len += b' ' * (self.HEADER_LEN - len(send_len))
return send_len
@staticmethod
def merge_command(cmd,msg):
message=f"{cmd}({msg})"
print(message)
return message
@staticmethod
def split_command(cmd,msg):
length=len(cmd)
message=msg[length+1:-1]
print(message)
return message
def create_message(self,msg,title,sender_ip=SERVER_IP,sender_port=SERVER_PORT,reciever_ip="all",reciever_port=None):
if(sender_port== None):
sender_port=self.SERVER_PORT
message = Message(sender_ip,sender_port,reciever_ip,reciever_port)
return message.msg(msg,title)