class Main(object): COMMAND_PORT = 7001 def __init__(self): log_file = "{}.log".format(self.__class__.__name__.lower()) LoggingUtil.setup(log_path=log_file, log_level=logging.DEBUG) self._logger = logging.getLogger(self.__class__.__name__) self._stopped = False self._console = Console(self._on_command_event) self._server_sock = ServerSocket(self.COMMAND_PORT, self._on_socket_accept_event) def run(self): self._logger.info("Started!") self._console.start_user_command_reader() self._server_sock.start_listening() while not self._stopped: time.sleep(0.1) self._logger.info("Ended!") def stop(self): self._console.message("This application is being shut down...") self._console.stop_clients() self._console.stop_user_command_reader() self._server_sock.stop_listening() self._stopped = True def _on_command_event(self, command, sender=None): """ Called every time we receive a new user or client command. """ if len(command.strip()) == 0: return if command in ["exit", "quit", "stop", "x", "q"]: self.stop() else: self._console.message("* From {}: {}".format(sender, command), sender=sender) def _on_socket_accept_event(self, sock, addr): """ Called every time a new socket connection has been accepted. """ self._console.add_client(sock, addr, self._on_command_event)
def __init__(self): log_file = "{}.log".format(self.__class__.__name__.lower()) LoggingUtil.setup(log_path=log_file, log_level=logging.DEBUG) self._logger = logging.getLogger(self.__class__.__name__) self._stopped = False self._console = Console(self._on_command_event) self._server_sock = ServerSocket(self.COMMAND_PORT, self._on_socket_accept_event)