diff --git a/gns3server/handlers/jsonrpc_websocket.py b/gns3server/handlers/jsonrpc_websocket.py index ecd91cf7..bf00888f 100644 --- a/gns3server/handlers/jsonrpc_websocket.py +++ b/gns3server/handlers/jsonrpc_websocket.py @@ -22,8 +22,9 @@ JSON-RPC protocol over Websockets. import zmq import uuid import tornado.websocket -from ..version import __version__ from tornado.escape import json_decode +from ..version import __version__ +from ..jsonrpc import JSONRPCResponse from ..jsonrpc import JSONRPCParseError from ..jsonrpc import JSONRPCInvalidRequest from ..jsonrpc import JSONRPCMethodNotFound @@ -52,6 +53,20 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler): self._session_id = str(uuid.uuid4()) self.zmq_router = zmq_router + # special built-in to return the server version + self.register_destination("builtin.version", self._server_version) + + def _server_version(self, request_id, params): + """ + Builtin destination to return the server version. + + :param request_id: JSON-RPC call identifier + :param params: JSON-RPC method params (not used here) + """ + + json_message = {"version": __version__} + self.write_message(JSONRPCResponse(json_message, request_id)()) + @property def session_id(self): """ @@ -101,6 +116,10 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler): :param module: module string """ + if destination.startswith("builtin") and destination in cls.destinations: + # ignore new built-in destination registration if already registered + return + # Make sure the destination is not already registered # by another module for instance assert destination not in cls.destinations @@ -114,8 +133,6 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler): """ log.info("Websocket client {} connected".format(self.session_id)) - # send this server version when a client connects - self.write_message({"version": __version__}) self.clients.add(self) def on_message(self, message): @@ -126,7 +143,7 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler): """ log.debug("Received Websocket message: {}".format(message)) - + if self.zmq_router.closed: # no need to proceed, the ZeroMQ router has been closed. return @@ -150,9 +167,9 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler): # This is a notification, silently ignore this error... return - if method.startswith("builtin"): + if method.startswith("builtin") and request_id: log.info("calling built-in method {}".format(method)) - self.destinations[method]() + self.destinations[method](request_id, request.get("params")) return module = self.destinations[method] diff --git a/gns3server/server.py b/gns3server/server.py index b8462ee3..b667be2a 100644 --- a/gns3server/server.py +++ b/gns3server/server.py @@ -115,9 +115,6 @@ class Server(object): # instance.start() # starts the new process #======================================================================= - # special built-in destination to stop the server - # JSONRPCWebSocket.register_destination("builtin.stop", self._cleanup) - for module in MODULES: instance = module(module.__name__.lower(), "127.0.0.1", # ZeroMQ server address