aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/connection.py
blob: 71c65881e00fe1eaf15fc3c40928d583eda530ff (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. import socket
  4. import json
  5. import sys
  6. from queue import Queue
  7. from gi.repository import Gtk, GObject
  8. log = logging.getLogger('Connection')
  9. conn = None
  10. port = 9999
  11. command_queue = Queue()
  12. signal_handlers = {}
  13. def establish(host):
  14. global conn, port, log
  15. log.info('establishing Connection to %s', host)
  16. conn = socket.create_connection( (host, port) )
  17. log.debug('Connection successful \o/')
  18. def fetchServerConfig():
  19. global conn, log
  20. log.info('reading server-config')
  21. fd = conn.makefile('rw')
  22. fd.write("get_config\n")
  23. fd.flush()
  24. while True:
  25. line = fd.readline()
  26. words = line.split(' ')
  27. signal = words[0]
  28. args = words[1:]
  29. if signal != 'server_config':
  30. continue
  31. server_config_json = " ".join(args)
  32. server_config = json.loads(server_config_json)
  33. return server_config
  34. def enterNonblockingMode():
  35. global conn, log
  36. log.debug('entering nonblocking-mode')
  37. conn.setblocking(False)
  38. GObject.io_add_watch(conn, GObject.IO_IN, on_data, [''])
  39. def on_data(conn, _, leftovers, *args):
  40. global log
  41. '''Asynchronous connection handler. Pushes data from socket
  42. into command queue linewise'''
  43. try:
  44. while True:
  45. try:
  46. leftovers.append(conn.recv(4096).decode(errors='replace'))
  47. if len(leftovers[-1]) == 0:
  48. log.info("Socket was closed")
  49. # FIXME try to reconnect
  50. conn.close()
  51. Gtk.main_quit()
  52. return False
  53. except UnicodeDecodeError as e:
  54. continue
  55. except:
  56. pass
  57. data = "".join(leftovers)
  58. del leftovers[:]
  59. lines = data.split('\n')
  60. for line in lines[:-1]:
  61. log.debug("got line: %r", line)
  62. line = line.strip()
  63. log.debug('re-starting on_loop scheduling')
  64. GObject.idle_add(on_loop)
  65. command_queue.put((line, conn))
  66. if lines[-1] != '':
  67. log.debug("remaining %r", lines[-1])
  68. leftovers.append(lines[-1])
  69. return True
  70. def on_loop():
  71. '''Command handler. Processes commands in the command queue whenever
  72. nothing else is happening (registered as GObject idle callback)'''
  73. global command_queue
  74. log.debug('on_loop called')
  75. if command_queue.empty():
  76. log.debug('command_queue is empty again, stopping on_loop scheduling')
  77. return False
  78. line, requestor = command_queue.get()
  79. words = line.split()
  80. if len(words) < 1:
  81. log.debug('command_queue is empty again, stopping on_loop scheduling')
  82. return True
  83. signal = words[0]
  84. args = words[1:]
  85. log.info('received signal %s, dispatching', signal)
  86. if signal not in signal_handlers:
  87. return True
  88. for handler in signal_handlers[signal]:
  89. cb = handler['cb']
  90. if 'one' in handler and handler['one']:
  91. log.debug('removing one-time handler')
  92. del signal_handlers[signal]
  93. cb(*args)
  94. return True
  95. def send(command, *args):
  96. global conn, log
  97. if len(args) > 0:
  98. command += ' '+(' '.join(args))
  99. command += '\n'
  100. conn.send(command.encode('ascii'))
  101. def on(signal, cb):
  102. if signal not in signal_handlers:
  103. signal_handlers[signal] = []
  104. signal_handlers[signal].append({'cb': cb})
  105. def one(signal, cb):
  106. if signal not in signal_handlers:
  107. signal_handlers[signal] = []
  108. signal_handlers[signal].append({'cb': cb, 'one': True})