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 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')
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()