def connectTo(self, host, port, timeout, callbackObject, useSSL=False): """ Connects to a remote server using its arguments @attention: This is a blocking operation. The calling thread will be blocked until the connection is established or until a timeout error is detected. Args: host: host IP address port: the port where the host is listenning timeout: timeout in seconds. callbackObject: the callback object that will process all the incoming packages received through this connection. Returns: Nothing Raises: NetworkManagerException: If no answer is received after timeout seconds, the connection process will be aborted and a NetworkManagerException will be raised. """ if self.__connectionPool.has_key(port) : raise NetworkManagerException("The port " + str(port) +" is already in use") # The port is free => proceed # Allocate the connection resources (queue, thread) = self.__allocateConnectionResources(callbackObject) # Create and configure the endpoint factory = _CygnusCloudProtocolFactory(queue) if (not useSSL) : endpoint = TCP4ClientEndpoint(reactor, host, port, timeout, None) else : keyPath = self.__certificatesDirectory + "/" + "server.key" certificatePath = self.__certificatesDirectory + "/" + "server.crt" try : endpoint = SSL4ClientEndpoint(reactor, host, port, ssl.DefaultOpenSSLContextFactory(keyPath, certificatePath)) except Exception: raise NetworkManagerException("The key, the certificate or both were not found") endpoint.connect(factory) # Wait until the connection is ready time = 0 while factory.isDisconnected() and time <= timeout: sleep(0.01) time += 0.01 if factory.isDisconnected() : raise NetworkManagerException("The host " + host + ":" + str(port) +" seems to be unreachable") # Create the new connection connection = _NetworkConnection(False, port, factory, queue, thread, callbackObject) # Add the new connection to the connection pool self.__connectionPool[port] = connection
def listenIn(self, port, callbackObject, useSSL=False): """ Creates a server using its arguments. @attention: This is a non-blocking operation. Please, check if the connection is ready BEFORE you send anything through it. Args: port: The port to listen in. If it's not free, a NetworkManagerException will be raised. callbackObject: the callback object that will process all the incoming packages received through this connection. Returns: Nothing """ if self.__connectionPool.has_key(port) : raise NetworkManagerException("The port " + str(port) +" is already in use") # The port is free => proceed # Allocate the connection resources (queue, thread) = self.__allocateConnectionResources(callbackObject) # Create and configure the endpoint if (not useSSL) : endpoint = TCP4ServerEndpoint(reactor, port) else : keyPath = self.__certificatesDirectory + "/" + "server.key" certificatePath = self.__certificatesDirectory + "/" + "server.crt" try : endpoint = SSL4ServerEndpoint(reactor, port, ssl.DefaultOpenSSLContextFactory(keyPath, certificatePath)) except Exception: raise NetworkManagerException("The key, the certificate or both were not found") factory = _CygnusCloudProtocolFactory(queue) # Create the connection connection = _NetworkConnection(True, port, factory, queue, thread, callbackObject) # Create the deferred to retrieve the IListeningPort object def registerListeningPort(port): connection.setListeningPort(port) def onError(failure): connection.setError(failure) deferred = endpoint.listen(factory) deferred.addCallback(registerListeningPort) deferred.addErrback(onError) connection.setDeferred(deferred) # Register the new connection self.__connectionPool[port] = connection