-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeerDiscoveryHandler.py
67 lines (55 loc) · 2.24 KB
/
PeerDiscoveryHandler.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
import threading
import time
from Message import Message
from Utils import Utils
# discovery new peers
class PeerDiscoveryHandler:
def __init__(self, node):
self.socketCommunication = node #???
# start peer discovery proccess
def start(self):
statusThread = threading.Thread(target=self.status, args=())
statusThread.start()
discoveryThread = threading.Thread(target=self.discovery, args=())
discoveryThread.start()
# signal current status
def status(self):
while True:
print('Current connections:')
for peer in self.socketCommunication.peers:
print(str(peer.ip) + ':' + str(peer.port))
time.sleep(10)
# broadcast to other nodes to get more new peers
def discovery(self):
while True:
handshakeMessage = self.handshakeMessage()
self.socketCommunication.broadcast(handshakeMessage)
time.sleep(10)
def handshake(self, connected_node):
handshakeMessage = self.handshakeMessage()
self.socketCommunication.send(connected_node, handshakeMessage)
def handshakeMessage(self):
ownConnector = self.socketCommunication.socketConnector
ownPeers = self.socketCommunication.peers
data = ownPeers
messageType = 'DISCOVERY'
message = Message(ownConnector, messageType, data)
encodedMessage = Utils.encode(message)
return encodedMessage
# handle handshake message
def handleMessage(self, message: Message):
peerSocketConnector = message.senderConnector
isNewPeer = True
for peer in self.socketCommunication.peers:
if peer.equals(peerSocketConnector):
isNewPeer = False
if isNewPeer:
self.socketCommunication.peers.append(peerSocketConnector)
peersOfPeers = message.data
for peerOfPeer in peersOfPeers:
isPeerKnown = False
for peer in self.socketCommunication.peers:
if peer.equals(peerOfPeer):
isPeerKnown = True
if not isPeerKnown and not peerOfPeer.equals(self.socketCommunication.socketConnector):
self.socketCommunication.peers.append(peerOfPeer)