Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

abpserver 3.1KB

8年前
8年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. elif action == "cz":
  24. local_state.act_cz(edit["start"], edit["end"])
  25. elif action == "hadamard":
  26. local_state.act_hadamard(edit["node"])
  27. elif action == "phase":
  28. local_state.act_local_rotation(edit["node"], "phase")
  29. elif action == "delete":
  30. local_state._del_node(edit["node"])
  31. elif action == "localcomplementation":
  32. local_state.local_complementation(edit["node"])
  33. elif action == "measure":
  34. local_state.measure(edit["node"], "p"+edit["basis"])
  35. else:
  36. pass
  37. server.send_message(client, json.dumps(local_state.to_json()))
  38. def new_message(client, server, message):
  39. if message.startswith("edit:"):
  40. print message[5:]
  41. edit = json.loads(message[5:])
  42. print "Received update from javascript."
  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()