Skip to content

Commit

Permalink
slight refactor of p2pnet
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Houwu committed Apr 8, 2014
1 parent 3d15673 commit d4d28ed
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions pyethereum/p2pnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def remove_peer(self, peer):
self.connected_peers.remove(peer)
# connect new peers if there are no candidates


def connect_peer(self, host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Expand Down Expand Up @@ -218,11 +217,12 @@ def _poll_more_candidates(self):
with peer.lock:
peer.protocol.send_GetPeers(peer)

def manage_connections(self):
def _connect_peers(self):
num_peers = self.config.getint('network', 'num_peers')
candidates = self._peer_candidates()
if len(self.connected_peers) < num_peers:
logger.debug('not enough peers: {0}'.format(len(self.connected_peers)))
logger.debug('not enough peers: {0}'.format(
len(self.connected_peers)))
logger.debug('num candidates: {0}'.format(len(candidates)))
if len(candidates):
ip, port, node_id = candidates.pop()
Expand All @@ -232,32 +232,38 @@ def manage_connections(self):
else:
self._poll_more_candidates()

def _check_alive(self, peer):
now = time.time()
dt_ping = now - peer.last_pinged
dt_seen = now - peer.last_valid_packet_received
# if ping was sent and not returned within last second
if dt_ping < dt_seen and dt_ping > self.max_ping_wait:
logger.debug(
'{0} last ping: {1} last seen: {2}'
.format(peer, dt_ping, dt_seen))
logger.debug(
'{0} did not respond to ping, disconnecting {1}:{2}'
.format(peer, peer.ip, peer.port))
self.remove_peer(peer)
elif min(dt_seen, dt_ping) > self.max_silence:
# ping silent peer
logger.debug('pinging silent peer {0}'.format(peer))

with peer.lock:
peer.protocol.send_Ping(peer)
peer.last_pinged = now

def manage_connections(self):
self._connect_peers()

for peer in list(self.connected_peers):
if peer.stopped():
self.remove_peer(peer)
continue

now = time.time()
dt_ping = now - peer.last_pinged
dt_seen = now - peer.last_valid_packet_received
# if ping was sent and not returned within last second
if dt_ping < dt_seen and dt_ping > self.max_ping_wait:
logger.debug(
'{0} last ping: {1} last seen: {2}'
.format(peer, dt_ping, dt_seen))
logger.debug(
'{0} did not respond to ping, disconnecting {1}:{2}'
.format(peer, peer.ip, peer.port))
self.remove_peer(peer)
elif min(dt_seen, dt_ping) > self.max_silence:
# ping silent peer
logger.debug('pinging silent peer {0}'.format(peer))
logger.debug(
'# connected peers: {0}/{1}'.format(len(self.connected_peers), num_peers))
self._check_alive(peer)

with peer.lock:
peer.protocol.send_Ping(peer)
peer.last_pinged = now
now = time.time()

# ask for peers
if now - peer.last_asked_for_peers >\
Expand Down

0 comments on commit d4d28ed

Please sign in to comment.