Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

abpserver 2.4KB

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