Anders and Briegel in Python
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

abpserver 2.6KB

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