pub struct Session(/* private fields */);
Expand description
The entrypoint of the zenoh API.
Zenoh session is instantiated using zenoh::open
and it can be used to declare various
entities like publishers, subscribers, or querybables, as well as issuing queries.
Session is an Arc
-like type, it can be cloned, and it is closed when the last instance
is dropped (see Session::close
).
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
session.put("key/expression", "value").await.unwrap();
Implementations§
Source§impl Session
impl Session
Sourcepub fn zid(&self) -> ZenohId
pub fn zid(&self) -> ZenohId
Returns the identifier of the current session. zid()
is a convenient shortcut.
See Session::info()
and SessionInfo::zid()
for more details.
Sourcepub fn close(&self) -> CloseBuilder<Self>
pub fn close(&self) -> CloseBuilder<Self>
Close the zenoh Session
.
Every subscriber and queryable declared will stop receiving data, and further attempt to
publish or query with the session or publishers will result in an error. Undeclaring an
entity after session closing is a no-op. Session state can be checked with
Session::is_closed
.
Session are automatically closed when all its instances are dropped, same as Arc
.
You may still want to use this function to handle errors or close the session
asynchronously.
Closing the session can also save bandwidth, as it avoids propagating the undeclaration
of the remaining entities.
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let subscriber = session
.declare_subscriber("key/expression")
.await
.unwrap();
let subscriber_task = tokio::spawn(async move {
while let Ok(sample) = subscriber.recv_async().await {
println!("Received: {} {:?}", sample.key_expr(), sample.payload());
}
});
session.close().await.unwrap();
// subscriber task will end as `subscriber.recv_async()` will return `Err`
// subscriber undeclaration has not been sent on the wire
subscriber_task.await.unwrap();
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Check if the session has been closed.
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
assert!(!session.is_closed());
session.close().await.unwrap();
assert!(session.is_closed());
pub fn undeclare<'a, T>(&'a self, decl: T) -> impl Resolve<ZResult<()>> + 'awhere
T: Undeclarable<&'a Session> + 'a,
Sourcepub fn config(&self) -> &Notifier<Config>
Available on crate feature unstable
only.
pub fn config(&self) -> &Notifier<Config>
unstable
only.Get the current configuration of the zenoh Session
.
The returned configuration Notifier
can be used to read the current
zenoh configuration through the get
function or
modify the zenoh configuration through the insert
,
or insert_json5
function.
§Examples
§Read current zenoh configuration
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let peers = session.config().get("connect/endpoints").unwrap();
§Modify current zenoh configuration
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let _ = session.config().insert_json5("connect/endpoints", r#"["tcp/127.0.0.1/7447"]"#);
Sourcepub fn new_timestamp(&self) -> Timestamp
pub fn new_timestamp(&self) -> Timestamp
Source§impl Session
impl Session
Sourcepub fn info(&self) -> SessionInfo
pub fn info(&self) -> SessionInfo
Sourcepub fn declare_subscriber<'b, TryIntoKeyExpr>(
&self,
key_expr: TryIntoKeyExpr,
) -> SubscriberBuilder<'_, 'b, DefaultHandler>
pub fn declare_subscriber<'b, TryIntoKeyExpr>( &self, key_expr: TryIntoKeyExpr, ) -> SubscriberBuilder<'_, 'b, DefaultHandler>
Create a Subscriber
for the given key expression.
§Arguments
key_expr
- The resourkey expression to subscribe to
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let subscriber = session.declare_subscriber("key/expression")
.await
.unwrap();
tokio::task::spawn(async move {
while let Ok(sample) = subscriber.recv_async().await {
println!("Received: {:?}", sample);
}
}).await;
Sourcepub fn declare_queryable<'b, TryIntoKeyExpr>(
&self,
key_expr: TryIntoKeyExpr,
) -> QueryableBuilder<'_, 'b, DefaultHandler>
pub fn declare_queryable<'b, TryIntoKeyExpr>( &self, key_expr: TryIntoKeyExpr, ) -> QueryableBuilder<'_, 'b, DefaultHandler>
Create a Queryable
for the given key expression.
§Arguments
key_expr
- The key expression matching the queries theQueryable
will reply to
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let queryable = session.declare_queryable("key/expression")
.await
.unwrap();
tokio::task::spawn(async move {
while let Ok(query) = queryable.recv_async().await {
query.reply(
"key/expression",
"value",
).await.unwrap();
}
}).await;
Sourcepub fn declare_publisher<'b, TryIntoKeyExpr>(
&self,
key_expr: TryIntoKeyExpr,
) -> PublisherBuilder<'_, 'b>
pub fn declare_publisher<'b, TryIntoKeyExpr>( &self, key_expr: TryIntoKeyExpr, ) -> PublisherBuilder<'_, 'b>
Create a Publisher
for the given key expression.
§Arguments
key_expr
- The key expression matching resources to write
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let publisher = session.declare_publisher("key/expression")
.await
.unwrap();
publisher.put("value").await.unwrap();
Sourcepub fn declare_querier<'b, TryIntoKeyExpr>(
&self,
key_expr: TryIntoKeyExpr,
) -> QuerierBuilder<'_, 'b>
Available on crate feature unstable
only.
pub fn declare_querier<'b, TryIntoKeyExpr>( &self, key_expr: TryIntoKeyExpr, ) -> QuerierBuilder<'_, 'b>
unstable
only.Create a Querier
for the given key expression.
§Arguments
key_expr
- The key expression matching resources to query
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let querier = session.declare_querier("key/expression")
.await
.unwrap();
let replies = querier.get().await.unwrap();
Sourcepub fn liveliness(&self) -> Liveliness<'_>
pub fn liveliness(&self) -> Liveliness<'_>
Obtain a Liveliness
struct tied to this Zenoh Session
.
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let liveliness = session
.liveliness()
.declare_token("key/expression")
.await
.unwrap();
Source§impl Session
impl Session
Sourcepub fn declare_keyexpr<'a, 'b: 'a, TryIntoKeyExpr>(
&'a self,
key_expr: TryIntoKeyExpr,
) -> impl Resolve<ZResult<KeyExpr<'b>>> + 'a
pub fn declare_keyexpr<'a, 'b: 'a, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr, ) -> impl Resolve<ZResult<KeyExpr<'b>>> + 'a
Informs Zenoh that you intend to use key_expr
multiple times and that it should optimize its transmission.
The returned KeyExpr
’s internal structure may differ from what you would have obtained through a simple
key_expr.try_into()
, to save time on detecting the optimizations that have been associated with it.
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let key_expr = session.declare_keyexpr("key/expression").await.unwrap();
Sourcepub fn put<'a, 'b: 'a, TryIntoKeyExpr, IntoZBytes>(
&'a self,
key_expr: TryIntoKeyExpr,
payload: IntoZBytes,
) -> SessionPutBuilder<'a, 'b>
pub fn put<'a, 'b: 'a, TryIntoKeyExpr, IntoZBytes>( &'a self, key_expr: TryIntoKeyExpr, payload: IntoZBytes, ) -> SessionPutBuilder<'a, 'b>
Put data.
§Arguments
key_expr
- Key expression matching the resources to putpayload
- The payload to put
§Examples
use zenoh::bytes::Encoding;
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
session
.put("key/expression", "payload")
.encoding(Encoding::TEXT_PLAIN)
.await
.unwrap();
Sourcepub fn delete<'a, 'b: 'a, TryIntoKeyExpr>(
&'a self,
key_expr: TryIntoKeyExpr,
) -> SessionDeleteBuilder<'a, 'b>
pub fn delete<'a, 'b: 'a, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr, ) -> SessionDeleteBuilder<'a, 'b>
Sourcepub fn get<'a, 'b: 'a, TryIntoSelector>(
&'a self,
selector: TryIntoSelector,
) -> SessionGetBuilder<'a, 'b, DefaultHandler>
pub fn get<'a, 'b: 'a, TryIntoSelector>( &'a self, selector: TryIntoSelector, ) -> SessionGetBuilder<'a, 'b, DefaultHandler>
Query data from the matching queryables in the system.
Unless explicitly requested via accept_replies
, replies are guaranteed to have
key expressions that match the requested selector
.
§Arguments
selector
- The selection of resources to query
§Examples
let session = zenoh::open(zenoh::Config::default()).await.unwrap();
let replies = session.get("key/expression").await.unwrap();
while let Ok(reply) = replies.recv_async().await {
println!(">> Received {:?}", reply.result());
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<Source> AccessAs for Source
impl<Source> AccessAs for Source
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more