当前位置: 首页>>代码示例>>Python>>正文


Python Dispatcher.dispatchRequest方法代码示例

本文整理汇总了Python中dispatcher.Dispatcher.dispatchRequest方法的典型用法代码示例。如果您正苦于以下问题:Python Dispatcher.dispatchRequest方法的具体用法?Python Dispatcher.dispatchRequest怎么用?Python Dispatcher.dispatchRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dispatcher.Dispatcher的用法示例。


在下文中一共展示了Dispatcher.dispatchRequest方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Server

# 需要导入模块: from dispatcher import Dispatcher [as 别名]
# 或者: from dispatcher.Dispatcher import dispatchRequest [as 别名]
class Server(QThread):
    clientIn = pyqtSignal(socket.socket)
    clientDisconnected = pyqtSignal(str)

    def __init__(self, port, parent=None):
        QThread.__init__(self, parent)
        self.port = port
        self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.serverSocket.bind(('', self.port))
        self.serverSocket.listen(5)
        self.serverSocket.setblocking(False)
        self.dispatcher = Dispatcher(self)
        #clients --> {clientId: Socket object}
        self.clients = {}
        EventManager.bind('Message.broadcast', self.broadcast)

    def broadcast(self, message, exception=()):
        for clientId in self.clients:
            if clientId not in exception:
                EventManager.trigger(Event('Socket.addReply.' + clientId, message))

    def setDispatcher(self, dispatcher):
        self.dispatcher = dispatcher

    def stop(self):
        self.serverSocket.shutdown(socket.SHUT_RDWR)
        self.serverSocket.close()

    def getFd(self):
        return self.serverSocket

    def run(self):
        while True:
            brokenClients = []
            readers, writers = self.setFs()
            rList, wList, eList = select.select(readers, writers, [], 0.2)
            if self.getFd() in rList:
                self.acceptNewClient()
            for identifier in self.clients:
                if self.clients[identifier].getFd() in rList:
                    status = self.clients[identifier].read()
                    if status == Socket.ReadError:
                        Log.e('Socket read error')
                        brokenClients.append(identifier)

                    elif status == Socket.NoMessage:
                        Log.i('No Message')
                        brokenClients.append(identifier)

                    elif status == Socket.NoMessage:
                        Log.i('New Message')

                if self.clients[identifier].hasRequests():
                    self.treatMessage(self.clients[identifier])

                if self.clients[identifier].getFd() in wList:
                    status = self.clients[identifier].reply()
                    if status == Socket.WriteError:
                        Log.e('Socket write error with socket %s' % (identifier, ))
                        if identifier not in brokenClients:
                            brokenClients.append(identifier)
                    elif status == Socket.WriteSuccess:
                        Log.i('Send Message success')

            for identifier in brokenClients:
                Log.i('删除客户端: ' + str(identifier))
                self.clients[identifier].close()
                del self.clients[identifier]
                self.clientDisconnected.emit(identifier)
                Log.i(self.clients)

    def acceptNewClient(self):
        Log.i('connect in')
        s, addr = self.serverSocket.accept()
        if not s:
            Log.e('Cannot accept connection')
            return
        identifier = uuid.uuid4().hex
        self.clients[identifier] = Socket(s, identifier)
        self.clients[identifier].addReply(Message(cmd=Message.CMD_CLIENT_VALIDATED, clientId=identifier))
        Log.i(self.clients)

    def setFs(self):
        readFs = []
        writeFs = []
        readFs.append(self.getFd())
        for identifier in self.clients:
            readFs.append(self.clients[identifier].getFd())
            if self.clients[identifier].hasReplies():
                writeFs.append(self.clients[identifier].getFd())
        return readFs, writeFs

    def treatMessage(self, client):
        request = client.getRequest()
        client.addReply(self.dispatcher.dispatchRequest(request))
开发者ID:RoyZeng,项目名称:ivy-webchat,代码行数:98,代码来源:network.py

示例2: ClientManager

# 需要导入模块: from dispatcher import Dispatcher [as 别名]
# 或者: from dispatcher.Dispatcher import dispatchRequest [as 别名]
class ClientManager(QThread):
    clientDisconnected = pyqtSignal(str)

    def __init__(self, parent=None):
        QThread.__init__(self, parent)
        self.address = None
        #clients --> {socket object: (address, identifier), }
        self.clients = {}
        self.dispatcher = Dispatcher(self)

    def setDispatcher(self, dispatcher):
        self.dispatcher = dispatcher

    def hasConnected(self, address):
        for client in self.clients:
            if self.clients[client][0] == address:
                return True

    def connectToServer(self, address):
        """address格式为(url, port)"""
        if self.hasConnected(address):
            Log.w('已经连接到服务器')
            return
        clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        clientSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            clientSocket.connect(address)
            client = Client(clientSocket, -1)
            self.clients[client] = (address, -1)
            Log.i('成功连接到服务器')
            return True
        except Exception as e:
            Log.e(getExceptionInfo(e))

    def disConnectToServer(self, address):
        for client in self.clients:
            if self.clients[client][0] == address:
                client.close()
                del self.clients[client]
                Log.i('删除客户端: ' + str(self.clients[client][0]))

    def run(self):
        while True:
            brokenClients = []
            readers, writers = self.setFs()
            rList, wList, eList = select.select(readers, writers, [], 0.2)

            for client in self.clients:

                if client.getFd() in rList:
                    status = client.read()
                    if status == Client.ReadError:
                        Log.e('Socket read error')
                        brokenClients.append(client)

                    elif status == Client.NoMessage:
                        Log.i('No Message')
                        brokenClients.append(client)

                    elif status == Client.NewMessage:
                        Log.i('New Message')

                    else:
                        Log.e('Unknown socket status')

                if client.hasRequests():
                    self.treatMessage(client)

                if client.getFd() in wList:
                    status = client.reply()
                    if status == Client.WriteError:
                        Log.e('Socket write error with socket %s' % (self.clients[client][0], ))
                        if client not in brokenClients:
                            brokenClients.append(client)
                    elif status == Client.WriteSuccess:
                        Log.i('Send Message success')

            for client in brokenClients:
                Log.i('删除客户端: ' + str(self.clients[client][0]))
                client.close()
                del self.clients[client]
                self.clientDisconnected.emit(str(client.clientId))
                Log.i(self.clients)

    def setFs(self):
        readFs = []
        writeFs = []
        for client in self.clients:
            readFs.append(client.getFd())
            if client.hasReplies():
                writeFs.append(client.getFd())
        return readFs, writeFs

    def treatMessage(self, client):
        client.addReply(self.dispatcher.dispatchRequest(client))
开发者ID:RoyZeng,项目名称:ivy-webchat,代码行数:97,代码来源:network.py


注:本文中的dispatcher.Dispatcher.dispatchRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。