Example #1
0
class _Factory(protocol.ClientFactory):
    '''The _Factory class is responsible for creating a _Server
    connection.

    '''
    # Define the name and log color used for logging messages for this class
    name = "ServerFactory"
    logColor = Colors.Foreground.Blue

    def __init__(self, logger=None):
        '''
        * logger -- The logger

        '''
        # Get an instance to the connection manager so we can properly
        # disconnect Apple's server connection when it is lost
        self.__connectionManager = ConnectionManager(logger)

        # If no logger is given, be sure to create it
        if logger is None:
            logger = LogData(self.name, color=self.logColor)

        self.log = logger.get(self.name)
        self.__logger = logger

    def buildProtocol(self, _addr):
        '''Build the protocol for the _Server connection.

        * _addr -- The address

        '''
        server = _Server(self.__logger)
        server.factory = self

        return server

    def clientConnectionFailed(self, connector, reason):
        self.log.debug("Connection failed: %s" % reason, level=2)
        protocol.ClientFactory.clientConnectionFailed(self, connector,
                                                      reason)

        # Delete the server connection
        self.__connectionManager.disconnect(Directions.From_Server)

    def clientConnectionLost(self, connector, reason):
        self.log.debug("Connection lost: %s" % reason, level=2)
        protocol.ClientFactory.clientConnectionLost(self, connector,
                                                    reason)

        # Delete the server connection
        self.__connectionManager.disconnect(Directions.From_Server)