Пример #1
0
class ServerFactory(protocol.ServerFactory):
    """
    The server factory.
    
    @ivar channelClass: This is the channel class that will be used when
        creating a new channel. Subclass L{ServerChannel} and replace this
        attribute if you want to change the behaviour of channels.
    @ivar maxPing: This is the time the client has to respond
        to pings (in seconds). Can be a number or None for no max ping (default)
    @ivar pingTime: The interval between pings in seconds
    @ivar maxUsers: The max number of users allowed on the server.
    @ivar timeOut: The number of seconds the client has to send a Hello packet
        before being disconnected. Can be a number or None for no timeout
    @ivar welcomeMessage: The message sent to accepted clients.
    @ivar ping: If True, pinging will be enabled on the server
    @ivar channelListing: If True, channelListing is enabled on the server
    @ivar masterRights: If True, this enables the autoclose feature for
        clients when creating channels
    """
    channelClass = ServerChannel
    timeOut = 8
    pingTime = 8
    maxPing = None
    maxUsers = 1000
    welcomeMessage = 'Welcome! Server is running pylacewing %s (%s)' % (
        lacewing.__version__, sys.platform)
    
    datagram = None
    ping = True
    channelListing = True
    masterRights = False
    
    _pinger = None

    def startFactory(self):
        self.connections = MultikeyDict()
        self.channels = MultikeyDict()
        self.userPool = IDPool()
        self.channelPool = IDPool()
        if self.ping:
            self._pinger = pinger = LoopingCall(self.globalPing)
            pinger.start(self.pingTime, False)
    
    def globalPing(self):
        """
        Pings all clients currently connected to the server
        """
        for connection in self.connections.values():
            connection.ping()
    
    def getWelcomeMessage(self, connection):
        """
        This method is called when a connection has been accepted, and
        a welcome message has to be sent. The default implementation just
        returns L{welcomeMessage}, but override this method to change that
        behaviour.
        @param connection: Connection that has been accepted
        @type connection: L{ServerProtocol} object
        @rtype: str
        """
        return self.welcomeMessage
    
    def createChannel(self, name, hidden, autoClose, master):
        if not self.masterRights:
            autoClose = False
        try:
            channel, = self.channels[name]
        except KeyError:
            id = self.channelPool.pop()
            channel = self.channelClass(name, id, hidden, autoClose, master)
            self.channels[name, id] = channel
            self.channelAdded(channel)
        return channel
    
    def destroyChannel(self, channel):
        del self.channels[channel]
        self.channelRemoved(channel)

    def channelRemoved(self, channel):
        """
        Called when a channel has no users in it, and
        is therefore removed.
        @arg channel: The channel that is being removed.
        """

    def channelAdded(self, channel):
        """