def test_user_roundtrip(self): scope = Scope("/test/it") in_connector = self._get_in_connector(scope, activate=False) out_connector = self._get_out_connector(scope, activate=False) in_configurator = rsb.eventprocessing.InRouteConfigurator( connectors=[in_connector]) out_configurator = rsb.eventprocessing.OutRouteConfigurator( connectors=[out_connector]) listener = create_listener(scope, configurator=in_configurator) publisher = create_informer(scope, data_type=str, configurator=out_configurator) receiver = SettingReceiver(scope) listener.add_handler(receiver) data1 = "a string to test" sent_event = Event(EventId(uuid.uuid4(), 0)) sent_event.data = data1 sent_event.data_type = str sent_event.scope = scope sent_event.meta_data.set_user_info("test", "it") sent_event.meta_data.set_user_info("test again", "it works?") sent_event.meta_data.set_user_time("blubb", 234234.0) sent_event.meta_data.set_user_time("bla", 3434343.45) sent_event.add_cause(EventId(uuid.uuid4(), 1323)) sent_event.add_cause(EventId(uuid.uuid4(), 42)) publisher.publish_event(sent_event) with receiver.result_condition: while receiver.result_event is None: receiver.result_condition.wait(10) if receiver.result_event is None: self.fail("Listener did not receive an event") assert receiver.result_event.meta_data.create_time <= \ receiver.result_event.meta_data.send_time assert receiver.result_event.meta_data.send_time <= \ receiver.result_event.meta_data.receive_time assert receiver.result_event.meta_data.receive_time <= \ receiver.result_event.meta_data.deliver_time sent_event.meta_data.receive_time = \ receiver.result_event.meta_data.receive_time sent_event.meta_data.deliver_time = \ receiver.result_event.meta_data.deliver_time # HACK: floating point precision leads to an imprecision here, # avoid this. sent_event.meta_data.send_time = \ receiver.result_event.meta_data.send_time sent_event.meta_data.create_time = \ receiver.result_event.meta_data.create_time assert sent_event == receiver.result_event publisher.deactivate() listener.deactivate()
def test_hierarchy_sending(self): send_scope = Scope("/this/is/a/test") super_scopes = send_scope.super_scopes(True) out_connector = self._get_out_connector(send_scope, activate=False) out_configurator = rsb.eventprocessing.OutRouteConfigurator( connectors=[out_connector]) informer = create_informer(send_scope, data_type=str, configurator=out_configurator) # set up listeners on the complete hierarchy listeners = [] receivers = [] for scope in super_scopes: in_connector = self._get_in_connector(scope, activate=False) in_configurator = rsb.eventprocessing.InRouteConfigurator( connectors=[in_connector]) listener = create_listener(scope, configurator=in_configurator) listeners.append(listener) receiver = SettingReceiver(scope) listener.add_handler(receiver) receivers.append(receiver) data = "a string to test" informer.publish_data(data) for receiver in receivers: with receiver.result_condition: while receiver.result_event is None: receiver.result_condition.wait(10) if receiver.result_event is None: pytest.fail( "Listener on scope {} did not receive an event".format( receiver.scope)) assert receiver.result_event.data == data for listener in listeners: listener.deactivate() informer.deactivate()
def test_informer_listener_roundtrip(self): with rsb.create_informer(self.scope, data_type=str) as informer, \ rsb.create_listener(self.scope) as listener: def set_received(event): with self.received_condition: self.received_data = event.data self.received_condition.notifyAll() listener.add_handler(set_received) data = 'our little test' informer.publish_data(data) start = time.time() with self.received_condition: while self.received_data is None: self.received_condition.wait(1) if time.time() > start + 10: break assert data == self.received_data
# GNU Lesser General Public License Version 3 (the ``LGPL''), # or (at your option) any later version. # # Software distributed under the License is distributed # on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either # express or implied. See the LGPL for the specific language # governing rights and limitations. # # You should have received a copy of the LGPL along with this # program. If not, go to http://www.gnu.org/licenses/lgpl.html # or write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # ============================================================ # mark-start::body import logging import rsb if __name__ == '__main__': # Pacify logger. logging.basicConfig() # Create an informer for strings on scope "/example/informer". with rsb.create_informer("/example/informer", data_type=str) as informer: # Send and event using a method that directly accepts data. informer.publish_data("example payload") # mark-end::body
def __init__(self): self._logger = get_logger_by_class(self.__class__) self._participants = [] self._process = ProcessInfo() self._host = HostInfo() self._informer = rsb.create_informer(PARTICIPANTS_SCOPE) self._listener = rsb.create_listener(PARTICIPANTS_SCOPE) def handle(event): # TODO use filter when we get conjunction filter if event.method not in ['REQUEST', 'SURVEY']: return participant_id = None participant = None if len(event.scope.components) > \ len(PARTICIPANTS_SCOPE.components): try: participant_id = uuid.UUID(event.scope.components[-1]) if participant_id is not None: participant = next( (p for p in self._participants if p.participant_id == participant_id), None) except Exception: self._logger.warn( 'Query event %s does not ' 'properly address a participant', event, exc_info=True) def process(thunk): if participant is not None and event.method == 'REQUEST': thunk(query=event, participant=participant) elif participant is None and event.method == 'SURVEY': for p in self._participants: thunk(query=event, participant=p) else: self._logger.warn('Query event %s not understood', event) if event.data is None: process(self.send_hello) elif event.data == 'ping': process(self.send_pong) else: self._logger.warn('Query event %s not understood', event) self._listener.add_handler(handle) self._server = rsb.create_local_server( process_scope(self._host.host_id or self._host.hostname, str(self._process.process_id))) def echo(request): reply = rsb.Event(scope=request.scope, data=request.data, data_type=type(request.data)) reply.meta_data.set_user_time('request.send', request.meta_data.send_time) reply.meta_data.set_user_time('request.receive', request.meta_data.receive_time) return reply self._server.add_method('echo', echo, request_type=rsb.Event, reply_type=rsb.Event)
def test_informer(self): participant = None with rsb.create_informer('/') as informer: participant = informer assert self.creation_calls == [(participant, None)] assert self.destruction_calls == [participant]
def test_create_informer(self): assert rsb.create_informer("/") is not None
def make_informer(self): return rsb.create_informer(self.scope, self.config, parent=self, data_type=self.request_type)
import time import rsb import uuid if __name__ == '__main__': logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s\n%(message)s', stream=sys.stderr) listener_pid = int(sys.argv[2]) for size in [4, 256, 400000]: scope = "/size-%d/sub_1/sub_2" % size print("[Python Informer] Processing scope %s" % scope) informer = rsb.create_informer(scope, data_type=str) for i in range(120): event = rsb.Event( scope=scope, data='c' * size, data_type=str, user_infos={ "informer-lang": "Python", "index": str(listener_pid + i) }, user_times={"informer-start": time.time()}, causes=set([ rsb.EventId( uuid.UUID('00000000-0000-0000-0000-000000000000'), 0) ]))
import rsb.converter # See ./registration.py. sys.path.append('.') from SimpleImage_pb2 import SimpleImage # noqa: I100 path required before if __name__ == '__main__': # Pacify logger. logging.basicConfig() # See ./registration.py. converter = rsb.converter.ProtocolBufferConverter( message_class=SimpleImage) rsb.converter.register_global_converter(converter) rsb.set_default_participant_config( rsb.ParticipantConfig.from_default_sources()) # Create an informer that will send the events carrying protocol # buffer payloads. See the informer.py example for a more detailed # explanation of informer creation. with rsb.create_informer("/example/converter", data_type=SimpleImage) as informer: image = SimpleImage() image.width = 100 image.height = 100 image.data = str('bla') informer.publish_data(image) # mark-end::body