コード例 #1
0
ファイル: server.py プロジェクト: marky1991/Legend-of-Wumpus
class Server(Network_Node, circuits.core.BaseComponent):
    def __init__(self, host="0.0.0.0", port=50551):
        super().__init__(host=host, port=port)
        #This maps user_id to client
        self.clients = {}
        #This is a temporary design. Ultimately this will be part of a client.
        #This is just here until clients are implemented
        #TODO: I'm from future and I don't remember what
        #this is talking about. Please investigate.
        self.sockets = set()
        self.id_generator = count()
        self.node = Node((self.host, self.port))
        self.node.register(self)
        self.id_generator = count() 
            
    @circuits.handler("connect")
    def connect(self, socket, host, port):
        self.sockets.add(socket)
        client = Client(host, port, socket=socket)
        self.clients[next(self.id_generator)] = client
    
    @circuits.handler("disconnect")
    def disconnect(self, socket):
        #This is an intentionally local event
        print("Updating code")
        events.Update_Code_Event().handle(self)

    def broadcast(self, event, exclude_list=None):
        exclude_list = exclude_list or []
        for client in filter(lambda client: client not in exclude_list,
                            self.clients.values()):
            self.fire(write(client.socket, bytify(event).encode("utf-8")))
    
    def cleanup(self):
        self.node.stop()
        self.stop()
    
    def shutdown(self):
        self.cleanup()
        #the fact that it tries to do this is gross.
        #Will need to replace the functionality somehow.
        #sys.exit(1)
    def restart(self):
        self.cleanup()