import time from zenoh.net import Session, SubscriberMode def listener(rname, data, info): print(">> [Subscription listener] Received ('{}': '{}') at {}" .format(rname, data.decode("utf-8"), info.tstamp)) if __name__ == '__main__': uri = "/demo/example/**" if len(sys.argv) > 1: uri = sys.argv[1] locator = None if len(sys.argv) > 2: locator = sys.argv[2] print("Openning session...") s = Session.open(locator) print("Declaring Subscriber on '{}'...".format(uri)) sub = s.declare_subscriber(uri, SubscriberMode.push(), listener) c = '\0' while c != 'q': c = sys.stdin.read(1) s.undeclare_subscriber(sub) s.close()
def client_test(self): global storage_replies global eval_replies locator = "tcp/127.0.0.1:7447" z1 = Session.open(locator) z1_peer = z1.info()[zenoh.net.ZN_INFO_PEER_KEY].decode().rstrip('\0') self.assertEqual(locator, z1_peer) z1_sub1 = z1.declare_subscriber("/test/python/client/**", SubscriberMode.push(), z1_sub1_listener) z1_sto1 = z1.declare_storage("/test/python/client/**", z1_sto1_listener, z1_sto1_handler) z1_eval1 = z1.declare_eval("/test/python/client/z1_eval1", z1_eval1_handler) z1_pub1 = z1.declare_publisher("/test/python/client/z1_pub1") z2 = Session.open(locator) z2_peer = z2.info()[zenoh.net.ZN_INFO_PEER_KEY].decode().rstrip('\0') self.assertEqual(locator, z2_peer) z2_sub1 = z2.declare_subscriber("/test/python/client/**", SubscriberMode.push(), z2_sub1_listener) z2_sto1 = z2.declare_storage("/test/python/client/**", z2_sto1_listener, z2_sto1_handler) z2_eval1 = z2.declare_eval("/test/python/client/z2_eval1", z2_eval1_handler) z2_pub1 = z2.declare_publisher("/test/python/client/z2_pub1") z3 = Session.open(locator) z3_peer = z3.info()[zenoh.net.ZN_INFO_PEER_KEY].decode().rstrip('\0') self.assertEqual(locator, z3_peer) z3_sub1 = z3.declare_subscriber("/test/python/client/**", SubscriberMode.pull(), z3_sub1_listener) time.sleep(1) self.assertTrue(z1.running) self.assertTrue(z2.running) self.assertTrue(z3.running) sent_res = ("/test/python/client/z1_wr1", "z1_wr1_spl1".encode()) z1.write_data(sent_res[0], sent_res[1]) rcvd_res = z1_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z1_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.pull(z3_sub1) rcvd_res = z3_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z1.query("/test/python/client/**", "", reply_handler, dest_storages=zenoh.net.QueryDest( zenoh.net.QueryDest.ZN_BEST_MATCH), dest_evals=zenoh.net.QueryDest(zenoh.net.QueryDest.ZN_NONE)) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] # self.assertEqual(0, len(eval_replies)) # This may not be true for now as : # - zenoh-c does not check received query properties # - zenohd does not filter out replies eval_replies = [] z1.query("/test/python/client/**", "", reply_handler, dest_storages=QueryDest(QueryDest.ZN_NONE), dest_evals=QueryDest(QueryDest.ZN_BEST_MATCH)) replies_mvar.get() # self.assertEqual(0, len(storage_replies)) # This may not be true for now as : # - zenoh-c does not check received query properties # - zenohd does not filter out replies storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z2.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z2.query("/test/python/client/**", "", reply_handler, dest_storages=QueryDest(QueryDest.ZN_BEST_MATCH), dest_evals=QueryDest(QueryDest.ZN_NONE)) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] # self.assertEqual(0, len(eval_replies)) # This may not be true for now as : # - zenoh-c does not check received query properties # - zenohd does not filter out replies eval_replies = [] z2.query("/test/python/client/**", "", reply_handler, dest_storages=QueryDest(QueryDest.ZN_NONE), dest_evals=QueryDest(QueryDest.ZN_BEST_MATCH)) replies_mvar.get() # self.assertEqual(0, len(storage_replies)) # This may not be true for now as : # - zenoh-c does not check received query properties # - zenohd does not filter out replies storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] sent_res = ("/test/python/client/**", "z2_wr1_spl1".encode()) z2.write_data(sent_res[0], sent_res[1]) rcvd_res = z1_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z1_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.pull(z3_sub1) rcvd_res = z3_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z2.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] sent_res = ("/test/python/client/z1_pub1", "z1_pub1_spl1".encode()) z1.stream_data(z1_pub1, sent_res[1]) rcvd_res = z1_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z1_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.pull(z3_sub1) rcvd_res = z3_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z2.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] sent_res = ("/test/python/client/z2_pub1", "z2_pub1_spl1".encode()) z2.stream_data(z2_pub1, sent_res[1]) rcvd_res = z1_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z1_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) rcvd_res = z2_sto1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.pull(z3_sub1) rcvd_res = z3_sub1_mvar.get() self.assertEqual(sent_res, rcvd_res) z1.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z2.query("/test/python/client/**", "", reply_handler) replies_mvar.get() self.assertEqual(2, len(storage_replies)) self.assertEqual(sent_res, storage_replies[0]) self.assertEqual(sent_res, storage_replies[1]) storage_replies = [] self.assertEqual(2, len(eval_replies)) eval_replies = [] z1.undeclare_subscriber(z1_sub1) z2.undeclare_subscriber(z2_sub1) z3.undeclare_subscriber(z3_sub1) z1.undeclare_storage(z1_sto1) z2.undeclare_storage(z2_sto1) z1.undeclare_eval(z1_eval1) z2.undeclare_eval(z2_eval1) z1.undeclare_publisher(z1_pub1) z2.undeclare_publisher(z2_pub1) z1.close() z2.close() z3.close() time.sleep(1) # let time for close msg to comme back from router self.assertFalse(z1.running) self.assertFalse(z2.running) self.assertFalse(z3.running)
def print_stats(start, stop): print("{:.6f} msgs/sec".format(N / (stop - start).total_seconds())) def listener(rname, data, info): global count, start, stop if count == 0: start = datetime.datetime.now() count += 1 elif count < N: count += 1 else: stop = datetime.datetime.now() print_stats(start, stop) count = 0 if __name__ == '__main__': locator = None if len(sys.argv) > 1: locator = sys.argv[1] s = Session.open(locator) sub = s.declare_subscriber('/test/thr', SubscriberMode.push(), listener) time.sleep(60) s.undeclare_subscriber(sub) s.close()
type=str, help='The locator to be used to boostrap the zenoh session.' ' By default dynamic discovery is used') args = parser.parse_args() locator = args.locator selector = args.selector # zenoh code --- --- --- --- --- --- --- --- --- --- --- def listener(rname, data, info): print(">> [Subscription listener] Received ('{}': '{}') at {}" .format(rname, data.decode("utf-8"), info.tstamp)) print("Openning session...") s = Session.open(locator) print("Declaring Subscriber on '{}'...".format(selector)) sub = s.declare_subscriber(selector, SubscriberMode.push(), listener) print('Press "q" at any time to terminate...') c = '\0' while c != 'q': c = sys.stdin.read(1) s.undeclare_subscriber(sub) s.close()
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 # which is available at https://www.apache.org/licenses/LICENSE-2.0. # # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 # # Contributors: # ADLINK zenoh team, <*****@*****.**> from zenoh.net import Session, SubscriberMode import time import sys def listener(rname, data, info): print("{}: {}".format(rname, data.decode())) if __name__ == "__main__": locator = None if len(sys.argv) > 1: locator = sys.argv[1] s = Session.open(locator) sub = s.declare_subscriber('/myhome/kitcken/temp', SubscriberMode.push(), listener) # Listen for one minute and then exit time.sleep(120) s.undeclare_subscriber(sub) s.close()