def main(): logger.info('server start!') loop = event_loop.EventLoop() accepter = Accpter(config.SERVER_IP, config.SERVER_PORT, loop) loop.add_event(accepter) first_expires = config.GAME21_EXPIRES if first_expires is None: t = time.localtime() t2 = time.struct_time((t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour + 1, 0, 0, t.tm_wday, t.tm_yday, t.tm_isdst)) first_expires = time.mktime(t2) # add timer to control 21 game time out game_time_out_timer = Game21TimeOutTimer(0) loop.add_timer(game_time_out_timer) # add 21 game timer to event loop game_timer = Game21Timer(first_expires, config.GAME21_PERIOD, config.GAME21_DUARATION, game_time_out_timer) loop.add_timer(game_timer) # add heart beat timer to event loop heart_beat_timer = HeartBeat(time.time(), 3, 10) loop.add_timer(heart_beat_timer) try: # start the event loop loop.loop(config.SELECT_TIME_OUT) finally: accepter.close()
def main(): pygame.init() canvas = screen.Screen() loop = event_loop.EventLoop(canvas) loop.start()
def main(argv): """Load the project and start it running""" media_environment = media.PyGameMediaEnvironment() loop = event_loop.EventLoop(media_environment) if len(argv) >= 2: loop.load_project_from_disk(argv[1]) loop.trigger_green_flag() loop.run_forever()
def main(): argvs = sys.argv if (len(argvs) == 1): server_ip = config.serverip server_port = config.serverport elif (len(argvs) == 3): server_ip = argvs[1] server_port = int(argvs[2]) else: print 'usage:\n' \ 'python client.py ip port use the assigned ip and port\n' \ 'python client.py use configure in config.py\n' loop = event_loop.EventLoop() connector = Connector(config.serverip, config.serverport, loop) cmdline = CommandLineClient(connector, sys.stdin, loop) loop.add_event(connector) loop.add_event(cmdline) loop.add_timer(HeartBeat(connector, time.time() + 3, 3, 10)) loop.loop()
def simulate( network: networkx.Graph, networking_class: Type[Networking] = Networking, message_ratio=(1, 5 * MILLISECOND), stop_world_period=50 * MILLISECOND, end_time: Optional[int] = None, ) -> Networking: nodes = {} node_count = count() for identifier, d in sorted(network.nodes.data(), key=lambda x: x[0]): nodes[identifier] = Node(identifier=identifier, ip=next(node_count), trait=d.get( "trait", frozenset((NetworkTraits.CLIENT, NetworkTraits.SERVER, NetworkTraits.ROUTER)))) channels = [] for node1, node2, data in network.edges.data(): node1 = nodes[node1] node2 = nodes[node2] data, per_time = data["bandwidth"] ratio = per_time / data c1 = Channel(node1, node2, ratio) c2 = Channel(node2, node1, ratio) c1.dual = c2 c2.dual = c1 node1.interfaces.append(c1) node2.interfaces.append(c2) channels.extend((c1, c2)) for _, node in nodes.items(): node.default_channel = node.interfaces[0] # building routing tables eloop.EventLoop(initial_events=list( chain.from_iterable( broadcast_events(node.interfaces, 0, node.ip) for node in nodes.values())), handlers={ BroadcastEvent: handle_broadcast }).run() controller = networking_class( nodes=list(nodes.values()), channels=channels, message_ratio=message_ratio, stop_world_period=stop_world_period, rand=random.Random(0), emit_ratio=(1, MILLISECOND), ) handlers = { StopWorldEvent: controller.stop_world, ChannelErrorEvent: controller.channel_error, MessageEvent: controller.message, PacketEmitEvent: controller.emit_package, PacketTransitEvent: controller.transit_package, RoutingEvent: controller.routing } eloop.EventLoop( initial_events=controller.initial_events(), handlers=handlers, end_time=end_time, ).run() return controller