|
@@ -7,51 +7,63 @@ import json |
|
|
import threading |
|
|
import threading |
|
|
import time |
|
|
import time |
|
|
|
|
|
|
|
|
state = 0 |
|
|
|
|
|
|
|
|
|
|
|
class VizHandler(SimpleHTTPRequestHandler): |
|
|
class VizHandler(SimpleHTTPRequestHandler): |
|
|
""" Handles requests to the server """ |
|
|
""" Handles requests to the server """ |
|
|
def __init__(self, *args, **kwargs): |
|
|
def __init__(self, *args, **kwargs): |
|
|
SimpleHTTPRequestHandler.__init__(self, *args, **kwargs) |
|
|
SimpleHTTPRequestHandler.__init__(self, *args, **kwargs) |
|
|
|
|
|
|
|
|
def get_state(self): |
|
|
def get_state(self): |
|
|
|
|
|
""" Get the current graph state """ |
|
|
self.send_response(200) |
|
|
self.send_response(200) |
|
|
self.send_header('Content-Type', 'application/json') |
|
|
self.send_header('Content-Type', 'application/json') |
|
|
self.end_headers() |
|
|
self.end_headers() |
|
|
|
|
|
state = self.server.state |
|
|
self.wfile.write(json.dumps({"state":"{}".format(state)})) |
|
|
self.wfile.write(json.dumps({"state":"{}".format(state)})) |
|
|
|
|
|
|
|
|
def do_GET(self, *args, **kwargs): |
|
|
def do_GET(self, *args, **kwargs): |
|
|
|
|
|
""" Someone belled the server """ |
|
|
parsed_path = urlparse.urlparse(self.path) |
|
|
parsed_path = urlparse.urlparse(self.path) |
|
|
print parsed_path.path |
|
|
|
|
|
if parsed_path.path == "/state": |
|
|
if parsed_path.path == "/state": |
|
|
return self.get_state() |
|
|
return self.get_state() |
|
|
else: |
|
|
else: |
|
|
return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs) |
|
|
return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs) |
|
|
|
|
|
|
|
|
class VizServer(SocketServer.TCPServer): |
|
|
|
|
|
""" Runs the server in a new thread """ |
|
|
|
|
|
|
|
|
class Server(SocketServer.TCPServer): |
|
|
|
|
|
""" Serves the good stuff """ |
|
|
allow_reuse_address = True |
|
|
allow_reuse_address = True |
|
|
|
|
|
|
|
|
def __init__(self, port = 8000): |
|
|
def __init__(self, port = 8000): |
|
|
self.port = port |
|
|
self.port = port |
|
|
|
|
|
self.state = None |
|
|
SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler) |
|
|
SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler) |
|
|
|
|
|
|
|
|
|
|
|
def update(self, state): |
|
|
|
|
|
""" Update the in-memory state """ |
|
|
|
|
|
self.state = state |
|
|
|
|
|
|
|
|
def run(self): |
|
|
def run(self): |
|
|
|
|
|
""" Run in such a way that keyboard interrupts are caught properly """ |
|
|
try: |
|
|
try: |
|
|
self.serve_forever() |
|
|
self.serve_forever() |
|
|
except KeyboardInterrupt: |
|
|
except KeyboardInterrupt: |
|
|
"Caught keyboard interrupt" |
|
|
|
|
|
self.shutdown() |
|
|
self.shutdown() |
|
|
|
|
|
|
|
|
def start(self): |
|
|
def start(self): |
|
|
|
|
|
""" Start in a new thread """ |
|
|
thread = threading.Thread(None, self.run) |
|
|
thread = threading.Thread(None, self.run) |
|
|
thread.daemon = True |
|
|
thread.daemon = True |
|
|
thread.start() |
|
|
thread.start() |
|
|
print "Go to 127.0.0.0:{}".format(self.port) |
|
|
print "Go to 127.0.0.0:{}".format(self.port) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
server = VizServer() |
|
|
|
|
|
|
|
|
server = Server() |
|
|
server.start() |
|
|
server.start() |
|
|
|
|
|
|
|
|
|
|
|
i=0 |
|
|
while True: |
|
|
while True: |
|
|
state += 1 |
|
|
|
|
|
|
|
|
server.update(i) |
|
|
|
|
|
i += 1 |
|
|
time.sleep(1) |
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
server.shutdown() |
|
|
server.shutdown() |
|
|
|
|
|
|