def start(self, force=False, inprocess=False): if inprocess: server = MessageServer(self._address, self.storeLocal, self._pidFile, self.echo) for address in self.forwardAddresses: client = MessageServerClient(address) server.forwardClients.add(client) server.start() return if self.isRunning and not force: printInDebugMode('Cannot start message server, already running') return command = self._createStartCommand() printInDebugMode('Starting message server with command: %s' % command) j.system.process.runDaemon(command) remainingSeconds = self._START_TIMEOUT time.sleep(5) while not self.isRunning: printInDebugMode('Waiting %d more seconds for message server to be' ' started' % remainingSeconds) time.sleep(1) remainingSeconds -= 1 if not remainingSeconds: raise RuntimeError('Failed to start message server') printInDebugMode('Started message server')
def main(): storage = MessageStorage() parser = MessageParser() clientlist = ClientList() message_handler = ServerMessageHandler(parser, storage) myserver = MessageServer(message_handler, clientlist) myserver.open_udp_socket() myserver.bind_socket() myserver.listen() myserver.close_socket()
return 'Good' return r def leaveHandler(conf): conf = vk.getUserId(conf) if conf > CONF_START: conf -= CONF_START if vk.leaveConf(conf): return 'Ok' else: return 'Fail' if config.get('inf.server_port', 'i'): srv = MessageServer() srv.addHandler('reply', lambda x: bot.interact('flat ' + x, False)) srv.addHandler('stem', lambda x: bot.interact('stem ' + x, False)) srv.addHandler('ignore', ignoreHandler) srv.addHandler('unignore', unignoreHandler) srv.addHandler('ban', banHandler) srv.addHandler('unban', unbanHandler) srv.addHandler('reload', reload) srv.addHandler('isignored', isignoredHandler) srv.addHandler('leave', leaveHandler) srv.listen() log.info('Running TCP server on port ' + config.get('inf.server_port')) dialogs = 0 reply_all = timeto('includeread', includeread_interval)
"p4c-bm2-ss", path, "-o build/compiled.json", "--p4runtime-files build/program.p4info.txt", "--p4v 16" ] compiler = subprocess.run(" ".join(cmd), stderr=subprocess.PIPE, shell=True) if compiler.stderr: raise IOError( f"Could not compile program:\n {compiler.stderr.decode()}") self.software_path = "build/compiled.json" def update(self, program) -> None: self.uninstall() self.install(program) def uninstall(self) -> None: self.logger.info("Software uninstalled") self.software_path = None def restart(self) -> None: if self.process is not None: self.stop() self.start() if __name__ == "__main__": driver = SimpleSwitchDriver("s1") with MessageServer("", 5555, driver.handle_message) as server: server.run()
def start_daemon(self): self.server = MessageServer("", 5555, self.handle_message) self.server.start()
class SimpleSwitchDriver(Switch): def __init__(self, name: str, **kwargs) -> None: super().__init__(name, **kwargs) self.switch_name = name self.switch = None self.software_path = None self.switch_args = kwargs self.server = None self.logger = logging.getLogger("MININET_DRIVER") file_handler = logging.FileHandler("logs/mininet_driver.log") file_handler.setFormatter(logging.Formatter("[%(asctime)s] %(name)s: %(message)s")) self.logger.addHandler(file_handler) self.logger.setLevel(logging.INFO) self.logger.info("[DRIVER] Initializing driver... ") self.start_daemon() self.logger.info("[DRIVER] Done!") def start_daemon(self): self.server = MessageServer("", 5555, self.handle_message) self.server.start() def handle_message(self, message: dict) -> None: if message["action"] == "install": self.install(message["path"]) self.restart() elif message["action"] == "start": self.start() elif message["action"] == "stop": self.stop() elif message["action"] == "uninstall": self.uninstall() self.restart() elif message["action"] == "update": self.update(message["path"]) self.restart() else: raise ValueError("[DRIVER] Unknown action") def start(self, controllers=None) -> None: if self.switch is not None: raise OSError("Switch is already running") logging.info("[DRIVER] >> Starting Switch... ") self.switch = SimpleSwitch(self.switch_name, self.software_path, **self.switch_args) self.switch.start() logging.info("[DRIVER] >> Done!") def stop(self) -> None: if self.switch is None: raise OSError(">> Switch is already stopped") logging.info(">> Stopping switch... ") self.switch.stop() self.switch = None logging.info(">> Done!") def install(self, path_to_program: str) -> None: logging.info(f">> Installing {path_to_program} on switch {self.switch}... ") os.system( f"p4c-bm2-ss {path_to_program} -o build/compiled.json --p4runtime-files build/program.p4info.txt --p4v 16" ) self.software_path = "build/compiled.json" logging.info(">> Done!") def update(self, path_to_program: str) -> None: self.uninstall() self.install(path_to_program) def uninstall(self) -> None: self.software_path = None def restart(self) -> None: self.stop() self.start()
#!/usr/bin/python3 from vars import log_file, socket_port import logging import os from server import MessageServer if os.path.exists(log_file): os.remove(log_file) logging.basicConfig(level=logging.DEBUG, filename=log_file) MessageServer(socket_port).start()
r = test_friend(user, True) if r is None: return 'Good' return r def leaveHandler(conf): conf = vk.getUserId(conf) if conf > CONF_START: conf -= CONF_START if vk.leaveConf(conf): return 'Ok' else: return 'Fail' if config.get('inf.server_port', 'i'): srv = MessageServer() srv.addHandler('reply', lambda x:bot.interact('flat ' + x, False)) srv.addHandler('stem', lambda x:bot.interact('stem ' + x, False)) srv.addHandler('ignore', ignoreHandler) srv.addHandler('unignore', unignoreHandler) srv.addHandler('ban', banHandler) srv.addHandler('unban', unbanHandler) srv.addHandler('reload', reload) srv.addHandler('isignored', isignoredHandler) srv.addHandler('leave', leaveHandler) srv.listen() log.info('Running TCP server on port ' + config.get('inf.server_port')) dialogs = 0 reply_all = timeto('includeread', includeread_interval) def main_loop():