def _deferred_sync_table(self): self._deferred_sync_table_pending += 1 if self._deferred_sync_table_pending > 1: return def do_it(): self.log.debug("Syncing table after %s deferrals", self._deferred_sync_table_pending) self._deferred_sync_table_pending = 0 self.sync_table() core.call_later(do_it)
def _delayed_send (self): if self._websocket_open is False: return try: written = self.connection.send(self._send_buffer) if written < len(self._send_buffer): # Didn't send all of it. self._send_buffer = self._send_buffer[written:] core.call_later(self._delayed_send) self._send_delay = min(1, self._send_delay * 2) else: self._send_buffer = b'' except Exception: self.disconnect()
def _ws_message (self, opcode, data): # It's a hack, but this is also used to push arbitrary function calls from # the WS thread to the cooperative context, by setting opcode as None and # the function as data. self._rx_queue.append((opcode,data)) cl = True if self._lock: with self._lock: if self._pending: cl = False else: self._pending = True if cl: core.call_later(self._ws_message2)
def _send_real (self, msg): if self._send_buffer: self._send_buffer += msg return try: written = self.connection.send(msg) if written < len(msg): # Didn't send all of it. assert not self._send_buffer self._send_delay = self._initial_send_delay self._send_buffer = msg[written:] core.call_later(self._delayed_send) except Exception as e: self.disconnect()
def interact(self): """ Begin user interaction """ import os history = self.history if history is True: history = ".pox_history" elif history: history = os.path.expanduser(history) if history: history = os.path.abspath(history) import readline, atexit _log = core.getLogger("py") try: readline.read_history_file(history) readline.set_history_length(10000) _log.debug("Read console history") except Exception: pass def save_history(): readline.write_history_file(history) _log.debug("Saved console history") atexit.register(save_history) if self.completion: import readline, rlcompleter ns = globals().copy() ns.update(self.variables) # Note that things added to self.variables later won't be available. # To fix this, make a dict proxy that actually reads from self.variables # *and* globals(). readline.set_completer(rlcompleter.Completer(ns).complete) readline.parse_and_bind("tab: complete") _monkeypatch_console() #print("This program comes with ABSOLUTELY NO WARRANTY. This program " \ # "is free software,") #print("and you are welcome to redistribute it under certain conditions.") #print("Type 'help(pox.license)' for details.") # Ridiculously gross code to wait for a while before showing the console is_up = [False] def notify_up(): is_up[0] = True core.call_later(notify_up) while not is_up[0]: time.sleep(0.2) if core._openflow_wanted: # Hacky time.sleep(0.6) # Long enough? else: time.sleep(0.2) if not core.running: return # A race condition, but probably okay import code import sys sys.ps1 = "POX> " sys.ps2 = " ... " self.running = True console = code.InteractiveConsole(self.variables) # Patch in the synchronized feature real_runcode = console.runcode def runcode(code): if self.variables['sync'] and core.running: with core.scheduler.synchronized(): return real_runcode(code) return real_runcode(code) console.runcode = runcode # Patch in the event hook; why don't we just subclass InteractiveConsole?! real_runsource = console.runsource def runsource(source, *args, **kw): e = SourceEntered(source) self.raiseEvent(e) source = e.source if source is None: return return real_runsource(source, *args, **kw) console.runsource = runsource try: import readline except ImportError: pass console.interact('Ready.', exitmsg='') self.running = False core.quit()