def _network_recv(self): """ Receive Kinetic message, response, and value from network (Kinetic device). Args: Returns: Message (message header), Command (proto response), String (Value) """ msg = self._fast_read(9) magic, proto_ln, value_ln = struct.unpack_from(">bii", buffer(msg)) # read proto message raw_proto = self._fast_read(proto_ln) value = '' if value_ln > 0: value = self._fast_read(value_ln) msg = kinetic_pb2.Message() msg.ParseFromString(str(raw_proto)) cmd = kinetic_pb2.Command() cmd.ParseFromString(msg.commandBytes) # update connectionId to whatever the drive said. if cmd.header.connectionID: self._connection_id = cmd.header.connectionID return msg, cmd, value
def _recv_delimited_v2(self): # receive the leading 9 bytes if self.wait_on_read: self.wait_on_read.wait() self.wait_on_read = None msg = self.fast_read(9) magic, proto_ln, value_ln = struct.unpack_from(">bii", buffer(msg)) if magic!= 70: LOG.warn("Magic number = {0}".format(magic)) raise common.KineticClientException("Invalid Magic Value!") # 70 = 'F' # read proto message raw_proto = self.fast_read(proto_ln) value = '' if value_ln > 0: if self.defer_read: # let user handle the read from socket value = common.DeferedValue(self.socket, value_ln) self.wait_on_read = value else: # normal code path, read value value = self.fast_read(value_ln) proto = messages.Message() proto.ParseFromString(str(raw_proto)) return (proto, value)
def _client_error_callback(self, status_code, status_msg): msg = kinetic_pb2.Message() msg.authType = kinetic_pb2.Message.INVALID_AUTH_TYPE cmd = kinetic_pb2.Command() cmd.status.statusMessage = "client reported."+status_msg cmd.status.code = status_code cmd.header.messageType = kinetic_pb2.Command.INVALID_MESSAGE_TYPE self._on_response(msg, cmd, "")
def authenticate(self, command): m = messages.Message() m.commandBytes = command.SerializeToString() if self.pin != None: m.authType = messages.Message.PINAUTH m.pinAuth.pin = self.pin else: # Hmac m.authType = messages.Message.HMACAUTH m.hmacAuth.identity = self.identity m.hmacAuth.hmac = calculate_hmac(self.secret, command) return m
def pin_operation(pin=None, **kwargs): """ PIN AUTH operations Args: pin(str): Pin for the pin based commands. """ if not self.is_connected and self.auto_reconnect_count < 1: self._client_error_callback(kinetic_pb2.Command.Status.NOT_ATTEMPTED, "call connect() before the operation") else: self._queue_semaphore.acquire() command_start_time = time.time() m = kinetic_pb2.Message() m.authType = kinetic_pb2.Message.PINAUTH if pin is not None: m.pinAuth.pin = pin cmd = kinetic_pb2.Command() cmd, _, message_build_time = self._build_message(cmd, kinetic_pb2.Command.PINOP, **kwargs) cmd.body.pinOp.pinOpType = pin_operation.__number__ self._network_send(m, cmd, "", command_start_time, message_build_time)
def _build_packet(self, m, command, value): """ Helper method that builds the network packets from the Kinetic objects. Args: m (Message): Message is an authorization and command bytes. command(Command): The Kinetic Command object. value(str): The value, during a put operation, that is being sent over. The kinetic message and the value are sent in different packets. Returns: bytearry: The kinetic packet, Message: message authorization """ command_bytes = command.SerializeToString() if not m: m = kinetic_pb2.Message() m.authType = kinetic_pb2.Message.HMACAUTH m.hmacAuth.identity = self.identity # zf: hack disble hmac on send if not _disable_hmac: m.hmacAuth.hmac = self._calculate_hmac(command_bytes) m.commandBytes = command_bytes # build message (without value) to write out = m.SerializeToString() value_ln = 0 if value: value_ln = len(value) buff = struct.pack(">Bii", ord('F'), len(out), value_ln) # Send it all in one packet aux = bytearray(buff) aux.extend(out) return aux, m