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.

abpserver 3.2KB

8 anni fa
8 anni fa
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. from pprint import pprint
  17. import time
  18. clients = []
  19. local_state = abp.GraphState()
  20. def process_edit(edit, client, server):
  21. action = edit["action"]
  22. pprint(edit)
  23. if action == "create":
  24. local_state.add_qubit(edit["name"], position=edit["position"], vop=0)
  25. elif action == "cz":
  26. local_state.act_cz(edit["start"], edit["end"])
  27. elif action == "hadamard":
  28. local_state.act_hadamard(edit["node"])
  29. elif action == "phase":
  30. local_state.act_local_rotation(edit["node"], "phase")
  31. elif action == "delete":
  32. local_state._del_node(edit["node"])
  33. elif action == "localcomplementation":
  34. local_state.local_complementation(edit["node"])
  35. elif action == "measure":
  36. local_state.measure(edit["node"], "p"+edit["basis"])
  37. else:
  38. pass
  39. #server.send_message(client, json.dumps(local_state.to_json()))
  40. server.send_message_to_all(json.dumps(local_state.to_json()))
  41. def new_message(client, server, message):
  42. if message.startswith("edit:"):
  43. edit = json.loads(message[5:])
  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()