|
| 1 | +from hazelcast.serialization.bits import * |
| 2 | +from hazelcast.protocol.builtin import FixSizedTypesCodec |
| 3 | +from hazelcast.protocol.client_message import OutboundMessage, REQUEST_HEADER_SIZE, create_initial_buffer, RESPONSE_HEADER_SIZE |
| 4 | +from hazelcast.protocol.builtin import StringCodec |
| 5 | +from hazelcast.protocol.builtin import ByteArrayCodec |
| 6 | +from hazelcast.protocol.builtin import ListMultiFrameCodec |
| 7 | +from hazelcast.protocol.codec.custom.address_codec import AddressCodec |
| 8 | +from hazelcast.protocol.builtin import CodecUtil |
| 9 | + |
| 10 | +# hex: 0x000200 |
| 11 | +_REQUEST_MESSAGE_TYPE = 512 |
| 12 | +# hex: 0x000201 |
| 13 | +_RESPONSE_MESSAGE_TYPE = 513 |
| 14 | + |
| 15 | +_REQUEST_UUID_OFFSET = REQUEST_HEADER_SIZE |
| 16 | +_REQUEST_SERIALIZATION_VERSION_OFFSET = _REQUEST_UUID_OFFSET + UUID_SIZE_IN_BYTES |
| 17 | +_REQUEST_INITIAL_FRAME_SIZE = _REQUEST_SERIALIZATION_VERSION_OFFSET + BYTE_SIZE_IN_BYTES |
| 18 | +_RESPONSE_STATUS_OFFSET = RESPONSE_HEADER_SIZE |
| 19 | +_RESPONSE_MEMBER_UUID_OFFSET = _RESPONSE_STATUS_OFFSET + BYTE_SIZE_IN_BYTES |
| 20 | +_RESPONSE_SERIALIZATION_VERSION_OFFSET = _RESPONSE_MEMBER_UUID_OFFSET + UUID_SIZE_IN_BYTES |
| 21 | +_RESPONSE_PARTITION_COUNT_OFFSET = _RESPONSE_SERIALIZATION_VERSION_OFFSET + BYTE_SIZE_IN_BYTES |
| 22 | +_RESPONSE_CLUSTER_ID_OFFSET = _RESPONSE_PARTITION_COUNT_OFFSET + INT_SIZE_IN_BYTES |
| 23 | +_RESPONSE_FAILOVER_SUPPORTED_OFFSET = _RESPONSE_CLUSTER_ID_OFFSET + UUID_SIZE_IN_BYTES |
| 24 | + |
| 25 | + |
| 26 | +def encode_request(cluster_name, credentials, uuid, client_type, serialization_version, client_hazelcast_version, client_name, labels): |
| 27 | + buf = create_initial_buffer(_REQUEST_INITIAL_FRAME_SIZE, _REQUEST_MESSAGE_TYPE) |
| 28 | + FixSizedTypesCodec.encode_uuid(buf, _REQUEST_UUID_OFFSET, uuid) |
| 29 | + FixSizedTypesCodec.encode_byte(buf, _REQUEST_SERIALIZATION_VERSION_OFFSET, serialization_version) |
| 30 | + StringCodec.encode(buf, cluster_name) |
| 31 | + ByteArrayCodec.encode(buf, credentials) |
| 32 | + StringCodec.encode(buf, client_type) |
| 33 | + StringCodec.encode(buf, client_hazelcast_version) |
| 34 | + StringCodec.encode(buf, client_name) |
| 35 | + ListMultiFrameCodec.encode(buf, labels, StringCodec.encode, True) |
| 36 | + return OutboundMessage(buf, True) |
| 37 | + |
| 38 | + |
| 39 | +def decode_response(msg): |
| 40 | + initial_frame = msg.next_frame() |
| 41 | + response = dict() |
| 42 | + response["status"] = FixSizedTypesCodec.decode_byte(initial_frame.buf, _RESPONSE_STATUS_OFFSET) |
| 43 | + response["member_uuid"] = FixSizedTypesCodec.decode_uuid(initial_frame.buf, _RESPONSE_MEMBER_UUID_OFFSET) |
| 44 | + response["serialization_version"] = FixSizedTypesCodec.decode_byte(initial_frame.buf, _RESPONSE_SERIALIZATION_VERSION_OFFSET) |
| 45 | + response["partition_count"] = FixSizedTypesCodec.decode_int(initial_frame.buf, _RESPONSE_PARTITION_COUNT_OFFSET) |
| 46 | + response["cluster_id"] = FixSizedTypesCodec.decode_uuid(initial_frame.buf, _RESPONSE_CLUSTER_ID_OFFSET) |
| 47 | + response["failover_supported"] = FixSizedTypesCodec.decode_boolean(initial_frame.buf, _RESPONSE_FAILOVER_SUPPORTED_OFFSET) |
| 48 | + response["address"] = CodecUtil.decode_nullable(msg, AddressCodec.decode) |
| 49 | + response["server_hazelcast_version"] = StringCodec.decode(msg) |
| 50 | + return response |
0 commit comments