def __init__(self, config): self.config = config self.logger.info("Configuring DataNode to listen on localhost:%d", self.config.port) self.logger.info("DataNode data dir: %s", config.datadir) self.logger.info("Using a fake out? %s", unicode(config.fakeout)) Server.__init__(self, DataNodeQuery, port=self.config.port) self.block_store = BlockStoreManager(self.config) if not self.config.isolated: self.notifier = DataNodeNotifier(self.config, self)
def main(): # print command line arguments args = sys.argv if len(args) != 6: print 'Insufficient Arguments' global lsListenPort global ts1Hostname global ts1ListenPort global ts2Hostname global ts2ListenPort lsListenPort = int(args[1]) ts1Hostname = args[2] ts1ListenPort = int(args[3]) ts2Hostname = args[4] ts2ListenPort = int(args[5]) server = Server(lsListenPort) ts1client = Client() ts2client = Client() server.accept() contactTSservers(server, ts1client, ts2client) server.close()
def create_game(self, name, ip_address, port, mode): """Player must create game, that is, a server client is created and it is set to listen for players in a new thread, so that current flow can create player instance and connect to server.""" try: self.server = Server(ip_address, port) except Exception as e: msgbox.showerror( "Error creating the server", f"The following error happened when creating the server.\n{e}") else: threading.Thread(target=self.server.accept_players).start() self.join_game(name, ip_address, port, mode)
def main(): args = sys.argv if len(args) != 2: print 'Insufficient Arguments' tsListenPort = int(args[1]) populateDNStable() server = Server(tsListenPort) server.accept() queryDNStable(server) server.close()
from networking import Server server = Server('localhost', 9234) # создали объект сервера server.accept_client() server.recieve_input()
def __init__(self): self.server = Server() self.connections = self.server.get_connections() self.reports = {}#Report keyed on connection self.messages_sent = 0
class master(object): def __init__(self): self.server = Server() self.connections = self.server.get_connections() self.reports = {}#Report keyed on connection self.messages_sent = 0 def send_hb(self, connection): try: heartbeat_msg = message.MsgHeartbeat() self.server.send_to(heartbeat_msg, connection) incoming = self.server.receive_from(message.parser, connection) self.messages_sent += 1 except Exception as e: print "aborted hb due to error:", e connection.close() """ Given a connection and a tube number attempt to fire the tube """ def fire_tube(self, tube_number, connection): try: msg = message.MsgFireTube(tube_number) self.server.send_to(msg, connection) incoming = self.server.receive_from(message.parser, connection) self.messages_sent += 1 except Exception as e: print "aborted fire due to error", e connection.close() """ Given a connection, request and save the report from the node that contains all of the key node information """ def update_report(self, connection): try: msg = message.MsgRequestReport() self.server.send_to(msg, connection) response = self.server.receive_from(message.parser, connection) report = None if response.isAck == 1: incoming = self.server.receive_from(message.parser, connection) if "REPORT" in incoming.id: report = incoming self.server.send_to(message.MsgResponse(1, 0), connection) self.reports[connection] = report self.messages_sent += 2 except Exception as e: print "aborted get report due to error", e connection.close() def get_report(self, connection): try: report = self.reports[connection] except KeyError: print "report for connection", connection, "is missing, fetching now." self.get_report(connection) report = self.reports[connection] return report """ Run a simple loop for debugging """ def loop(self): heartbeat_interval = utils.IntervalChecker(0.5) heartbeat_interval.start() report_interval = utils.IntervalChecker(1) report_interval.start() stats_interval = utils.IntervalChecker(1) stats_interval.start() fire_interval = utils.IntervalChecker(10) fire_interval.start() startOfLoop = time.time() loopTime = 0 heartbeat_time = 0 reportTime = 0 fireTime = 0 while(1): loopTime = time.time() - startOfLoop startOfLoop = time.time() if stats_interval.check(): print "Connections:", len(self.connections), "Messages:", self.messages_sent, "Loop time:", loopTime print "Heartbeat time:", heartbeat_time, "reportTime:", reportTime, "Fire time:", fireTime self.messages_sent = 0 if len(self.connections) > 0: #send heartbeats to the connection if heartbeat_interval.check(): hbStartTime = time.time() for con in self.connections: self.send_hb(con) heartbeat_time = time.time() - hbStartTime if report_interval.check(): reportStartTime = time.time() for con in self.connections: self.update_report(con) reportTime = time.time() - reportStartTime if fire_interval.check(): fireStartTime = time.time() for con in self.connections: report = self.get_report(con) ready = True for t in report.tube_state: ready = ready and t == 1 if ready: #fire everything! for i in range(report.num_tubes): self.fire_tube(i, con) fireTime = time.time() - fireStartTime time.sleep(0.1)
from networking import Server from game import MasterPlatformer import json if __name__ == '__main__': json_file = open('network_settings.json', "r") network_settings = json.load(json_file) vidja_game = MasterPlatformer() if network_settings['localhost'] == "True": server = Server(vidja_game) else: server = Server(vidja_game, network_settings['ip_file'], network_settings['port']) server.start_game() server.run()
def start_server(self): self.server = Server() self.server.start()
class Main: def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = SCREEN_SIZE pygame.display.set_caption("Sokorez") self.done = False self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.size) self.ui = MenuUI(self, None) self.server = None self.client = None self.clicked = 0 def start_server(self): self.server = Server() self.server.start() def restart(self): self.ui = self.ui.reload_level() def ui_back(self): """ Set the current ui-view to its parent, discarding the current """ if self.ui.parent is not None: child = self.ui self.ui = self.ui.parent self.ui.on_reentry(child) def push_ui(self, cls, *args, **kwargs): """ Push the new UI onto the old one. """ self.ui = cls(self, self.ui, *args, **kwargs) def change_screen(self, which, **kwargs): if which == "editor": self.push_ui(EditorUI) elif which == "game": self.ui_back() self.push_ui(GameUI, **kwargs) # self.ui = GameUI(self, self.ui.player2) # oh dis is bad. elif which == "host": self.start_server() self.push_ui(WaitUI, False) self.join_server() elif which == "join": self.push_ui(JoinUI) elif which == "join ip": ip = self.ui.ip_box.text self.ui_back() self.push_ui(WaitUI, True) self.join_server(ip) self.client.send("JOIN") elif which == "editor load": self.push_ui(LoadEditorUI) elif which == "level select": # before a game # self.ui_back() self.push_ui(LoadUI) elif which == "no connect": self.ui_back() self.ui.set_message("Couldn't connect") elif which == "save": self.push_ui(SaveUI) elif which == "test": self.push_ui(SinglePlayerUI) elif which == "glossary": self.push_ui(GlossaryUI) else: raise ValueError("I don't know the class " + str(which)) def send_msg(self, msg): self.client.send(msg) def join_server(self, host=None): self.client = Client(self, host) self.client.start() def stop_multiplayer(self): if self.server is not None: self.server.stop() self.server = None if self.client is not None: self.client.stop() self.client = None print "multiplayer stopped" def stop(self): self.done = True self.stop_multiplayer() def run(self): while not self.done: self.clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: self.stop() elif event.type == pygame.KEYDOWN: self.ui.handle_key(event) elif event.type == pygame.KEYUP: self.ui.handle_key_up(event) elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.clicked = True self.ui.handle_click(event) elif event.button in (4, 5): # scrollin! event.scroll_dir = event.button * 2 - 9 self.ui.handle_scroll(event) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: self.clicked = False self.ui.handle_click_up(event) elif event.type == pygame.MOUSEMOTION: if event.buttons[0]: self.ui.handle_drag(event) else: self.ui.handle_motion(event) self.screen.fill((0,) * 3) self.ui.reblit(self.screen) pygame.display.flip() pygame.quit()
from networking.Server import * if __name__ == "__main__": computer = Server() while True: x = computer.receive_data() if x != "no data within listening time": print(x) # run rrt computer.send_update([0, 0, 0, 0, 0, 0]) # placeholder break