Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

8 年前
8 年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. clients = []
  17. local_state = abp.GraphState()
  18. def process_edit(edit, client, server):
  19. action = edit["action"]
  20. print edit
  21. if action == "create":
  22. local_state.add_qubit(edit["name"], position=edit["position"], vop=0)
  23. if action == "cz":
  24. local_state.act_cz(edit["start"], edit["end"])
  25. if action == "hadamard":
  26. local_state.act_cz(edit["start"], edit["end"])
  27. if action == "delete":
  28. local_state._del_node(edit["node"])
  29. server.send_message(client, json.dumps(local_state.to_json()))
  30. def new_message(client, server, message):
  31. if message.startswith("edit:"):
  32. print message[5:]
  33. edit = json.loads(message[5:])
  34. print "Received update from javascript."
  35. process_edit(edit, client, server)
  36. else:
  37. print "Received update from python {}.".format(client["id"])
  38. print message
  39. local_state.from_json(json.loads(message))
  40. print local_state
  41. server.send_message_to_all(message)
  42. def new_client(client, server):
  43. print "Client {} connected.".format(client["id"])
  44. clients.append(client)
  45. print "Sent state of {} nodes to client {}".format(local_state.order(), client["id"])
  46. server.send_message(client, json.dumps(local_state.to_json()))
  47. def client_left(client, server):
  48. print "Client {} disconnected.".format(client["id"])
  49. clients.remove(client)
  50. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  51. """ Handle requests in a separate thread """
  52. if __name__ == '__main__':
  53. parser = argparse.ArgumentParser(description = "ABP websocket server")
  54. parser.add_argument("-v", action="store_true", help="Launch browser")
  55. args = parser.parse_args()
  56. # Change to the right working dir
  57. where = os.path.abspath(resource_filename("abp.static", ""))
  58. os.chdir(where)
  59. # Start the HTTP server
  60. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  61. thread = threading.Thread(target = httpserver.serve_forever)
  62. thread.daemon = True
  63. thread.start()
  64. if args.v:
  65. webbrowser.open("http://localhost:5001/")
  66. # Start the websocket server
  67. server = WebsocketServer(5000)
  68. server.set_fn_new_client(new_client)
  69. server.set_fn_message_received(new_message)
  70. server.set_fn_client_left(client_left)
  71. server.run_forever()
  72. httpserver.shutdown()