-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeerDiscovery.py
58 lines (46 loc) · 1.91 KB
/
PeerDiscovery.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
from BlockchainUtils import BlockchainUtils
import threading
import time
class PeerDiscovery():
def __init__(self, socketCommunication):
self.p2p = socketCommunication
def start(self):
statusThread = threading.Thread(target=self.status, args=[])
statusThread.start()
discoveryThread = threading.Thread(target=self.discovery, args=[])
discoveryThread.start()
def status(self):
while True:
print('Current Connections:')
for peer in self.p2p.peers:
print(' ' + str(peer.ip) + ':' + str(peer.port))
time.sleep(30)
def discovery(self):
while True:
handshakeMessage = self.handshakeMessage()
self.p2p.broadcast(handshakeMessage)
time.sleep(10)
def handshake(self, node):
self.p2p.send(node, self.handshakeMessage())
def handshakeMessage(self):
encodedMessage = BlockchainUtils.createEncodedMessage(
self.p2p.socketConnector, 'DISCOVERY', self.p2p.peers)
return encodedMessage
def handleMessage(self, message):
peerConnector = message.connector
peersPeerList = message.data
newPeer = True
for peer in self.p2p.peers:
if peer.equals(peerConnector):
newPeer = False
if newPeer:
print("Adding new node " + message.connector.ip+":"+ str(message.connector.port))
self.p2p.peers.append(peerConnector)
for peersPeer in peersPeerList:
knownPeer = False
for peer in self.p2p.peers:
if peer.equals(peersPeer):
knownPeer = True
if not knownPeer and not peersPeer.equals(self.p2p.socketConnector):
print("Connecting to new node " + message.connector.ip+":"+ str(message.connector.port))
self.p2p.connect_with_node(peersPeer.ip, peersPeer.port)