Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

97 lines
3.1KB

  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. def new_message(client, server, message):
  41. if message.startswith("edit:"):
  42. edit = json.loads(message[5:])
  43. process_edit(edit, client, server)
  44. else:
  45. print "Received update from python {}.".format(client["id"])
  46. print message
  47. local_state.from_json(json.loads(message))
  48. print local_state
  49. server.send_message_to_all(message)
  50. def new_client(client, server):
  51. print "Client {} connected.".format(client["id"])
  52. clients.append(client)
  53. print "Sent state of {} nodes to client {}".format(local_state.order(), client["id"])
  54. server.send_message(client, json.dumps(local_state.to_json()))
  55. def client_left(client, server):
  56. print "Client {} disconnected.".format(client["id"])
  57. clients.remove(client)
  58. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  59. """ Handle requests in a separate thread """
  60. if __name__ == '__main__':
  61. parser = argparse.ArgumentParser(description = "ABP websocket server")
  62. parser.add_argument("-v", action="store_true", help="Launch browser")
  63. args = parser.parse_args()
  64. # Change to the right working dir
  65. where = os.path.abspath(resource_filename("abp.static", ""))
  66. os.chdir(where)
  67. # Start the HTTP server
  68. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  69. thread = threading.Thread(target = httpserver.serve_forever)
  70. thread.daemon = True
  71. thread.start()
  72. if args.v:
  73. webbrowser.open("http://localhost:5001/")
  74. # Start the websocket server
  75. server = WebsocketServer(5000)
  76. server.set_fn_new_client(new_client)
  77. server.set_fn_message_received(new_message)
  78. server.set_fn_client_left(client_left)
  79. server.run_forever()
  80. httpserver.shutdown()