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 handle_read(self):
        start_cycle_time = time.time()
        bytes_read = 0
        num_pkt_read = 0
        read_pkt_list = []

        read_batch_time_sec = self.socket_options.nw_read_batch_time_ms * constants.MS_TO_SEC
        read_batch_size_bytes = self.socket_options.nw_read_batch_size_bytes

        while (time.time() - start_cycle_time - read_batch_time_sec) < 0 and \
                bytes_read < read_batch_size_bytes:
            if self.incomplete_pkt is None:
                # incomplete packet doesn't exist
                pkt = HeronProtocol.read_new_packet(self)
                pkt.read(self)
            else:
                # continue reading into the incomplete packet
                Log.debug("In handle_read(): Continue reading")
                pkt = self.incomplete_pkt
                pkt.read(self)

            if pkt.is_complete:
                num_pkt_read += 1
                bytes_read += pkt.get_pktsize()
                Log.debug("Read a complete packet of size %d" % bytes_read)
                self.incomplete_pkt = None
                read_pkt_list.append(pkt)
            else:
                Log.debug("In handle_read(): Packet read not yet complete")
                self.incomplete_pkt = pkt
                break

        self.total_bytes_received += bytes_read
        self.total_pkt_received += num_pkt_read

        for pkt in read_pkt_list:
            self._handle_packet(pkt)
Beispiel #5
0
  def handle_read(self):
    start_cycle_time = time.time()
    bytes_read = 0
    num_pkt_read = 0
    read_pkt_list = []

    read_batch_time_sec = self.socket_options.nw_read_batch_time_ms * constants.MS_TO_SEC
    read_batch_size_bytes = self.socket_options.nw_read_batch_size_bytes

    while (time.time() - start_cycle_time - read_batch_time_sec) < 0 and \
            bytes_read < read_batch_size_bytes:
      if self.incomplete_pkt is None:
        # incomplete packet doesn't exist
        pkt = HeronProtocol.read_new_packet(self)
      else:
        # continue reading into the incomplete packet
        Log.debug("In handle_read(): Continue reading")
        pkt = self.incomplete_pkt
        pkt.read(self)

      if pkt.is_complete:
        num_pkt_read += 1
        bytes_read += pkt.get_pktsize()
        Log.debug("Read a complete packet of size %d" % bytes_read)
        self.incomplete_pkt = None
        read_pkt_list.append(pkt)
      else:
        Log.debug("In handle_read(): Packet read not yet complete")
        self.incomplete_pkt = pkt
        break

    self.total_bytes_received += bytes_read
    self.total_pkt_received += num_pkt_read

    for pkt in read_pkt_list:
      self._handle_packet(pkt)
Beispiel #6
0
 def test_fail_decode_packet(self):
   packet = mock_generator.get_fail_packet()
   with self.assertRaises(RuntimeError):
     HeronProtocol.decode_packet(packet)