Beispiel #1
0
 def test_encode_decode_packet(self):
   # get_mock_packets() uses OutgoingPacket.create_packet() to encode
   pkt_list, raw_list = mock_generator.get_mock_requst_packets(is_message=False)
   for pkt, raw in zip(pkt_list, raw_list):
     raw_reqid, raw_message = raw
     typename, reqid, seriazelid_msg = HeronProtocol.decode_packet(pkt)
     self.assertEqual(reqid, raw_reqid)
     self.assertEqual(typename, raw_message.DESCRIPTOR.full_name)
     self.assertEqual(seriazelid_msg, raw_message.SerializeToString())
Beispiel #2
0
    def _handle_packet(self, packet):
        # only called when packet.is_complete is True
        # otherwise, it's just an message -- call on_incoming_message()
        typename, reqid, serialized_msg = HeronProtocol.decode_packet(packet)
        if self.context_map.has_key(reqid):
            # this incoming packet has the response of a request
            context = self.context_map.pop(reqid)
            response_msg = self.response_message_map.pop(reqid)

            try:
                response_msg.ParseFromString(serialized_msg)
            except Exception as e:
                Log.error("Invalid Packet Error: %s" % e.message)
                self._handle_close()
                self.on_error()
                return

            if response_msg.IsInitialized():
                self.on_response(StatusCode.OK, context, response_msg)
            else:
                Log.error("Response not initialized")
                self._handle_close()
                self.on_error()
        elif reqid.is_zero():
            # this is a Message -- no need to send back response
            try:
                if typename not in self.registered_message_map:
                    raise ValueError("%s is not registered in message map" %
                                     typename)
                msg_builder = self.registered_message_map[typename]
                message = msg_builder()
                message.ParseFromString(serialized_msg)
                if message.IsInitialized():
                    self.on_incoming_message(message)
                else:
                    raise RuntimeError("Message not initialized")
            except Exception as e:
                Log.error("Error when handling message packet: %s" % e.message)
                Log.error(traceback.format_exc())
                raise RuntimeError("Problem reading message")
        else:
            # might be a timeout response
            Log.info(
                "In handle_packet(): Received message whose REQID is not registered: %s"
                % str(reqid))
Beispiel #3
0
  def _handle_packet(self, packet):
    # only called when packet.is_complete is True
    # otherwise, it's just an message -- call on_incoming_message()
    typename, reqid, serialized_msg = HeronProtocol.decode_packet(packet)
    if self.context_map.has_key(reqid):
      # this incoming packet has the response of a request
      context = self.context_map.pop(reqid)
      response_msg = self.response_message_map.pop(reqid)

      try:
        response_msg.ParseFromString(serialized_msg)
      except Exception as e:
        Log.error("Invalid Packet Error: %s" % e.message)
        self.on_response(StatusCode.INVALID_PACKET, context, None)
        return

      if response_msg.IsInitialized():
        self.on_response(StatusCode.OK, context, response_msg)
      else:
        Log.error("Response not initialized")
        self.on_response(StatusCode.INVALID_PACKET, context, None)
    elif reqid.is_zero():
      # this is a Message -- no need to send back response
      try:
        if typename not in self.registered_message_map:
          raise ValueError("%s is not registered in message map" % typename)
        msg_builder = self.registered_message_map[typename]
        message = msg_builder()
        message.ParseFromString(serialized_msg)
        if message.IsInitialized():
          self.on_incoming_message(message)
        else:
          raise RuntimeError("Message not initialized")
      except Exception as e:
        Log.error("Error when handling message packet: %s" % e.message)
        Log.error(traceback.format_exc())
    else:
      # might be a timeout response
      Log.info("In handle_packet(): Received message whose REQID is not registered: %s"
               % str(reqid))
Beispiel #4
0
 def test_fail_decode_packet(self):
   packet = mock_generator.get_fail_packet()
   with self.assertRaises(RuntimeError):
     HeronProtocol.decode_packet(packet)