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
, project
=None):
40 super().__init
__(log
, dts
, loop
, project
)
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
64 if self
.project
and not self
.project
.rpc_check(msg
, xact_info
=xact_info
):
68 rpc_op
= yield from self
.callback(ks_path
, msg
)
69 xact_info
.respond_xpath(
70 rwdts
.XactRspCode
.ACK
,
74 except Exception as e
:
76 xact_info
.respond_xpath(
77 rwdts
.XactRspCode
.NACK
,
83 self
._log
.warning("RPC already registered for project {}".
84 format(self
._project
.name
))
87 reg_event
= asyncio
.Event(loop
=self
.loop
)
90 def on_ready(regh
, status
):
93 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
94 on_prepare
=self
.on_prepare
,
97 with self
.dts
.group_create() as group
:
98 self
.reg
= group
.register(
99 xpath
=self
.input_xpath
,
103 yield from reg_event
.wait()
105 def deregister(self
):
106 self
.reg
.deregister()
111 def callback(self
, ks_path
, msg
):
112 """Subclass needs to override this method
115 ks_path : Key spec path