-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrpc_api.py
41 lines (32 loc) · 1.5 KB
/
rpc_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
import threading
class RPCApi():
functions = []
def __init__(self, config):
self.config = config
self.server = SimpleJSONRPCServer((self.config['rpc_host'], self.config['rpc_port']))
self.server.timeout = self.config['rpc_timeout'] if "rpc_timeout" in config else 1
self.register_function(self.list_functions, "list_functions")
def register_functions(self, **kwargs):
"""Registers functions with the server."""
for function_name in kwargs:
function = kwargs[function_name]
self.register_function(function, function_name)
def register_function(self, function, function_name):
"""Registers a single function with the server."""
self.server.register_function(function, function_name)
self.functions.append(function_name)
def list_functions(self):
"""An externally accessible function returning all the registered function names"""
return list(set(self.functions))
def poll(self):
"""Serves one request from the waiting requests and returns"""
self.server.handle_request()
def run(self):
"""Blocks execution and runs the server till the program shutdown"""
self.server.serve_forever()
def start_thread(self):
"""Starts self.run() in a separate thread"""
self.thread = threading.Thread(target=self.run)
self.thread.daemon = True
self.thread.start()