def connect_to_server(self, address: str, port: int) -> None: if self.__connected: self.disconnect() self.__network_module.connect_to_host(address, port) self.__network_module.send(Protocol.hello_message()) # first message self.__connected = True
def __receive_hello(peer: Peer, command: bytes): """ Tries to receives HELLO, if the message is not HELLO, terminates the peer :param peer: :param command: :return: """ if command == Protocol.Flags.HELLO: logging.info("HELLO message received") peer.send(Protocol.hello_message()) peer.send( Protocol.server_message(Protocol.ServerFlags.ACK, "Welcome to the server")) peer.hello_done = True else: logging.warning("No HELLO received, closing connection") peer.terminate() peer.hello_done = False
def __process_message(self, peer: Peer, command: bytes, message: bytes) -> bool: """ Processes the received message :param peer: :param command: :param message: :return: True, if connection should be closed """ if command == Protocol.Flags.HELLO: logging.warning("HELLO message received again") peer.send(Protocol.hello_message()) elif command == Protocol.Flags.LOGIN: logging.warning("User \"" + peer.name + "\" is already logged in") peer.send( Protocol.server_message(Protocol.ServerFlags.ACK, "You are already logged in")) elif command == Protocol.Flags.PING: logging.info("PING message received") peer.send(Protocol.pong_message()) elif command == Protocol.Flags.PONG: logging.info("PONG message received") elif command == Protocol.Flags.EXIT: logging.info("EXIT message received, connection closed") # peer.send(Protocol.server_message(Protocol.ServerFlags.NORMAL, "See you later")) return True elif command == Protocol.Flags.LOGOUT: if not peer.logged_in: return False logging.info("LOGOUT message received from \"" + peer.name + "\"") peer.logged_in = False peer.name = None elif command == Protocol.Flags.JOIN: if not peer.logged_in: return False pool_name = message.split(bytes([Protocol.Flags.SEPARATOR ]))[0].decode() passwd = message.split(bytes([Protocol.Flags.SEPARATOR ]))[1].decode() hashed = str( hashpw(passwd.encode("utf-8"), b"$2a$12$" + b"SZ4R4Z3G3SZ4DJ4LS0RT..")).split("..")[1][:-1] logging.info("JOIN from \"" + peer.name + "\" for pool \"" + pool_name + "\"") pool_id = SQLModule.PoolsSQLModule.get_id(pool_name) if pool_id == -1 or hashed == SQLModule.PoolsSQLModule.get_hashed_pwd( pool_name): if pool_name in self.__pools: logging.debug("Pool already exists") self.__pools[pool_name].add_peer(peer) else: logging.debug("Pool not exists, creating") self.__pools[pool_name] = Pool(pool_name) self.__pools[pool_name].add_peer(peer) if pool_id == -1: SQLModule.PoolsSQLModule.add_pool(pool_name, hashed) logging.info("Pool created with name \"" + pool_name + "\"") peer.send( Protocol.server_message( Protocol.ServerFlags.ACK, "Pool created with name \"" + pool_name + "\"")) pool_id = SQLModule.PoolsSQLModule.get_id(pool_name) peer_id = SQLModule.PeersSQLModule.get_id(peer.name) SQLModule.SwitchTable.add_peer_pool(peer_id, pool_id) logging.info("\"" + peer.name + "\" has joined \"" + pool_name + "\" succesfully") peer.send( Protocol.server_message(Protocol.ServerFlags.ACK, "Successful join")) peer.pool = self.__pools[pool_name] peer.pool.send_message( Protocol.server_message(Protocol.ServerFlags.NORMAL, peer.name + " has joined the room"), peer) else: logging.warning("\"" + pool_name + "\" failed to log in") peer.send( Protocol.server_message(Protocol.ServerFlags.NAK, "Wrong password for this pool")) elif command == Protocol.Flags.LEAVE: if not peer.logged_in: return False # pool_name = message.split(bytes([Protocol.Flags.SEPARATOR]))[0].decode() pool_name = peer.pool.name logging.info("LEAVE from \"" + peer.name + "\" for pool \"" + pool_name + "\"") peer_id = SQLModule.PeersSQLModule.get_id(peer.name) pool_id = SQLModule.PoolsSQLModule.get_id(pool_name) if SQLModule.SwitchTable.remove_peer_pool(peer_id, pool_id): peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "You've left the group")) peer.leave_pool() elif command == Protocol.Flags.USER: if not peer.logged_in: peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "You gotta log in first")) return False if peer.pool is None: peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "You gotta join a room first")) return False logging.info("USER message received") # peer.pool.send_message(message.split(bytes([Protocol.Flags.SEPARATOR]))[-1], peer) # peer.send(Protocol.Protocol.user_message(self.__name, peer.name, message.decode())) peer.pool.send_message( Protocol.user_message( peer.pool.name, peer.name, message.split(bytes([Protocol.Flags.SEPARATOR ]))[-1].decode()), peer) elif command == Protocol.Flags.SERVER: logging.warning( "Server received SERVER message, connection closed") return True else: peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "Invalid message received")) logging.warning("Invalid message received") return False