summaryrefslogtreecommitdiff
path: root/voctogui/lib/connection.py
diff options
context:
space:
mode:
authorMaZderMind <git@mazdermind.de>2016-09-26 20:42:07 +0200
committerMaZderMind <git@mazdermind.de>2016-09-26 20:42:07 +0200
commit6e846c7081e242213cd70ceacfcff319e221942c (patch)
tree9d037e9272e65aea06c74775b2af1eae2f50a46e /voctogui/lib/connection.py
parentfbcbc3743e9179738244a96be54aa588e44314cb (diff)
parent1972da7f6aa60d3cb3b68808d0381e37898665f3 (diff)
Merge branch 'pep8'
Diffstat (limited to 'voctogui/lib/connection.py')
-rw-r--r--voctogui/lib/connection.py187
1 files changed, 97 insertions, 90 deletions
diff --git a/voctogui/lib/connection.py b/voctogui/lib/connection.py
index 6f8245f..500973d 100644
--- a/voctogui/lib/connection.py
+++ b/voctogui/lib/connection.py
@@ -12,140 +12,147 @@ port = 9999
command_queue = Queue()
signal_handlers = {}
+
def establish(host):
- global conn, port, log, ip
+ global conn, port, log, ip
+
+ log.info('establishing Connection to %s', host)
+ conn = socket.create_connection((host, port))
+ log.debug('Connection successful \o/')
- log.info('establishing Connection to %s', host)
- conn = socket.create_connection( (host, port) )
- log.debug('Connection successful \o/')
+ ip = conn.getpeername()[0]
+ log.debug('Remote-IP is %s', ip)
- ip = conn.getpeername()[0]
- log.debug('Remote-IP is %s', ip)
def fetchServerConfig():
- global conn, log
+ global conn, log
- log.info('reading server-config')
- fd = conn.makefile('rw')
- fd.write("get_config\n")
- fd.flush()
+ log.info('reading server-config')
+ fd = conn.makefile('rw')
+ fd.write("get_config\n")
+ fd.flush()
- while True:
- line = fd.readline()
- words = line.split(' ')
+ while True:
+ line = fd.readline()
+ words = line.split(' ')
- signal = words[0]
- args = words[1:]
+ signal = words[0]
+ args = words[1:]
- if signal != 'server_config':
- continue
+ if signal != 'server_config':
+ continue
- server_config_json = " ".join(args)
- server_config = json.loads(server_config_json)
- return server_config
+ server_config_json = " ".join(args)
+ server_config = json.loads(server_config_json)
+ return server_config
def enterNonblockingMode():
- global conn, log
+ global conn, log
+
+ log.debug('entering nonblocking-mode')
+ conn.setblocking(False)
+ GObject.io_add_watch(conn, GObject.IO_IN, on_data, [''])
- log.debug('entering nonblocking-mode')
- conn.setblocking(False)
- GObject.io_add_watch(conn, GObject.IO_IN, on_data, [''])
def on_data(conn, _, leftovers, *args):
- global log
+ global log
+
+ '''Asynchronous connection handler. Pushes data from socket
+ into command queue linewise'''
+ try:
+ while True:
+ try:
+ leftovers.append(conn.recv(4096).decode(errors='replace'))
+ if len(leftovers[-1]) == 0:
+ log.info("Socket was closed")
- '''Asynchronous connection handler. Pushes data from socket
- into command queue linewise'''
- try:
- while True:
- try:
- leftovers.append(conn.recv(4096).decode(errors='replace'))
- if len(leftovers[-1]) == 0:
- log.info("Socket was closed")
+ # FIXME try to reconnect
+ conn.close()
+ Gtk.main_quit()
+ return False
- # FIXME try to reconnect
- conn.close()
- Gtk.main_quit()
- return False
+ except UnicodeDecodeError as e:
+ continue
+ except:
+ pass
- except UnicodeDecodeError as e:
- continue
- except:
- pass
+ data = "".join(leftovers)
+ del leftovers[:]
- data = "".join(leftovers)
- del leftovers[:]
+ lines = data.split('\n')
+ for line in lines[:-1]:
+ log.debug("got line: %r", line)
- lines = data.split('\n')
- for line in lines[:-1]:
- log.debug("got line: %r", line)
+ line = line.strip()
+ log.debug('re-starting on_loop scheduling')
+ GObject.idle_add(on_loop)
- line = line.strip()
- log.debug('re-starting on_loop scheduling')
- GObject.idle_add(on_loop)
+ command_queue.put((line, conn))
- command_queue.put((line, conn))
+ if lines[-1] != '':
+ log.debug("remaining %r", lines[-1])
- if lines[-1] != '':
- log.debug("remaining %r", lines[-1])
+ leftovers.append(lines[-1])
+ return True
- leftovers.append(lines[-1])
- return True
def on_loop():
- '''Command handler. Processes commands in the command queue whenever
- nothing else is happening (registered as GObject idle callback)'''
+ '''Command handler. Processes commands in the command queue whenever
+ nothing else is happening (registered as GObject idle callback)'''
- global command_queue
+ global command_queue
- log.debug('on_loop called')
+ log.debug('on_loop called')
- if command_queue.empty():
- log.debug('command_queue is empty again, stopping on_loop scheduling')
- return False
+ if command_queue.empty():
+ log.debug('command_queue is empty again, stopping on_loop scheduling')
+ return False
- line, requestor = command_queue.get()
+ line, requestor = command_queue.get()
- words = line.split()
- if len(words) < 1:
- log.debug('command_queue is empty again, stopping on_loop scheduling')
- return True
+ words = line.split()
+ if len(words) < 1:
+ log.debug('command_queue is empty again, stopping on_loop scheduling')
+ return True
- signal = words[0]
- args = words[1:]
+ signal = words[0]
+ args = words[1:]
- log.info('received signal %s, dispatching', signal)
- if signal not in signal_handlers:
- return True
+ 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]
+ 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)
+ cb(*args)
+
+ return True
- return True
def send(command, *args):
- global conn, log
- if len(args) > 0:
- command += ' '+(' '.join(args))
+ global conn, log
+ if len(args) > 0:
+ command += ' ' + (' '.join(args))
+
+ command += '\n'
- command += '\n'
+ conn.send(command.encode('ascii'))
- conn.send(command.encode('ascii'))
def on(signal, cb):
- if signal not in signal_handlers:
- signal_handlers[signal] = []
+ if signal not in signal_handlers:
+ signal_handlers[signal] = []
+
+ signal_handlers[signal].append({'cb': cb})
- signal_handlers[signal].append({'cb': cb})
def one(signal, cb):
- if signal not in signal_handlers:
- signal_handlers[signal] = []
+ if signal not in signal_handlers:
+ signal_handlers[signal] = []
- signal_handlers[signal].append({'cb': cb, 'one': True})
+ signal_handlers[signal].append({'cb': cb, 'one': True})