示例#1
0
 async def create_playground_connection(self, protocolFactory, destination, destinationPort, vnicName="default", cbPort=0, timeout=60):
     if not self._ready:
         await self.create_callback_service(lambda: VNICCallbackProtocol(self._callbackService))#.buildConnectDataProtocol)
     if destination == "localhost":
         destination = self._vnicService.getVnicPlaygroundAddress(self._vnicService.getDefaultVnic())
     if not isinstance(destination, PlaygroundAddress):
         destination = PlaygroundAddress.FromString(destination)
         
     if vnicName == "default":
         vnicName = self._vnicService.getVnicByDestination(destination, destinationPort)
     if not vnicName:
         raise Exception("Could not find a valid vnic four outbound connection {}:{}".format(destination, destinationPort))
     location = self._vnicService.getVnicTcpLocation(vnicName)
     if not location:
         raise Exception("Playground network not ready. Could not find interface to connect to {}:{}".format(destination, destinationPort))
     vnicAddr, vnicPort = location
     connectProtocol = VNICConnectProtocol(destination, destinationPort, self._callbackService, protocolFactory)
     coro = asyncio.get_event_loop().create_connection(lambda: connectProtocol, vnicAddr, vnicPort)
     transport, protocol  = await coro
     # now we have to wait for protocol to make it's callback.
     coro = self._callbackService.waitForConnections(protocol)
     connections = await asyncio.wait_for(coro, timeout)
     if len(connections) != 1:
         # First close protocol
         protocol.transport.close()
         raise Exception("VNIC Open to {} Failed (Unexpected Error, connections={})!".format((destination, destinationPort), len(connections)))
         
     playgroundProtocol = connections[0]
     while isinstance(playgroundProtocol, StackingProtocol) and playgroundProtocol.higherProtocol():
         playgroundProtocol = playgroundProtocol.higherProtocol()
     
     connectionMadeCoro = connectionMadeObserver.awaitConnection(playgroundProtocol)
     await asyncio.wait_for(connectionMadeCoro, timeout)
         
     return playgroundProtocol.transport, playgroundProtocol
示例#2
0
 async def create_playground_server(self, protocolFactory, port, host="default", vnicName="default", cbPort=0):
     if not self._ready:
         await self.create_callback_service(lambda: VNICCallbackProtocol(self._callbackService))
         #await self.create_callback_service(self._callbackService.buildListenDataProtocol)
         
     # find the address to host on.
     if host == "default":
         vnic = self._vnicService.getDefaultVnic()
     else:
         vnic = self._vnicService.getVnicByLocalAddress(host) 
     if not vnic:
         raise Exception("Could not find a valid VNIC.")
         
     address = self._vnicService.getVnicPlaygroundAddress(vnic)
     location = self._vnicService.getVnicTcpLocation(vnic)
     
     if not location:
         raise Exception("Could not find an active VNIC.")
     
     vnicAddr, vnicPort = location
     
     if not vnicAddr or not vnicPort:
         raise Exception("Invalid VNIC address and/or port")
     
     listenProtocol = VNICListenProtocol(port, self._callbackService, protocolFactory)
     coro = asyncio.get_event_loop().create_connection(lambda: listenProtocol, vnicAddr, vnicPort)
     transport, protocol  = await coro
     
     server = PlaygroundServer(protocol, host, port, self._callbackService.getConnections(protocol))
     return server
示例#3
0
 async def create_playground_server(self, protocolFactory, port, host="default", vnicName="default", cbPort=0):
     if not self._ready:
         await self.create_callback_service(lambda: VNICCallbackProtocol(self._callbackService))
         #await self.create_callback_service(self._callbackService.buildListenDataProtocol)
         
     # find the address to host on.
     if host == "default" or host == "localhost":
         vnic = self._vnicService.getDefaultVnic()
     else:
         vnic = self._vnicService.getVnicByLocalAddress(host) 
     if not vnic:
         raise Exception("Could not find a valid VNIC.")
         
     address = self._vnicService.getVnicPlaygroundAddress(vnic)
     location = self._vnicService.getVnicTcpLocation(vnic)
     
     if not location:
         raise Exception("Could not find an active VNIC.")
     
     vnicAddr, vnicPort = location
     
     if not vnicAddr or not vnicPort:
         raise Exception("Invalid VNIC address and/or port")
     
     logger.info("vnic connections {}".format(self._vnicConnections))
     if not location in self._vnicConnections:
         logger.info("No control conenction to VNIC {} yet. Connecting".format(location))
         controlProtocol = VNICSocketControlClientProtocol(self._callbackService)
         coro = asyncio.get_event_loop().create_connection(lambda: controlProtocol, vnicAddr, vnicPort)
         res = await coro
         logger.info("Control protocol connected. res={}".format(res))
         self._vnicConnections[location] = controlProtocol
     while self._vnicConnections[location] == "CONNECTING":
         # another co-routing is rying to make the connection
         logger.debug("Waiting to connect for {}".format(location))
         await asyncio.sleep(0.5)
     logger.debug("Ready to proceed for {}".format(location))
     
     controlProtocol = self._vnicConnections[location]
     logger.info("For vnic {}, location {}, got controlProtocol {}, transport {}".format(vnic, location, controlProtocol, controlProtocol.transport))
     future = controlProtocol.listen(port, protocolFactory)
     logger.debug("Awaiting listening to port {} to complete".format(port))
     try:
         connectionId, port = await asyncio.wait_for(future, 30.0)
         logger.debug("Connection complete. Listening port is {}".format(port))
     except TimeoutError:
         raise Exception("Could not open listening port {} in {} seconds.".format(port, 30.0))
     
     server = PlaygroundServer(connectionId, host, port, self._callbackService.getConnections, controlProtocol.close)
     return server
示例#4
0
 def buildDataProtocol(self):
     higherProtocol = self._protocolStack and self._protocolStack() or None
     return VNICCallbackProtocol(self, higherProtocol)
示例#5
0
    async def create_playground_connection(self,
                                           protocolFactory,
                                           destination,
                                           destinationPort,
                                           vnicName="default",
                                           cbPort=0,
                                           timeout=60):
        startTime = time.time()

        logger.info("Create playground connection to {}:{}".format(
            destination, destinationPort))
        if not self._ready:
            await self.create_callback_service(
                lambda: VNICCallbackProtocol(self._callbackService)
            )  #.buildConnectDataProtocol)
        if destination == "localhost":
            destination = self._vnicService.getVnicPlaygroundAddress(
                self._vnicService.getDefaultVnic())
        if not isinstance(destination, PlaygroundAddress):
            destination = PlaygroundAddress.FromString(destination)

        if vnicName == "default":
            vnicName = self._vnicService.getVnicByDestination(
                destination, destinationPort)
        if not vnicName:
            raise Exception(
                "Could not find a valid vnic four outbound connection {}:{}".
                format(destination, destinationPort))
        location = self._vnicService.getVnicTcpLocation(vnicName)
        if not location:
            raise Exception(
                "Playground network not ready. Could not find interface to connect to {}:{}"
                .format(destination, destinationPort))
        vnicAddr, vnicPort = location
        if not location in self._vnicConnections:
            self._vnicConnections[location] = "CONNECTING"
            logger.info(
                "No control conenction to VNIC {} yet. Connecting".format(
                    location))
            controlProtocol = VNICSocketControlClientProtocol(
                self._callbackService)
            coro = asyncio.get_event_loop().create_connection(
                lambda: controlProtocol, vnicAddr, vnicPort)
            res = await coro
            logger.info("Control protocol connected. {}".format(res))
            self._vnicConnections[location] = controlProtocol
        while self._vnicConnections[location] == "CONNECTING":
            # another co-routing is rying to make the connection
            logger.debug("Waiting to connect for {}".format(location))
            await asyncio.sleep(0.5)
        logger.debug("Ready to proceed for {}".format(location))

        controlTime = time.time()
        controlProtocol = self._vnicConnections[location]
        future = controlProtocol.connect(destination, destinationPort,
                                         protocolFactory)
        logger.debug("Awaiting outbound connection to complete")
        try:
            connectionId, port = await asyncio.wait_for(future, timeout)
            logger.debug(
                "Connection complete. Outbound port is {} for connection {}".
                format(port, connectionId))
        except TimeoutError:
            raise Exception("Could not connect to {}:{} in {} seconds.".format(
                destination, destinationPort, timeout))
        callbackTime = time.time()
        logger.debug(
            "Complete playground connection. Total Time: {} (Control {}, callback {})"
            .format(callbackTime - startTime, controlTime - startTime,
                    callbackTime - controlTime))
        connections = await self._callbackService.waitForConnections(
            connectionId, n=1)
        if len(connections) != 1:
            raise Exception(
                "VNIC Unexpected Error connecting to {}:{} (ID {}). Should be one connection, but got {})!"
                .format(destination, destinationPort, connectionId,
                        len(connections)))
        dataProtocol = connections[0]
        playgroundProtocol = dataProtocol
        while isinstance(
                playgroundProtocol,
                StackingProtocol) and playgroundProtocol.higherProtocol():
            playgroundProtocol = playgroundProtocol.higherProtocol()

        connectionMadeCoro = connectionMadeObserver.awaitConnection(
            playgroundProtocol)
        try:
            await asyncio.wait_for(connectionMadeCoro, timeout)
        except TimeoutError as te:
            logger.debug(
                "Playground protocol could not connect in {} seconds.".format(
                    timeout))
            try:
                dataProtocol.transport.close()
            except:
                pass
            raise te

        return playgroundProtocol.transport, playgroundProtocol