summaryrefslogtreecommitdiff
path: root/voctogui/lib/connection.py
diff options
context:
space:
mode:
Diffstat (limited to 'voctogui/lib/connection.py')
-rw-r--r--voctogui/lib/connection.py34
1 files changed, 28 insertions, 6 deletions
diff --git a/voctogui/lib/connection.py b/voctogui/lib/connection.py
index b56a490..42a403e 100644
--- a/voctogui/lib/connection.py
+++ b/voctogui/lib/connection.py
@@ -10,6 +10,7 @@ log = logging.getLogger('Connection')
conn = None
port = 9999
command_queue = Queue()
+signal_handlers = {}
def establish(host):
global conn, port, log
@@ -101,15 +102,36 @@ def on_loop():
args = words[1:]
log.info('received signal %s, dispatching', signal)
+ if signal not in signal_handlers:
+ return True
+
+ for handler in signal_handlers[signal]:
+ cb = handler['cb']
+ if 'one' in handler and handler['one']:
+ log.debug('removing one-time handler')
+ del signal_handlers[signal]
+
+ cb(*args)
+
+ return True
-def send(command):
+def send(command, *args):
global conn, log
- if isinstance(command, str):
- command = command.encode('ascii')
- conn.send(command)
+ if len(args) > 0:
+ command += ' '+(' '.join(args))
+
+ command += '\n'
+
+ conn.send(command.encode('ascii'))
def on(signal, cb):
- pass
+ if signal not in signal_handlers:
+ signal_handlers[signal] = []
+
+ signal_handlers[signal].append({'cb': cb})
def one(signal, cb):
- pass
+ if signal not in signal_handlers:
+ signal_handlers[signal] = []
+
+ signal_handlers[signal].append({'cb': cb, 'one': True})