def testParallelCallOfOneMethod(self): numParallelCalls = 3 runningCalls = [0] callLock = Condition() with rsb.createLocalServer('/takesometime', inProcessNoIntrospectionConfig) \ as localServer: def takeSomeTime(e): with callLock: runningCalls[0] = runningCalls[0] + 1 callLock.notifyAll() with callLock: while runningCalls[0] < numParallelCalls: callLock.wait() localServer.addMethod("takeSomeTime", takeSomeTime, str, allowParallelExecution=True) with rsb.createRemoteServer('/takesometime', inProcessNoIntrospectionConfig) \ as remoteServer: results = [ remoteServer.takeSomeTime. async ('call{}'.format(x)) for x in range(numParallelCalls) ] for r in results: r.get(10)
def testRpcRoundtrip(self): with rsb.createServer(self.scope) as server, \ rsb.createRemoteServer(self.scope) as client: methodName = 'test' data = 'bla' server.addMethod(methodName, lambda x: x, str, str) self.assertEqual(data, client.test(data))
def testRemoteServer(self): server = None method = None with rsb.createRemoteServer('/') as participant: server = participant self.assertEqual(self.creationCalls, [(server, None)]) method = server.echo self.assertTrue((method, server) in self.creationCalls) self.assertTrue(server in self.destructionCalls) self.assertTrue(method in self.destructionCalls)
def _get_remote_server(self, event_or_iu): '''Return (or create, store and return) a remote server.''' _owner = self._get_owner(event_or_iu) if _owner: try: return self._remote_server_store[_owner] except KeyError: remote_server = rsb.createRemoteServer(rsb.Scope(str(_owner))) self._remote_server_store[_owner] = remote_server return remote_server else: None
def testNonIdentifierMethodName(self): serverScope = '/non-identifier-server' methodName = 'non-identifier-method' with rsb.createLocalServer(serverScope, inProcessNoIntrospectionConfig) \ as localServer: localServer.addMethod(methodName, lambda x: x, str, str) with rsb.createRemoteServer(serverScope, inProcessNoIntrospectionConfig) \ as remoteServer: self.assertEqual( remoteServer.getMethod(methodName)('foo'), 'foo')
def testVoidMethods(self): with rsb.createLocalServer('/void', inProcessNoIntrospectionConfig) \ as localServer: def nothing(e): pass localServer.addMethod("nothing", nothing, str) with rsb.createRemoteServer('/void', inProcessNoIntrospectionConfig) \ as remoteServer: future = remoteServer.nothing. async ("test") future.get(1)
def update_light_color(self, current_consumption=0): with rsb.createRemoteServer(self.light_scope) as server: print 'update light color related to power consumption.' # compute color value consumption_color = HSBColor() consumption_color.saturation = 100 consumption_color.brightness = 100 lowerhue, higherhue = sorted([self.hue1, self.hue2]) doeswrap = (lowerhue - higherhue + 360) < (higherhue - lowerhue) if doeswrap: lowerhue, higherhue = (higherhue, lowerhue + 360) consumption_color.hue = self._linmap(current_consumption, [0, 1000], [lowerhue, higherhue], crop=True) % 360 print 'computed hue related to power consumption:', consumption_color.hue print 'setting light color' server.setColor.async(consumption_color)
def testRoundTrip(self): with rsb.createLocalServer('/roundtrip', methods=[('addone', lambda x: long(x + 1), long, long)], config=inProcessNoIntrospectionConfig): with rsb.createRemoteServer('/roundtrip', inProcessNoIntrospectionConfig) \ as remoteServer: # Call synchronously self.assertEqual(map(remoteServer.addone, range(100)), range(1, 101)) # Call synchronously with timeout self.assertEqual( [remoteServer.addone(x, timeout=10) for x in range(100)], range(1, 101)) # Call asynchronously self.assertEqual( map(lambda x: x.get(), map(remoteServer.addone. async, range(100))), range(1, 101))
# CoR-Lab, Research Institute for Cognition and Robotics # Bielefeld University # # ============================================================ # mark-start::body import logging import rsb if __name__ == "__main__": # Pacify logger. logging.basicConfig() # Create a RemoteServer object for the remote server at scope # /example/server. Method calls should complete within five # seconds. with rsb.createRemoteServer('/example/server') as server: # Call the method 'echo' on the remote server passing it a # string argument. The server's reply is returned from the call as # for a regular function call. print('server replied to synchronous call: "%s"' % server.echo('bla')) # Call the method 'echo' again, this time asynchronously. future = server.echo. async ('bla') # do other things print('server replied to asynchronous call: "%s"' % future.get(timeout=10)) # mark-end::body
def __init__(self, scope, config): self.scope = scope self.remote = rsb.createRemoteServer(scope, config=config) self.converters = {}