示例#1
0
 def __init__(
     self,
     protocol_context: 'ProtocolContext',
     username: Optional[str] = None,
     password: Optional[str] = None,
     timezone: str = None,
 ):
     fields = [
         ('length', Int),
         ('op_code', Byte),
         ('version_major', Short),
         ('version_minor', Short),
         ('version_patch', Short),
         ('client_code', Byte),
     ]
     self.protocol_context = protocol_context
     self.timezone = timezone
     if self.protocol_context.is_feature_flags_supported():
         fields.append(('features', ByteArrayObject))
     if self.protocol_context.is_user_attributes_supported():
         fields.append(('user_attributes', MapObject))
     if username and password:
         self.username = username
         self.password = password
         fields.extend([
             ('username', String),
             ('password', String),
         ])
     self.handshake_struct = Struct(fields)
示例#2
0
    def read_response(self) -> Union[dict, OrderedDict]:
        """
        Processes server's response to the handshake request.

        :return: handshake data.
        """
        response_start = Struct([
            ('length', Int),
            ('op_code', Byte),
        ])
        start_class, start_buffer = response_start.parse(self)
        start = start_class.from_buffer_copy(start_buffer)
        data = response_start.to_python(start)
        if data['op_code'] == 0:
            response_end = Struct([
                ('version_major', Short),
                ('version_minor', Short),
                ('version_patch', Short),
                ('message', String),
            ])
        else:
            response_end = Struct([
                ('node_uuid', UUIDObject),
            ])
        end_class, end_buffer = response_end.parse(self)
        end = end_class.from_buffer_copy(end_buffer)
        data.update(response_end.to_python(end))
        return data
示例#3
0
    def read_response_130(self):
        """
        Processes server's response to the handshake request (thin protocol
        version 1.2.0).

        :return: handshake data.
        """
        response_start = Struct([
            ('length', Int),
            ('op_code', Byte),
        ])
        start_class, start_buffer = response_start.parse(self)
        start = start_class.from_buffer_copy(start_buffer)
        data = response_start.to_python(start)
        if data['op_code'] == 0:
            response_end = Struct([
                ('version_major', Short),
                ('version_minor', Short),
                ('version_patch', Short),
                ('message', String),
            ])
            end_class, end_buffer = response_end.parse(self)
            end = end_class.from_buffer_copy(end_buffer)
            data.update(response_end.to_python(end))
        return data
示例#4
0
 def __create_response_end(cls, start_data, protocol_context):
     response_end = None
     if start_data['op_code'] == 0:
         response_end = Struct([('version_major', Short),
                                ('version_minor', Short),
                                ('version_patch', Short),
                                ('message', String),
                                ('client_status', Int)])
     elif protocol_context.is_feature_flags_supported():
         response_end = Struct([
             ('features', ByteArrayObject),
             ('node_uuid', UUIDObject),
         ])
     elif protocol_context.is_partition_awareness_supported():
         response_end = Struct([
             ('node_uuid', UUIDObject),
         ])
     return response_end
示例#5
0
 def __init__(self,
              protocol_version: Tuple[int, int, int],
              username: Optional[str] = None,
              password: Optional[str] = None):
     fields = [
         ('length', Int),
         ('op_code', Byte),
         ('version_major', Short),
         ('version_minor', Short),
         ('version_patch', Short),
         ('client_code', Byte),
     ]
     self.protocol_version = protocol_version
     if username and password:
         self.username = username
         self.password = password
         fields.extend([
             ('username', String),
             ('password', String),
         ])
     self.handshake_struct = Struct(fields)
示例#6
0
class HandshakeRequest:
    """ Handshake request. """
    handshake_struct = None
    username = None
    password = None
    protocol_version = None

    def __init__(self,
                 protocol_version: Tuple[int, int, int],
                 username: Optional[str] = None,
                 password: Optional[str] = None):
        fields = [
            ('length', Int),
            ('op_code', Byte),
            ('version_major', Short),
            ('version_minor', Short),
            ('version_patch', Short),
            ('client_code', Byte),
        ]
        self.protocol_version = protocol_version
        if username and password:
            self.username = username
            self.password = password
            fields.extend([
                ('username', String),
                ('password', String),
            ])
        self.handshake_struct = Struct(fields)

    def __bytes__(self) -> bytes:
        handshake_data = {
            'length': 8,
            'op_code': OP_HANDSHAKE,
            'version_major': self.protocol_version[0],
            'version_minor': self.protocol_version[1],
            'version_patch': self.protocol_version[2],
            'client_code': 2,  # fixed value defined by protocol
        }
        if self.username and self.password:
            handshake_data.update({
                'username': self.username,
                'password': self.password,
            })
            handshake_data['length'] += sum([
                10,  # each `String` header takes 5 bytes
                len(self.username),
                len(self.password),
            ])
        return self.handshake_struct.from_python(handshake_data)
示例#7
0
node_partitions = StructArray([
    ('partition_id', Int),
])

node_mapping = StructArray([('node_uuid', UUIDObject),
                            ('node_partitions', node_partitions)])

cache_mapping = StructArray([
    ('cache_id', Int),
    ('cache_config', cache_config),
])

empty_cache_mapping = StructArray([('cache_id', Int)])

empty_node_mapping = Struct([])

partition_mapping = StructArray([
    ('is_applicable', Bool),
    ('cache_mapping',
     Conditional(
         ['is_applicable'],
         lambda ctx: ctx['is_applicable'] and ctx['is_applicable'].value == 1,
         lambda ctx: ctx['is_applicable'], cache_mapping,
         empty_cache_mapping)),
    ('node_mapping',
     Conditional(
         ['is_applicable'],
         lambda ctx: ctx['is_applicable'] and ctx['is_applicable'].value == 1,
         lambda ctx: ctx['is_applicable'], node_mapping, empty_node_mapping)),
])
示例#8
0
class HandshakeRequest:
    """ Handshake request. """
    handshake_struct = None
    username = None
    password = None
    protocol_context = None
    timezone = None

    def __init__(
        self,
        protocol_context: 'ProtocolContext',
        username: Optional[str] = None,
        password: Optional[str] = None,
        timezone: str = None,
    ):
        fields = [
            ('length', Int),
            ('op_code', Byte),
            ('version_major', Short),
            ('version_minor', Short),
            ('version_patch', Short),
            ('client_code', Byte),
        ]
        self.protocol_context = protocol_context
        self.timezone = timezone
        if self.protocol_context.is_feature_flags_supported():
            fields.append(('features', ByteArrayObject))
        if self.protocol_context.is_user_attributes_supported():
            fields.append(('user_attributes', MapObject))
        if username and password:
            self.username = username
            self.password = password
            fields.extend([
                ('username', String),
                ('password', String),
            ])
        self.handshake_struct = Struct(fields)

    def from_python(self, stream):
        self.handshake_struct.from_python(stream,
                                          self.__create_handshake_data())

    async def from_python_async(self, stream):
        await self.handshake_struct.from_python_async(
            stream, self.__create_handshake_data())

    def __create_handshake_data(self):
        version = self.protocol_context.version
        handshake_data = {
            'length': 8,
            'op_code': OP_HANDSHAKE,
            'version_major': version[0],
            'version_minor': version[1],
            'version_patch': version[2],
            'client_code': 2,  # fixed value defined by protocol
        }
        if self.protocol_context.is_feature_flags_supported():
            features = bytes(self.protocol_context.features)
            handshake_data['features'] = features
            handshake_data['length'] += 5 + len(features)
        if self.protocol_context.is_user_attributes_supported():
            user_attributes = (MapObject.HASH_MAP, {
                USER_ATTR_TIMEZONE: self.timezone
            })
            handshake_data['user_attributes'] = user_attributes
            tz_len = 5 + len(self.timezone) if self.timezone else 1
            handshake_data['length'] += sum([
                6,  # Map header and type
                5,  # String header for key
                len(USER_ATTR_TIMEZONE),
                tz_len,
            ])
        if self.username and self.password:
            handshake_data.update({
                'username': self.username,
                'password': self.password,
            })
            handshake_data['length'] += sum([
                10,  # each `String` header takes 5 bytes
                len(self.username),
                len(self.password),
            ])
        return handshake_data
示例#9
0
class HandshakeResponse(dict):
    """
    Handshake response.
    """
    __response_start = Struct([
        ('length', Int),
        ('op_code', Byte),
    ])

    def __init__(self, data):
        super().__init__()
        self.update(data)

    def __getattr__(self, item):
        return self.get(item)

    @classmethod
    def parse(cls, stream, protocol_context):
        start_class = cls.__response_start.parse(stream)
        start = stream.read_ctype(start_class, direction=READ_BACKWARD)
        data = cls.__response_start.to_python(start)

        response_end = cls.__create_response_end(data, protocol_context)
        if response_end:
            end_class = response_end.parse(stream)
            end = stream.read_ctype(end_class, direction=READ_BACKWARD)
            data.update(response_end.to_python(end))

        return cls(data)

    @classmethod
    async def parse_async(cls, stream, protocol_context):
        start_class = cls.__response_start.parse(stream)
        start = stream.read_ctype(start_class, direction=READ_BACKWARD)
        data = await cls.__response_start.to_python_async(start)

        response_end = cls.__create_response_end(data, protocol_context)
        if response_end:
            end_class = await response_end.parse_async(stream)
            end = stream.read_ctype(end_class, direction=READ_BACKWARD)
            data.update(await response_end.to_python_async(end))

        return cls(data)

    @classmethod
    def __create_response_end(cls, start_data, protocol_context):
        response_end = None
        if start_data['op_code'] == 0:
            response_end = Struct([('version_major', Short),
                                   ('version_minor', Short),
                                   ('version_patch', Short),
                                   ('message', String),
                                   ('client_status', Int)])
        elif protocol_context.is_feature_flags_supported():
            response_end = Struct([
                ('features', ByteArrayObject),
                ('node_uuid', UUIDObject),
            ])
        elif protocol_context.is_partition_awareness_supported():
            response_end = Struct([
                ('node_uuid', UUIDObject),
            ])
        return response_end