comparison src/server/server.py @ 935:c3ae008f7a0f

server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
author Goffi <goffi@goffi.org>
date Thu, 27 Apr 2017 01:11:53 +0200
parents 8a393ae90f8c
children 78692d47340d
comparison
equal deleted inserted replaced
934:a21fee7e30ee 935:c3ae008f7a0f
316 def __init__(self, sat_host): 316 def __init__(self, sat_host):
317 jsonrpc.JSONRPC.__init__(self) 317 jsonrpc.JSONRPC.__init__(self)
318 self.sat_host = sat_host 318 self.sat_host = sat_host
319 319
320 def asyncBridgeCall(self, method_name, *args, **kwargs): 320 def asyncBridgeCall(self, method_name, *args, **kwargs):
321 """Call an asynchronous bridge method and return a deferred 321 return self.sat_host.bridgeCall(method_name, *args, **kwargs)
322
323 @param method_name: name of the method as a unicode
324 @return: a deferred which trigger the result
325
326 """
327 d = defer.Deferred()
328
329 def _callback(*args):
330 if not args:
331 d.callback(None)
332 else:
333 if len(args) != 1:
334 Exception("Multiple return arguments not supported")
335 d.callback(args[0])
336
337 def _errback(result):
338 d.errback(failure.Failure(jsonrpclib.Fault(C.ERRNUM_BRIDGE_ERRBACK, result.classname)))
339
340 kwargs["callback"] = _callback
341 kwargs["errback"] = _errback
342 getattr(self.sat_host.bridge, method_name)(*args, **kwargs)
343 return d
344 322
345 323
346 class MethodHandler(JSONRPCMethodManager): 324 class MethodHandler(JSONRPCMethodManager):
347 325
348 def __init__(self, sat_host): 326 def __init__(self, sat_host):
1945 self._version_cache = u"{} ({})".format(version, utils.getRepositoryData(libervia)) 1923 self._version_cache = u"{} ({})".format(version, utils.getRepositoryData(libervia))
1946 return self._version_cache 1924 return self._version_cache
1947 else: 1925 else:
1948 return version 1926 return version
1949 1927
1928 def bridgeCall(self, method_name, *args, **kwargs):
1929 """Call an asynchronous bridge method and return a deferred
1930
1931 @param method_name: name of the method as a unicode
1932 @return: a deferred which trigger the result
1933
1934 """
1935 d = defer.Deferred()
1936
1937 def _callback(*args):
1938 if not args:
1939 d.callback(None)
1940 else:
1941 if len(args) != 1:
1942 Exception("Multiple return arguments not supported")
1943 d.callback(args[0])
1944
1945 def _errback(result):
1946 d.errback(failure.Failure(jsonrpclib.Fault(C.ERRNUM_BRIDGE_ERRBACK, result.classname)))
1947
1948 kwargs["callback"] = _callback
1949 kwargs["errback"] = _errback
1950 getattr(self.bridge, method_name)(*args, **kwargs)
1951 return d
1952
1950 def addCleanup(self, callback, *args, **kwargs): 1953 def addCleanup(self, callback, *args, **kwargs):
1951 """Add cleaning method to call when service is stopped 1954 """Add cleaning method to call when service is stopped
1952 cleaning method will be called in reverse order of they insertion 1955 cleaning method will be called in reverse order of they insertion
1953 @param callback: callable to call on service stop 1956 @param callback: callable to call on service stop
1954 @param *args: list of arguments of the callback 1957 @param *args: list of arguments of the callback