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 __init__(self, stupid=False): self.uuid = None self.ui = None self.net_cli = Client() if not stupid: self.ui = ui.SnakeUI()
def main(): args = sys.argv if len(args) != 4: print 'Insufficient Arguments' global rsHostname global rsListenPort global tsListenPort rsHostname = args[1] rsListenPort = int(args[2]) tsListenPort = int(args[3]) rsclient = Client() tsclient = Client() queryHostNames(rsclient, tsclient)
class RemoteNetCodingReader(InputStreamReader): def __init__(self, node_addr, block_id, coding_id, stream_id, nodes, **kwargs): nodes = ';'.join(map(str, nodes)) self.client = Client(*node_addr) self.header = DataNodeHeader.generate(DataNodeHeader.OP_CODING, block_id, coding_id, stream_id, nodes) self.client.send(self.header) super(RemoteNetCodingReader, self).__init__(self.client.recv_stream(), async=True, **kwargs) def finalize(self, kill=False): if not kill: self.client.assert_ack() super(RemoteNetCodingReader, self).finalize(kill)
def __init__(self, node_addr, block_id, coding_id, stream_id, nodes, **kwargs): nodes = ';'.join(map(str, nodes)) self.client = Client(*node_addr) self.header = DataNodeHeader.generate(DataNodeHeader.OP_CODING, block_id, coding_id, stream_id, nodes) self.client.send(self.header) super(RemoteNetCodingReader, self).__init__(self.client.recv_stream(), async=True, **kwargs)
def main(): args = sys.argv if len(args) != 3: print 'Insufficient Arguments' global lsHostname global lsListenPort lsHostname = args[1] lsListenPort = int(args[2]) lsclient = Client() queryHostNames(lsclient)
def run(): print(f'Enter the host name of the server [Default = {DEFAULT_SERVER_HOSTNAME}]:') hostname = input() or DEFAULT_SERVER_HOSTNAME print(f'Enter the number of messages per second [Default = {str(DEFAULT_MESSAGES_PER_SECOND)}]:') mps = int(input() or DEFAULT_MESSAGES_PER_SECOND) joystick = get_joystick() # cv_adjust = CVAdjust() # parameters = { # 'hostname': hostname, # 'data_to_send_func': joystick.get_serialized_info, # 'receiver_func': handle_input # } # x = SenderReceiver(**parameters) # x.run() while True: try: while True: try: client = Client(hostname=hostname, port=SERVER_PORT) break except Exception: print('Error happened while connecting to the server. Retrying in 1 second...') sleep(1) controller = JoystickController(client=client, joystick=joystick) clock = pygame.time.Clock() while True: clock.tick(mps) controller.get_and_send() except Exception: # TODO end the client. print('Error happened while sending the info. Possibly the server has disconnected.') print('Trying to connect again...') sleep(1)
def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = (800, 600) pygame.display.set_caption("Moonbase") self.done = False self.clock = pygame.time.Clock() self.keys = set() self.screen = pygame.display.set_mode(self.size) self.screen.fill((0xFF, ) * 3) self.clicked = 0 self.model = Model() self.ui = WaitUI(self, None) self.client = Client(self) self.client.start()
def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = (600, 450) # self.ui.screen_size() pygame.display.set_caption("Mapper") self.done = False self.clock = pygame.time.Clock() self.keys = set() self.map = MapDS() self.map.expand_room() self.screen = pygame.display.set_mode(self.size) self.screen.fill((0xFF, ) * 3) self.last = None self.clicked = 0 self.client = Client(self) self.ui = DungeonUI(self, None)
class SnakeClient(object): def __init__(self, stupid=False): self.uuid = None self.ui = None self.net_cli = Client() if not stupid: self.ui = ui.SnakeUI() def create(self): self.uuid = str(uuid.uuid1()) self.net_cli.set_uuid(self.uuid) self.net_cli.send(NEW_CLIENT) def get_world(self): res = self.net_cli.send(GET_WORLD) try: return json.loads(res) except json.decoder.JSONDecodeError: with open("dump", "w") as fl: print("Broken world recieved.") fl.write(res) return self.get_world() def commands_handler(self): cmd = self.ui.get_event() if cmd in [GO_UP, GO_DOWN, GO_RIGHT, GO_LEFT]: self.net_cli.send(cmd) if cmd == CLIENT_RESET: self.create() def display_map(self): w = self.get_world() self.ui.draw(w) def run(self): while True: self.commands_handler() self.display_map()
def __init__(self): self.client = Client() self.tubes = Tubes(10) #set the LED color to the default color self.led_color = (255, 255, 255) #create a counter for the HB time self.last_hb_time = 0 #create an action handler for acting on incoming messages self.actions = utils.ActionHandler() self.actions.add_action("REQUEST_REPORT\0", self._request_report) self.actions.add_action("SET_LED\0", self._set_led) self.actions.add_action("FIRE_TUBE\0", self._fire_tube) self.actions.add_action("HEARTBEAT\0", self._heartbeat) self.actions.add_action("RESPONSE\0", self._response) self.actions.add_action("REPORT\0", self._report) #extra trackers for internal use only self._last_fire_time = 0 self._LOAD_DELAY_S = 1 self._TIME_BETWEEN_LOADS_S = 0.1 self._last_load_time = time.time()
class SimNode(object): START_BYTE = 0xFE TIME_OUT_S = 1 def __init__(self): self.client = Client() self.tubes = Tubes(10) #set the LED color to the default color self.led_color = (255, 255, 255) #create a counter for the HB time self.last_hb_time = 0 #create an action handler for acting on incoming messages self.actions = utils.ActionHandler() self.actions.add_action("REQUEST_REPORT\0", self._request_report) self.actions.add_action("SET_LED\0", self._set_led) self.actions.add_action("FIRE_TUBE\0", self._fire_tube) self.actions.add_action("HEARTBEAT\0", self._heartbeat) self.actions.add_action("RESPONSE\0", self._response) self.actions.add_action("REPORT\0", self._report) #extra trackers for internal use only self._last_fire_time = 0 self._LOAD_DELAY_S = 1 self._TIME_BETWEEN_LOADS_S = 0.1 self._last_load_time = time.time() def time_since_fire(self): return time.time() - self._last_fire_time def time_since_HB(self): actualTime = time.time() - self.last_hb_time return actualTime if actualTime <= 4294967295 else 4294967295 #what do we have to do? def main(self): #we need to check the state of the tubes and update tube_state #for the simulation we are going to just assume that the tubes get #reloaded at some rate some time after the last fire command. self._update_tubes() #we need to check for and handle new messages if self.client.is_connected(): try: incoming = self.client.receive(message.parser) if incoming is not None: self.actions.do_action(incoming.id, incoming) else: pass except Exception as e: print "Exception occured while trying to process messages" print e self.client.close() else: time.sleep(0.01) """ Each of the "handler methods" below are for internal use only and are used to orginize the behivors that are linked to each incoming message type. """ def _fire_tube(self, incoming): tube_number = incoming.tube_number if self.time_since_HB() < self.TIME_OUT_S: if tube_number <= self.tubes.get_num_tubes(): success = self.tubes.fire_tube(tube_number) if success: #print "firing tube number", tube_number print "firing tube:", tube_number self._last_fire_time = time.time() response = message.MsgResponse(success, 1) else: #print "Tube is not loaded", tube_number response = message.MsgResponse(success, 4) else: #print "Tube number does not exist" response = message.MsgResponse(0, 2) else: #print "Heatbeat has expired" response = message.MsgResponse(0, 0) self.client.send(response) def _response(self, incoming): print "in response, code is", incoming.isAck, incoming.flags def _request_report(self, incoming): print "in request report" self.client.send(message.MsgResponse(1, 0)) report_msg = message.MsgReport(self.tubes.get_num_tubes(), self.tubes.get_tubes(), self.led_color, self.time_since_HB()) self.client.send(report_msg) def _set_led(self, incoming): print "in set led", incoming.red, incoming.green, incoming.blue self.client.send(message.MsgResponse(1,1)) self.led_color = (incoming.red, incoming.green, incoming.blue) def _heartbeat(self, incoming): print "in heartbeat at time", time.time() self.last_hb_time = time.time() self.client.send(message.MsgResponse(1, 0)) def _report(self, incoming): print "wtf is the master sending report messages?" """ Update the state of the tubes. For the simulation all this will do is check for unloaded tubes and load a random tube. """ def _update_tubes(self): if self.time_since_fire() > self._LOAD_DELAY_S: if time.time() - self._last_load_time > self._TIME_BETWEEN_LOADS_S: if self.tubes.get_num_empty() > 0: tube_num = random.choice(self.tubes.get_empty_tubes()) self.tubes.load_tube(tube_num) self._last_load_time = time.time() print self.tubes.get_num_tubes() - self.tubes.get_num_empty(), "+", tube_num def close(self): self.client.close()
from networking.Client import * import numpy as np if __name__ == "__main__": robot = Client() for i in range(5): # compute grasp coords and bounding boxes grasp_coords = [[[-.1], [-.2], [.5]], [[0], [-.2], [.5]]] # send object containing grasp coords to the computer data_packet = grasp_coords robot.send_data(data_packet) response = robot.listen() print(response)
def handle_message(user_uuid): cli = Client() cli.set_uuid(user_uuid) resp = cli.send(GET_WORLD) emit("map", resp)
def join_server(self, host=None): self.client = Client(self, host) self.client.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()
class Main: def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = (800, 600) pygame.display.set_caption("Moonbase") self.done = False self.clock = pygame.time.Clock() self.keys = set() self.screen = pygame.display.set_mode(self.size) self.screen.fill((0xFF, ) * 3) self.clicked = 0 self.model = Model() self.ui = WaitUI(self, None) self.client = Client(self) self.client.start() def ui_push(self, cls): self.ui = cls(self, self.ui) def ui_pop(self): self.ui = self.ui.parent def ui_swap(self, cls): self.ui = cls(self, self.ui.parent) def stop(self): self.done = True self.client.stop() 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: if event.key == pygame.K_q and pygame.key.get_mods() & pygame.KMOD_CTRL: self.stop() self.keys.add(event.key) self.ui.handle_key(event) elif event.type == pygame.KEYUP: self.keys.discard(event.key) 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.update() self.ui.reblit(self.screen, self.clock.get_time()) pygame.display.flip() pygame.quit()
def handle_message(data): cli = Client() client_uuid, command = data.split("|") cli.set_uuid(client_uuid) cli.send(NEW_CLIENT) if command == "up": cli.send(GO_UP) elif command == "down": cli.send(GO_DOWN) elif command == "left": cli.send(GO_LEFT) elif command == "right": cli.send(GO_RIGHT) elif command == "r": cli.send(NEW_CLIENT)
from networking import Client client = Client() client.connect('localhost', 9234) client.send_data("Hello!") client.send_input()