Pete Shadbolt 8 лет назад
Родитель
Сommit
c797b8abd1
3 измененных файлов: 41 добавлений и 100 удалений
  1. +22
    -0
      examples/dream.py
  2. +18
    -99
      server/server.py
  3. +1
    -1
      server/static/poll.js

+ 22
- 0
examples/dream.py Просмотреть файл

@@ -0,0 +1,22 @@
import abp
import time

psi = abp.GraphState(use_server = True)

# This all happens instantly
psi.add_qubits(xrange(10))
psi.add_qubit(500)
psi.add_qubit(10) # fails

for i in range(10):
psi.act_hadamard(i)

for i in range(10, 100)
psi.act_local_rotation(i, "hadamard")

time.sleep(10)
# The user does some stuff and edits the state

print psi # We see the new qubits



+ 18
- 99
server/server.py Просмотреть файл

@@ -1,112 +1,31 @@
from flask import Flask, request, render_template, jsonify, g
from flask import Flask, request, render_template, jsonify
from flask_sockets import Sockets
from werkzeug.contrib.cache import SimpleCache
import werkzeug.serving
import json
import abp
import argparse

#TODO: only send deltas

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

class InvalidUsage(Exception):
status_code = 400

def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload

def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv

@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response

@app.before_request
def before_request():
g.state = cache.get("state")

@app.after_request
def after_request(response):
cache.set("state", g.state)
return response
sockets = Sockets(app)

@app.route("/")
def index():
return render_template("index.html")

@app.route("/state", methods = ["GET", "POST"])
def state():
if request.method == "GET":
output = g.state.to_json()
output["needs_update"] = cache.get("needs_update")
cache.set("needs_update", False)
return jsonify(output)
elif request.method == "POST":
g.state = abp.GraphState()
g.state.from_json(json.loads(request.data))
cache.set("needs_update", True)
return jsonify({"update": "ok"})

@app.route("/add_node/<int:node>")
def add_node(node):
""" Add a node to the graph """
if node in g.state.vops:
raise InvalidUsage("Node {} is already in the graph".format(node))

g.state.add_node(node)
g.state.layout()
cache.set("needs_update", True)
return jsonify({"add_node": "okay"})

@app.route("/act_local_rotation/<int:node>/<int:operation>")
def act_local_rotation(node, operation):
""" Add a node to the graph """
# TODO: try to lookup the operation first
if not node in g.state.vops:
raise InvalidUsage("Node {} does not exist".format(node))
if not operation in range(24):
raise InvalidUsage("Invalid local rotation {}".format(operation))

g.state.act_local_rotation(node, operation)
cache.set("needs_update", True)
return jsonify({"act_local_rotation": "okay"})

@app.route("/act_cz/<int:a>/<int:b>")
def act_cz(a, b):
""" Add a node to the graph """
for node in (a, b):
if not node in g.state.vops:
raise InvalidUsage("Node {} does not exist".format(node))
g.state.act_cz(a, b)
g.state.layout()
cache.set("needs_update", True)
return jsonify({"act_cz": "okay"})

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

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="Run in debug mode", action="store_true", default=False)
args = parser.parse_args()
app.debug = args.debug

app.run(host="0.0.0.0")

@sockets.route('/diff')
def diff_socket(ws):
while not ws.closed:
message = ws.receive()
print message
ws.send("Hi from the server, you said '{}'".format(message))

@werkzeug.serving.run_with_reloader
def runServer():
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app.debug = True
ws = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
ws.serve_forever()


+ 1
- 1
server/static/poll.js Просмотреть файл

@@ -1,6 +1,6 @@

function poll() {
var ws = new WebSocket("ws://localhost:5000/echo");
var ws = new WebSocket("ws://localhost:5000/diff");
ws.onopen = function()
{
// Web Socket is connected, send data using send()


Загрузка…
Отмена
Сохранить