Example #1
0
 def encode(self, packet: Packet) -> bytearray:
     """
     Python object (PiCN's internal representation) to NDN TLV wire format
     :param packet: Packet in PiCN's representation
     :return: Packet in NDN TLV representation
     """
     if isinstance(packet, Interest):
         self.logger.info("Encode interest")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_interest(packet.name)
     if isinstance(packet, Content):
         self.logger.info("Encode content object")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_data(packet.name, packet.get_bytes())
     if isinstance(packet, Nack):
         self.logger.info("Encode NACK")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_nack(packet.name, packet.reason, packet.interest)
     if isinstance(packet, UnknownPacket):
         self.logger.info("Encode UnknownPacket")
         return packet.wire_format
Example #2
0
 def encode(self, packet: Packet) -> bytearray:
     if isinstance(packet, Interest):
         self.logger.info("Encode interest")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_interest(packet.name)
     if isinstance(packet, Content):
         self.logger.info("Encode content object")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_data(packet.name, packet.get_bytes())
     if isinstance(packet, Nack):
         self.logger.info("Encode NACK")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_nack(packet.name, packet.reason,
                                     packet.interest)
     if isinstance(packet, Heartbeat):
         self.logger.info("Encode Heartbeat")
         if isinstance(packet.wire_format, bytes):
             return packet.wire_format
         else:
             return self.encode_heartbeat(packet.name)
     if isinstance(packet, UnknownPacket):
         self.logger.info("Encode UnknownPacket")
         return packet.wire_format
Example #3
0
 def test_packet_equal(self):
     """Test if two packet objects are equal"""
     p1 = Packet("/test/data")
     p1.name_payload = "Hello World"
     p2 = Packet("/test/data")
     p2.name_payload = "Hello World"
     self.assertEqual(p1, p2)
Example #4
0
    def test_packet_not_equal_name(self):
        """Test if two packet objects are not equal: name"""
        p1 = Packet("/test/data")
        p2 = Packet("/test/data1")
        self.assertNotEqual(p1, p2)

    # disabled name payload in compare, since it breaks 1 content <-> 1 interest
    # def test_packet_not_equal_namepayload(self):
    #     """Test if two packet objects are not equal: payload"""
    #     p1 = Packet("/test/data")
    #     p1.name_payload = "Hello World"
    #     p2 = Packet("/test/data")
    #     p2.name_payload = "Hello World2"
    #     self.assertNotEqual(p1, p2)
    #
    #
    # def test_packet_not_equal_namepayload_not_available(self):
    #     """Test if two packet objects are not equal: payload once not available"""
    #     p1 = Packet("/test/data")
    #     p1.name_payload = "Hello World"
    #     p2 = Packet("/test/data")
    #     self.assertNotEqual(p1, p2)
Example #5
0
 def __init__(self, name=None, wire_format=None):
     Packet.__init__(self, name, wire_format)
     assert (type(self._wire_format)
             in [bytes, bytearray, type(None)]), "MUST be raw bytes or None"
Example #6
0
 def test_packet_not_equal_name(self):
     """Test if two packet objects are not equal: name"""
     p1 = Packet("/test/data")
     p2 = Packet("/test/data1")
     self.assertNotEqual(p1, p2)