def submit_message(conn: Connection, data: bytes) -> (AerospikeOuterHeader, AerospikeASMSGHeader, list, list): ohdr = AerospikeOuterHeader(2, 3, len(data)) buf = pack_outer_header(ohdr) + data yield from conn.open_connection() try: try: yield from conn.write(buf) except ASConnectionError as e: raise ASIOException('write: %r' % e) hdr_payload = yield from conn.read(8, True) if not hdr_payload: raise ASIOException('read') header, _ = unpack_message(hdr_payload) data = hdr_payload data += yield from conn.read(header.sz, False) header, payload = unpack_message(data) asmsg_header, asmsg_fields, asmsg_ops, _ = unpack_asmsg(payload) if asmsg_header.result_code != 0: raise ASMSGProtocolException(asmsg_header.result_code) return header, asmsg_header, asmsg_fields, asmsg_ops finally: conn.close_connection()
def request_info_keys(conn: Connection, commands: list) -> (AerospikeOuterHeader, dict): payload = pack_message('\n'.join(commands).encode('UTF-8'), 1) conn.write(payload) hdr_payload = yield from conn.read(8) header, _ = unpack_message(hdr_payload) message = hdr_payload message += yield from conn.read(header.sz) header, payload = unpack_message(message) lines = payload.decode('UTF-8') infokeys = {} for line in lines.split('\n'): k, _, v = line.partition('\t') if ';' in v: infokeys[k] = v.split(';') else: infokeys[k] = v return header, infokeys
def submit_multi_message(conn: Connection, data: bytes) -> list: ohdr = AerospikeOuterHeader(2, 3, len(data)) buf = pack_outer_header(ohdr) + data yield from conn.open_connection() try: try: yield from conn.write(buf) except ASConnectionError as e: raise ASIOException('write: %r' % e) not_last = True messages = [] while not_last: hdr_payload = yield from conn.read(8, True) if not hdr_payload: raise ASIOException('read') header, _ = unpack_message(hdr_payload) data = hdr_payload data += yield from conn.read(header.sz, False) if len(data) != 8 + header.sz: raise ASIOException('read') header, payload = unpack_message(data) while payload: asmsg_header, asmsg_fields, asmsg_ops, payload = unpack_asmsg(payload) messages += [(header, asmsg_header, asmsg_fields, asmsg_ops)] if asmsg_header.result_code not in (0, 2): raise ASMSGProtocolException(asmsg_header.result_code) if (asmsg_header.info3 & AS_INFO3_LAST) == AS_INFO3_LAST: not_last = False continue return messages finally: conn.close_connection()