# redraw the next screen
def __redraw (self):
buffer = StringIO()
+
self.redraw_cb(buffer)
with self.lock:
rows)
super(TrexTUI.ScreenSizeException, self).__init__(msg)
+
def __init__ (self, stateless_client):
self.stateless_client = stateless_client
+ self.tui_global_lock = threading.Lock()
self.pm = TrexTUIPanelManager(self)
self.sb = ScreenBuffer(self.redraw_handler)
def redraw_handler (self, buffer):
- self.pm.show(show_legend = self.async_keys.is_legend_mode(), buffer = buffer)
+ # this is executed by the screen buffer - should be protected against TUI commands
+ with self.tui_global_lock:
+ self.pm.show(show_legend = self.async_keys.is_legend_mode(), buffer = buffer)
def clear_screen (self, lines = 50):
# reposition the cursor
# reposition the cursor
sys.stdout.write("\x1b[0;0H")
- #sys.stdout.write("\x1b[2J\x1b[H")
def show (self, client, save_console_history, show_log = False, locked = False):
if (int(rows) < TrexTUI.MIN_ROWS) or (int(cols) < TrexTUI.MIN_COLS):
raise self.ScreenSizeException(rows = rows, cols = cols)
- with AsyncKeys(client, save_console_history, locked) as async_keys:
+ with AsyncKeys(client, save_console_history, self.tui_global_lock, locked) as async_keys:
sys.stdout.write("\x1bc")
self.async_keys = async_keys
self.show_internal(show_log, locked)
self.state = self.STATE_ACTIVE
self.last_redraw_ts = 0
-
try:
self.sb.start()
if status == AsyncKeys.STATUS_NONE:
time.sleep(0.001)
- # regular state
- if self.state == self.STATE_ACTIVE:
- # if no connectivity - move to lost connecitivty
- if not self.stateless_client.async_client.is_alive():
- self.stateless_client._invalidate_stats(self.pm.ports)
- self.state = self.STATE_LOST_CONT
+ if self.tui_global_lock.locked():
+ self.x += 1
+ with self.tui_global_lock:
+ self.handle_state_machine()
+ except TUIQuit:
+ print("\nExiting TUI...")
- # lost connectivity
- elif self.state == self.STATE_LOST_CONT:
- # got it back
- if self.stateless_client.async_client.is_alive():
- # move to state reconnect
- self.state = self.STATE_RECONNECT
+ finally:
+ self.sb.stop()
+ print("")
- # restored connectivity - try to reconnect
- elif self.state == self.STATE_RECONNECT:
- try:
- self.stateless_client.connect()
- self.stateless_client.acquire()
- self.state = self.STATE_ACTIVE
- except STLError:
- self.state = self.STATE_LOST_CONT
+ # handle state machine
+ def handle_state_machine (self):
+ # regular state
+ if self.state == self.STATE_ACTIVE:
+ # if no connectivity - move to lost connecitivty
+ if not self.stateless_client.async_client.is_alive():
+ self.stateless_client._invalidate_stats(self.pm.ports)
+ self.state = self.STATE_LOST_CONT
- except TUIQuit:
- print("\nExiting TUI...")
+ # lost connectivity
+ elif self.state == self.STATE_LOST_CONT:
+ # got it back
+ if self.stateless_client.async_client.is_alive():
+ # move to state reconnect
+ self.state = self.STATE_RECONNECT
- finally:
- self.sb.stop()
- print("")
+ # restored connectivity - try to reconnect
+ elif self.state == self.STATE_RECONNECT:
+
+ try:
+ self.stateless_client.connect()
+ self.stateless_client.acquire()
+ self.state = self.STATE_ACTIVE
+ except STLError:
+ self.state = self.STATE_LOST_CONT
# draw once
STATUS_REDRAW_KEYS = 1
STATUS_REDRAW_ALL = 2
- def __init__ (self, client, save_console_history, locked = False):
+ def __init__ (self, client, save_console_history, tui_global_lock, locked = False):
+ self.tui_global_lock = tui_global_lock
+
self.engine_console = AsyncKeysEngineConsole(self, client, save_console_history)
self.engine_legend = AsyncKeysEngineLegend(self)
self.locked = locked
def handle_single_key (self, ch):
-
# newline
if ch == '\n':
self.handle_cmd()
def handle_cmd (self):
+
cmd = self.lines[self.line_index].get().strip()
if not cmd:
return
func = self.ac.get(op)
if func:
- func_rc = func(param)
+ with self.async.tui_global_lock:
+ func_rc = func(param)
# take out the empty line
empty_line = self.lines.popleft()
# success
if func_rc:
self.last_status = format_text("[OK]", 'green')
-
# errors
else:
err_msgs = ascii_split(str(func_rc))
self.last_status += " [{0} more errors messages]".format(len(err_msgs) - 1)
color = 'red'
+
+
# trim too long lines
if ansi_len(self.last_status) > TrexTUI.MIN_COLS:
self.last_status = format_text(self.last_status[:TrexTUI.MIN_COLS] + "...", color, 'bold')