Skip to content

Commit

Permalink
link to DEFAULT_CONFIG.json5, add main function to ensure scoping wor…
Browse files Browse the repository at this point in the history
…ks (#135)
  • Loading branch information
p-avital authored Jan 11, 2024
1 parent d832d94 commit 48a775b
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 205 deletions.
16 changes: 9 additions & 7 deletions examples/z_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@
key = args.key

# Zenoh code --- --- --- --- --- --- --- --- --- --- ---
def main():
# initiate logging
zenoh.init_logger()

# initiate logging
zenoh.init_logger()
print("Opening session...")
session = zenoh.open(conf)

print("Opening session...")
session = zenoh.open(conf)
print("Deleting resources matching '{}'...".format(key))
session.delete(key)

print("Deleting resources matching '{}'...".format(key))
session.delete(key)
session.close()

session.close()
main()
30 changes: 16 additions & 14 deletions examples/z_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,24 @@
}.get(args.target)

# Zenoh code --- --- --- --- --- --- --- --- --- --- ---
def main():
# initiate logging
zenoh.init_logger()

# initiate logging
zenoh.init_logger()
print("Opening session...")
session = zenoh.open(conf)

print("Opening session...")
session = zenoh.open(conf)
print("Sending Query '{}'...".format(selector))
replies = session.get(selector, zenoh.Queue(), target=target, value=args.value, consolidation=zenoh.QueryConsolidation.NONE())
for reply in replies.receiver:
try:
print(">> Received ('{}': '{}')"
.format(reply.ok.key_expr, reply.ok.payload.decode("utf-8")))
except:
print(">> Received (ERROR: '{}')"
.format(reply.err.payload.decode("utf-8")))

print("Sending Query '{}'...".format(selector))
replies = session.get(selector, zenoh.Queue(), target=target, value=args.value, consolidation=zenoh.QueryConsolidation.NONE())
for reply in replies.receiver:
try:
print(">> Received ('{}': '{}')"
.format(reply.ok.key_expr, reply.ok.payload.decode("utf-8")))
except:
print(">> Received (ERROR: '{}')"
.format(reply.err.payload.decode("utf-8")))

session.close()

session.close()
main()
20 changes: 11 additions & 9 deletions examples/z_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
# Zenoh code --- --- --- --- --- --- --- --- --- --- ---
def main():
# initiate logging
zenoh.init_logger()

# initiate logging
zenoh.init_logger()
print("Opening session...")
session = zenoh.open(conf)

print("Opening session...")
session = zenoh.open(conf)
info = session.info()
print(f"zid: {info.zid()}")
print(f"routers: {info.routers_zid()}")
print(f"peers: {info.peers_zid()}")
session.close()

info = session.info()
print(f"zid: {info.zid()}")
print(f"routers: {info.routers_zid()}")
print(f"peers: {info.peers_zid()}")
session.close()
main()
29 changes: 16 additions & 13 deletions examples/z_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@
key = args.key
value = args.value

# initiate logging
zenoh.init_logger()
def main():
# initiate logging
zenoh.init_logger()

print("Opening session...")
session = zenoh.open(conf)
print("Opening session...")
session = zenoh.open(conf)

print(f"Declaring Publisher on '{key}'...")
pub = session.declare_publisher(key)
print(f"Declaring Publisher on '{key}'...")
pub = session.declare_publisher(key)

for idx in itertools.count() if args.iter is None else range(args.iter):
time.sleep(1)
buf = f"[{idx:4d}] {value}"
print(f"Putting Data ('{key}': '{buf}')...")
pub.put(buf)
for idx in itertools.count() if args.iter is None else range(args.iter):
time.sleep(1)
buf = f"[{idx:4d}] {value}"
print(f"Putting Data ('{key}': '{buf}')...")
pub.put(buf)

pub.undeclare()
session.close()
pub.undeclare()
session.close()

main()
24 changes: 13 additions & 11 deletions examples/z_pub_thr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@
size = args.payload_size

# Zenoh code --- --- --- --- --- --- --- --- --- --- ---
def main():
# initiate logging
zenoh.init_logger()

# initiate logging
zenoh.init_logger()
data = bytearray()
for i in range(0, size):
data.append(i % 10)
data = Value(bytes(data))
congestion_control = CongestionControl.BLOCK()

data = bytearray()
for i in range(0, size):
data.append(i % 10)
data = Value(bytes(data))
congestion_control = CongestionControl.BLOCK()
session = zenoh.open(conf)
pub = session.declare_publisher('test/thr', congestion_control=congestion_control)

session = zenoh.open(conf)
pub = session.declare_publisher('test/thr', congestion_control=congestion_control)
while True:
pub.put(data)

while True:
pub.put(data)
main()
35 changes: 19 additions & 16 deletions examples/z_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,27 @@ def listen(sample):
print(f">> [Subscriber] Received {sample.kind} ('{sample.key_expr}': '{sample.payload.decode('utf-8')}')")


# initiate logging
zenoh.init_logger()
def main():
# initiate logging
zenoh.init_logger()

print("Opening session...")
session = zenoh.open(conf)
print("Opening session...")
session = zenoh.open(conf)

print("Declaring Subscriber on '{}'...".format(key))
print("Declaring Subscriber on '{}'...".format(key))

sub = session.declare_pull_subscriber(key, listen, reliability=Reliability.RELIABLE())
sub = session.declare_pull_subscriber(key, listen, reliability=Reliability.RELIABLE())

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

sub.undeclare()
session.close()
sub.undeclare()
session.close()

main()
58 changes: 30 additions & 28 deletions examples/z_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,45 @@
value = args.value

# Zenoh code --- --- --- --- --- --- --- --- --- --- ---
def main():
# initiate logging
zenoh.init_logger()

# initiate logging
zenoh.init_logger()
print("Opening session...")
session = zenoh.open(conf)

print("Opening session...")
session = zenoh.open(conf)
print("Putting Data ('{}': '{}')...".format(key, value))
session.put(key, value)

print("Putting Data ('{}': '{}')...".format(key, value))
session.put(key, value)
# --- Examples of put with other types:

# --- Examples of put with other types:
# - Integer
# session.put('/demo/example/Integer', 3)

# - Integer
# session.put('/demo/example/Integer', 3)
# - Float
# session.put('/demo/example/Float', 3.14)

# - Float
# session.put('/demo/example/Float', 3.14)
# - Properties (as a Dictionary with str only)
# session.put('/demo/example/Properties', {'p1': 'v1', 'p2': 'v2'})

# - Properties (as a Dictionary with str only)
# session.put('/demo/example/Properties', {'p1': 'v1', 'p2': 'v2'})
# - Json (str format)
# session.put('/demo/example/Json',
# json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]),
# encoding=Encoding.TEXT_JSON)

# - Json (str format)
# session.put('/demo/example/Json',
# json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]),
# encoding=Encoding.TEXT_JSON)
# - Raw ('application/octet-stream' encoding by default)
# session.put('/demo/example/Raw', b'\x48\x69\x21')

# - Raw ('application/octet-stream' encoding by default)
# session.put('/demo/example/Raw', b'\x48\x69\x21')
# - Custom encoding
# session.put('/demo/example/Custom',
# b'\x48\x69\x21',
# encoding='my_encoding')

# - Custom encoding
# session.put('/demo/example/Custom',
# b'\x48\x69\x21',
# encoding='my_encoding')
# - UTF-16 String specifying the charset as Encoding suffix
# session.put('/demo/example/UTF-16',
# 'hello'.encode('utf-16'),
# encoding=Encoding.TEXT_PLAIN.with_suffix(';charset=utf-16'))

# - UTF-16 String specifying the charset as Encoding suffix
# session.put('/demo/example/UTF-16',
# 'hello'.encode('utf-16'),
# encoding=Encoding.TEXT_PLAIN.with_suffix(';charset=utf-16'))
session.close()

session.close()
main()
35 changes: 19 additions & 16 deletions examples/z_queryable.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,26 @@ def queryable_callback(query):
query.reply(Sample(key, value))


# initiate logging
zenoh.init_logger()
def main():
# initiate logging
zenoh.init_logger()

print("Opening session...")
session = zenoh.open(conf)
print("Opening session...")
session = zenoh.open(conf)

print("Declaring Queryable on '{}'...".format(key))
queryable = session.declare_queryable(key, queryable_callback, complete)
print("Declaring Queryable on '{}'...".format(key))
queryable = session.declare_queryable(key, queryable_callback, complete)

print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c != 'q':
print("getting")
session.get(key, print, consolidation=zenoh.QueryConsolidation.NONE())
time.sleep(1)
print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c != 'q':
print("getting")
session.get(key, print, consolidation=zenoh.QueryConsolidation.NONE())
time.sleep(1)

queryable.undeclare()
session.close()
queryable.undeclare()
session.close()

main()
15 changes: 9 additions & 6 deletions examples/z_scout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

import zenoh

# initiate logging
zenoh.init_logger()
def main():
# initiate logging
zenoh.init_logger()

print("Scouting...")
scout = zenoh.scout(what = "peer|router", timeout=1.0)
print("Scouting...")
scout = zenoh.scout(what = "peer|router", timeout=1.0)

for hello in scout.receiver():
print(hello)
for hello in scout.receiver():
print(hello)

main()
39 changes: 21 additions & 18 deletions examples/z_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,29 @@ def query_handler(query: Query):
query.reply(sample)


# initiate logging
zenoh.init_logger()
def main():
# initiate logging
zenoh.init_logger()

print("Opening session...")
session = zenoh.open(conf)
print("Opening session...")
session = zenoh.open(conf)

print("Declaring Subscriber on '{}'...".format(key))
sub = session.declare_subscriber(
key, listener, reliability=Reliability.RELIABLE())
print("Declaring Subscriber on '{}'...".format(key))
sub = session.declare_subscriber(
key, listener, reliability=Reliability.RELIABLE())

print("Declaring Queryable on '{}'...".format(key))
queryable = session.declare_queryable(key, query_handler, complete)
print("Declaring Queryable on '{}'...".format(key))
queryable = session.declare_queryable(key, query_handler, complete)

print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)
print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)

sub.undeclare()
queryable.undeclare()
session.close()
sub.undeclare()
queryable.undeclare()
session.close()

main()
Loading

0 comments on commit 48a775b

Please sign in to comment.