Beispiel #1
0
    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()
selector = args.selector
locator = args.locator


# zenoh-net code  --- --- --- --- --- --- --- --- --- --- ---
def listener(rname, data, info):
    print(">> [Subscription listener] Received ('{}': '{}')".format(
        rname, data.decode("utf-8")))


print("Openning session...")
s = Session.open(locator)

print("Declaring Subscriber on '{}'...".format(selector))
sub = s.declare_subscriber(selector, SubscriberMode.pull(), listener)

print("Press <enter> to pull data...")
c = '\0'
while c != 'q':
    c = sys.stdin.read(1)
    s.pull(sub)

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)
Beispiel #3
0

def listener(rname, data, info):
    print(">> [Subscription listener] Received ('{}': '{}')".format(
        rname, data.decode("utf-8")))


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.pull(), listener)

    print("Press <enter> to pull data...")
    c = '\0'
    while c != 'q':
        c = sys.stdin.read(1)
        s.pull(sub)

    s.undeclare_subscriber(sub)
    s.close()