def main(): parser = argparse.ArgumentParser( description="Peer Daemon for PDS18 P2P Chat") parser.add_argument("-i", "--id", type=tools.id_check, required=True, metavar="<id>", help="unique id of the peer") parser.add_argument("-u", "--username", required=True, metavar="<username>", help="unique username of the peer") parser.add_argument( "-ci", "--chat-ipv4", type=tools.ip_check, required=True, metavar="<ip addr>", help="IP address on which the chat peer should listen for messages") parser.add_argument( "-cp", "--chat-port", type=tools.port_check, required=True, metavar="<port>", help="port on which the chat peer should listen for messages") parser.add_argument("-ri", "--reg-ipv4", type=tools.ip_check, required=True, metavar="<ip addr>", help="IP address of registration node") parser.add_argument( "-rp", "--reg-port", type=tools.port_check, required=True, metavar="<port>", help="port on which registration node listens for connections") args = parser.parse_args() try: daemon = PeerDaemon(args) except OSError as err: tools.err_print("OS error: {0}".format(err)) return 1 try: tools.run_server(daemon, args.id % 19991 + 10000) except KeyboardInterrupt: tools.err_print("\nStopping daemon...") daemon.finish() tools.err_print("Bye.")
def main(): parser = argparse.ArgumentParser(description="Node Daemon for PDS18 P2P Chat") parser.add_argument("-i", "--id", type = tools.id_check, required = True, metavar = "<id>", help = "unique id of the node") parser.add_argument("-ri", "--reg-ipv4", type = tools.ip_check, required = True, metavar = "<ip addr>", help = "IP address of the node") parser.add_argument("-rp", "--reg-port", type = tools.port_check, required = True, metavar = "<port>", help = "port on which the node should listen") args = parser.parse_args() try: daemon = NodeDaemon(args) except OSError as err: tools.err_print("OS error: {0}".format(err)) return 1 try: tools.run_server(daemon, args.id % 19991 + 10000) except KeyboardInterrupt: tools.err_print("\nStopping daemon...") daemon.finish() tools.err_print("Bye.")
def run(self): ack_wait_event = threading.Event() self.packet_queue.queue_message(tools.create_packet("getlist"), self.node, 1, 2, ack_event=ack_wait_event) if not ack_wait_event.wait(2): tools.err_print("ERROR: GETLIST not ACK'ed.") return if self.peerlist.update_event.wait(2): address = self.peerlist.get_address(self.message["to"]) if not address == None: self.packet_queue.queue_message(self.message, address, 2, 2) else: tools.err_print( "ERROR: No peer with username {} found.".format( self.message["to"])) else: tools.err_print("ERROR: No LIST recieved.")
def timeout_hanle(self): """ method for handeling of timeouts """ while True: try: #delete inactive peers to_delete = [] for i, item in enumerate(self.local_peers_timeouts): if (timer() - item) > 30: to_delete.append(i) for i, item in enumerate(reversed(to_delete)): del self.local_peers[i] del self.local_peers_timeouts[i] #delete inactive nodes to_delete = [] for i, item in enumerate(self.nodes_timeouts.keys()): if (timer() - self.nodes_timeouts[item]) > 12: to_delete.append(item) for i, item in enumerate(to_delete): del self.nodes[item] del self.nodes_timeouts[item] del self.nodes_last_notification[item] #waiting for ACKs to_delete = [] for i, item in enumerate(self.acks.keys()): if (timer() - self.acks[item][0]) > 2: msg = Message_Error('error', self.txid, "ACK for message with txid {} wasn't recieved." .format(item)) msg_b = msg.encoded_msg() self.send_message_to(msg_b, self.acks[item][2], self.acks[item][3]) err_print("ACK for message with txid {} wasn't recieved." .format(item)) to_delete.append(item) for i, item in enumerate(to_delete): del self.acks[item] #send notification to nodes for i, item in enumerate(self.nodes_last_notification.keys()): if (timer() - self.nodes_last_notification[item]) > 4: recs = Db_Records() records = Peer_Records() for e, elem in enumerate(self.local_peers): records.add_record(elem) rec_db = Db_Record(self.reg_ipv4, self.reg_port, records) recs.records.append(rec_db) for e, elem in enumerate(self.nodes.keys()): if self.nodes[elem] != -1: rec_db = Db_Record(elem.split(",")[0], int(elem.split(",")[1]), self.nodes[elem]) recs.records.append(rec_db) msg = Message_Update('update', self.txid, recs) msg_b = msg.encoded_msg() self.send_message_to(msg_b, item.split(",")[0], int(item.split(",")[1])) self.nodes_last_notification[item] = timer() time.sleep(0.5) except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] err_print(exc_type, fname, exc_tb.tb_lineno)
def listen_pipe(self): """ method for handeling of named pipe listening """ while True: try: with open(self.pipe) as p: data = p.read() data = data.split() if self.debug: err_print(data) if data[0] == "database": counter = 0 string = "{" for i, item in enumerate(self.local_peers): string += str(item) + ',' counter += 1 for i, item in enumerate(self.nodes.keys()): if self.nodes[item] != -1: for e, elem in enumerate(self.nodes[item].records): string += str(elem) + ',' counter += 1 if counter != 0: string = string[:-1] string += "}" err_print(string) elif data[0] == "neighbors": string = "{" for i, item in enumerate(self.nodes.keys()): string += "{\'ipv4\': \'" + item.split(',')[0] + "\', \'port\': " + str(int(item.split(',')[1])) + "}" string += ',' if len(self.nodes) != 0: string = string[:-1] string += "}" err_print(string) elif data[0] == "connect": recs = Db_Records() records = Peer_Records() for i, item in enumerate(self.local_peers): records.add_record(item) rec_db = Db_Record(self.reg_ipv4, self.reg_port, records) recs.records.append(rec_db) for i, item in enumerate(self.nodes.keys()): if self.nodes[item] != -1: rec_db = Db_Record(item.split(",")[0], int(item.split(",")[1]), self.nodes[item]) recs.records.append(rec_db) msg = Message_Update('update', self.txid, recs) msg_b = msg.encoded_msg() self.send_message_to(msg_b, data[1], int(data[2])) self.nodes_last_notification[data[1]+','+str(data[2])] = timer() elif data[0] == "disconnect": for i, item in enumerate(list(self.nodes.keys())): msg = Message_Disconnect('disconnect', self.txid) msg_b = msg.encoded_msg() self.acks[self.txid] = [timer(), "disconnect", item.split(",")[0], int(item.split(",")[1])] self.send_message_to(msg_b, item.split(",")[0], int(item.split(",")[1])) self.nodes = {} self.nodes_timeouts = {} self.nodes_last_notification = {} elif data[0] == "sync": recs = Db_Records() records = Peer_Records() for i, item in enumerate(self.local_peers): records.add_record(item) rec_db = Db_Record(self.reg_ipv4, self.reg_port, records) recs.records.append(rec_db) for i, item in enumerate(self.nodes.keys()): if self.nodes[item] != -1: rec_db = Db_Record(item.split(",")[0], int(item.split(",")[1]), self.nodes[item]) recs.records.append(rec_db) for i, item in enumerate(self.nodes.keys()): msg = Message_Update('update', self.txid, recs) msg_b = msg.encoded_msg() self.send_message_to(msg_b, item.split(",")[0], int(item.split(",")[1])) self.nodes_last_notification[item] = timer() else: #ignore other pipe messages pass except Exception as e: if type(e).__name__ != 'FileNotFoundError': #pipe is not created exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] err_print(exc_type, fname, exc_tb.tb_lineno)
def listen_reg(self): """ method for handeling of network listening """ while True: try: data, addr = self.reg_sock.recvfrom(buffer_size) data = decode(data) if self.debug: err_print(addr, data) type_ = data[str.encode("type")] type_ = type_.decode('UTF-8') if type_ == "hello": if data[str.encode("ipv4")].decode('UTF-8') == "0.0.0.0" and data[str.encode("port")] == 0: for i, item in enumerate(self.local_peers): if data[str.encode("username")].decode('UTF-8') == item.username_: del self.local_peers[i] del self.local_peers_timeouts[i] else: record = Peer_Record(data[str.encode("username")], data[str.encode("ipv4")], data[str.encode("port")], bytes_=True) existing = False for i, item in enumerate(self.local_peers): if item.username_ == record.username_ and item.ipv4_ == record.ipv4_ and item.port_ == record.port_: self.local_peers_timeouts[i] = timer() existing = True if not existing: self.local_peers.append(record) self.local_peers_timeouts.append(timer()) elif type_ == "getlist": is_authorized = False for i, item in enumerate(self.local_peers): if item.ipv4_ == addr[0] and item.port_ == addr[1]: is_authorized = True self.acknowlidge(addr[0], addr[1], data[str.encode("txid")]) if not is_authorized: msg = Message_Error('error', data[str.encode("txid")], "You aren't logged to this node, you don't have a permission to get LIST.") encoded = msg.encoded_msg() self.send_message_to(msg_b, addr[0], addr[1]) err_print("Not logged peer asked for LIST message.") else: records = Peer_Records() for i, item in enumerate(self.local_peers): records.add_record(item) for i, item in enumerate(self.nodes.keys()): if self.nodes[item] != -1: for i, item in enumerate(self.nodes[item].records): records.add_record(item) msg = Message_List('list', self.txid, records) msg_b = msg.encoded_msg() self.acks[self.txid] = [timer(), "list", addr[0], addr[1]] self.send_message_to(msg_b, addr[0], addr[1]) elif type_ == "error": err_print(data[str.encode("verbose")].decode('UTF-8')) elif type_ == "update": if (addr[0]+','+str(addr[1])) in self.nodes.keys(): new = False else: new = True for e, elem in enumerate(sorted(data[str.encode("db")].keys())): #authoritative record if elem.decode('UTF-8').split(",")[0] == addr[0] and int(elem.decode('UTF-8').split(",")[1]) == addr[1]: p_records = Peer_Records() for i in range(len(data[str.encode("db")][elem].keys())): rec_l = Peer_Record(data[str.encode("db")][elem][str.encode(str(i))][str.encode("username")], data[str.encode("db")][elem][str.encode(str(i))][str.encode("ipv4")], data[str.encode("db")][elem][str.encode(str(i))][str.encode("port")], bytes_=True) p_records.add_record(rec_l) self.nodes[elem.decode('UTF-8')] = p_records self.nodes_timeouts[elem.decode('UTF-8')] = timer() #nonauthoritative record else: if elem.decode('UTF-8').split(",")[0] == self.reg_ipv4 and int(elem.decode('UTF-8').split(",")[1]) == self.reg_port: pass elif elem.decode('UTF-8') not in self.nodes: self.nodes[elem.decode('UTF-8')] = -1 self.nodes_timeouts[elem.decode('UTF-8')] = timer() for i, item in enumerate(self.nodes.keys()): if self.nodes[item] == -1 or (addr[0] == item.split(",")[0] and addr[1] == int(item.split(",")[1]) and new): recs = Db_Records() records = Peer_Records() for e, elem in enumerate(self.local_peers): records.add_record(elem) rec_db = Db_Record(self.reg_ipv4, self.reg_port, records) recs.records.append(rec_db) for e, elem in enumerate(self.nodes.keys()): if self.nodes[elem] != -1: rec_db = Db_Record(elem.split(",")[0], int(elem.split(",")[1]), self.nodes[elem]) recs.records.append(rec_db) msg = Message_Update('update', self.txid, recs) msg_b = msg.encoded_msg() self.send_message_to(msg_b, item.split(",")[0], int(item.split(",")[1])) self.nodes_last_notification[item] = timer() elif type_ == "disconnect": if addr[0]+','+str(addr[1]) in self.nodes: del self.nodes[addr[0]+','+str(addr[1])] del self.nodes_timeouts[addr[0]+','+str(addr[1])] del self.nodes_last_notification[addr[0]+','+str(addr[1])] self.acknowlidge(addr[0], addr[1], data[str.encode("txid")]) elif type_ == "ack": if data[str.encode("txid")] in self.acks: del self.acks[data[str.encode("txid")]] else: err_print("Unexpected ACK with txid {} received." .format(data[str.encode("txid")])) else: #ignore other messages pass except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] err_print(exc_type, fname, exc_tb.tb_lineno)
def get_args(): """ method for parsing of arguments """ parser = argparse.ArgumentParser() parser.add_argument( "-init", action="store_true", default=False, help="init replay memory with random agent observations") parser.add_argument("-mem", action="store", dest="memory", choices=["basic", "prioritized"], default="basic", help="type of memory") parser.add_argument("-net", action="store", dest="network", choices=["basic", "dueling", "experimental"], default="basic", help="type of neural network architecture") parser.add_argument("-alg", action="store", dest="algorithm", choices=["DQN", "DQN+TN", "DDQN"], default="DQN", help="type of algorithm") parser.add_argument("-env", action="store", dest="environment", required=True, choices=[ "CartPole-v0", "CartPole-v1", "MountainCar-v0", "Acrobot-v1", "2048-v0", "Breakout-v0", "SpaceInvaders-v0", "BeamRider-v0", "Breakout-ram-v0", "SpaceInvaders-ram-v0", "BeamRider-ram-v0" ], help="name of game environment") parser.add_argument("-eps", action="store", dest="episodes", type=int, required=True, help="number of training episodes") parser.add_argument( "-mdl", action="store", dest="model", help="name of file which contains already trained model to load") parser.add_argument("-pu", action="store", dest="process_unit", choices=["CPU", "GPU"], default="CPU", help="script will run on this processing unit") parser.add_argument("-vids", action="store_true", default=False, help="script will record videos using gym.wrappers") parser.add_argument("-update_f", action="store", dest="update_f", type=int, default=10000, help="frequency of updating target net") parser.add_argument("-save_f", action="store", dest="save_f", type=int, default=25, help="frequency of saving model") parser.add_argument( "-frames", action="store", dest="frames", type=int, default=1, choices=[1, 2, 3, 4], help="Number of frames to process as a information of state.") parser.add_argument("-mode", action="store", dest="mode", required=True, choices=["train", "test", "render"], default="train", help="application mode") args = parser.parse_args() basic = [ "CartPole-v0", "CartPole-v1", "MountainCar-v0", "Acrobot-v1", "2048-v0" ] image = ["Breakout-v0", "SpaceInvaders-v0", "BeamRider-v0"] ram = ["Breakout-ram-v0", "SpaceInvaders-ram-v0", "BeamRider-ram-v0"] if args.network == "basic": if args.environment in basic: args.network = "2layer_bsc_mdl" if args.environment in image: args.network = "bsc_img_mdl" if args.environment in ram: args.network = "1layer_ram_mdl" if args.network == "dueling": if args.environment in basic: args.network = "2layer_duel_mdl" if args.environment in image: args.network = "duel_img_model" if args.environment in ram: err_print("There is no dueling model for this environment.") sys.exit(-1) if args.model is not None: if not os.path.isfile(args.model): err_print("Model file doesn't exist.") sys.exit(-1) if args.frames != 1 and args.environment not in image: err_print( "Application doesn't support buffer of states for this environment." ) sys.exit(-1) elif args.frames == 1 and args.environment in image: err_print("Application needs buffer of states for this environment.") sys.exit(-1) return args
force=True) if task.args.mode == "train": task, true_scores, episodes_numbers, _, normalized_scores = train( task, True) task.agent.save_model_weights("{}-last.h5".format(task.name)) task.agent.save_target_weights("{}-last.h5".format(task.name)) try: combined_graph(true_scores, episodes_numbers, "{}_results".format(task.name), [episodes_numbers[-1] + 10, max(true_scores) + 10], {task.average_rand_score: task.average_rand_score}, True) except ValueError: err_print("[Not enough values to make graph.]") elif task.args.mode == "test": agent_score_estimate(task, task.args.episodes, render=False, show_bar=True) elif task.args.mode == "render": agent_score_estimate(task, task.args.episodes, render=True, show_bar=True) K.clear_session() print("[SUCCESSFUL RUN]")
def listen_pipe(self): """ method for handeling of named pipe listening """ while True: try: with open(self.pipe) as p: data = p.read() data = data.split() if self.debug: err_print(data) if data[0] == "message": self.actual_peers = False msg = Message_GetList('getlist', self.txid) msg_b = msg.encoded_msg() stamp = self.txid self.acks[stamp] = [ timer(), "list", self.reg_ipv4, self.reg_port ] self.send_message(msg_b) while True: if stamp not in self.acks and stamp not in self.missing_acks: break start = timer() while True: if self.actual_peers or (timer() - start) > 2: break if data[1] == self.username: reachable = False for i, item in enumerate(self.peers.records): if data[2] == item.username_: reachable = True text = "" for e, elem in enumerate(data[3:]): if e == 0: text += elem else: text += " " + elem msg = Message_Message('message', self.txid, self.username, item.username_, text) msg_b = msg.encoded_msg() self.acks[self.txid] = [ timer(), "message", item.ipv4_, item.port_ ] self.send_message_to(msg_b, item.ipv4_, item.port_) if not reachable: err_print("User is unreachable.") elif data[0] == "getlist": msg = Message_GetList('getlist', self.txid) msg_b = msg.encoded_msg() self.print_ack = True self.acks[self.txid] = [ timer(), "list", self.reg_ipv4, self.reg_port ] self.send_message(msg_b) elif data[0] == "peers": msg = Message_GetList('getlist', self.txid) msg_b = msg.encoded_msg() self.print_nextlist = True self.acks[self.txid] = [ timer(), "list", self.reg_ipv4, self.reg_port ] self.send_message(msg_b) elif data[0] == "reconnect": msg = Message_Hello('hello', self.txid, self.username, '0.0.0.0', 0) msg_b = msg.encoded_msg() self.send_message(msg_b) self.reg_ipv4 = data[1] self.reg_port = int(data[2]) msg = Message_Hello('hello', self.txid, self.username, self.chat_ipv4, self.chat_port) msg_b = msg.encoded_msg() self.send_message(msg_b) else: #ignore other pipe messages pass except Exception as e: if type( e ).__name__ != 'FileNotFoundError': #pipe is not created exception exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split( exc_tb.tb_frame.f_code.co_filename)[1] err_print(exc_type, fname, exc_tb.tb_lineno)
def listen_chat(self): """ method for handeling of network listening """ while True: try: data, addr = self.chat_sock.recvfrom(buffer_size) data = decode(data) if self.debug: err_print(data) type_ = data[str.encode("type")] type_ = type_.decode('UTF-8') if type_ == "error": err_print(data[str.encode("verbose")].decode('UTF-8')) elif type_ == "list": self.peers = Peer_Records() for i, item in enumerate(data[str.encode("peers")].keys()): rec = Peer_Record( data[str.encode("peers")][str.encode( item.decode('UTF-8'))][str.encode("username")], data[str.encode("peers")][str.encode( item.decode('UTF-8'))][str.encode("ipv4")], data[str.encode("peers")][str.encode( item.decode('UTF-8'))][str.encode("port")], bytes_=True) self.peers.add_record(rec) self.actual_peers = True if self.print_nextlist: string = "{" for i, item in enumerate(self.peers.records): string += str(item) + ',' if self.peers != 0: string = string[:-1] string += "}" err_print(string) self.print_nextlist = False self.acknowlidge(addr[0], addr[1], data[str.encode("txid")]) elif type_ == "message": print(data[str.encode("message")].decode('UTF-8')) self.acknowlidge(addr[0], addr[1], data[str.encode("txid")]) elif type_ == "ack": if data[str.encode("txid")] in self.acks: del self.acks[data[str.encode("txid")]] else: err_print( "Unexpected ACK with txid {} received.".format( data[str.encode("txid")])) if self.print_ack: err_print("Message GETLIST with txid {} acknowlidged.". format(data[str.encode("txid")])) self.print_ack = False else: #ignore other messages pass except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] err_print(exc_type, fname, exc_tb.tb_lineno)