def download_state_from_random_peer(self): """ This will get the state of a random peer. """ random_peer = Prisma().db.get_random_peer() # if list is empty if not random_peer: self.logger.info( 'No peers to connect to. Wait to some peer to connect with you or restart with a peer.' ) self.callLater(2, lambda: self.download_state_from_random_peer()) return random_peer = random_peer.pop() host = random_peer['host'] port = random_peer['port'] client = TCP4ClientEndpoint(self.reactor, host, port, self.timeout) d = client.connect(self.factory) # in case of connection ok, add the callbacks for get_state def connection_ok(protocol): protocol.d = defer.Deferred() def get_state_ok(_): self.logger.info( 'Successfully got the state from {0}:{1}'.format( host, port)) self.status = STATUS_READY protocol.d.addCallback(get_state_ok) def get_state_error(reason): error_message = reason.getErrorMessage() self.logger.error( 'Error when getting state from {0}:{1}: {2}'.format( host, port, error_message)) protocol.close_connection() self.callLater(0, lambda: self.download_state_from_random_peer()) protocol.d.addErrback(get_state_error) protocol.send_get_state() d.addCallback(connection_ok) # in case of connection error show in debug and try again def connection_error(reason): # in case of error remove the peer from the database addr = random_peer['host'] + ':' + str(random_peer['port']) self.logger.debug('Error while connecting to {0}: {1}'.format( addr, reason.getErrorMessage())) Prisma().db.delete_peer(random_peer['_id']) # then call later again self.callLater(0, lambda: self.download_state_from_random_peer()) d.addErrback(connection_error)
def get_peers_from_random_peer(self): """ Gets a random a peer from the database and connects to it and asks for peers. """ # get a random peer from database random_peer = Prisma().db.get_random_peer() # if list is empty if not random_peer: self.logger.info( 'No peers to connect to. Wait to some peer to connect with you or restart with a peer.' ) return random_peer = random_peer.pop() host = random_peer['host'] port = random_peer['port'] client = TCP4ClientEndpoint(self.reactor, host, port, self.timeout) d = client.connect(self.factory) # in case of connection ok, add the callbacks for get_peers and call send_get_peers def connection_ok(protocol): protocol.d = defer.Deferred() def get_peers_ok(_): self.logger.info('Successfully got peers from {0}:{1}'.format( host, port)) protocol.d.addCallback(get_peers_ok) def get_peers_error(reason): error_message = reason.getErrorMessage() self.logger.error( 'Error when getting peers from {0}:{1}: {2}'.format( host, port, error_message)) protocol.close_connection() self.get_peers_lc.reset() self.get_peers_from_random_peer() protocol.d.addErrback(get_peers_error) protocol.send_get_peers() d.addCallback(connection_ok) # in case of connection error show in debug and try again def connection_error(reason): # in case of error remove the peer from the database addr = random_peer['host'] + ':' + str(random_peer['port']) self.logger.debug('Error while connecting to {0}: {1}'.format( addr, reason.getErrorMessage())) Prisma().db.delete_peer(random_peer['_id']) # then restart timer and try again get_peers_from_random_peer self.get_peers_lc.reset() self.get_peers_from_random_peer() d.addErrback(connection_error)