def shutdown_controller(self): if self._process is None: return try: print(colorize("Shutting down controller with [SIGINT], waiting for termination...")) os.kill(self._process.pid, signal.SIGINT) # Kill the process after 4 seconds if it didn't terminate, be a daemon, don't annoy us by # staying alive t = Timer(4, self.kill_controller) t.daemon = True t.start() self._process.wait() t.cancel() # Cancel if everything is fine print(colorize("Shut down controller done exit code {}".format(self._process.returncode))) if self._process.returncode is not None: self._process = None except Exception as e: print(colorize("An error occured while waiting the process to terminate:{}".format(e))) if self.is_alive(): self.kill_controller() print(colorize("Terminated"))
def kill_controller(self): if self._process is None: return print(colorize("Killing controller with [SIGKILL]")) os.killpg(os.getpgid(self._process.pid), signal.SIGKILL) exit_code = self._process.wait() print(colorize("Shutting down controller done exit code: {}".format(exit_code)))
def wait_till_terminate_requested(self): command = self._channel.recv() # Returns None for some reason if command is not None: print(colorize("Rx... {}".format(command))) else: print(colorize("Rx...")) if command == KILL_MSG: print(colorize("Killing controller")) self._kill_controller() return
def test(): (options, args) = parser.parse_args() client_count = options.client_count switch_count = options.switch_count device_count = options.device_count proxy_ip = options.proxy_ip proxy_port = options.proxy_port system('sudo mn -c > /dev/null 2>&1' ) # Clean old mininet runs without showing output to screen. print( colorize("Server started, waiting {} clients...".format(client_count))) server = DiscoveryServer(DISCOVERY_PORT, client_count) tcp_server = TcpServer(get_ip(), SERVER_PORT) server.start(SERVER_PORT) clients = server.get_clients() print(colorize("Clients:{}".format(clients))) txs = [] for i in range(client_count): socket = tcp_server.accept() tx = ControllerTx(socket) txs.append(tx) for tx in txs: print(colorize("Waiting controller to be ready...")) tx.wait_ready() print(colorize("Controllers ready, Running mininet...")) machine = MininetMachine() print(colorize("Mininet connecting to {}:{}".format(proxy_ip, proxy_port))) machine.start(switch_count, device_count, proxy_ip, int(proxy_port)) hosts = machine.get_hosts() for host in hosts: for dest in hosts: if host == dest: continue # Ping pair machine.ping_hosts([host, dest]) machine.terminate() print("Mininet Done...") for tx in txs: tx.kill_controller() tcp_server.close() print("Done...")
def start(self, tcp_port=0, max_try_count=-1, onreceive=None): try_count = 0 while len(self._clients) < self._expected_count: try: data, address = self._sock.recvfrom(256) data = str(data.decode('UTF-8')) if not data == DISCOVERY_PREFIX: raise AssertionError( "Invalid Identification message {}".format(data)) except socket.timeout: try_count += 1 if try_count == max_try_count: self.stop() return continue # Update state client_ip = address[0] # Ignore client port, it's meaningless self._clients.append(client_ip) print( colorize("{}/{} connected".format(len(self._clients), self._expected_count))) if onreceive is not None: onreceive(address) # Reply with TCP port sent = self._sock.sendto( (DISCOVERY_PREFIX + str(tcp_port)).encode(), address) if sent == -1: self.stop() raise ConnectionError("Socket crashed") print(colorize("All clients are now connected..."))
def _create_network(self, ): for i in range(self.switch_count): s = self.net.addSwitch('s{}'.format(i)) self._switches.append(s) for j in range(self.hosts_per_switch): # This basically creates host with a seriesed numbers 0,1,2,...etc # without restting the count in each loop h = self.net.addHost('h{}'.format(j + (i * self.hosts_per_switch))) self._hosts.append(h) print(colorize("Adding:{} to {}".format(h.name, s.name))) self.net.addLink(s, h)
def _connect_switches(self): for i in range(len(self._switches) - 1): s = self._switches[i] sn = self._switches[i + 1] print(colorize("Connecting:{} to {}".format(s.name, sn.name))) self.net.addLink(s, sn)
def _keep_printing_output(self): try: for line in iter(self._process.stderr.readline, b''): print(line.decode('utf-8')) except AttributeError: print(colorize("Process is now killed, will no longer print std stream outputs"))
def launch_controller(self): print(colorize("Starting controller")) # POX uses stderr for logging self._process = subprocess.Popen(shlex.split(self.command), bufsize=0, stderr=subprocess.PIPE) print(colorize("Controller started"))