X-Git-Url: https://gerrit.akraino.org/r/gitweb?p=ta%2Fdistributed-state-server.git;a=blobdiff_plain;f=src%2Fdss%2Fserver%2Fdss_rpc_processor.py;fp=src%2Fdss%2Fserver%2Fdss_rpc_processor.py;h=05908c7219fbf01adc51d3d706b433c2058e907a;hp=0000000000000000000000000000000000000000;hb=bd5a0a173f1ae9c64782fbf47565cc26ed23b448;hpb=03589032bfdfba119409e7c1c5bfa82ca52f53f7 diff --git a/src/dss/server/dss_rpc_processor.py b/src/dss/server/dss_rpc_processor.py new file mode 100644 index 0000000..05908c7 --- /dev/null +++ b/src/dss/server/dss_rpc_processor.py @@ -0,0 +1,66 @@ +# Copyright 2019 Nokia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +from dss.api import dss_msg +from dss.server import dss_rpc_handler + +class RPCProcessor(object): + def __init__(self): + self.handlers = [] + + def add_handler(self, handler): + self.handlers.append(handler) + + def get_handler(self, name): + for handler in self.handlers: + if handler.get_name() == name: + return handler + return None + + ''' + process the request + Arguments: + reqstring the request string + Return: + repstring the reply string + ''' + def process(self, reqstring): + reppayload = {} + result = 'OK' + id = None + name = None + try: + # deserialize req string + reqmsg = dss_msg.Msg() + reqmsg.deserialize(reqstring) + name = reqmsg.get_name() + + logging.info("Processing %s" % name) + + id = reqmsg.get_id() + + # find suitable handler + handler = self.get_handler(name) + + # process message + reppayload = handler.handle(reqmsg.get_payload()) + except Exception as exp: + logging.warning('Failed when processing %s' % reqstring) + result = str(exp) + + repmsg = dss_msg.Msg(name, id, reppayload, result) + + return repmsg.serialize()