Skip to content

Commit

Permalink
Adding Querier Example
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Schleich committed Dec 9, 2024
1 parent 9b883eb commit bd64a24
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
61 changes: 61 additions & 0 deletions zenoh-ts/examples/src/z_querier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// 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:
// ZettaScale Zenoh Team, <[email protected]>
//

import { Duration, deserialize_string, ReplyError, Config, Receiver, RecvErr, Reply, Sample, Session, QueryTarget } from "@eclipse-zenoh/zenoh-ts";


export async function main() {
const session = await Session.open(new Config("ws/127.0.0.1:10000"));


let querier = session.declare_querier("demo/example/**",
{
target: QueryTarget.BestMatching,
timeout: Duration.milliseconds.of(10000),
}
);

for(let i =0; i<1000; i++) {
sleep(1000)
let payload = "["+i+"] Querier Get from Zenoh-ts!";
let receiver = querier.get({payload:payload}) as Receiver;

let reply = await receiver.receive();

while (reply != RecvErr.Disconnected) {
if (reply == RecvErr.MalformedReply) {
console.warn("MalformedReply");
} else {
let resp = reply.result();
if (resp instanceof Sample) {
let sample: Sample = resp;
console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().deserialize(deserialize_string), "')");
} else {
let reply_error: ReplyError = resp;
console.warn(">> Received (ERROR: '{", reply_error.payload().deserialize(deserialize_string), "}')");
}
}
reply = await receiver.receive();
}
console.warn("Get Finished");

}

}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

main()
5 changes: 4 additions & 1 deletion zenoh-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import { Session, RecvErr, Receiver, DeleteOptions, PutOptions, GetOptions, Quer
import { Config } from "./config.js";
import { Encoding, IntoEncoding } from "./encoding.js";
import { Liveliness, LivelinessToken } from "./liveliness.js";
import { Querier, QueryTarget, Locality, ReplyKeyExpr, QuerierOptions, QuerierGetOptions } from './querier.js'

// Re-export duration external library
import { TimeDuration as Duration } from 'typed-duration'
import { Duration } from 'typed-duration'

// Exports
export { KeyExpr, IntoKeyExpr };
Expand All @@ -36,3 +38,4 @@ export { Config };
export { Encoding, IntoEncoding };
export { Liveliness, LivelinessToken };
export { Duration };
export { Querier, QueryTarget, Locality, ReplyKeyExpr, QuerierOptions, QuerierGetOptions }
6 changes: 3 additions & 3 deletions zenoh-ts/src/querier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ export function reply_key_expr_to_int(query_target?: ReplyKeyExpr): number {

export interface QuerierOptions {
congestion_control?: CongestionControl,
consolidation: ConsolidationMode,
consolidation?: ConsolidationMode,
priority?: Priority,
express?: boolean,
target: QueryTarget
timeout?: TimeDuration,
allowed_destination: Locality
allowed_destination?: Locality
//
accept_replies?: ReplyKeyExpr
}
Expand Down Expand Up @@ -205,7 +205,7 @@ export class Querier {
* Issue a Get request on this querier
* @returns Promise <Receiever | void>
*/
get(get_options: QuerierGetOptions): Receiver | undefined {
get(get_options?: QuerierGetOptions): Receiver | undefined {
if (this.undeclared == true) {
return undefined;
}
Expand Down

0 comments on commit bd64a24

Please sign in to comment.