def testParsing(self): root = rsb.Scope("/") self.assertEqual(0, len(root.getComponents())) onePart = rsb.Scope("/test/") self.assertEqual(1, len(onePart.getComponents())) self.assertEqual("test", onePart.getComponents()[0]) manyParts = rsb.Scope("/this/is/a/dumb3/test/") self.assertEqual(5, len(manyParts.getComponents())) self.assertEqual("this", manyParts.getComponents()[0]) self.assertEqual("is", manyParts.getComponents()[1]) self.assertEqual("a", manyParts.getComponents()[2]) self.assertEqual("dumb3", manyParts.getComponents()[3]) self.assertEqual("test", manyParts.getComponents()[4]) # also ensure that the shortcut syntax works shortcut = rsb.Scope("/this/is") self.assertEqual(2, len(shortcut.getComponents())) self.assertEqual("this", shortcut.getComponents()[0]) self.assertEqual("is", shortcut.getComponents()[1]) # Non-ASCII characters are not allowed. However, unicode # object consisting of acceptable characters are OK. Scope(u'/') Scope(u'/test') self.assertRaises(ValueError, Scope, u'/br\xc3\xb6tchen')
def test_matching_sinks(self): dispatcher = rsb.eventprocessing.ScopeDispatcher() dispatcher.add_sink(rsb.Scope('/foo'), 1) dispatcher.add_sink(rsb.Scope('/foo'), 2) dispatcher.add_sink(rsb.Scope('/bar'), 3) def check(scope, expected): assert set( dispatcher.matching_sinks(rsb.Scope(scope))) == set(expected) check("/", ()) check("/foo", (1, 2)) check("/foo/baz", (1, 2)) check("/bar", (3,)) check("/bar/fez", (3,))
def notificationToEvent(notification, wireData, wireSchema, converter): """ Build event from notification. """ event = rsb.Event( rsb.EventId(uuid.UUID(bytes=notification.event_id.sender_id), notification.event_id.sequence_number)) event.scope = rsb.Scope(notification.scope) if notification.HasField("method"): event.method = notification.method event.type = converter.getDataType() event.data = converter.deserialize(wireData, wireSchema) # Meta data event.metaData.createTime = unixMicrosecondsToTime( notification.meta_data.create_time) event.metaData.sendTime = unixMicrosecondsToTime( notification.meta_data.send_time) event.metaData.setReceiveTime() for info in notification.meta_data.user_infos: event.metaData.setUserInfo(info.key, info.value) for time in notification.meta_data.user_times: event.metaData.setUserTime(time.key, unixMicrosecondsToTime(time.timestamp)) # Causes for cause in notification.causes: id = rsb.EventId(uuid.UUID(bytes=cause.sender_id), cause.sequence_number) event.addCause(id) return event
def notification_to_event(notification, wire_data, wire_schema, converter): """Build an event from a notification.""" event = rsb.Event( rsb.EventId(uuid.UUID(bytes=notification.event_id.sender_id), notification.event_id.sequence_number)) event.scope = rsb.Scope(notification.scope.decode('ASCII')) if notification.HasField("method"): event.method = notification.method.decode('ASCII') event.data_type = converter.data_type event.data = converter.deserialize(wire_data, wire_schema) # Meta data event.meta_data.create_time = unix_microseconds_to_time( notification.meta_data.create_time) event.meta_data.send_time = unix_microseconds_to_time( notification.meta_data.send_time) event.meta_data.set_receive_time() for info in notification.meta_data.user_infos: event.meta_data.set_user_info(info.key.decode('ASCII'), info.value.decode('ASCII')) for time in notification.meta_data.user_times: event.meta_data.set_user_time( time.key.decode('ASCII'), unix_microseconds_to_time(time.timestamp)) # Causes for cause in notification.causes: event_id = rsb.EventId(uuid.UUID(bytes=cause.sender_id), cause.sequence_number) event.add_cause(event_id) return event
def handleMessage(self, message): """ Maybe returns notification extracted from `message`. If `message` is one part of a fragmented notification for which some parts are still pending, a complete notification cannot be constructed and ``None`` is returned. Args: message: The received Spread message. Returns: notification: The assembled notification or ``None``. """ # Only handle regular messages. if not hasattr(message, 'msg_type'): return None fragment = FragmentedNotification() fragment.ParseFromString(message.message) self.__logger.debug( "Received notification fragment " "from bus (%s/%s), data length: %s", fragment.data_part, fragment.num_data_parts, len(fragment.notification.data)) result = self.__assemblyPool.add(fragment) if result is not None: notification, wireData, wireSchema = result return IncomingNotification(rsb.Scope(notification.scope), wireSchema, wireData, notification)
def _add_category_listener(self, iu_category): '''Create and store a listener on a specific category.''' if iu_category not in self._listener_store: cat_listener = rsb.createListener(rsb.Scope("/ipaaca/channel/"+str(self._channel)+"/category/"+str(iu_category)), config=self._participant_config) cat_listener.addHandler(self._handle_iu_events) self._listener_store[iu_category] = cat_listener self._category_interests.append(iu_category) LOGGER.info("Added listener in scope /ipaaca/channel/" + str(self._channel) + "/category/" + iu_category)
def test_parsing(self, str_repr, components): scope = rsb.Scope(str_repr) assert scope.components == components # Non-ASCII characters are not allowed. However, unicode # object consisting of acceptable characters are OK. with pytest.raises(ValueError): Scope('/br\xc3\xb6tchen')
def testConstruction(self): # Test creating a server without methods with rsb.createLocalServer('/some/scope', inProcessNoIntrospectionConfig) as server: self.assertEqual(server.methods, []) with rsb.createLocalServer(rsb.Scope('/some/scope'), inProcessNoIntrospectionConfig) as server: self.assertEqual(server.methods, []) # Test creating a server with directly specified methods with rsb.createLocalServer(rsb.Scope('/some/scope'), methods=[('foo', lambda x: x, str, str)], config=inProcessNoIntrospectionConfig) \ as server: self.assertEqual([m.name for m in server.methods], ['foo']) # Test creating a server that exposes method of an existing # object class SomeClass(object): def bar(x): pass someObject = SomeClass() with rsb.createLocalServer(rsb.Scope('/some/scope'), object=someObject, expose=[('bar', str, None)], config=inProcessNoIntrospectionConfig) \ as server: self.assertEqual([m.name for m in server.methods], ['bar']) # Cannot supply expose without object self.assertRaises(ValueError, rsb.createLocalServer, '/some/scope', expose=[('bar', str, None)]) # Cannot supply these simultaneously self.assertRaises(ValueError, rsb.createLocalServer, '/some/scope', object=someObject, expose=[('bar', str, None)], methods=[('foo', lambda x: x, str, str)])
def _get_informer(self, iu_category): '''Return (or create, store and return) an informer object for IUs of the specified category.''' if iu_category in self._informer_store: LOGGER.info("Returning informer on scope "+"/ipaaca/channel/"+str(self._channel)+"/category/"+str(iu_category)) return self._informer_store[iu_category] informer_iu = rsb.createInformer( rsb.Scope("/ipaaca/channel/"+str(self._channel)+"/category/"+str(iu_category)), config=self._participant_config, dataType=object) self._informer_store[iu_category] = informer_iu #new_tuple LOGGER.info("Returning NEW informer on scope "+"/ipaaca/channel/"+str(self._channel)+"/category/"+str(iu_category)) return informer_iu #return new_tuple
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 ensure_method(self, name): method = super().get_method(name) if method is None: scope = self.scope.concat(rsb.Scope('/' + name)) method = rsb.create_participant(RemoteMethod, scope, self.config, parent=self, server=self, name=name, request_type=object, reply_type=object) self.add_method(method) return method
def __init__(self, owning_component_name, channel=None, participant_config=None): '''Create an OutputBuffer. Keyword arguments: owning_component_name -- name of the entity that own this buffer participant_config -- RSB configuration ''' super(OutputBuffer, self).__init__(owning_component_name, channel, participant_config) self._unique_name = '/ipaaca/component/' + str(owning_component_name) + 'ID' + self._uuid + '/OB' self._server = rsb.createLocalServer(rsb.Scope(self._unique_name)) self._server.addMethod('updateLinks', self._remote_update_links, ipaaca.converter.IULinkUpdate, int) self._server.addMethod('updatePayload', self._remote_update_payload, ipaaca.converter.IUPayloadUpdate, int) self._server.addMethod('commit', self._remote_commit, ipaaca.ipaaca_pb2.IUCommission, int) self._server.addMethod('resendRequest', self._remote_request_resend, ipaaca.ipaaca_pb2.IUResendRequest, int) self._informer_store = {} self._id_prefix = str(owning_component_name)+'-'+str(self._uuid)+'-IU-' self.__iu_id_counter_lock = threading.Lock()
def add_method(self, name, func, request_type=object, reply_type=object, allow_parallel_execution=False): """ Add a method named ``name`` that is implemented by ``func``. Args: name (str): The name of of the new method. func: A callable object or a single argument that implements the desired behavior of the new method. request_type (types.TypeType): A type object indicating the type of request data passed to the method. reply_type: A type object indicating the type of reply data of the method. allow_parallel_execution(bool): if set to True, the method will be called fully asynchronously and even multiple calls may enter the method in parallel. Also, no ordering is guaranteed anymore. Returns: LocalMethod: The newly created method. """ scope = self.scope.concat(rsb.Scope('/' + name)) method = rsb.create_participant( LocalMethod, scope, self.config, parent=self, server=self, name=name, func=func, request_type=request_type, reply_type=reply_type, allow_parallel_execution=allow_parallel_execution) super().add_method(method) return method
def testSuperScopes(self): self.assertEqual(0, len(rsb.Scope("/").superScopes())) supers = rsb.Scope("/this/is/a/test/").superScopes() self.assertEqual(4, len(supers)) self.assertEqual(rsb.Scope("/"), supers[0]) self.assertEqual(rsb.Scope("/this/"), supers[1]) self.assertEqual(rsb.Scope("/this/is/"), supers[2]) self.assertEqual(rsb.Scope("/this/is/a/"), supers[3]) supers = rsb.Scope("/").superScopes(True) self.assertEqual(1, len(supers)) self.assertEqual(rsb.Scope("/"), supers[0]) supers = rsb.Scope("/this/is/a/test/").superScopes(True) self.assertEqual(5, len(supers)) self.assertEqual(rsb.Scope("/"), supers[0]) self.assertEqual(rsb.Scope("/this/"), supers[1]) self.assertEqual(rsb.Scope("/this/is/"), supers[2]) self.assertEqual(rsb.Scope("/this/is/a/"), supers[3]) self.assertEqual(rsb.Scope("/this/is/a/test/"), supers[4])
def testHierarchyComparison(self): self.assertTrue(rsb.Scope("/a/").isSubScopeOf(rsb.Scope("/"))) self.assertTrue(rsb.Scope("/a/b/c/").isSubScopeOf(rsb.Scope("/"))) self.assertTrue(rsb.Scope("/a/b/c/").isSubScopeOf(rsb.Scope("/a/b/"))) self.assertFalse( rsb.Scope("/a/b/c/").isSubScopeOf(rsb.Scope("/a/b/c/"))) self.assertFalse( rsb.Scope("/a/b/c/").isSubScopeOf(rsb.Scope("/a/b/c/d/"))) self.assertFalse(rsb.Scope("/a/x/c/").isSubScopeOf(rsb.Scope("/a/b/"))) self.assertTrue(rsb.Scope("/").isSuperScopeOf(rsb.Scope("/a/"))) self.assertTrue(rsb.Scope("/").isSuperScopeOf(rsb.Scope("/a/b/c/"))) self.assertTrue( rsb.Scope("/a/b/").isSuperScopeOf(rsb.Scope("/a/b/c/"))) self.assertFalse( rsb.Scope("/a/b/c/").isSuperScopeOf(rsb.Scope("/a/b/c/"))) self.assertFalse( rsb.Scope("/a/b/c/d/").isSuperScopeOf(rsb.Scope("/a/b/c/"))) self.assertFalse(rsb.Scope("/b/").isSuperScopeOf(rsb.Scope("/a/b/c/")))
def testCompareOtherTypeNoCrash(self): self.assertFalse(rsb.Scope("/foo") == "test") self.assertFalse("test" == rsb.Scope("/foo"))
def testComparison(self): self.assertTrue(rsb.Scope("/") == rsb.Scope("/")) self.assertFalse(rsb.Scope("/") != rsb.Scope("/")) self.assertFalse(rsb.Scope("/") == rsb.Scope("/foo/")) self.assertTrue(rsb.Scope("/") != rsb.Scope("/foo/")) self.assertTrue(rsb.Scope("/a/") < rsb.Scope("/c/")) self.assertTrue(rsb.Scope("/a/") <= rsb.Scope("/c/")) self.assertTrue(rsb.Scope("/a/") <= rsb.Scope("/a")) self.assertFalse(rsb.Scope("/a/") > rsb.Scope("/c/")) self.assertTrue(rsb.Scope("/c/") > rsb.Scope("/a/")) self.assertTrue(rsb.Scope("/c/") >= rsb.Scope("/a/")) self.assertTrue(rsb.Scope("/c/") >= rsb.Scope("/c/"))
def testConcat(self): self.assertEqual(rsb.Scope("/"), rsb.Scope("/").concat(rsb.Scope("/"))) self.assertEqual(rsb.Scope("/a/test/"), rsb.Scope("/").concat(rsb.Scope("/a/test/"))) self.assertEqual(rsb.Scope("/a/test/"), rsb.Scope("/a/test/").concat(rsb.Scope("/"))) self.assertEqual(rsb.Scope("/a/test/example"), rsb.Scope("/a/test/").concat(rsb.Scope("/example/")))
def testToString(self): self.assertEqual("/", rsb.Scope("/").toString()) self.assertEqual("/foo/", rsb.Scope("/foo/").toString()) self.assertEqual("/foo/bar/", rsb.Scope("/foo/bar/").toString()) self.assertEqual("/foo/bar/", rsb.Scope("/foo/bar").toString())
def process_scope(host_id, process_id, base_scope=HOSTS_SCOPE): return (base_scope.concat(rsb.Scope('/' + host_id)).concat( rsb.Scope('/' + process_id)))
import rsb import rsb.converter # See ./registration.py. import sys sys.path.append('.') from SimpleImage_pb2 import SimpleImage if __name__ == '__main__': # Pacify logger. logging.basicConfig() # See ./registration.py converter = rsb.converter.ProtocolBufferConverter(messageClass=SimpleImage) rsb.converter.registerGlobalConverter(converter) rsb.setDefaultParticipantConfig(rsb.ParticipantConfig.fromDefaultSources()) # Create a listener that will receive the events carrying protocol # buffer payloads. See the listener.py example for a more detailed # explanation of listener creation. with rsb.createListener(rsb.Scope("/example/converter")) as listener: def printData(event): print("Received %s object with fields:\n%s" % (type(event.data).__name__, str(event.data))) listener.addHandler(printData) # wait endlessly for received events while True: time.sleep(100)
def test_compare_other_type_no_crash(self): assert not (rsb.Scope("/foo") == "test") assert not ("test" == rsb.Scope("/foo"))
def participantScope(participantId, baseScope=PARTICIPANTS_SCOPE): return baseScope.concat(rsb.Scope('/' + str(participantId)))
converter = rsb.converter.ProtocolBufferConverter(message_class=Image) rsb.converter.register_global_converter(converter) rsb.__default_participant_config = \ rsb.ParticipantConfig.from_default_sources() parser = optparse.OptionParser() parser.add_option( '--cookie', dest='cookie', type=int, default=0, help='A cookie for verification in \"ping\" method call.') options, args = parser.parse_args() cookie = options.cookie scope = rsb.Scope('/rsb-integration-test/request-reply') print('[Python Server] Providing service at scope %s' % scope) with rsb.create_local_server(scope) as local_server: def ping(request): print('[Python Server] "ping" method called ' 'with request {}'.format(request)) assert (request == cookie) return 'pong' local_server.add_method('ping', ping, int, str) def echo(x): print('[Python Server] "echo*" method called ' 'with argument {}'.format(x))
def test_hierarchy_comparison(self): assert rsb.Scope("/a/").is_sub_scope_of(rsb.Scope("/")) assert rsb.Scope("/a/b/c/").is_sub_scope_of(rsb.Scope("/")) assert rsb.Scope("/a/b/c/").is_sub_scope_of(rsb.Scope("/a/b/")) assert not rsb.Scope("/a/b/c/").is_sub_scope_of(rsb.Scope("/a/b/c/")) assert not rsb.Scope("/a/b/c/").is_sub_scope_of(rsb.Scope("/a/b/c/d/")) assert not rsb.Scope("/a/x/c/").is_sub_scope_of(rsb.Scope("/a/b/")) assert rsb.Scope("/").is_super_scope_of(rsb.Scope("/a/")) assert rsb.Scope("/").is_super_scope_of(rsb.Scope("/a/b/c/")) assert rsb.Scope("/a/b/").is_super_scope_of(rsb.Scope("/a/b/c/")) assert not rsb.Scope("/a/b/c/").is_super_scope_of(rsb.Scope("/a/b/c/")) assert not rsb.Scope("/a/b/c/d/").is_super_scope_of( rsb.Scope("/a/b/c/")) assert not rsb.Scope("/b/").is_super_scope_of(rsb.Scope("/a/b/c/"))
def setUp(self): self.scope = rsb.Scope('/one/test') self.receivedCondition = Condition() self.receivedData = None
softwareVersion = property(getSoftwareVersion) def __str__(self): return '<%s %s %s %s at 0x%0x>' \ % (type(self).__name__, self.hostname, self.machineType, self.softwareType, id(self)) def __repr__(self): return str(self) # IntrospectionSender BASE_SCOPE = rsb.Scope('/__rsb/introspection/') PARTICIPANTS_SCOPE = BASE_SCOPE.concat(rsb.Scope('/participants/')) HOSTS_SCOPE = BASE_SCOPE.concat(rsb.Scope('/hosts/')) def participantScope(participantId, baseScope=PARTICIPANTS_SCOPE): return baseScope.concat(rsb.Scope('/' + str(participantId))) def processScope(hostId, processId, baseScope=HOSTS_SCOPE): return (baseScope .concat(rsb.Scope('/' + hostId)) .concat(rsb.Scope('/' + processId))) class IntrospectionSender(object):
def set_up(self): self.scope = rsb.Scope('/one/test') self.received_condition = Condition() self.received_data = None
def processScope(hostId, processId, baseScope=HOSTS_SCOPE): return (baseScope .concat(rsb.Scope('/' + hostId)) .concat(rsb.Scope('/' + processId)))
self.counter += 1 if self.is_done(): self.condition.notify_all() def is_done(self): return self.counter == self.expectedCount listeners = [] receivers = [] try: for size in [4, 256, 400000]: # Create listeners for all scopes below the /sizeSIZE scope: # * /size-SIZE # * /size-SIZE/sub_1 # * /size-SIZE/sub_1/sub_2 scope = rsb.Scope("/size-%d/sub_1/sub_2" % size) scopes = scope.super_scopes(True) for superscope in scopes[1:]: listener = rsb.create_listener(superscope) listeners.append(listener) receiver = Receiver( scope, size, rsb.EventId( uuid.UUID('00000000-0000-0000-0000-000000000000'), 0), 120) receivers.append(receiver) listener.add_handler(receiver) open('ready', 'w').close() print("[Python Listener] Ready")