3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from dss.api import dss_msg
18 from dss.server import dss_rpc_handler
20 class RPCProcessor(object):
24 def add_handler(self, handler):
25 self.handlers.append(handler)
27 def get_handler(self, name):
28 for handler in self.handlers:
29 if handler.get_name() == name:
36 reqstring the request string
38 repstring the reply string
40 def process(self, reqstring):
46 # deserialize req string
47 reqmsg = dss_msg.Msg()
48 reqmsg.deserialize(reqstring)
49 name = reqmsg.get_name()
51 logging.info("Processing %s" % name)
55 # find suitable handler
56 handler = self.get_handler(name)
59 reppayload = handler.handle(reqmsg.get_payload())
60 except Exception as exp:
61 logging.warning('Failed when processing %s' % reqstring)
64 repmsg = dss_msg.Msg(name, id, reppayload, result)
66 return repmsg.serialize()