Beispiel #1
0
 async def input_packet(self, packet):
     packet = memoryview(packet)
     typ, typ_len = parse_tl_num(packet)
     siz, siz_len = parse_tl_num(packet, typ_len)
     offset = typ_len + siz_len
     assert len(packet) == offset + siz
     await self.callback(typ, packet)
Beispiel #2
0
    async def _receive(self, typ: int, data: BinaryStr):
        """
        Pipeline when a packet is received.

        :param typ: the Type.
        :param data: the Value of the packet with TL.
        """
        logging.debug('Packet received %s, %s' % (typ, bytes(data)))
        if typ == LpTypeNumber.LP_PACKET:
            try:
                nack_reason, fragment = parse_lp_packet(data, with_tl=True)
            except (DecodeError, TypeError, ValueError, struct.error):
                logging.warning('Unable to decode received packet')
                return
            data = fragment
            typ, _ = parse_tl_num(data)
        else:
            nack_reason = None

        if nack_reason is not None:
            try:
                name, _, _, _ = parse_interest(data, with_tl=True)
            except (DecodeError, TypeError, ValueError, struct.error):
                logging.warning('Unable to decode the fragment of LpPacket')
                return
            logging.debug('NetworkNack received %s, reason=%s' %
                          (Name.to_str(name), nack_reason))
            self._on_nack(name, nack_reason)
        else:
            if typ == TypeNumber.INTEREST:
                try:
                    name, param, app_param, sig = parse_interest(data,
                                                                 with_tl=True)
                except (DecodeError, TypeError, ValueError, struct.error):
                    logging.warning('Unable to decode received packet')
                    return
                logging.debug('Interest received %s' % Name.to_str(name))
                await self._on_interest(name,
                                        param,
                                        app_param,
                                        sig,
                                        raw_packet=data)
            elif typ == TypeNumber.DATA:
                try:
                    name, meta_info, content, sig = parse_data(data,
                                                               with_tl=True)
                except (DecodeError, TypeError, ValueError, struct.error):
                    logging.warning('Unable to decode received packet')
                    return
                logging.debug('Data received %s' % Name.to_str(name))
                await self._on_data(name,
                                    meta_info,
                                    content,
                                    sig,
                                    raw_packet=data)
            else:
                logging.warning('Unable to decode received packet')
Beispiel #3
0
 def parse(buf):
     # Verify the Type
     typ, pos = parse_tl_num(buf)
     if typ != StateVectorModelTypes.VECTOR.value:
         return None
     # Check the length
     length, l = parse_tl_num(buf, pos)
     pos += l
     if pos + length != len(buf):
         return None
     # Decode components
     ret = StateVectorModel()
     ret.value = []
     while pos < len(buf):
         # Node ID
         typ, l = parse_tl_num(buf, pos)
         pos += l
         if typ != StateVectorModelTypes.KEY.value:
             return None
         length, l = parse_tl_num(buf, pos)
         pos += l
         node_id = buf[pos:pos + length]
         pos += length
         # Value
         typ, l = parse_tl_num(buf, pos)
         pos += l
         if typ != StateVectorModelTypes.VALUE.value:
             return None
         length, l = parse_tl_num(buf, pos)
         pos += l
         if length == 1:
             value = struct.unpack_from('!B', buf, pos)[0]
         elif length == 2:
             value = struct.unpack_from('!H', buf, pos)[0]
         elif length == 4:
             value = struct.unpack_from('!I', buf, pos)[0]
         elif length == 8:
             value = struct.unpack_from('!Q', buf, pos)[0]
         else:
             return None
         pos += length
         # Append the component
         comp = StateVectorComponentModel()
         comp.node_id = node_id
         comp.seq_num = value
         ret.value.append(comp)
     return ret
Beispiel #4
0
 def parse(buf: Component) -> Optional[StateVectorModel]:
     # Verify the Type
     typ, pos = parse_tl_num(buf)
     if typ != SVSyncTlvTypes.VECTOR.value:
         return None
     # Check the length
     length, l = parse_tl_num(buf, pos)
     pos += l
     if pos + length != len(buf):
         return None
     # Decode components
     ret: StateVectorModel = StateVectorModel([])
     ret.value = []
     while pos < len(buf):
         # Entry ID
         typ, l = parse_tl_num(buf, pos)
         pos += l
         if typ != SVSyncTlvTypes.VECTOR_ENTRY.value:
             return None
         length, l = parse_tl_num(buf, pos)
         pos += l
         entry = buf[pos:pos + length]
         pos += length
         # Seqno
         typ, l = parse_tl_num(buf, pos)
         pos += l
         if typ != SVSyncTlvTypes.SEQNO.value:
             return None
         length, l = parse_tl_num(buf, pos)
         pos += l
         if length == 1:
             seqno = unpack_from('!B', buf, pos)[0]
         elif length == 2:
             seqno = unpack_from('!H', buf, pos)[0]
         elif length == 4:
             seqno = unpack_from('!I', buf, pos)[0]
         elif length == 8:
             seqno = unpack_from('!Q', buf, pos)[0]
         else:
             return None
         pos += length
         # Append the component
         ret.value.append(StateVectorEntry(bytes(entry).decode(), seqno))
     return ret
Beispiel #5
0
 def test_3():
     assert parse_tl_num(b'\xfe\xfe\x00\x01\x00\x01', 1) == (65537, 5)
Beispiel #6
0
 def test_2():
     assert parse_tl_num(b'\x00\xfd\x00\xff\x00\x00\x00\x00\x00\x00',
                         1) == (255, 3)
Beispiel #7
0
 def test_1():
     assert parse_tl_num(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') == (0,
                                                                          1)
Beispiel #8
0
 def test_4():
     assert parse_tl_num(b'\xff\x00\x00\x00\x01*\x05\xf2\x00') == (
         5000000000, 9)