svgui/pyjs/jsonrpc/jsonrpc.py
author Edouard Tisserant
Mon, 09 Dec 2019 14:53:42 +0100
branchsvghmi
changeset 2820 d9b5303d43dc
parent 1881 091005ec69c4
permissions -rw-r--r--
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Since Xenomai's cobalt thread are definitely incompatible with normal posix python interpreter binary's thread, we must synchronize them with arcane rt_pipes (the only ones that really work cross domain) as already done in debug and python async eval blocks.
from __future__ import absolute_import
import sys
import gluon.contrib.simplejson as simplejson


class JSONRPCServiceBase(object):

    def __init__(self):
        self.methods = {}

    def response(self, id, result):
        return simplejson.dumps({'version': '1.1', 'id': id,
                                 'result': result, 'error': None})

    def error(self, id, code, message):
        return simplejson.dumps({
            'id': id,
            'version': '1.1',
            'error': {'name': 'JSONRPCError',
                      'code': code,
                      'message': message}
        })

    def add_method(self, name, method):
        self.methods[name] = method

    def process(self, data):
        data = simplejson.loads(data)
        id, method, params = data["id"], data["method"], data["params"]
        if method in self.methods:
            try:
                result = self.methods[method](*params)
                return self.response(id, result)
            except Exception:
                etype, eval, _etb = sys.exc_info()
                return self.error(id, 100, 'Exception %s: %s' % (etype, eval))
            except BaseException:
                etype, eval, _etb = sys.exc_info()
                return self.error(id, 100, '%s: %s' % (etype.__name__, eval))
        else:
            return self.error(id, 100, 'method "%s" does not exist' % method)

    def listmethods(self):
        return self.methods.keys()