def run(self): """ The main loop of the program. Code design suggestions: 1. Parse server in_buf of the stream. 2. Handle all packets were received from our Stream server. 3. Parse user_interface_buffer to make message packets. 4. Send packets stored in nodes buffer of our Stream object. 5. ** sleep the current thread for 2 seconds ** Warnings: 1. At first check reunion daemon condition; Maybe we have a problem in this time and so we should hold any actions until Reunion acceptance. 2. In every situation checkout Advertise Response packets; even is Reunion in failure mode or not :return: """ try: while True: in_buff = self.stream.read_in_buf() for message in in_buff: packet = PacketFactory.parse_buffer(message) self.handle_packet(packet) self.stream.clear_in_buff() self.handle_user_interface_buffer() self.stream.send_out_buf_messages( self.reunion_mode == ReunionMode.FAILED) time.sleep(2) except KeyboardInterrupt: log('KeyboardInterrupt') try: sys.exit(0) except SystemExit: os._exit(0)
def _predictChord(self, history): """ Predicts the next chord from a history. """ if self._structure[self._instruments.top].expected(history): return self._structure[self._instruments.top].expectedFuzz(history) logger.log("Failed to find prediction for history %s" % str(history)) return random.choice(self._structure[self._instruments.top].sampleSpace)
def get_argument(args, arg_name): try: return args[arg_name] except (KeyError, IndexError): logger.log("Key not found: " + str(arg_name), "Arg Fetcher", logger.WARNING) return None
def __handle_reunion_hello_back(self, packet: Packet): if packet.get_addresses()[-1] == self.address: # It's our hello back! self.last_hello_back_time = time.time() log('We received our HelloBack.') else: self.__pass_reunion_hello_back(packet)
def __handle_message_packet(self, packet: Packet): """ Only broadcast message to the other nodes. Warnings: 1. Do not forget to ignore messages from unknown sources. 2. Make sure that you are not sending a message to a register_connection. :param packet: Arrived message packet :type packet Packet :return: """ log(f'New message arrived: {packet.get_body()}') sender_address = packet.get_source_server_address() updated_packet = PacketFactory.new_message_packet( packet.get_body(), self.address) if self.__check_neighbour(sender_address): # From known source for neighbor_address in [ *self.children_addresses, self.parent_address ]: if neighbor_address is not None and neighbor_address != sender_address: self.stream.add_message_to_out_buff( neighbor_address, updated_packet)
def __pass_reunion_hello_back(self, packet: Packet): new_addresses = packet.get_addresses()[1:] next_node_address = new_addresses[0] log(f'HelloBack packet passed down to Node({next_node_address}).') passed_packet = PacketFactory.new_reunion_packet( ReunionType.RES, self.address, new_addresses) self.stream.add_message_to_out_buff(next_node_address, passed_packet)
def __init__(self, code=500, msg="", args=None, log=True, variant="error"): self.code = code self.msg = msg.replace('"', "'") self.args = args if args else dict() if log: logger.log(code, msg, variant)
def handle_packet(self, packet): """ This function act as a wrapper for other handle_###_packet methods to handle the packet. Code design suggestion: 1. It's better to check packet validation right now; For example Validation of the packet length. :param packet: The arrived packet that should be handled. :type packet Packet """ if not self.__validate_received_packet(packet): return packet_type = packet.get_type() log(f'Packet of type {packet_type.name} received.') if self.reunion_mode == ReunionMode.FAILED: if packet_type == PacketType.ADVERTISE: self.__handle_advertise_packet(packet) return if packet_type == PacketType.MESSAGE: self.__handle_message_packet(packet) elif packet_type == PacketType.ADVERTISE: self.__handle_advertise_packet(packet) elif packet_type == PacketType.JOIN: self.__handle_join_packet(packet) elif packet_type == PacketType.REGISTER: self.__handle_register_packet(packet) elif packet_type == PacketType.REUNION: self.__handle_reunion_packet(packet)
def __handle_register_packet(self, packet: Packet): """ For registration a new node to the network at first we should make a Node with stream.add_node for'sender' and save it. Code design suggestion: 1.For checking whether an address is registered since now or not you can use SemiNode object except Node. Warnings: 1. Don't forget to ignore Register Request packets when you are a non-root peer. :param packet: Arrived register packet :type packet Packet :return: """ register_type = self.__identify_register_type(packet) if self.is_root and register_type == RegisterType.REQ: new_node = SemiNode(packet.get_source_server_ip(), packet.get_source_server_port()) if new_node in self.registered: return self.registered.append(new_node) sender_address = packet.get_source_server_address() self.stream.add_node(sender_address, set_register_connection=True) register_response_packet = PacketFactory.new_register_packet( RegisterType.RES, self.address) self.stream.add_message_to_out_buff(sender_address, register_response_packet, want_register=True) elif register_type == RegisterType.RES: log('Register request ACKed by root. You are now registered.')
def handle_advertise_command(self) -> None: advertise_packet = PacketFactory.new_advertise_packet( AdvertiseType.REQ, self.address) self.stream.add_message_to_out_buff(self.root_address, advertise_packet, want_register=True) log(f'Advertise packet added to out buff of Node({self.root_address}).' )
def _predictChord(self, history): """ Predicts the next chord from a history. """ if self._structure[self._instruments.top].expected(history): return self._structure[self._instruments.top].expectedFuzz(history) logger.log("Failed to find prediction for history %s" % str(history)) return random.choice( self._structure[self._instruments.top].sampleSpace)
def filter_chat_message(msg, client_id): if client_id == -1: if msg.startswith("/"): Main.Command(msg, client_id) return None return msg acid = "" displaystring = "" currentname = "" for i in _ba.get_game_roster(): if i['client_id'] == client_id: acid = i['account_id'] try: currentname = i['players'][0]['name_full'] except: currentname = "<in-lobby>" displaystring = i['display_string'] if acid: msg = ChatFilter.filter(msg, acid, client_id) if msg.startswith("/"): return Main.Command(msg, client_id) if msg.startswith(",") and settings["allowTeamChat"]: return Main.QuickAccess(msg, client_id) logger.log(acid + " | " + displaystring + "|" + currentname + "| " + msg, "chat") if acid in serverdata.clients and serverdata.clients[acid]["verified"]: if serverdata.muted: _ba.screenmessage("Server on mute", transient=True, clients=[client_id]) return elif serverdata.clients[acid]["isMuted"]: _ba.screenmessage("You are on mute", transient=True, clients=[client_id]) return None elif servercheck.get_account_age(serverdata.clients[acid]["accountAge"] ) < settings['minAgeToChatInHours']: _ba.screenmessage("New accounts not allowed to chat here", transient=True, clients=[client_id]) return None else: return msg else: _ba.screenmessage("Fetching your account info , Wait a minute", transient=True, clients=[client_id]) return None
def start_user_interface(self) -> None: """ For starting UserInterface thread. :return: """ if not self.is_root: log('UserInterface started.') self.user_interface.start()
def _predictNote(self, realHistory, partName): """ Predicts the next note from a history that is being provided. """ normalizedHistory = music42.normalizeNotes(realHistory) if self._melody[partName].expected(normalizedHistory): return music42.denormalizeNote(self._melody[partName].expectedFuzz(normalizedHistory), realHistory) logger.log("Failed to find prediction for history %s" % (normalizedHistory,)) return random.choice(self._alphabets[partName])
def __register(self) -> None: if self.stream.add_node(self.root_address, set_register_connection=True): register_packet = PacketFactory.new_register_packet( RegisterType.REQ, self.address) self.stream.add_message_to_out_buff(self.root_address, register_packet, want_register=True) log(f'Register packet added to out buff of Node({self.root_address}).' )
def check_heart_beat(ws): should_exit = False while not should_exit: time.sleep(10) now = time.time() for _, last_tick in last_heart_beat_map.items(): # If 10 seconds a topic missed, then should reconnect if now - last_tick > 10: log('Not got pong, exit process') ws.close() should_exit = True
def signup(self): u"""用户注册""" param = { "p_username": self.username, "p_mobile": self.mobile, "p_password": Encrypt().encrypt(self.password, 'SHA1') } response = self._signandpost(param, 'AddUser') log(response, 'Sign Up', 'info') return json.loads(response)['p_userid']
def _predictNote(self, realHistory, partName): """ Predicts the next note from a history that is being provided. """ normalizedHistory = music42.normalizeNotes(realHistory) if self._melody[partName].expected(normalizedHistory): return music42.denormalizeNote( self._melody[partName].expectedFuzz(normalizedHistory), realHistory) logger.log("Failed to find prediction for history %s" % (normalizedHistory, )) return random.choice(self._alphabets[partName])
def callback(address, queue, data): """ The callback function will run when a new data received from server_buffer. :param address: Source address. :param queue: Response queue. :param data: The data received from the socket. :return: """ log('New data received.') queue.put(bytes('ACK', 'utf8')) self._server_in_buf.append(data)
def save_ids(ids, pb_id, display_string): pdata.update_display_string(pb_id, ids) if display_string not in ids: msg = "Spoofed Id detected , Goodbye" _ba.pushcall(Call(kick_by_pb_id, pb_id, msg), from_other_thread=True) serverdata.clients[pb_id]["verified"] = False logger.log(pb_id + "|| kicked , for using spoofed id " + display_string) else: serverdata.clients[pb_id]["verified"] = True
def save_age(age, pb_id, display_string): pdata.add_profile(pb_id, display_string, display_string, age) time.sleep(2) thread2 = FetchThread(target=get_device_accounts, callback=save_ids, pb_id=pb_id, display_string=display_string) thread2.start() if get_account_age(age) < settings["minAgeToJoinInHours"]: msg = "New Accounts not allowed to play here , come back tmrw." logger.log(pb_id + "|| kicked > new account") _ba.pushcall(Call(kick_by_pb_id, pb_id, msg), from_other_thread=True)
def __run_root_reunion_daemon(self): graph_nodes = self.network_graph.nodes for graph_node in graph_nodes: if graph_node.address == self.address: continue time_passed_since_last_hello = time.time() - graph_node.last_hello log(f'Time passed since last hello from Node({graph_node.address}): {time_passed_since_last_hello}' ) if time_passed_since_last_hello > MAX_HELLO_INTERVAL: self.stream.remove_node( self.stream.get_node_by_address(graph_node.address[0], graph_node.address[1])) self.network_graph.remove_node(graph_node.address)
def send_message(self) -> None: """ Final function to send buffer to the client's socket. :return: """ for packet in self.out_buff: response = self.client.send(packet.get_buf()) if response != b'ACK': log(f"Node({self.get_server_address()}): Message of type {packet.get_type()} not ACKed." ) self.out_buff.clear()
def __handle_advertise_response(self, packet: Packet) -> None: self.last_hello_time = time.time() self.last_hello_back_time = time.time() parent_address = packet.get_advertised_address() log(f'Trying to join Node({parent_address})...') self.parent_address = parent_address join_packet = PacketFactory.new_join_packet(self.address) self.stream.add_node( parent_address) # Add a non_register Node to stream to the parent log(f'Join Request added to out buf on Node({parent_address}).') self.stream.add_message_to_out_buff(parent_address, join_packet) self.reunion_mode = ReunionMode.ACCEPTANCE if not self.reunion_daemon.is_alive(): self.reunion_daemon.start()
def dump(dataType, data, *corpusID): """ Dumps an analysis of a certain corpus to `dumps/<corpusID>/<dataType>.p`. >>> data = "test" >>> dump('melody', data, 'nonsense') True Test to see which music21 objects cause trouble. >>> from music21 import note >>> noteData = note.Note('A4') >>> dump('melody', noteData, 'weakref') True >>> restData = note.Rest('half') >>> dump('melody', restData, 'weakref') True This (Note and Rest) seems to have worked. Next. >>> from music21 import stream >>> measureData = stream.Measure() It works for empty streams. >>> dump('melody', measureData, 'weakref') True But not for streams with children. # >>> measureData.append(noteData) # >>> dump('melody', measureData, 'weakref') # True Boom. But we don't use streams in the Note ngrams. So what's the dealio? # >>> noteData.quarterLength = 3 # >>> dump('melody', noteData, 'weakref') # True OK, got it. Durations seems to be saved as weakrefs, too. Avoided the issue by creating "normalized" note objects. <http://docs.python.org/library/pickle.html#pickling-and-unpickling-extension-types> God save the Queen, pickling is already supported in music21: <http://code.google.com/p/music21/source/browse/trunk/music21/converter.py> """ try: with open(_pathForDump(corpusID, dataType, True), 'w') as f: try: pickle.dump(data, f) return True except pickle.PicklingError: logger.log("%s can not be pickled." % (data)) return False except IOError: logger.log("Couldn't open file for writing.") return False
def on_open(ws): log("### opened ###") # Ticker for currency in CURRENCY_LIST: ws.send('{"event":"subscribe", "channel":"ticker", "pair":"' + currency + 'USD"}') # ws.send('{"event":"subscribe", "channel":"trades", "pair":"' + currency + 'USD"}') # ws.send('{"event":"subscribe", "channel":"book", "pair":"' + currency + 'USD", "prec":"P0", "freq":"F0"}') # Health Beat global checker_process checker_process = Process(target=check_heart_beat, args=(ws, )) checker_process.start()
def __init__(self, mobile=None, username=None, password=None): u"""如果没有同时传入mobile,username和password,则随机生成,用户名与密码等于随机生成的手机号""" if mobile is None and username is None and password is None: r_phone = random_phone_number() self.mobile = self.username = self.password = r_phone elif password is not None and username is None and mobile is None: r_phone = random_phone_number() self.mobile = self.username = r_phone self.password = password else: self.mobile = mobile self.username = username self.password = password # 把初始化的用户信息打印出来以供阅读 log(self.__repr__(), 'Init', 'info') print self
def bind(self): u"""商户审核通过后,绑定best,如果best绑定失败,换一个best账号,继续绑定,直到绑定成功,成功后可开通店铺""" param = {'user_id': self.userid, 'user_name': self.mobile, 'user_type': '2', 'id_code': self.best_id, 'BEST_user_id': self.best_id, 'BEST_password': Encrypt(pwd_key='111111').encrypt(self.best_id, 'MD5')} res = self._signandpost(param, 'Bind') log('Merchant - bind', res, 'info') # print res if res != '{}': self._changebestid() res = self.bind() # print res return res
def __handle_join_packet(self, packet: Packet): """ When a Join packet received we should add a new node to our nodes array. In reality, there is a security level that forbids joining every node to our network. :param packet: Arrived register packet. :type packet Packet :return: """ new_member_address = packet.get_source_server_address() log(f'New JOIN packet from Node({new_member_address}).') self.stream.add_node(new_member_address) self.children_addresses.append(new_member_address)
def bind(self): u"""商户审核通过后,绑定best,如果best绑定失败,换一个best账号,继续绑定,直到绑定成功,成功后可开通店铺""" param = { 'user_id': self.userid, 'user_name': self.mobile, 'user_type': '2', 'id_code': self.best_id, 'BEST_user_id': self.best_id, 'BEST_password': Encrypt(pwd_key='111111').encrypt(self.best_id, 'MD5') } res = self._signandpost(param, 'Bind') log('Merchant - bind', res, 'info') # print res if res != '{}': self._changebestid() res = self.bind() # print res return res
def remove_node(self, node: Node): """ Remove the node from our Stream. Warnings: 1. Close the node after deletion. :param node: The node we want to remove. :type node: Node :return: """ try: log(f"Something happened to Node({node.get_server_address()}).\n\tI'm Going to kill him. Right NOW!" ) self.nodes.remove(node) node.close() except: return
def __handle_advertise_request(self, packet: Packet) -> None: sender_address = packet.get_source_server_address() sender_semi_node = SemiNode(sender_address[0], sender_address[1]) if sender_semi_node not in self.registered: log(f'Advertise Request from unregistered source({sender_address}).' ) return advertised_address = self.__get_neighbour(sender_address) log(f'Advertising Node({advertised_address}) to Node({sender_address}).' ) advertise_response_packet = PacketFactory.new_advertise_packet( AdvertiseType.RES, self.address, advertised_address) self.stream.add_message_to_out_buff(sender_address, advertise_response_packet, want_register=True) # Add to network_graph self.network_graph.add_node(sender_semi_node.get_ip(), sender_semi_node.get_port(), advertised_address)
def filter(msg, pb_id, client_id): new_msg = profanity.censor(msg) if new_msg != msg: addWarn(pb_id, client_id) now = time.time() if pb_id not in serverdata.clients: return None if "lastMsgTime" in serverdata.clients[pb_id]: count = serverdata.clients[pb_id]["cMsgCount"] smsgcount = serverdata.clients[pb_id]['cSameMsg'] if now - serverdata.clients[pb_id]["lastMsgTime"] < 5: count += 1 if count >= 2: addWarn(pb_id, client_id) count = 0 elif now - serverdata.clients[pb_id]["lastMsgTime"] < 30: if serverdata.clients[pb_id]["lastMsg"] == msg: if len(msg) > 5: smsgcount += 1 if smsgcount >= 3: logger.log(pb_id + " | kicked for chat spam") _ba.disconnect_client(client_id) smsgcount = 0 addWarn(pb_id, client_id) else: smsgcount = 0 else: count = 0 smsgcount = 0 serverdata.clients[pb_id]['cMsgCount'] = count serverdata.clients[pb_id]['lastMsgTime'] = now serverdata.clients[pb_id]['lastMsg'] = msg serverdata.clients[pb_id]['cSameMsg'] = smsgcount else: serverdata.clients[pb_id]['cMsgCount'] = 0 serverdata.clients[pb_id]['lastMsgTime'] = now serverdata.clients[pb_id]['lastMsg'] = msg serverdata.clients[pb_id]['cSameMsg'] = 0 return new_msg
def load(dataType, *corpusID): """ Loads an analysis for corpus and data type. >>> dump('melody', 'test', 'nonsense') True >>> load('melody', 'nonsense') 'test' >>> load('melody', 'not_present') == None True """ corpusName = os.path.join(*corpusID) try: with open(_pathForDump(corpusID, dataType), 'r') as f: try: return pickle.load(f) except pickle.UnpicklingError: logger.log("%s's %s can not be unpickled." % ('/'.join(corpusName), dataType)) return None except EOFError: # Previous write of dump failed. logger.log("Removing corrupted dump for %s' %s." % ('/'.join(corpusName), dataType)) os.remove(_pathForDump(corpusID, dataType)) return None except IOError: logger.log("Couldn't open file for reading.") return None