I understand that there are similar topics on this (such as here) but my intended design is a little more complex.
I'm designing a CLI script that will be run in an SSH window. The script will be hosted and executed on an Ubuntu 14.10 server. It's intended to actively monitor, in the foreground, the current status of ports and clients on a host switch. Every 30 seconds or as defined by the user it will fetch data via SNMP and then refresh information and display it to the screen. When it's waiting for the next refresh there is a timer indicating when it will query the device again for information.
I want to allow the user to press specific keys to change the output view or edit key variables at any time. (The functionality is similar to the Unix top
.) For example, pressing t
would request them to enter a number of seconds desired between loops. h
, m
, or i
would toggle showing/hiding certain columns. These would not pause the timer nor exit the loop since changes would be applied at the next refresh. r
would force an immediate refresh and apply changes. q
or Ctrl+C
would exit the script.
The primary activity would look like this:
Query loop <-----------------------------
| |
| Process Data
| ^
| |
v Query Data #SNMPBULKWALK
Timer <------------- ^
| | | |
| | Check time remaining |
| | ^ |
| |_______________| |
|___________________________________|
With key-press interrupts it would act like this:
Query loop <----------------------
| | ???<---Change variables
| (Continue) ^
V | |
Timer <--------- !!INTERRUPT!!---------> Identify key
| | | ^
| | Check time remaining |
| | ^ |
| |___________| |
|_____________________________|
I'm kind of stumped here. I'm led to believe that I'll probably need to implement threading - which I do not have experience with - as a while
loop by itself doesn't seem to satisfy what we need. I'm also unsure of how to inject the changes to the object that contains the variables (e.g. timer, flags for display formatting) since it will be constantly used by our loop.