Struct zenoh::Session

source ·
pub struct Session { /* private fields */ }
Expand description

A zenoh session.

Implementations§

source§

impl Session

source

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;
source

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;
source

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.

source

pub fn hlc(&self) -> Option<&HLC>

source

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();
source

pub fn undeclare<'a, T, O>(&'a self, decl: T) -> O

source

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

source

pub fn declare_keyexpr<'a, 'b: 'a, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> impl Resolve<ZResult<KeyExpr<'b>>> + 'a
where TryIntoKeyExpr: TryInto<KeyExpr<'b>>, <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>,

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();
source

pub fn put<'a, 'b: 'a, TryIntoKeyExpr, IntoValue>( &'a self, key_expr: TryIntoKeyExpr, value: IntoValue ) -> PutBuilder<'a, 'b>
where TryIntoKeyExpr: TryInto<KeyExpr<'b>>, <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>, IntoValue: Into<Value>,

Put data.

§Arguments
  • key_expr - Key expression matching the resources to put
  • value - 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();
source

pub fn delete<'a, 'b: 'a, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> DeleteBuilder<'a, 'b>
where TryIntoKeyExpr: TryInto<KeyExpr<'b>>, <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>,

Delete data.

§Arguments
  • key_expr - Key expression matching the resources to delete
§Examples
use zenoh::prelude::r#async::*;

let session = zenoh::open(config::peer()).res().await.unwrap();
session.delete("key/expression").res().await.unwrap();
source

pub fn get<'a, 'b: 'a, TryIntoSelector>( &'a self, selector: TryIntoSelector ) -> GetBuilder<'a, 'b, DefaultHandler>
where TryIntoSelector: TryInto<Selector<'b>>, <TryIntoSelector as TryInto<Selector<'b>>>::Error: Into<Error>,

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 Debug for Session

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Session

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> SessionDeclarations<'a, 'a> for Session

source§

fn info(&self) -> SessionInfo<'_>

Get informations about the zenoh Session. Read more
source§

fn declare_subscriber<'b, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> SubscriberBuilder<'a, 'b, PushMode, DefaultHandler>
where TryIntoKeyExpr: TryInto<KeyExpr<'b>>, <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>,

Create a Subscriber for the given key expression. Read more
source§

fn declare_queryable<'b, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> QueryableBuilder<'a, 'b, DefaultHandler>
where TryIntoKeyExpr: TryInto<KeyExpr<'b>>, <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>,

Create a Queryable for the given key expression. Read more
source§

fn declare_publisher<'b, TryIntoKeyExpr>( &'a self, key_expr: TryIntoKeyExpr ) -> PublisherBuilder<'a, 'b>
where TryIntoKeyExpr: TryInto<KeyExpr<'b>>, <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>,

Create a Publisher for the given key expression. Read more
source§

fn liveliness(&'a self) -> Liveliness<'_>

Available on crate feature unstable only.
Obtain a Liveliness struct tied to this Zenoh Session. Read more
source§

impl<'a> Undeclarable<&'a Session, KeyExprUndeclaration<'a>> for KeyExpr<'a>

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsNode<T> for T

source§

fn as_node(&self) -> &T

source§

impl<T> AsNodeMut<T> for T

source§

fn as_node_mut(&mut self) -> &mut T

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<'a, O, T, G> Undeclarable<&'a Session, O, T> for G
where O: Resolve<T> + Send, G: Undeclarable<(), O, T>,

source§

fn undeclare_inner(self, _: &'a Session) -> O

source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more