def app(manager, watcher, bind): server = Node(port=bind[1], server_ip=bind[0]) server.register(manager) server.bind = bind assert watcher.wait('registered', channel='node') return server
def test_auto_reconnect(app, watcher, manager): # add client client = App().register(manager) node = Node().register(client) chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1) assert watcher.wait('connected', channel=chan) # close server app.fire(close(), app.channel) assert watcher.wait('closed', channel=app.channel) app.unregister() assert watcher.wait('unregistered', channel=app.channel) for i in range(5): watcher.clear() assert watcher.wait('connect', channel=chan) assert watcher.wait('unreachable', channel=chan) # open server app = Node(port=app.bind[1], server_ip=app.bind[0]) app.register(manager) assert watcher.wait('registered', channel=app.channel) assert watcher.wait('connected_to', channel=app.channel) client.unregister()
def test_auto_reconnect(app, watcher, manager): # add client client = App().register(manager) node = Node().register(client) chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1) assert watcher.wait('connected', channel=chan) watcher.clear() # close server app.fire(close(), app.channel) assert watcher.wait('closed', channel=app.channel) watcher.clear() # client gets an unreachable assert watcher.wait('connect', channel=chan) assert watcher.wait('unreachable', channel=chan) watcher.clear() # start a new server node2 = Node(port=app.bind[1], server_ip=app.bind[0]) node2.register(manager) assert watcher.wait('ready', channel=node2.channel) watcher.clear() assert watcher.wait('connected', channel=chan) client.unregister()
def test_auto_reconnect(app, watcher, manager): # add client client = App().register(manager) node = Node().register(client) chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1) assert watcher.wait('connected', channel=chan) # close server app.fire(close(), app.channel) assert watcher.wait('closed', channel=app.channel) app.unregister() assert watcher.wait('unregistered', channel=app.channel) for _ in range(5): watcher.clear() assert watcher.wait('connect', channel=chan) assert watcher.wait('unreachable', channel=chan) # open server app = Node(port=app.bind[1], server_ip=app.bind[0]) app.register(manager) assert watcher.wait('registered', channel=app.channel) assert watcher.wait('connected_to', channel=app.channel) client.unregister()
class Client(Network_Node, circuits.core.BaseComponent): def __init__(self, host="0.0.0.0", port=50552, socket=None): super().__init__() self.node = Node((self.host, self.port)) self.node.register(self) self.socket = socket #This is the channel that the server's client is listening on. #The fact that I have to use this to talk exclusively to the server strongly #suggests that either circuits' API is all sorts of broken or I horribly misunderstand #it #This is GROOOOOSSSS self.server_channel = "" #Eventually, I'll need to grab player name somehow. self.player = Player() def connect(self, host="0.0.0.0", port=50551): self.node.add("server", host, port) self.server_channel = sha256("{0:s}:{1:d}".format(host, port).encode("utf-8") ).hexdigest() @circuits.handler("connected") def connected(self, host, port): #self.fire(remote(write(Join_Event(self.player).bytify()), "server")) self.fire(write(bytify(Join_Event(self.player))), self.server_channel) def go(self): self.gui = views.GUI() debug("Made gui") self.gui.first_view_class = views.Login_View self.start() debug("Srtarting gui") self.gui.run() debug("Started") def update(self): self.game.update()
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()