Beispiel #1
0
class CygnusCloudProtocolFactory(Factory):
    """
    CygnusCloud 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, transferQueue):
        """
        Initializes the protocol factory
        Args:
            transferQueue: the incoming data transferQueue to use by all protocol instances
        """
        self.protocol = CygnusCloudProtocol
        self._queue = transferQueue
        self.__protocolPool = GenericThreadSafeDictionary()

    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.__protocolPool[(addr.host, addr.port)] = instance
        return instance

    def removeProtocol(self, protocol):
        """
        Removes a protocol from the protocol pool
        """
        self.__protocolPool.removeElement(protocol)

    def isDisconnected(self):
        """
        Determines if there are active connections or not.
        """
        return self.__protocolPool.isEmpty()

    def sendPacket(self, packet, ip=None, port=None):
        """
        Returns the last built instance
        Args:
            None
        Returns:
            the last built protocol instance
        """
        if ip == None:
            for key in self.__protocolPool.keys():
                self.__protocolPool[key].sendPacket(packet)
        else:
            self.__protocolPool[(ip, port)].sendPacket(packet)

    def disconnect(self):
        """
        Asks Twisted to close the connection.
        Args:
            None
        Returns:
            Nothing
        """
        for key in self.__protocolPool.keys():
            self.__protocolPool[key].disconnect()

        while not self.__protocolPool.isEmpty():
            sleep(0.1)

    def onPacketReceived(self, p, peerAddr, peerPort):
        """
        Returns the incoming packages queue
        Args:
            None
        Returns:
            The incoming packages queue
        """
        p = _Packet._deserialize(p)
        p._setSenderData(peerAddr, peerPort)
        self._queue.queue(p.getPriority(), p)
Beispiel #2
0
class CygnusCloudProtocolFactory(Factory):
    """
    CygnusCloud 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, transferQueue):
        """
        Initializes the protocol factory
        Args:
            transferQueue: the incoming data transferQueue to use by all protocol instances
        """
        self.protocol = CygnusCloudProtocol
        self._queue = transferQueue        
        self.__protocolPool = GenericThreadSafeDictionary()
    
    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.__protocolPool[(addr.host, addr.port)] = instance
        return instance   
        
    def removeProtocol(self, protocol):
        """
        Removes a protocol from the protocol pool
        """
        self.__protocolPool.removeElement(protocol)
        
    def isDisconnected(self):
        """
        Determines if there are active connections or not.
        """
        return self.__protocolPool.isEmpty()

    def sendPacket(self, packet, ip=None, port=None):
        """
        Returns the last built instance
        Args:
            None
        Returns:
            the last built protocol instance
        """
        if (ip == None) :
            for key in self.__protocolPool.keys():
                self.__protocolPool[key].sendPacket(packet)        
        else :
            self.__protocolPool[(ip, port)].sendPacket(packet)    
            
    def disconnect(self):
        """
        Asks Twisted to close the connection.
        Args:
            None
        Returns:
            Nothing
        """
        for key in self.__protocolPool.keys() :
            self.__protocolPool[key].disconnect()         
        
        while (not self.__protocolPool.isEmpty()) :
            sleep(0.1)
    
    def onPacketReceived(self, p, peerAddr, peerPort):
        """
        Returns the incoming packages queue
        Args:
            None
        Returns:
            The incoming packages queue
        """
        p = _Packet._deserialize(p)
        p._setSenderData(peerAddr, peerPort)
        self._queue.queue(p.getPriority(), p)