pub struct Session { /* private fields */ }
Expand description
A zenoh session.
Implementations§
source§impl Session
impl Session
sourcepub fn into_arc(self) -> Arc<Self>
pub fn into_arc(self) -> Arc<Self>
Consumes the given Session
, returning a thread-safe reference-counting
pointer to it (Arc<Session>
). This is equivalent to Arc::new(session)
.
This is useful to share ownership of the Session
between several threads
and tasks. It also alows to create Subscriber
and
Queryable
with static lifetime that can be moved to several
threads and tasks
Note: the given zenoh Session
will be closed when the last reference to
it is dropped.
§Examples
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap().into_arc();
let subscriber = session.declare_subscriber("key/expression")
.res()
.await
.unwrap();
tokio::task::spawn(async move {
while let Ok(sample) = subscriber.recv_async().await {
println!("Received: {:?}", sample);
}
}).await;
sourcepub fn leak(s: Self) -> &'static mut Self
pub fn leak(s: Self) -> &'static mut Self
Consumes and leaks the given Session
, returning a 'static
mutable
reference to it. The given Session
will live for the remainder of
the program’s life. Dropping the returned reference will cause a memory
leak.
This is useful to move entities (like Subscriber
) which
lifetimes are bound to the session lifetime in several threads or tasks.
Note: the given zenoh Session
cannot be closed any more. At process
termination the zenoh session will terminate abruptly. If possible prefer
using Session::into_arc()
.
§Examples
use zenoh::prelude::r#async::*;
use zenoh::Session;
let session = Session::leak(zenoh::open(config::peer()).res().await.unwrap());
let subscriber = session.declare_subscriber("key/expression").res().await.unwrap();
tokio::task::spawn(async move {
while let Ok(sample) = subscriber.recv_async().await {
println!("Received: {:?}", sample);
}
}).await;
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.
pub fn hlc(&self) -> Option<&HLC>
sourcepub fn close(self) -> impl Resolve<ZResult<()>>
pub fn close(self) -> impl Resolve<ZResult<()>>
Close the zenoh Session
.
Sessions are automatically closed when dropped, but you may want to use this function to handle errors or close the Session asynchronously.
§Examples
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap();
session.close().res().await.unwrap();
pub fn undeclare<'a, T, O>(&'a self, decl: T) -> O
sourcepub fn config(&self) -> &Notifier<Config>
pub fn config(&self) -> &Notifier<Config>
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
funtion.
§Examples
§Read current zenoh configuration
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap();
let peers = session.config().get("connect/endpoints").unwrap();
§Modify current zenoh configuration
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap();
let _ = session.config().insert_json5("connect/endpoints", r#"["tcp/127.0.0.1/7447"]"#);
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
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap();
let key_expr = session.declare_keyexpr("key/expression").res().await.unwrap();
sourcepub fn put<'a, 'b: 'a, TryIntoKeyExpr, IntoValue>(
&'a self,
key_expr: TryIntoKeyExpr,
value: IntoValue
) -> PutBuilder<'a, 'b>
pub fn put<'a, 'b: 'a, TryIntoKeyExpr, IntoValue>( &'a self, key_expr: TryIntoKeyExpr, value: IntoValue ) -> PutBuilder<'a, 'b>
Put data.
§Arguments
key_expr
- Key expression matching the resources to putvalue
- The value to put
§Examples
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap();
session
.put("key/expression", "value")
.encoding(KnownEncoding::TextPlain)
.res()
.await
.unwrap();
sourcepub fn delete<'a, 'b: 'a, TryIntoKeyExpr>(
&'a self,
key_expr: TryIntoKeyExpr
) -> DeleteBuilder<'a, 'b>
pub fn delete<'a, 'b: 'a, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> DeleteBuilder<'a, 'b>
sourcepub fn get<'a, 'b: 'a, TryIntoSelector>(
&'a self,
selector: TryIntoSelector
) -> GetBuilder<'a, 'b, DefaultHandler>
pub fn get<'a, 'b: 'a, TryIntoSelector>( &'a self, selector: TryIntoSelector ) -> GetBuilder<'a, 'b, DefaultHandler>
Query data from the matching queryables in the system.
Unless explicitly requested via GetBuilder::accept_replies
, replies are guaranteed to have
key expressions that match the requested selector
.
§Arguments
selector
- The selection of resources to query
§Examples
use zenoh::prelude::r#async::*;
let session = zenoh::open(config::peer()).res().await.unwrap();
let replies = session.get("key/expression").res().await.unwrap();
while let Ok(reply) = replies.recv_async().await {
println!(">> Received {:?}", reply.sample);
}
Trait Implementations§
source§impl<'a> SessionDeclarations<'a, 'a> for Session
impl<'a> SessionDeclarations<'a, 'a> for Session
source§fn declare_subscriber<'b, TryIntoKeyExpr>(
&'a self,
key_expr: TryIntoKeyExpr
) -> SubscriberBuilder<'a, 'b, PushMode, DefaultHandler>
fn declare_subscriber<'b, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> SubscriberBuilder<'a, 'b, PushMode, DefaultHandler>
Subscriber
for the given key expression. Read moresource§fn declare_queryable<'b, TryIntoKeyExpr>(
&'a self,
key_expr: TryIntoKeyExpr
) -> QueryableBuilder<'a, 'b, DefaultHandler>
fn declare_queryable<'b, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> QueryableBuilder<'a, 'b, DefaultHandler>
source§fn declare_publisher<'b, TryIntoKeyExpr>(
&'a self,
key_expr: TryIntoKeyExpr
) -> PublisherBuilder<'a, 'b>
fn declare_publisher<'b, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> PublisherBuilder<'a, 'b>
source§fn liveliness(&'a self) -> Liveliness<'_>
fn liveliness(&'a self) -> Liveliness<'_>
unstable
only.