2022-02-27 19:34:16 +00:00
|
|
|
from gossip.commands import GossipCommands, command_to_byte
|
2022-03-21 06:03:53 +00:00
|
|
|
from .peerset import gossip_peer_set
|
|
|
|
|
2022-02-26 07:07:18 +00:00
|
|
|
import logger
|
|
|
|
|
|
|
|
|
2022-03-21 06:03:53 +00:00
|
|
|
def connect_peer(peer):
|
|
|
|
if peer in gossip_peer_set:
|
2022-02-27 19:34:16 +00:00
|
|
|
return
|
2022-02-26 07:07:18 +00:00
|
|
|
try:
|
2022-04-20 05:28:29 +00:00
|
|
|
s = peer.get_socket(15)
|
2022-02-26 07:07:18 +00:00
|
|
|
except Exception:
|
|
|
|
logger.warn(f"Could not connect to {peer.transport_address}")
|
|
|
|
else:
|
2022-05-20 15:13:12 +00:00
|
|
|
s.sendall(command_to_byte(GossipCommands.PING))
|
|
|
|
if s.recv(5).decode('utf-8') == 'PONG':
|
|
|
|
gossip_peer_set.add(peer)
|
|
|
|
logger.info(f"connected to {peer.transport_address}")
|
2022-02-27 19:34:16 +00:00
|
|
|
s.close()
|