Possibility to get the server version over JSON-RPC/Websocket.

This commit is contained in:
grossmj 2014-05-01 15:10:06 -06:00
parent 93058f92d4
commit ed8b93dee2
2 changed files with 23 additions and 9 deletions

View File

@ -22,8 +22,9 @@ JSON-RPC protocol over Websockets.
import zmq import zmq
import uuid import uuid
import tornado.websocket import tornado.websocket
from ..version import __version__
from tornado.escape import json_decode from tornado.escape import json_decode
from ..version import __version__
from ..jsonrpc import JSONRPCResponse
from ..jsonrpc import JSONRPCParseError from ..jsonrpc import JSONRPCParseError
from ..jsonrpc import JSONRPCInvalidRequest from ..jsonrpc import JSONRPCInvalidRequest
from ..jsonrpc import JSONRPCMethodNotFound from ..jsonrpc import JSONRPCMethodNotFound
@ -52,6 +53,20 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler):
self._session_id = str(uuid.uuid4()) self._session_id = str(uuid.uuid4())
self.zmq_router = zmq_router 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 @property
def session_id(self): def session_id(self):
""" """
@ -101,6 +116,10 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler):
:param module: module string :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 # Make sure the destination is not already registered
# by another module for instance # by another module for instance
assert destination not in cls.destinations assert destination not in cls.destinations
@ -114,8 +133,6 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler):
""" """
log.info("Websocket client {} connected".format(self.session_id)) 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) self.clients.add(self)
def on_message(self, message): def on_message(self, message):
@ -126,7 +143,7 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler):
""" """
log.debug("Received Websocket message: {}".format(message)) log.debug("Received Websocket message: {}".format(message))
if self.zmq_router.closed: if self.zmq_router.closed:
# no need to proceed, the ZeroMQ router has been closed. # no need to proceed, the ZeroMQ router has been closed.
return return
@ -150,9 +167,9 @@ class JSONRPCWebSocket(tornado.websocket.WebSocketHandler):
# This is a notification, silently ignore this error... # This is a notification, silently ignore this error...
return return
if method.startswith("builtin"): if method.startswith("builtin") and request_id:
log.info("calling built-in method {}".format(method)) log.info("calling built-in method {}".format(method))
self.destinations[method]() self.destinations[method](request_id, request.get("params"))
return return
module = self.destinations[method] module = self.destinations[method]

View File

@ -115,9 +115,6 @@ class Server(object):
# instance.start() # starts the new process # 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: for module in MODULES:
instance = module(module.__name__.lower(), instance = module(module.__name__.lower(),
"127.0.0.1", # ZeroMQ server address "127.0.0.1", # ZeroMQ server address