From 7785d264c92b25c5a89901f77c672a8164744e28 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Tue, 17 May 2016 17:06:52 +0100 Subject: [PATCH] Add stuff --- abp/__init__.py | 1 - abp/client.py | 48 ---------------------------- abp/server.py | 4 +-- abp/{visiblegraphstate.py => viz.py} | 9 ++---- client/api.js | 14 ++++++-- client/index.html | 4 +++ client/main.css | 17 +++++++++- 7 files changed, 36 insertions(+), 61 deletions(-) delete mode 100644 abp/client.py rename abp/{visiblegraphstate.py => viz.py} (95%) diff --git a/abp/__init__.py b/abp/__init__.py index b5e9b1f..a2258ac 100644 --- a/abp/__init__.py +++ b/abp/__init__.py @@ -1,4 +1,3 @@ # Alias some stuff to make imports cleaner from abp.graphstate import GraphState from abp.qi import CircuitModel -from abp.visiblegraphstate import VisibleGraphState diff --git a/abp/client.py b/abp/client.py deleted file mode 100644 index 7d127cb..0000000 --- a/abp/client.py +++ /dev/null @@ -1,48 +0,0 @@ -import requests -import abp, json - -class ClientError(Exception): - def __init__(self, message): - self.message = message - -class Client(object): - def __init__(self, host="localhost", port=5000, clear=False): - self.session = requests.Session() - self.root = "http://{}:{}".format(host, port) - if clear: - self.clear() - - def get(self, endpoint): - url =self.root+endpoint - response = self.session.get(url) - if response.status_code == 404: - message = "404. Check that the server is running!".format(self.root, endpoint) - raise ClientError(message) - return response.content - - def get_state(self): - response = self.get("/state") - output = abp.GraphState() - output.from_json(json.loads(response)) - return output - - def set_state(self, state): - response = self.session.post(self.root+"/state", data=state.to_json()) - if not response.status_code == 200: - print response.status_code - return response.content - - def add_node(self, node): - return self.get("/add_node/{}".format(node)) - - def act_local_rotation(self, node, operation): - return self.get("/act_local_rotation/{}/{}".format(node, operation)) - - def act_cz(self, a, b): - return self.get("/act_cz/{}/{}".format(a, b)) - - def clear(self): - return self.get("/clear") - - def kill(self): - self.session.close() diff --git a/abp/server.py b/abp/server.py index 6d61098..34b433a 100644 --- a/abp/server.py +++ b/abp/server.py @@ -1,7 +1,7 @@ +import json from websocket_server import WebsocketServer -import threading import abp -import json + clients = [] diff --git a/abp/visiblegraphstate.py b/abp/viz.py similarity index 95% rename from abp/visiblegraphstate.py rename to abp/viz.py index 2d73d68..a1ac25e 100644 --- a/abp/visiblegraphstate.py +++ b/abp/viz.py @@ -2,13 +2,9 @@ Allows us to visualize the state in a browser """ -import atexit -import threading -import time -from websocket import create_connection +import atexit, json from graphstate import GraphState -import json - +from websocket import create_connection class VisibleGraphState(GraphState): @@ -35,3 +31,4 @@ class VisibleGraphState(GraphState): """ Call this function when you are ready to send data to the browser """ data = json.dumps(self.to_json()) self.ws.send(data) + diff --git a/client/api.js b/client/api.js index 7e3f154..7b93671 100644 --- a/client/api.js +++ b/client/api.js @@ -1,10 +1,17 @@ var ws; function connect_to_server() { - ws = new WebSocket("ws://localhost:5001"); + ws = new WebSocket("ws://localhost:5000"); ws.onopen = function() { - console.log("Connected to server."); + message.innerHTML = "Connected to server."; + message.className = "visible"; + }; + + ws.onerror = function(err) + { + message.innerHTML = "Could not connect to server."; + message.className = "visible"; }; ws.onmessage = function (evt) @@ -18,7 +25,8 @@ function connect_to_server() { ws.onclose = function() { - console.log("Connection was closed."); + message.innerHTML = "Connection to server lost. Reconnect."; + message.className = "visible"; }; } diff --git a/client/index.html b/client/index.html index 4e4a49b..1d0a582 100644 --- a/client/index.html +++ b/client/index.html @@ -35,5 +35,9 @@ + + diff --git a/client/main.css b/client/main.css index ff0f9a6..074bbb6 100644 --- a/client/main.css +++ b/client/main.css @@ -16,7 +16,6 @@ html, body { margin: 0; padding: 0; overflow: hidden; font-size: 10pt; font-fam #pallette { background-color: black; - /*border-radius:3px;*/ color:white; padding: 10px; font-family:"courier new"; @@ -26,6 +25,18 @@ html, body { margin: 0; padding: 0; overflow: hidden; font-size: 10pt; font-fam font-size: 9pt; } +#message { + background-color: black; + color:white; + padding: 10px; + font-family:"courier new"; + position: absolute; + bottom: 10px; + right: 10px; + font-size: 9pt; +} + + ul { list-style-type: none; padding: 0px; @@ -45,3 +56,7 @@ ul { transform: scale(.5); transition: visibility .08s, opacity .08s linear, transform .08s linear; } + +a { + color: yellow; +}