3 # Copyright 2016 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 @author Varun Prasad (varun.prasad@riftio.com)
28 gi
.require_version("RwDts", "1.0")
30 from gi
.repository
import RwDts
as rwdts
33 from ..core
import DtsHandler
36 class AbstractRpcHandler(DtsHandler
):
37 """Base class to simplify RPC implementation
39 def __init__(self
, log
, dts
, loop
):
40 super().__init
__(log
, dts
, loop
)
42 if not asyncio
.iscoroutinefunction(self
.callback
):
43 raise ValueError('%s has to be a coroutine' % (self
.callback
))
50 def input_xpath(self
):
51 return "I,{}".format(self
.xpath
)
54 def output_xpath(self
):
55 return "O,{}".format(self
.xpath
)
58 return rwdts
.Flag
.PUBLISHER
61 def on_prepare(self
, xact_info
, action
, ks_path
, msg
):
62 assert action
== rwdts
.QueryAction
.RPC
65 rpc_op
= yield from self
.callback(ks_path
, msg
)
66 xact_info
.respond_xpath(
67 rwdts
.XactRspCode
.ACK
,
71 except Exception as e
:
73 xact_info
.respond_xpath(
74 rwdts
.XactRspCode
.NACK
,
79 reg_event
= asyncio
.Event(loop
=self
.loop
)
82 def on_ready(regh
, status
):
85 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
86 on_prepare
=self
.on_prepare
,
89 with self
.dts
.group_create() as group
:
90 self
.reg
= group
.register(
91 xpath
=self
.input_xpath
,
95 yield from reg_event
.wait()
99 def callback(self
, ks_path
, msg
):
100 """Subclass needs to override this method
103 ks_path : Key spec path