| @@ -1,4 +1,3 @@ | |||||
| # Alias some stuff to make imports cleaner | # Alias some stuff to make imports cleaner | ||||
| from abp.graphstate import GraphState | from abp.graphstate import GraphState | ||||
| from abp.qi import CircuitModel | from abp.qi import CircuitModel | ||||
| from abp.visiblegraphstate import VisibleGraphState | |||||
| @@ -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() | |||||
| @@ -1,7 +1,7 @@ | |||||
| import json | |||||
| from websocket_server import WebsocketServer | from websocket_server import WebsocketServer | ||||
| import threading | |||||
| import abp | import abp | ||||
| import json | |||||
| clients = [] | clients = [] | ||||
| @@ -2,13 +2,9 @@ | |||||
| Allows us to visualize the state in a browser | 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 | from graphstate import GraphState | ||||
| import json | |||||
| from websocket import create_connection | |||||
| class VisibleGraphState(GraphState): | class VisibleGraphState(GraphState): | ||||
| @@ -35,3 +31,4 @@ class VisibleGraphState(GraphState): | |||||
| """ Call this function when you are ready to send data to the browser """ | """ Call this function when you are ready to send data to the browser """ | ||||
| data = json.dumps(self.to_json()) | data = json.dumps(self.to_json()) | ||||
| self.ws.send(data) | self.ws.send(data) | ||||
| @@ -1,10 +1,17 @@ | |||||
| var ws; | var ws; | ||||
| function connect_to_server() { | function connect_to_server() { | ||||
| ws = new WebSocket("ws://localhost:5001"); | |||||
| ws = new WebSocket("ws://localhost:5000"); | |||||
| ws.onopen = function() | 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) | ws.onmessage = function (evt) | ||||
| @@ -18,7 +25,8 @@ function connect_to_server() { | |||||
| ws.onclose = function() | ws.onclose = function() | ||||
| { | { | ||||
| console.log("Connection was closed."); | |||||
| message.innerHTML = "Connection to server lost. <a href='#' onclick='javascript:connect_to_server()'>Reconnect</a>."; | |||||
| message.className = "visible"; | |||||
| }; | }; | ||||
| } | } | ||||
| @@ -35,5 +35,9 @@ | |||||
| </ul> | </ul> | ||||
| </div> | </div> | ||||
| <div id=message class=hidden> | |||||
| hello | |||||
| </div> | |||||
| </body> | </body> | ||||
| </html> | </html> | ||||
| @@ -16,7 +16,6 @@ html, body { margin: 0; padding: 0; overflow: hidden; font-size: 10pt; font-fam | |||||
| #pallette { | #pallette { | ||||
| background-color: black; | background-color: black; | ||||
| /*border-radius:3px;*/ | |||||
| color:white; | color:white; | ||||
| padding: 10px; | padding: 10px; | ||||
| font-family:"courier new"; | font-family:"courier new"; | ||||
| @@ -26,6 +25,18 @@ html, body { margin: 0; padding: 0; overflow: hidden; font-size: 10pt; font-fam | |||||
| font-size: 9pt; | 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 { | ul { | ||||
| list-style-type: none; | list-style-type: none; | ||||
| padding: 0px; | padding: 0px; | ||||
| @@ -45,3 +56,7 @@ ul { | |||||
| transform: scale(.5); | transform: scale(.5); | ||||
| transition: visibility .08s, opacity .08s linear, transform .08s linear; | transition: visibility .08s, opacity .08s linear, transform .08s linear; | ||||
| } | } | ||||
| a { | |||||
| color: yellow; | |||||
| } | |||||