Anders and Briegel in Python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

99 Zeilen
3.2KB

  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. print where
  68. os.chdir(where)
  69. # Start the HTTP server
  70. httpserver = ThreadedHTTPServer(('', 5001), SimpleHTTPRequestHandler)
  71. thread = threading.Thread(target = httpserver.serve_forever)
  72. thread.daemon = True
  73. thread.start()
  74. if args.v:
  75. webbrowser.open("http://localhost:5001/")
  76. # Start the websocket server
  77. server = WebsocketServer(5000)
  78. server.set_fn_new_client(new_client)
  79. server.set_fn_message_received(new_message)
  80. server.set_fn_client_left(client_left)
  81. server.run_forever()
  82. httpserver.shutdown()