def calc_latency(self, node_to_calc): """ calculate latency of given node, remove if node is not connected """ start = time.clock() success = False try: for i in range(5): node_to_calc.client.ping() node_to_calc.latency = ( (time.clock() - start) / 5) * 1000 # converting to ms success = True except: # node not connected print(str(sys.exc_info())) logger().warning( "error attempting to ping an unregistered node: disconnecting node" ) if node_to_calc in self.connections: # if a registered node disconnects self.connections.remove(node_to_calc) self.remove_from_peer_dict(node_to_calc) try: net_dao.update_failed_ping(node_to_calc) except Exception as ex: template = "An exception of type {0} occured. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) logger().warning(message) return success
def discover_node(self, node_to_discover): """ discover node, get_verifications node info from unregistered nodes (updating unregistered nodes) """ if self.connect_thrift_node(node_to_discover): logger().info('successfully connected to unregistered node port: %s', node_to_discover.port) try: # if able to connect to unregistered node if self.calc_latency(node_to_discover): net_dao.update_con_activity(node_to_discover) finally: # disconnect node logger().info('disconnecting unregistered node port: %s', node_to_discover.port) self.disconnect_node(node_to_discover) else: # failed to ping node (not connected) net_dao.update_failed_ping(node_to_discover) logger().warning('unsuccessful connection attempt to unregistered node port: %s', node_to_discover.port)
def calc_latency(self, node_to_calc): """ calculate latency of given node, remove if node is not connected """ start = time.clock() success = False try: for i in range(5): node_to_calc.client.ping() node_to_calc.latency = ((time.clock() - start) / 5) * 1000 # converting to ms success = True except: # node not connected print(str(sys.exc_info())) logger().warning("error attempting to ping an unregistered node: disconnecting node") if node_to_calc in self.connections: # if a registered node disconnects self.connections.remove(node_to_calc) self.remove_from_peer_dict(node_to_calc) try: net_dao.update_failed_ping(node_to_calc) except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) logger().warning(message) return success