示例#1
0
 def __init__(self, queue):
     """
     Initializes the protocol factory
     Args:
         queue: the incoming data queue to use by all protocol instances
     """
     self.protocol = _CygnusCloudProtocol
     self.__queue = queue
     self.__connections = GenericThreadSafeList()
示例#2
0
 def __init__(self, queue):
     """
     Initializes the protocol factory
     Args:
         queue: the incoming data queue to use by all protocol instances
     """
     self.protocol = _CygnusCloudProtocol
     self.__queue = queue        
     self.__connections = GenericThreadSafeList()
示例#3
0
class _CygnusCloudProtocolFactory(Factory):
    """
    Protocol factory. These objects are used to create protocol instances
    within the Twisted Framework, and store all the data shared by multiple
    protocol instances.
    """
    def __init__(self, queue):
        """
        Initializes the protocol factory
        Args:
            queue: the incoming data queue to use by all protocol instances
        """
        self.protocol = _CygnusCloudProtocol
        self.__queue = queue
        self.__connections = GenericThreadSafeList()

    def buildProtocol(self, addr):
        """
        Builds a protocol, stores a pointer to it and finally returns it.
        This method is called inside Twisted code.
        """
        instance = _CygnusCloudProtocol(self)
        self.__connections.append(instance)
        return instance

    def removeConnection(self, connection):
        self.__connections.remove(connection)

    def isDisconnected(self):
        return self.__connections.getSize() == 0

    def sendPacket(self, packet):
        """
        Returns the last built instance
        Args:
            None
        Returns:
            the last built protocol instance
        """
        i = 0
        while (i < self.__connections.getSize()):
            self.__connections[i].sendPacket(packet)
            i += 1

    def disconnect(self):
        i = 0
        while i < self.__connections.getSize():
            p = self.__connections[i]
            p.disconnect()
            i += 1

    def onPacketReceived(self, p):
        """
        Returns the incoming packages queue
        Args:
            None
        Returns:
            The incoming packages queue
        """
        p = _Packet._deserialize(p)
        self.__queue.queue(p.getPriority(), p)
示例#4
0
 def __init__(self):
     self.__backgroundProcesses = GenericThreadSafeList()
     self.__thread = BackgroundProcessesPollingThread(
         self.__backgroundProcesses)
     self.__thread.start()
示例#5
0
class ChildProcessManager(object):
    """
    These objects create child processes and manage their
    return values.
    """
    def __init__(self):
        self.__backgroundProcesses = GenericThreadSafeList()
        self.__thread = BackgroundProcessesPollingThread(
            self.__backgroundProcesses)
        self.__thread.start()

    def waitForBackgroundChildrenToTerminate(self):
        self.__thread.stop()
        while not self.__thread.finish():
            sleep(0.1)

    @staticmethod
    def runCommandInForeground(cmd, ExceptionClass):
        """
        Runs a command in foreground
        Args:
            cmd: a string with the command's name and its arguments
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns:
            The run command's output
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong when running the command.
        """
        p = Popen(cmd,
                  shell=True,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=STDOUT,
                  close_fds=True)
        code = p.wait()
        if (code != 0):
            raise ExceptionClass(p.stdout.read())
        else:
            return p.stdout.read()

    def runCommandInBackground(self, cmd):
        """
        Runs a command in background
            Args:
                cmd: a string with the command's name and its arguments
            Returns:
                a PID
        """
        p = Popen(cmd,
                  shell=False,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=STDOUT,
                  close_fds=True)
        self.__backgroundProcesses.append(p)
        return p.pid

    @staticmethod
    def terminateProcess(pid, ExceptionClass):
        """
        Sends a SIGTERM signal to a process
        Args:
            pid: A PID
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns :
            Nothing
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong while killing the process.
        """
        ChildProcessManager.runCommandInForeground(
            ["kill -s TERM " + str(pid)], ExceptionClass)

    @staticmethod
    def runCommandInForegroundAsRoot(cmd, ExceptionClass):
        """
        Runs a command in foreground and as the super-user.
        Args:
            cmd: a string with the command's name and its arguments
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns:
            The run command's output
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong when running the command.
        """
        password = RootPasswordHandler.getInstance().getRootsPassword()
        header = "echo " + password + " | sudo -S "
        return ChildProcessManager.runCommandInForeground(
            header + cmd, ExceptionClass)
示例#6
0
class _CygnusCloudProtocolFactory(Factory):
    """
    Protocol factory. These objects are used to create protocol instances
    within the Twisted Framework, and store all the data shared by multiple
    protocol instances.
    """    
    def __init__(self, queue):
        """
        Initializes the protocol factory
        Args:
            queue: the incoming data queue to use by all protocol instances
        """
        self.protocol = _CygnusCloudProtocol
        self.__queue = queue        
        self.__connections = GenericThreadSafeList()
    
    def buildProtocol(self, addr):
        """
        Builds a protocol, stores a pointer to it and finally returns it.
        This method is called inside Twisted code.
        """
        instance = _CygnusCloudProtocol(self)        
        self.__connections.append(instance)
        return instance   
        
    def removeConnection(self, connection):
        self.__connections.remove(connection)
        
    def isDisconnected(self):
        return self.__connections.getSize() == 0

    def sendPacket(self, packet):
        """
        Returns the last built instance
        Args:
            None
        Returns:
            the last built protocol instance
        """
        i = 0
        while (i < self.__connections.getSize()) :
            self.__connections[i].sendPacket(packet)
            i += 1
            
    def disconnect(self):
        i = 0
        while i < self.__connections.getSize() :
            p = self.__connections[i]
            p.disconnect()
            i += 1
    
    def onPacketReceived(self, p):
        """
        Returns the incoming packages queue
        Args:
            None
        Returns:
            The incoming packages queue
        """
        p = _Packet._deserialize(p)
        self.__queue.queue(p.getPriority(), p)
 def __init__(self):
     self.__backgroundProcesses = GenericThreadSafeList()
     self.__thread = BackgroundProcessesPollingThread(self.__backgroundProcesses)
     self.__thread.start()
class ChildProcessManager(object):
    """
    These objects create child processes and manage their
    return values.
    """
    def __init__(self):
        self.__backgroundProcesses = GenericThreadSafeList()
        self.__thread = BackgroundProcessesPollingThread(self.__backgroundProcesses)
        self.__thread.start()
        
    def waitForBackgroundChildrenToTerminate(self):
        self.__thread.stop()
        while not self.__thread.finish() :
            sleep(0.1)
          
    @staticmethod  
    def runCommandInForeground(cmd, ExceptionClass):
        """
        Runs a command in foreground
        Args:
            cmd: a string with the command's name and its arguments
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns:
            The run command's output
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong when running the command.
        """
        p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
        code = p.wait()
        if (code != 0) :            
            raise ExceptionClass(p.stdout.read())  
        else :
            return p.stdout.read()
        
    def runCommandInBackground(self, cmd):
        """
        Runs a command in background
            Args:
                cmd: a string with the command's name and its arguments
            Returns:
                a PID
        """
        p = Popen(cmd, shell=False, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
        self.__backgroundProcesses.append(p)
        return p.pid
    
    @staticmethod
    def terminateProcess(pid, ExceptionClass):
        """
        Sends a SIGTERM signal to a process
        Args:
            pid: A PID
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns :
            Nothing
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong while killing the process.
        """        
        ChildProcessManager.runCommandInForeground(["kill -s TERM " + str(pid)], ExceptionClass)
        
    @staticmethod
    def runCommandInForegroundAsRoot(cmd, ExceptionClass):
        """
        Runs a command in foreground and as the super-user.
        Args:
            cmd: a string with the command's name and its arguments
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns:
            The run command's output
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong when running the command.
        """ 
        password = RootPasswordHandler.getInstance().getRootsPassword()
        header = "echo " + password + " | sudo -S "
        return ChildProcessManager.runCommandInForeground(header + cmd, ExceptionClass)