diff --git a/src/server/__init__.py b/src/server/__init__.py deleted file mode 100644 index 6ca0c37..0000000 --- a/src/server/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ - -from ws import MMServerProtocol -from common import clients -from common import get_client -from common import add_client - - -__all__ = ["MMServerProtocol", "clients", "add_client", "get_client"] diff --git a/src/server/common.py b/src/server/common.py deleted file mode 100644 index 7c8b217..0000000 --- a/src/server/common.py +++ /dev/null @@ -1,15 +0,0 @@ - -clients = dict() - -def get_client(name): - return clients[name] - - -def add_client(name, client): - global clients - clients[name] = client - - -def remove_client(name): - global clients - del clients[name] diff --git a/src/server/connection.py b/src/server/connection.py deleted file mode 100644 index 2969392..0000000 --- a/src/server/connection.py +++ /dev/null @@ -1,74 +0,0 @@ - -import zlib -import json -import rospy -import time -import common -import struct -import multiprocessing -from std_msgs.msg import Float32 -import tornado.web -import tornado.websocket -import tornado.httpserver -import tornado.ioloop -from tornado import gen -from concurrent.futures import ProcessPoolExecutor - -def call_process(message): - MMServerProtocol.instance.process_message(message) - -class MMServerProtocol(tornado.websocket.WebSocketHandler): - lat_pubs = dict() - - def open(self, name): - common.add_client(name, self) - self.name_of_client = name - MMServerProtocol.lat_pubs[name] = rospy.Publisher("/jammi/" + name - + "/latency", Float32, queue_size=2) - print "Connected to: {}".format(name) - self.ioloop = tornado.ioloop.IOLoop.instance() - MMServerProtocol.instance = self - self.pool = multiprocessing.Pool(multiprocessing.cpu_count()) - - def send_to_client(self, to, message): - to.write_message(message, True) - - def process_message(self, message, callback): - received_time = time.time() - decompressed = zlib.decompress(message) - size = struct.unpack('=I', decompressed[:4]) - frmt = "%ds" % size[0] - unpacked = struct.unpack('=I' + frmt, decompressed) - msg = json.loads(unpacked[1]) - acknowledge = struct.pack('=b', 0) - self.ioloop.add_callback( - self.send_to_client, - common.get_client(msg["from"]), acknowledge) - latency = Float32() - latency.data = received_time - msg["stamp"] - MMServerProtocol.lat_pubs[msg["from"]].publish(latency) - if msg["to"][0] == "*": - for name in common.clients.keys(): - if name != msg["from"]: - self.ioloop.add_callback( - self.send_to_client, - common.get_client(name), message) - else: - for name in msg["to"]: - self.ioloop.add_callback( - self.send_to_client, - common.get_client(name), message) - return callback(message) - - @tornado.gen.coroutine - def on_message(self, message): - print len(message) - result = yield tornado.gen.Task(self.process_message, message) - return - pool = ProcessPoolExecutor() - fut = pool.submit(call_process, message) - ret = yield fut - pool.shutdown() - - def on_close(self): - common.remove_client(self.name_of_client) diff --git a/src/server/server_node.py b/src/server/server_node.py deleted file mode 100755 index 60674b9..0000000 --- a/src/server/server_node.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python - -import connection -import rospy -import signal -import tornado.web -import tornado.websocket -import tornado.httpserver -import tornado.ioloop - - -NODE_NAME = "jammi_server" - -settings = {'debug': False} -app = tornado.web.Application([ - (r'/(.*)', connection.MMServerProtocol), - ], **settings) - -def sig_handler(sig, frame): - tornado.ioloop.IOLoop.current().add_callback(shutdown) - -def shutdown(): - tornado.ioloop.IOLoop.current().stop() - -def run_server(host, port): - url = "ws://{}:{}".format(host, port) - http_server = tornado.httpserver.HTTPServer(app) - http_server.bind(port) - http_server.start(0) - tornado.ioloop.IOLoop.current().start() - -if __name__ == "__main__": - rospy.init_node(NODE_NAME, anonymous=False) - host = rospy.get_param("~host", "localhost") - port = rospy.get_param("~port", 9000) - signal.signal(signal.SIGTERM, sig_handler) - signal.signal(signal.SIGINT, sig_handler) - run_server(host, port)