def login(locator, properties=None): ''' Establish a zenoh session via a provided locator. Locator is a string representing the network endpoint to which establish the session. If the provided locator is ``None``, login will perform some dynamic discovery and try to establish the session automatically. When not ``None``, the locator must have the format: ``tcp/<ip>:<port>`` (for instance ``tcp/127.0.0.1:7447``). :param locator: a Zenoh locator or ``None``. :param properties: the Properties to be used for this session (e.g. "user", "password", ...). Can be ``None``. :returns: a Zenoh object. ''' zprops = {} if properties is None else { zenoh.net.ZN_USER_KEY if k == "user" else zenoh.net.ZN_PASSWD_KEY: val for k, val in properties.items() if k == "user" or k == "password"} return Zenoh(Session.open(locator, zprops))
parser = argparse.ArgumentParser( prog='zn_info', description='Shows how to retrieve peers information') parser.add_argument( '--locator', '-l', dest='locator', default=None, 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 # zenoh-net code --- --- --- --- --- --- --- --- --- --- --- print("Openning session...") s = Session.open(locator, {ZN_USER_KEY: "user".encode(), ZN_PASSWD_KEY: "password".encode()}) info = s.info() peer = info[ZN_INFO_PEER_KEY] pid = info[ZN_INFO_PID_KEY] peer_pid = info[ZN_INFO_PEER_PID_KEY] print("LOCATOR : {}".format(peer.decode("utf-8"))) print("PID : {}".format(binascii.hexlify(pid).decode("ascii"))) print("PEER PID : {}".format(binascii.hexlify(peer_pid).decode("ascii"))) s.close()
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)