Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/python
  2. """
  3. This is a simple websocket relay server that facilitates realtime visualization of GraphStates.
  4. It doesn't do much except echo websocket messages to all connected clients, and serve some static content over HTTP.
  5. """
  6. from websocket_server import WebsocketServer
  7. from SimpleHTTPServer import SimpleHTTPRequestHandler
  8. from BaseHTTPServer import HTTPServer
  9. from SocketServer import ThreadingMixIn
  10. import os, sys, threading
  11. import webbrowser
  12. import argparse
  13. import abp
  14. import json
  15. from pkg_resources import resource_filename
  16. import time
  17. clients = []
  18. local_state = abp.GraphState()
  19. def process_edit(edit, client, server):
  20. action = edit["action"]
  21. print edit
  22. if action == "create":
  23. local_state.add_qubit(edit["name"], position=edit["position"], vop=0)
  24. elif action == "cz":
  25. local_state.act_cz(edit["start"], edit["end"])
  26. elif action == "hadamard":
  27. local_state.act_hadamard(edit["node"])
  28. elif action == "phase":
  29. local_state.act_local_rotation(edit["node"], "phase")
  30. elif action == "delete":
  31. local_state._del_node(edit["node"])
  32. elif action == "localcomplementation":
  33. local_state.local_complementation(edit["node"])
  34. elif action == "measure":
  35. local_state.measure(edit["node"], "p"+edit["basis"])
  36. else:
  37. pass
  38. server.send_message(client, json.dumps(local_state.to_json()))
  39. def new_message(client, server, message):
  40. if message.startswith("edit:"):
  41. print message[5:]
  42. edit = json.loads(message[5:])
  43. print "Received update from javascript."
  44. process_edit(edit, client, server)
  45. else:
  46. print "Received update from python {}.".format(client["id"])
  47. print message
  48. local_state.from_json(json.loads(message))
  49. print local_state
  50. server.send_message_to_all(message)
  51. def new_client(client, server):
  52. print "Client {} connected.".format(client["id"])
  53. clients.append(client)
  54. print "Sent state of {} nodes to client {}".format(local_state.order(), client["id"])
  55. server.send_message(client, json.dumps(local_state.to_json()))
  56. def client_left(client, server):
  57. print "Client {} disconnected.".format(client["id"])
  58. clients.remove(client)
  59. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  60. """ Handle requests in a separate thread """
  61. if __name__ == '__main__':
  62. parser = argparse.ArgumentParser(description = "ABP websocket server")
  63. parser.add_argument("-v", action="store_true", help="Launch browser")
  64. args = parser.parse_args()
  65. # Change to the right working dir
  66. where = os.path.abspath(resource_filename("abp.static", ""))
  67. os.chdir(where)
  68. # Start the HTTP server
  69. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  70. thread = threading.Thread(target = httpserver.serve_forever)
  71. thread.daemon = True
  72. thread.start()
  73. if args.v:
  74. webbrowser.open("http://localhost:5001/")
  75. # Start the websocket server
  76. server = WebsocketServer(5000)
  77. server.set_fn_new_client(new_client)
  78. server.set_fn_message_received(new_message)
  79. server.set_fn_client_left(client_left)
  80. server.run_forever()
  81. httpserver.shutdown()