Zenoh-net API Reference¶
The network level zenoh API.
Examples:¶
Publish¶
>>> import zenoh
>>> s = zenoh.net.open({})
>>> s.write('/resource/name', bytes('value', encoding='utf8'))
Subscribe¶
>>> import zenoh
>>> from zenoh.net import SubInfo, Reliability, SubMode
>>> def listener(sample):
... print("Received : {}".format(sample))
>>>
>>> s = zenoh.net.open({})
>>> sub_info = SubInfo(Reliability.Reliable, SubMode.Push)
>>> sub = s.declare_subscriber('/resource/name', sub_info, listener)
Query¶
>>> import zenoh, time
>>> from zenoh.net import QueryTarget, queryable
>>> def query_callback(reply):
... print("Received : {}".format(reply))
>>>
>>> s = zenoh.net.open({})
>>> s.query('/resource/name', 'predicate', query_callback)
>>> time.sleep(1)
-
zenoh.net.
open
()¶ Open a zenoh-net Session.
Parameters: config (dict {str: str}) – The configuration of the zenoh-net session Return type: Session Example: >>> import zenoh >>> z = zenoh.net.open(zenoh.net.config::peer())
-
zenoh.net.
scout
()¶ Scout for routers and/or peers.
This spawns a task that periodically sends scout messages for a specified duration and returns a list of received
Hello
messages.Parameters: - whatami (int) – The kind of zenoh process to scout for
- config (dict {str: str}) – The configuration to use for scouting
- scout_duration (float) – the duration of scout (in seconds)
Return type: list of
Hello
Example: >>> import zenoh >>> hellos = zenoh.net.scout(zenoh.net.whatami.PEER | zenoh.net.whatami.ROUTER, {}, 1.0) >>> for hello in hellos: >>> print(hello)
Hello¶
Session¶
-
class
zenoh.net.
Session
¶ A zenoh-net session.
-
close
()¶ Close the zenoh-net Session.
-
declare_publisher
()¶ Declare a Publisher for the given resource key.
Written resources that match the given key will only be sent on the network if matching subscribers exist in the system.
The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: resource (ResKey) – The resource key to publish Return type: Publisher Examples: >>> import zenoh >>> s = zenoh.net.open({}) >>> rid = s.declare_publisher('/resource/name') >>> s.write('/resource/name', bytes('value', encoding='utf8'))
- int for a
-
declare_queryable
()¶ Declare a Queryable for the given resource key.
The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: Return type: Examples: >>> import zenoh, time >>> from zenoh.net import Sample, queryable >>> def callback(query): ... print("Received : {}".format(query)) ... query.reply(Sample('/resource/name', bytes('value', encoding='utf8'))) >>> >>> s = zenoh.net.open({}) >>> q = s.declare_queryable('/resource/name', queryable.EVAL, callback) >>> time.sleep(60)
- int for a
-
declare_resource
()¶ Associate a numerical Id with the given resource key.
This numerical Id will be used on the network to save bandwidth and ease the retrieval of the concerned resource in the routing tables.
The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: resource (ResKey) – The resource key to map to a numerical Id Return type: int Examples: >>> import zenoh >>> s = zenoh.net.open({}) >>> rid = s.declare_resource('/resource/name')
- int for a
-
declare_subscriber
()¶ Declare a Subscxriber for the given resource key.
The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: Return type: Examples: >>> import zenoh, time >>> from zenoh.net import SubInfo, Reliability, SubMode >>> >>> s = zenoh.net.open({}) >>> sub_info = SubInfo(Reliability.Reliable, SubMode.Push) >>> sub = s.declare_subscriber('/resource/name', sub_info, lambda sample: ... print("Received : {}".format(sample))) >>> time.sleep(60)
- int for a
-
info
()¶ Get informations about the zenoh-net Session.
Return type: dict {str: str} Example: >>> import zenoh >>> s = zenoh.net.open({}) >>> info = s.info() >>> for key in info: >>> print("{} : {}".format(key, info[key]))
-
query
()¶ Query data from the matching queryables in the system.
The replies are provided by calling the provided
callback
for each reply. Thecallback
is called a last time withNone
when the query is complete.The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: - resource (ResKey) – The resource key to query
- predicate (str) – An indication to matching queryables about the queried data
- callback (function(
Reply
)) – the query callback which will receive the replies - target (QueryTarget, optional) – The kind of queryables that should be target of this query
- consolidation (QueryConsolidation, optional) – The kind of consolidation that should be applied on replies
Examples: >>> import zenoh, time >>> from zenoh.net import QueryTarget, queryable >>> >>> s = zenoh.net.open({}) >>> s.query('/resource/name', 'predicate', lambda reply: ... print("Received : {}".format( ... reply.data if reply is not None else "FINAL")))
- int for a
-
query_collect
()¶ Query data from the matching queryables in the system.
Replies are collected in a list.
The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: - resource (ResKey) – The resource key to query
- predicate (str) – An indication to matching queryables about the queried data
- target (QueryTarget, optional) – The kind of queryables that should be target of this query
- consolidation (QueryConsolidation, optional) – The kind of consolidation that should be applied on replies
Return type: [
Reply
]Examples: >>> import zenoh, time >>> from zenoh.net import QueryTarget, queryable >>> >>> s = zenoh.net.open({}) >>> replies = s.query_collect('/resource/name', 'predicate') >>> for reply in replies: ... print("Received : {}".format(reply.data))
- int for a
-
undeclare_resource
()¶ Undeclare the numerical Id/resource key association previously declared with
declare_resource()
.Parameters: rid (ResKey) – The numerical Id to unmap Examples: >>> import zenoh >>> s = zenoh.net.open({}) >>> rid = s.declare_resource('/resource/name') >>> s.undeclare_resource(rid)
-
write
()¶ Write data.
The resource parameter also accepts the following types that can be converted to a
ResKey
:- int for a
ResKey.Rid(int)
- str for a
ResKey.RName(str)
- (int, str) for a
ResKey.RIdWithSuffix(int, str)
Parameters: - resource (ResKey) – The resource key to write
- payload (bytes) – The value to write
Examples: >>> import zenoh >>> s = zenoh.net.open({}) >>> s.write('/resource/name', bytes('value', encoding='utf8'))
- int for a
-
Subscriber¶
ResKey¶
-
class
zenoh.net.
ResKey
¶ A resource key
-
static
RId
()¶ Creates a resource key from a resource id returned by
Session.declare_resource()
.Parameters: id (int) – the resrouce id
-
static
RIdWithSuffix
()¶ Creates a resource key from a resource id returned by
Session.declare_resource()
and a suffix.Parameters: - id (int) – the resrouce id
- suffix (str) – the suffix
-
static
RName
()¶ Creates a resource key from a name.
Parameters: name (str) – the resrouce name
-
static
Timestamp¶
DataInfo¶
-
class
zenoh.net.
DataInfo
¶ Some informations about the associated data
-
encoding
¶ The encoding flag of the data.
Type: int or None
-
first_router_sn
¶ The first router sequence number of the data.
Type: int or None
-
kind
¶ The kind of the data.
Type: int or None
-
source_sn
¶ The source sequence number of the data.
Type: int or None
-
Sample¶
SubInfo¶
-
class
zenoh.net.
SubInfo
¶ Informations to configure a subscription.
Parameters: - reliability (Reliability, optional) – the reliability mode (default:
Reliability.Reliable
) - mode (SubMode, optional) – the subscription mode (default:
SubMode.Push
) - period (Period, optional) – the pull period
- reliability (Reliability, optional) – the reliability mode (default:
Query¶
Queryable¶
QueryTarget¶
ConsolidationMode¶
-
class
zenoh.net.
ConsolidationMode
¶ The kind of consolidation that should be applied on replies to a
Session.query()
.-
No
¶
-
Lazy
¶
-
Full
¶
-
QueryConsolidation¶
-
class
zenoh.net.
QueryConsolidation
¶ The kind of consolidation that should be applied on replies to a
Session.query()
at the different stages of the reply process.Parameters: - first_routers (ConsolidationMode, optional) – the consolidation mode to apply on first routers of the replies routing path (default:
ConsolidationMode.Lazy
) - last_router (ConsolidationMode, optional) – the consolidation mode to apply on last router of the replies routing path (default:
ConsolidationMode.Lazy
) - reception (ConsolidationMode, optional) – the consolidation mode to apply at reception of the replies (default:
ConsolidationMode.Full
)
- first_routers (ConsolidationMode, optional) – the consolidation mode to apply on first routers of the replies routing path (default:
Reply¶
module zenoh.net.config¶
Constants and helpers to build the configuration to pass to zenoh.net.open()
.
module zenoh.net.info¶
Constants and helpers to interpret the properties returned by zenoh.net.Session.info()
.
module zenoh.net.whatami¶
Constants defining the different zenoh process to look for with zenoh.net.scout()
.
-
zenoh.net.whatami.
to_string
()¶
module zenoh.net.queryable¶
Constants defining the different modes of a zenoh Queryable
.