|
| 1 | +# |
| 2 | +# Copyright (c) 2024 ZettaScale Technology |
| 3 | +# |
| 4 | +# This program and the accompanying materials are made available under the |
| 5 | +# terms of the Eclipse Public License 2.0 which is available at |
| 6 | +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | +# which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +# |
| 9 | +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | +# |
| 11 | +# Contributors: |
| 12 | +# ZettaScale Zenoh Team, <[email protected]> |
| 13 | +# |
| 14 | +import itertools |
| 15 | +import time |
| 16 | +from typing import Optional, Tuple |
| 17 | + |
| 18 | +import zenoh |
| 19 | + |
| 20 | + |
| 21 | +def main( |
| 22 | + conf: zenoh.Config, |
| 23 | + selector: str, |
| 24 | + target: zenoh.QueryTarget, |
| 25 | + payload: str, |
| 26 | + timeout: float, |
| 27 | + iter: Optional[int], |
| 28 | +): |
| 29 | + # initiate logging |
| 30 | + zenoh.init_log_from_env_or("error") |
| 31 | + print("Opening session...") |
| 32 | + with zenoh.open(conf) as session: |
| 33 | + query_selector = zenoh.Selector(selector) |
| 34 | + |
| 35 | + print(f"Declaring Querier on '{query_selector.key_expr}'...") |
| 36 | + querier = session.declare_querier( |
| 37 | + query_selector.key_expr, target=target, timeout=timeout |
| 38 | + ) |
| 39 | + |
| 40 | + print("Press CTRL-C to quit...") |
| 41 | + for idx in itertools.count() if iter is None else range(iter): |
| 42 | + time.sleep(1.0) |
| 43 | + buf = f"[{idx:4d}] {payload if payload else ''}" |
| 44 | + print(f"Querying '{selector}' with payload '{buf}')...") |
| 45 | + |
| 46 | + replies = querier.get(parameters=query_selector.parameters, payload=buf) |
| 47 | + for reply in replies: |
| 48 | + try: |
| 49 | + print( |
| 50 | + f">> Received ('{reply.ok.key_expr}': '{reply.ok.payload.to_string()}')" |
| 51 | + ) |
| 52 | + except: |
| 53 | + print(f">> Received (ERROR: '{reply.err.payload.to_string()}')") |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + # --- Command line argument parsing --- --- --- --- --- --- |
| 58 | + import argparse |
| 59 | + import json |
| 60 | + |
| 61 | + parser = argparse.ArgumentParser( |
| 62 | + prog="z_querier", description="zenoh querier example" |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + "--mode", |
| 66 | + "-m", |
| 67 | + dest="mode", |
| 68 | + choices=["peer", "client"], |
| 69 | + type=str, |
| 70 | + help="The zenoh session mode.", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--connect", |
| 74 | + "-e", |
| 75 | + dest="connect", |
| 76 | + metavar="ENDPOINT", |
| 77 | + action="append", |
| 78 | + type=str, |
| 79 | + help="Endpoints to connect to.", |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + "--listen", |
| 83 | + "-l", |
| 84 | + dest="listen", |
| 85 | + metavar="ENDPOINT", |
| 86 | + action="append", |
| 87 | + type=str, |
| 88 | + help="Endpoints to listen on.", |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "--selector", |
| 92 | + "-s", |
| 93 | + dest="selector", |
| 94 | + default="demo/example/**", |
| 95 | + type=str, |
| 96 | + help="The selection of resources to query.", |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + "--target", |
| 100 | + "-t", |
| 101 | + dest="target", |
| 102 | + choices=["ALL", "BEST_MATCHING", "ALL_COMPLETE", "NONE"], |
| 103 | + default="BEST_MATCHING", |
| 104 | + type=str, |
| 105 | + help="The target queryables of the query.", |
| 106 | + ) |
| 107 | + parser.add_argument( |
| 108 | + "--payload", |
| 109 | + "-p", |
| 110 | + dest="payload", |
| 111 | + type=str, |
| 112 | + help="An optional payload to send in the query.", |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + "--timeout", |
| 116 | + "-o", |
| 117 | + dest="timeout", |
| 118 | + default=10.0, |
| 119 | + type=float, |
| 120 | + help="The query timeout", |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "--config", |
| 124 | + "-c", |
| 125 | + dest="config", |
| 126 | + metavar="FILE", |
| 127 | + type=str, |
| 128 | + help="A configuration file.", |
| 129 | + ) |
| 130 | + parser.add_argument( |
| 131 | + "--iter", dest="iter", type=int, help="How many gets to perform" |
| 132 | + ) |
| 133 | + |
| 134 | + args = parser.parse_args() |
| 135 | + conf = ( |
| 136 | + zenoh.Config.from_file(args.config) |
| 137 | + if args.config is not None |
| 138 | + else zenoh.Config() |
| 139 | + ) |
| 140 | + if args.mode is not None: |
| 141 | + conf.insert_json5("mode", json.dumps(args.mode)) |
| 142 | + if args.connect is not None: |
| 143 | + conf.insert_json5("connect/endpoints", json.dumps(args.connect)) |
| 144 | + if args.listen is not None: |
| 145 | + conf.insert_json5("listen/endpoints", json.dumps(args.listen)) |
| 146 | + target = { |
| 147 | + "ALL": zenoh.QueryTarget.ALL, |
| 148 | + "BEST_MATCHING": zenoh.QueryTarget.BEST_MATCHING, |
| 149 | + "ALL_COMPLETE": zenoh.QueryTarget.ALL_COMPLETE, |
| 150 | + }.get(args.target) |
| 151 | + |
| 152 | + main(conf, args.selector, target, args.payload, args.timeout, args.iter) |
0 commit comments