From 1328d62f9d3a2eb9f089d531614302005c29ec37 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 21 Jun 2015 21:56:59 +0200 Subject: Replace MGMT socket by ZMQ, make services shared_ptr --- gui/muxconfig.py | 43 ++++++++++++++++++++++++------------------- gui/odr-dabmux-gui.py | 36 +++++++++++++++++++++++++++++++++++- gui/views/configeditor.tpl | 29 +++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 gui/views/configeditor.tpl (limited to 'gui') diff --git a/gui/muxconfig.py b/gui/muxconfig.py index f725462..4b307fd 100644 --- a/gui/muxconfig.py +++ b/gui/muxconfig.py @@ -20,7 +20,7 @@ # # You should have received a copy of the GNU General Public License # along with ODR-DabMux. If not, see . -import socket +import zmq import json class General(object): @@ -97,38 +97,43 @@ class ConfigurationHandler(object): self._config = None self._statistics = None + self._ctx = zmq.Context() + self.sock = zmq.Socket(self._ctx, zmq.REQ) + self.sock.connect("tcp://{}:{}".format(self._host, self._port)) + def load(self): """Load the configuration from the multiplexer and save it locally""" - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.send(b'info') + server_info = self.sock.recv() + + self.sock.send(b'getptree') + config_info = self.sock.recv() - s.connect((self._host, self._port)) - server_info = s.recv(32768) - s.sendall(b'getptree\n') - config_info = s.recv(32768) - s.close() + print("Config '%r'" % config_info) - self._server_version = json.loads(server_info.decode())['service'] - self._config = json.loads(config_info.decode()) + self._server_version = json.loads(server_info)['service'] + self._config = json.loads(config_info) def update_stats(self): """Load the statistics from the multiplexer and save them locally""" - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((self._host, self._port)) - server_info = s.recv(32768) + self.sock.send(b'info') + server_info = self.sock.recv() - s.sendall(b'values\n') + self.sock.send(b'values') + stats_info = self.sock.recv() - stats_info = s.recv(32768) - s.close() - - print("STATS: {}".format(stats_info.decode())) - self._statistics = json.loads(stats_info.decode())['values'] + self._statistics = json.loads(stats_info)['values'] def get_full_configuration(self): - return self._config + return json.dumps(self._config, indent=4) + + def set_full_configuration(self, config_json): + self.sock.send(b'setptree', flags=zmq.SNDMORE) + self.sock.send(config_json) + return self.sock.recv() == "OK" def get_mux_version(self): return self._server_version diff --git a/gui/odr-dabmux-gui.py b/gui/odr-dabmux-gui.py index 19f3ae8..caa4ea7 100755 --- a/gui/odr-dabmux-gui.py +++ b/gui/odr-dabmux-gui.py @@ -28,13 +28,46 @@ # along with ODR-DabMux. If not, see . from muxconfig import * -from bottle import route, run, template, static_file +from bottle import route, run, template, static_file, request import json conf = ConfigurationHandler('localhost') @route('/config') def config(): + """Show the JSON ptree in a textbox for editing""" + + conf.load() + + return template('configeditor', + version = conf.get_mux_version(), + config = conf.get_full_configuration(), + message = "") + +@route('/config', method="POST") +def config_json_post(): + """Update the ODR-DabMux configuration with the JSON ptree + given as POST data""" + + new_config = request.forms.get('config') + print("New config %s" % new_config) + + success = conf.set_full_configuration(new_config) + + if success: + successmessage = "Success" + else: + successmessage = "Failure" + + conf.load() + + return template('configeditor', + version = conf.get_mux_version(), + config = conf.get_full_configuration(), + message = successmessage) + +@route('/config.json', method="GET") +def config_json_get(): """Return a application/json containing the full ptree of the mux""" @@ -43,6 +76,7 @@ def config(): return {'version': conf.get_mux_version(), 'config': conf.get_full_configuration()} + @route('/') def index(): conf.load() diff --git a/gui/views/configeditor.tpl b/gui/views/configeditor.tpl new file mode 100644 index 0000000..9d1d193 --- /dev/null +++ b/gui/views/configeditor.tpl @@ -0,0 +1,29 @@ + + + + ODR-DabMux Configuration Editor + + + + +

Configuration for {{version}}

+ +

Reload

+ + % if message: +

{{message}}

+ % end + +
+

+ +

+ +

+ +

+
+ + + + -- cgit v1.2.3