def __init__( self, ip, port, verbose=False, wallet="poswallet.json", version="", source_ip="", timeout=10, read_timeout=30, ): """ :param ip: :param port: :param verbose: :param wallet: :param version: :param source_ip: :param timeout: in seconds :param read_timeout: in seconds """ self.ip = ip self.port = port self.verbose = verbose self.client_version = version self.source_ip = source_ip self.timeout = timeout self.read_timeout = read_timeout poscrypto.load_keys(wallet, verbose=self.verbose) self.hello_string = poshelpers.hello_string(port=101)
async def action(loop): ip = hn_config.IP port = hn_config.PORT timeout = hn_config.TIMEOUT_1 read_timeout = hn_config.TIMEOUT_1 tcp_client = TCPClient() stream = await tcp_client.connect(ip, port, timeout=timeout) address = hn_config.HN_ADDRESS hello_string = poshelpers.hello_string(port=101, address=address) await com_helpers.async_send_string(commands_pb2.Command.hello, hello_string, stream, ip) msg = await com_helpers.async_receive(stream, ip, timeout=read_timeout) await com_helpers.async_send_void(commands_pb2.Command.getheights, stream, ip) msg = await com_helpers.async_receive(stream, ip, timeout=read_timeout) if msg.command == commands_pb2.Command.getheights: S['heights'] = json.loads(msg.string_value) await com_helpers.async_send_void(commands_pb2.Command.status, stream, ip) msg = await com_helpers.async_receive(stream, ip, timeout=read_timeout) if msg.command == commands_pb2.Command.status: S['status'] = json.loads(msg.string_value) await com_helpers.async_send_void(commands_pb2.Command.gethypernodes, stream, ip) msg = await com_helpers.async_receive(stream, ip, timeout=read_timeout) if msg.command == commands_pb2.Command.gethypernodes: status = json.loads(msg.string_value) S['data'] = status L = len(status) task = [] for i in range(0, L): data = status[i] pingstatus.append(0) pingversion.append("?") task.append(loop.create_task(ping(i, data[1], data[2], timeout))) for i in range(0, L): await task[i] return status
async def ping(i, ip, port, T): timeout = T read_timeout = T try: pingstatus[i] = 0 tcp_client = TCPClient() stream = await tcp_client.connect(ip, port, timeout=timeout) address = hn_config.HN_ADDRESS hello_string = poshelpers.hello_string(port=101, address=address) await com_helpers.async_send_string(commands_pb2.Command.hello, hello_string, stream, ip) msg = await com_helpers.async_receive(stream, ip, timeout=read_timeout) await com_helpers.async_send_void(commands_pb2.Command.status, stream, ip) msg = await com_helpers.async_receive(stream, ip, timeout=read_timeout) if msg.command == commands_pb2.Command.status: data = json.loads(msg.string_value) pingstatus[i] = data['chain']['height'] pingversion[i] = data['instance']['hn_version'] except: pingstatus[i] = 0 pingversion[i] = "?"