summaryrefslogtreecommitdiff
path: root/voctogui/lib/connection.py
diff options
context:
space:
mode:
authorMaZderMind <git@mazdermind.de>2015-10-05 23:00:17 +0200
committerMaZderMind <git@mazdermind.de>2015-10-05 23:00:17 +0200
commit9677585e2ac1529be7cb3d417545bbf5ba634201 (patch)
tree70defc697710200bc2dc9762c9ba49681b578f88 /voctogui/lib/connection.py
parentffe7e70f0b6c58f323e9b471c9e0a18549f6c9ee (diff)
implement signal-handler, connect audio-selector to it
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})