Procházet zdrojové kódy

Server / client architecture is looking good!

master
Pete Shadbolt před 8 roky
rodič
revize
f76b0d9595
8 změnil soubory, kde provedl 113 přidání a 74 odebrání
  1. +1
    -0
      abp/__init__.py
  2. +39
    -0
      abp/client.py
  3. +3
    -3
      abp/graphstate.py
  4. +5
    -10
      examples/client.py
  5. +25
    -14
      server/server.py
  6. +28
    -1
      server/static/main.css
  7. +0
    -44
      server/static/poll.js
  8. +12
    -2
      server/templates/index.html

+ 1
- 0
abp/__init__.py Zobrazit soubor

@@ -1,3 +1,4 @@
# Alias some stuff to make imports cleaner
from abp.graphstate import GraphState
from abp.qi import CircuitModel
from abp.client import Client

+ 39
- 0
abp/client.py Zobrazit soubor

@@ -0,0 +1,39 @@
import requests
import abp, json

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)
assert response.status_code == 200
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):
return self.session.post(self.root+"/state", data=state.to_json())

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()

+ 3
- 3
abp/graphstate.py Zobrazit soubor

@@ -164,9 +164,9 @@ class GraphState(object):
def from_json(self, data):
""" Reconstruct from JSON """
self.__init__([])
self.vops = data["nodes"]
self.meta = data["meta"]
self.ngbh = {key: set() for key in self.vops}
self.vops = {int(key): value for key, value in data["nodes"].items()}
self.meta = {int(key): value for key, value in data["meta"].items()}
self.ngbh = {int(key): set() for key in self.vops}
self.add_edges(data["edges"])

def to_networkx(self):


+ 5
- 10
examples/client.py Zobrazit soubor

@@ -1,15 +1,10 @@
import requests
import abp, json
import time

s = requests.Session()
output = s.get("http://localhost:5000/state").content
s.post("http://localhost:5000/state", output).content
client = abp.Client()

s.get("http://localhost:5000/add/99")
s.get("http://localhost:5000/add/100")

print s.get("http://localhost:5000/state").content

s.get("http://localhost:5000/clear")
s.close()
client.add_node(0)
client.add_node(1)

print client.get_state()

+ 25
- 14
server/server.py Zobrazit soubor

@@ -1,8 +1,13 @@
from flask import Flask, request, render_template, jsonify
from werkzeug.contrib.cache import SimpleCache
import json
import abp

graphstate = abp.GraphState()
#TODO: only send deltas

#graphstate = abp.GraphState()
cache=SimpleCache()
cache.set("state", abp.GraphState())
app = Flask(__name__)

@app.route("/")
@@ -12,38 +17,44 @@ def index():
@app.route("/state", methods = ["GET", "POST"])
def state():
if request.method == "GET":
return jsonify(graphstate.to_json())
return jsonify(cache.get("state").to_json())
elif request.method == "POST":
graphstate = abp.GraphState()
graphstate.from_json(json.loads(request.data))
return jsonify(graphstate.to_json())
cache.set("state", graphstate)
return jsonify({"update": "ok"})

@app.route("/add/<int:node>")
def add(node):
@app.route("/add_node/<int:node>")
def add_node(node):
""" Add a node to the graph """
graphstate = cache.get("state")
graphstate.add_node(node)
return jsonify(graphstate.to_json())
cache.set("state", graphstate)
return jsonify({"add_node": "okay"})

@app.route("/rotate/<int:node>/<int:operation>")
def rotate(node):
@app.route("/act_local_rotation/<int:node>/<int:operation>")
def act_local_rotation(node):
""" Add a node to the graph """
# TODO: try to lookup the operation first
graphstate.act_local_rotation(node, operation)
return jsonify(graphstate.to_json())
return jsonify({"act_local_rotation": "okay"})

@app.route("/cz/<int:a>/<int:b>")
def cz(a, b):
@app.route("/act_cz/<int:a>/<int:b>")
def act_cz(a, b):
""" Add a node to the graph """
graphstate.act_cz(a, b)
return jsonify(graphstate.to_json())
return jsonify({"act_cz": "okay"})

@app.route("/clear")
def clear():
""" Clear the current state """
graphstate = abp.GraphState()
return jsonify({"clear": "ok"})
cache.set("state", graphstate)
return jsonify({"clear": "okay"})

if __name__ == "__main__":
app.debug = True
app.debug = False
app.run(host="0.0.0.0")



+ 28
- 1
server/static/main.css Zobrazit soubor

@@ -1,10 +1,37 @@
html, body { margin: 0; padding: 0; overflow: hidden; font-size: 10pt; font-family: "courier new"; }
#infoholder {
background: black; color:white; padding: 5px; margin:0px; position: absolute; top:5px; left:5px; font-family:"courier new"; text-align: center; font-size:10pt;
background: black;
color:white;
padding: 5px;
margin:0px;
position: absolute;
top:5px;
left:5px;
font-family:"courier new";
text-align: center;
font-size:10pt;
height:15px;
border-radius:3px;
}

#pallette {
background-color: black;
/*border-radius:3px;*/
color:white;
padding: 10px;
font-family:"courier new";
position: absolute;
top: 10px;
right: 10px;
font-size: 9pt;
}

ul {
list-style-type: none;
padding: 0px;
margin: 0px;
}

.visible {
visibility: visible;
opacity: 1;


+ 0
- 44
server/static/poll.js Zobrazit soubor

@@ -1,46 +1,2 @@
var url = "ws://localhost:8000/";

function doConnect() {
websocket = new WebSocket(url);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage;
websocket.onerror = onError;
}

function onOpen(evt) {
writeToScreen("connected\n");
doSend("Hello from the browser");
}

function onClose(evt) {
writeToScreen("disconnected\n");
}

function onMessage(evt) {
writeToScreen("response: " + evt.data + '\n');
}

function onError(evt) {
writeToScreen('error: ' + evt.data + '\n');
websocket.close();
}

function doSend(message) {
writeToScreen("sent: " + message + '\n');
websocket.send(message);
}

function writeToScreen(message) {
console.log(message);
}

function init() {
doConnect();
}

function doDisconnect() {
websocket.close();
}

window.addEventListener("load", init, false);

+ 12
- 2
server/templates/index.html Zobrazit soubor

@@ -5,7 +5,7 @@
<meta charset="utf-8">
<title>abp</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1" />
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="/static/main.css">
</head>

<body>
@@ -19,8 +19,18 @@
<img id="ball" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABmJLR0QAAAAAAA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O+Q8ppT8dV5kvxyH+L1Gz8+8QQu6M4/ibW1tbO/31dWxzDmJsqTP/+VLWwWIHXZjOhq3hEI7jbFBKf8IY+wYh5N/jemP1AGBkAH4F4Gtpmt4WRdED586d2xg8+SS24hip4xRdJ1UW6+WxbbKtN2EOfXB3FYDR0aM4cuQIwjBEo9EAY8wD8GoArwLwMgDHARwD0AQQaCO5HQDnxrOJJwE8BuDhfr+fdrtdtNttuOfOYct1wXwfvu/D8zxwzsE5R6VSQRAECIIAvu+Dc158Rl3NjeFXZnIJoAEgBLAEIL7xRoRhCMaYGsEXm7P1Q+3xU9coilB/7DH8B8BFAF1KAUrhum6xhd7zPHieNyG02kushFbv6xut1DbbKwaADQwKoKJdTfsbAJAA+pqP2R2/VkxyxtpTW+eUkGpzpWKEP2aIDpS+3U6165+f/w6Pz8PaKWI2RBST9H3C+p8rfN+HEGKC+sph/hckywMtfS8qcQAAAABJRU5ErkJggg=="/
style=display:none;>

<div id=test>Hello World</div>
<div id=infoholder class=hidden>
</div>

<div id=pallette>
<ul>
<li>add_qubit</li>
<li>delete_qubit</li>
<li>hadamard</li>
<li>cz</li>
<li>clear</li>
</ul>
</div>

</body>
</html>

Načítá se…
Zrušit
Uložit