コード例 #1
0
ファイル: udp_transport.py プロジェクト: peurpdapeurp/PyNDN2
    def connect(self, connectionInfo, elementListener, onConnected):
        """
        Connect according to the info in connectionInfo, and use
        elementListener.

        :param UdpTransport.ConnectionInfo connectionInfo: A
          UdpTransport.ConnectionInfo.
        :param elementListener: The elementListener must remain valid during the
          life of this object.
        :type elementListener: An object with onReceivedElement
        :param onConnected: This calls onConnected() when the connection is
          established.
        :type onConnected: function object
        """
        self.close()
        # Save the _address to use in sendto.
        self._address = (connectionInfo.getHost(), connectionInfo.getPort())
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        if connectionInfo.getLocalPort() != None:
            self._socket.bind(("0.0.0.0", connectionInfo.getLocalPort()))

        self._socketPoller = SocketPoller(self._socket)
        self._elementReader = ElementReader(elementListener)

        if onConnected != None:
            onConnected()
コード例 #2
0
    def start(self):
        # init btle ElementReader
        el = BtleNode(self._producer.onBtleData, None, None)
        em = ElementReader(el)

        class MyDelegate(DefaultDelegate):
            def __init__(self):
                DefaultDelegate.__init__(self)
                
            def handleNotification(self, cHandle, data):
                # TODO: this should handle incorrect format caused by packet losses
                try:
                    em.onReceivedData(data[2:])
                except ValueError as e:
                    print "Decoding value error: " + str(e)
                # connect ble
        
        while not self._p:
            try:
                self._p = Peripheral(self._addr, "random")
            except BTLEException as e:
                print "Failed to connect: " + str(e) + "; trying again"
                self._p = None

        # tell rfduino we are ready for notifications
        self._p.setDelegate(MyDelegate())

        self._p.writeCharacteristic(self._p.getCharacteristics(uuid = self._receive_uuid)[0].valHandle + 1, "\x01\x00")
        self._loop.create_task(self.btleNotificationListen())
        
        if self._security:
            # send our public key if configured to do so
            print "security on, sending our public key"
            interest = self._producer.makePublicKeyInterest()
            # write characteristics
            data = interest.wireEncode().toRawStr()
            num_fragments = int(math.ceil(float(len(data)) / 18))
            print "length of data: " + str(len(data)) + "; number of fragments: " + str(num_fragments)
            current = 0
            for i in range(0, num_fragments - 1):
                fragment = struct.pack(">B", i) + struct.pack(">B", num_fragments) + data[current:current + 18]
                current += 18
                self._p.writeCharacteristic(self._p.getCharacteristics(uuid = self._send_uuid)[0].valHandle, fragment)
                print " ".join(x.encode('hex') for x in fragment)
            fragment = struct.pack(">B", num_fragments - 1) + struct.pack(">B", num_fragments) + data[current:]
            self._p.writeCharacteristic(self._p.getCharacteristics(uuid = self._send_uuid)[0].valHandle, fragment)

            print " ".join(x.encode('hex') for x in fragment)
コード例 #3
0
    def _connectHelper(self, elementListener, connectCoroutine):
        """
        This is a protected helper method to Connect using the connectCoroutine,
        and use elementListener.

        :param elementListener: The elementListener must remain valid during the
          life of this object.
        :type elementListener: An object with onReceivedElement
        :param coroutine connectionInfo: The connect coroutine which uses
          _ReceiveProtocol, e.g. self._loop.create_connection(
          lambda: AsyncSocketTransport._ReceiveProtocol(self, onConnected), host, port).
        :type onConnected: function object
        """
        self.close()
        asyncio.async(connectCoroutine, loop = self._loop)
        self._elementReader = ElementReader(elementListener)
コード例 #4
0
 def connect(self, connectionInfo, elementListener):
     """
     Connect according to the info in connectionInfo, and use 
     elementListener.
     
     :param UnixTransport.ConnectionInfo connectionInfo: A 
       UnixTransport.ConnectionInfo.
     :param elementListener: The elementListener must remain valid during the 
       life of this object.
     :type elementListener: An object with onReceivedData
     """
     self.close()
     self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     self._socket.connect(connectionInfo.getFilePath())
       
     self._socketPoller = SocketPoller(self._socket)
     self._elementReader = ElementReader(elementListener)
コード例 #5
0
    def connect(self, connectionInfo, elementListener):
        """
        Connect according to the info in connectionInfo, and use 
        elementListener.
        
        :param UdpTransport.ConnectionInfo connectionInfo: A 
          UdpTransport.ConnectionInfo.
        :param elementListener: The elementListener must remain valid during the 
          life of this object.
        :type elementListener: An object with onReceivedData
        """
        self.close()
        # Save the _address to use in sendto.
        self._address = (connectionInfo.getHost(), connectionInfo.getPort())
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        self._socketPoller = SocketPoller(self._socket)
        self._elementReader = ElementReader(elementListener)
コード例 #6
0
    def connect(self, connectionInfo, elementListener, onConnected):
        """
        Connect according to the info in connectionInfo, and use
        elementListener.

        :param UnixTransport.ConnectionInfo connectionInfo: A
          UnixTransport.ConnectionInfo.
        :param elementListener: The elementListener must remain valid during the
          life of this object.
        :type elementListener: An object with onReceivedElement
        :param onConnected: This calls onConnected() when the connection is
          established.
        :type onConnected: function object
        """
        self.close()
        self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self._socket.connect(connectionInfo.getFilePath())

        self._socketPoller = SocketPoller(self._socket)
        self._elementReader = ElementReader(elementListener)

        onConnected()
コード例 #7
0
from pyndn.encoding.element_reader import ElementReader
from btle_node import BtleNode

from bluepy.btle import UUID, Peripheral, DefaultDelegate

service_uuid = UUID(0x2220)
my_uuid = UUID(0x2221)
p = Peripheral("EE:C5:46:65:D3:C1", "random")


def onBtleData(data):
    print "got data: " + data.getName().toUri()


el = BtleNode(onBtleData, None, None)
em = ElementReader(el)


class MyDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        em.onReceivedData(data[2:])


p.setDelegate(MyDelegate())
p.writeCharacteristic(
    p.getCharacteristics(uuid=my_uuid)[0].valHandle + 1, "\x01\x00")

while True: