Struct safe_drive::service::client::ClientRecv

source ·
pub struct ClientRecv<T> { /* private fields */ }
Expand description

Receiver to receive a response.

Implementations§

source§

impl<T: ServiceMsg> ClientRecv<T>

source

pub fn try_recv( self ) -> RecvResult<(Client<T>, <T as ServiceMsg>::Response, Header), Self>

Receive a message. try_recv is a non-blocking function, and this returns RecvResult::RetryLater(self). So, please retry later if this value is returned.

§Errors
  • RCLError::InvalidArgument if any arguments are invalid, or
  • RCLError::ClientInvalid if the client is invalid, or
  • RCLError::Error if an unspecified error occurs.
source

pub fn recv(self) -> AsyncReceiver<T>

Receive a response asynchronously. this returns super::Header including some information together.

§Example
use safe_drive::{
    logger::Logger, msg::common_interfaces::std_srvs, pr_error, pr_info, pr_warn, service::client::Client,
};
use std::time::Duration;

async fn run_client(mut client: Client<std_srvs::srv::Empty>, logger: Logger) {
    let dur = Duration::from_millis(100);

    loop {
        let request = std_srvs::srv::EmptyRequest::new().unwrap();
        let mut receiver = client.send(&request).unwrap().recv();
        match async_std::future::timeout(dur, &mut receiver).await {
            Ok(Ok((c, response, header))) => {
                pr_info!(logger, "received: header = {:?}", header);
                client = c;
            }
            Ok(Err(e)) => {
                pr_error!(logger, "error: {e}");
                break;
            }
            Err(_) => {
                pr_warn!(logger, "timeout");
                client = receiver.give_up();
            }
        }
    }
}
§Errors
  • RCLError::InvalidArgument if any arguments are invalid, or
  • RCLError::ClientInvalid if the client is invalid, or
  • RCLError::Error if an unspecified error occurs.
source

pub fn recv_timeout( self, t: Duration, selector: &mut Selector ) -> RecvResult<(Client<T>, <T as ServiceMsg>::Response, Header), Self>

Receive a message.

§Example
use safe_drive::{
    error::DynError,
    logger::Logger,
    msg::common_interfaces::{std_msgs, std_srvs},
    pr_fatal,
    selector::Selector,
    service::client::Client,
    topic::subscriber::Subscriber,
    RecvResult,
};
use std::time::Duration;

fn worker(
    mut selector: Selector,
    mut selector_client: Selector,
    subscriber: Subscriber<std_msgs::msg::Empty>,
    client: Client<std_srvs::srv::Empty>,
) -> Result<(), DynError> {
    let mut client = Some(client);
    let logger = Logger::new("listen_client");

    selector.add_subscriber(
        subscriber,
        Box::new(move |_msg| {
            // Take the client.
            let c = client.take().unwrap();

            let request = std_srvs::srv::EmptyRequest::new().unwrap();

            // Send a request.
            let receiver = c.send(&request).unwrap();

            // Receive a response.
            match receiver.recv_timeout(Duration::from_millis(20), &mut selector_client) {
                RecvResult::Ok((c, _response, _header)) => client = Some(c),
                RecvResult::RetryLater(r) => client = Some(r.give_up()),
                RecvResult::Err(e) => {
                    pr_fatal!(logger, "{e}");
                    panic!()
                }
            }
        }),
    );

    loop {
        selector.wait()?;
    }
}
source

pub fn give_up(self) -> Client<T>

Give up to receive a response. If there is no server, nobody responds requests.

Trait Implementations§

source§

impl<T: Clone> Clone for ClientRecv<T>

source§

fn clone(&self) -> ClientRecv<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ClientRecv<T>

§

impl<T> !RefUnwindSafe for ClientRecv<T>

§

impl<T> Send for ClientRecv<T>
where T: Send,

§

impl<T> !Sync for ClientRecv<T>

§

impl<T> Unpin for ClientRecv<T>
where T: Unpin,

§

impl<T> UnwindSafe for ClientRecv<T>
where T: UnwindSafe,

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> 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, 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.