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

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