Exemplo n.º 1
0
 def __init__(self, elementListener):
     self._elementListener = elementListener
     self._binaryXmlStructureDecoder = BinaryXmlStructureDecoder()
     self._tlvStructureDecoder = TlvStructureDecoder()
     self._usePartialData = False
     self._partialData = DynamicByteArray(1000)
     self._useTlv = None
Exemplo n.º 2
0
 def __init__(self, elementListener):
     self._elementListener = elementListener
     self._binaryXmlStructureDecoder = BinaryXmlStructureDecoder()
     self._tlvStructureDecoder = TlvStructureDecoder()
     self._usePartialData = False
     self._partialData = DynamicByteArray(1000)
     self._useTlv = None
Exemplo n.º 3
0
    def onReceivedData(self, data):
        """
        Continue to read data until the end of an element, then call
        elementListener.onReceivedElement(element). The buffer passed to
        onReceivedElement is only valid during this call.  If you need the data
        later, you must copy.

        :param data: The buffer with the incoming element's bytes.
        :type data: An array type with int elements
        """
        # Create a Blob and take its buf() since this creates a memoryview
        #   which is more efficient for slicing.
        data = Blob(data, False).buf()

        # Process multiple objects in the data.
        while True:
            try:
                if not self._usePartialData:
                    # This is the beginning of an element.
                    if len(data) <= 0:
                        # Wait for more data.
                        return

                # Scan the input to check if a whole TLV element has been read.
                self._tlvStructureDecoder.seek(0)
                gotElementEnd = self._tlvStructureDecoder.findElementEnd(data)
                offset = self._tlvStructureDecoder.getOffset()
            except ValueError as ex:
                # Reset to read a new element on the next call.
                self._usePartialData = False
                self._tlvStructureDecoder = TlvStructureDecoder()

                raise ex

            if gotElementEnd:
                # Got the remainder of an element. Report to the caller.
                if self._usePartialData:
                    # We have partial data from a previous call, so append this
                    #   data and use partialData for onReceivedElement.
                    self._partialData.copy(
                      data[:offset], self._partialDataLength)
                    self._partialDataLength += offset

                    # Create a Blob and take its buf() since this creates a
                    #   memoryview which is more efficient for slicing.
                    partialDataView = Blob(
                      self._partialData.getArray(), False).buf()
                    element = partialDataView[:self._partialDataLength]
                    # Assume we don't need to use partialData anymore until
                    #   needed.
                    self._usePartialData = False
                else:
                    # We are not using partialData, so just point to the input
                    #   data buffer.
                    element = data[:offset]

                # Reset to read a new object. Do this before calling
                # onReceivedElement in case it throws an exception.
                data = data[offset:]
                self._tlvStructureDecoder = TlvStructureDecoder()

                self._elementListener.onReceivedElement(element)
                if len(data) == 0:
                    # No more data in the packet.
                    return

                # else loop back to decode.
            else:
                # Save remaining data for a later call.
                if not self._usePartialData:
                    self._usePartialData = True
                    self._partialDataLength = 0

                if self._partialDataLength + len(data) > Common.MAX_NDN_PACKET_SIZE:
                    # Reset to read a new element on the next call.
                    self._usePartialData = False
                    self._tlvStructureDecoder = TlvStructureDecoder()

                    raise ValueError(
                      "The incoming packet exceeds the maximum limit Face.getMaxNdnPacketSize()")

                self._partialData.copy(data, self._partialDataLength)
                self._partialDataLength += len(data)

                return
Exemplo n.º 4
0
    def onReceivedData(self, data):
        """
        Continue to read data until the end of an element, then call
        elementListener.onReceivedElement(element). The buffer passed to
        onReceivedElement is only valid during this call.  If you need the data
        later, you must copy.

        :param data: The buffer with the incoming element's bytes.
        :type data: An array type with int elements
        """
        # Create a Blob and take its buf() since this creates a memoryview
        #   which is more efficient for slicing.
        data = Blob(data, False).buf()

        # Process multiple objects in the data.
        while True:
            try:
                if not self._usePartialData:
                    # This is the beginning of an element.
                    if len(data) <= 0:
                        # Wait for more data.
                        return

                # Scan the input to check if a whole TLV element has been read.
                self._tlvStructureDecoder.seek(0)
                gotElementEnd = self._tlvStructureDecoder.findElementEnd(data)
                offset = self._tlvStructureDecoder.getOffset()
            except ValueError as ex:
                # Reset to read a new element on the next call.
                self._usePartialData = False
                self._tlvStructureDecoder = TlvStructureDecoder()

                raise ex

            if gotElementEnd:
                # Got the remainder of an element. Report to the caller.
                if self._usePartialData:
                    # We have partial data from a previous call, so append this
                    #   data and use partialData for onReceivedElement.
                    self._partialData.copy(
                      data[:offset], self._partialDataLength)
                    self._partialDataLength += offset

                    # Create a Blob and take its buf() since this creates a
                    #   memoryview which is more efficient for slicing.
                    partialDataView = Blob(
                      self._partialData.getArray(), False).buf()
                    element = partialDataView[:self._partialDataLength]
                    # Assume we don't need to use partialData anymore until
                    #   needed.
                    self._usePartialData = False
                else:
                    # We are not using partialData, so just point to the input
                    #   data buffer.
                    element = data[:offset]

                # Reset to read a new object. Do this before calling
                # onReceivedElement in case it throws an exception.
                data = data[offset:]
                self._tlvStructureDecoder = TlvStructureDecoder()

                self._elementListener.onReceivedElement(element)
                if len(data) == 0:
                    # No more data in the packet.
                    return

                # else loop back to decode.
            else:
                # Save remaining data for a later call.
                if not self._usePartialData:
                    self._usePartialData = True
                    self._partialDataLength = 0

                if self._partialDataLength + len(data) > Common.MAX_NDN_PACKET_SIZE:
                    # Reset to read a new element on the next call.
                    self._usePartialData = False
                    self._tlvStructureDecoder = TlvStructureDecoder()

                    raise ValueError(
                      "The incoming packet exceeds the maximum limit Face.getMaxNdnPacketSize()")

                self._partialData.copy(data, self._partialDataLength)
                self._partialDataLength += len(data)

                return
Exemplo n.º 5
0
    def onReceivedData(self, data):
        """
        Continue to read data until the end of an element, then call 
        elementListener.onReceivedElement(element). The buffer passed to 
        onReceivedElement is only valid during this call.  If you need the data 
        later, you must copy.
 
        :param data: The buffer with the incoming element's bytes.
        :type data: An array type with int elements
        """
        # Create a Blob and take its buf() since this creates a memoryview
        #   which is more efficient for slicing.
        data = Blob(data, False).buf()

        # Process multiple objects in the data.
        while True:
            if not self._usePartialData:
                # This is the beginning of an element. Check whether it is 
                #  Binary XML or TLV.
                if len(data) <= 0:
                    # Wait for more data.
                    return

                # The type codes for TLV Interest and Data packets are chosen to not
                #   conflict with the first byte of a binary XML packet, so we can
                #   just look at the first byte.
                if (data[0] == Tlv.Interest or data[0] == Tlv.Data or 
                    data[0] == 0x80):
                    self._useTlv = True
                else:
                    # Binary XML.
                    self._useTlv = False

            if self._useTlv:
                # Scan the input to check if a whole TLV element has been read.
                self._tlvStructureDecoder.seek(0)    
                gotElementEnd = self._tlvStructureDecoder.findElementEnd(data)
                offset = self._tlvStructureDecoder.getOffset()
            else:
                # Scan the input to check if a whole Binary XML element has been 
                #   read.
                self._binaryXmlStructureDecoder.seek(0)    
                gotElementEnd = self._binaryXmlStructureDecoder.findElementEnd(data)
                offset = self._binaryXmlStructureDecoder.getOffset()
            
            if gotElementEnd:
                # Got the remainder of an element. Report to the caller.
                if self._usePartialData:
                    # We have partial data from a previous call, so append this 
                    #   data and use partialData for onReceivedElement.
                    self._partialData.copy(
                      data[:offset], self._partialDataLength)
                    self._partialDataLength += offset

                    # Create a Blob and take its buf() since this creates a 
                    #   memoryview which is more efficient for slicing.
                    partialDataView = Blob(
                      self._partialData.getArray(), False).buf()
                    self._elementListener.onReceivedElement(
                      partialDataView[:self._partialDataLength])
                    # Assume we don't need to use partialData anymore until 
                    #   needed.
                    self._usePartialData = False
                else:
                    # We are not using partialData, so just point to the input 
                    #   data buffer.
                    self._elementListener.onReceivedElement(data[:offset])

                # Need to read a new object.
                data = data[offset:]
                self._binaryXmlStructureDecoder = BinaryXmlStructureDecoder()
                self._tlvStructureDecoder = TlvStructureDecoder()
                if len(data) == 0:
                    # No more data in the packet.
                    return

                # else loop back to decode.
            else:
                # Save remaining data for a later call.
                if not self._usePartialData:
                    self._usePartialData = True
                    self._partialDataLength = 0

                self._partialData.copy(data, self._partialDataLength)
                self._partialDataLength += len(data)

                return
Exemplo n.º 6
0
    def onReceivedData(self, data):
        """
        Continue to read data until the end of an element, then call 
        elementListener.onReceivedElement(element). The buffer passed to 
        onReceivedElement is only valid during this call.  If you need the data 
        later, you must copy.
 
        :param data: The buffer with the incoming element's bytes.
        :type data: An array type with int elements
        """
        # Create a Blob and take its buf() since this creates a memoryview
        #   which is more efficient for slicing.
        data = Blob(data, False).buf()

        # Process multiple objects in the data.
        while True:
            if not self._usePartialData:
                # This is the beginning of an element. Check whether it is
                #  Binary XML or TLV.
                if len(data) <= 0:
                    # Wait for more data.
                    return

                # The type codes for TLV Interest and Data packets are chosen to not
                #   conflict with the first byte of a binary XML packet, so we can
                #   just look at the first byte.
                if (data[0] == Tlv.Interest or data[0] == Tlv.Data
                        or data[0] == 0x80):
                    self._useTlv = True
                else:
                    # Binary XML.
                    self._useTlv = False

            if self._useTlv:
                # Scan the input to check if a whole TLV element has been read.
                self._tlvStructureDecoder.seek(0)
                gotElementEnd = self._tlvStructureDecoder.findElementEnd(data)
                offset = self._tlvStructureDecoder.getOffset()
            else:
                # Scan the input to check if a whole Binary XML element has been
                #   read.
                self._binaryXmlStructureDecoder.seek(0)
                gotElementEnd = self._binaryXmlStructureDecoder.findElementEnd(
                    data)
                offset = self._binaryXmlStructureDecoder.getOffset()

            if gotElementEnd:
                # Got the remainder of an element. Report to the caller.
                if self._usePartialData:
                    # We have partial data from a previous call, so append this
                    #   data and use partialData for onReceivedElement.
                    self._partialData.copy(data[:offset],
                                           self._partialDataLength)
                    self._partialDataLength += offset

                    # Create a Blob and take its buf() since this creates a
                    #   memoryview which is more efficient for slicing.
                    partialDataView = Blob(self._partialData.getArray(),
                                           False).buf()
                    self._elementListener.onReceivedElement(
                        partialDataView[:self._partialDataLength])
                    # Assume we don't need to use partialData anymore until
                    #   needed.
                    self._usePartialData = False
                else:
                    # We are not using partialData, so just point to the input
                    #   data buffer.
                    self._elementListener.onReceivedElement(data[:offset])

                # Need to read a new object.
                data = data[offset:]
                self._binaryXmlStructureDecoder = BinaryXmlStructureDecoder()
                self._tlvStructureDecoder = TlvStructureDecoder()
                if len(data) == 0:
                    # No more data in the packet.
                    return

                # else loop back to decode.
            else:
                # Save remaining data for a later call.
                if not self._usePartialData:
                    self._usePartialData = True
                    self._partialDataLength = 0

                self._partialData.copy(data, self._partialDataLength)
                self._partialDataLength += len(data)

                return