Struct coap_lite::CoapRequest

source ·
pub struct CoapRequest<Endpoint> {
    pub message: Packet,
    pub response: Option<CoapResponse>,
    pub source: Option<Endpoint>,
}
Expand description

The CoAP request.

Fields§

§message: Packet§response: Option<CoapResponse>§source: Option<Endpoint>

Implementations§

source§

impl<Endpoint> CoapRequest<Endpoint>

source

pub fn new() -> CoapRequest<Endpoint>

Creates a new request.

Examples found in repository?
examples/client.rs (line 5)
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let mut request: CoapRequest<SocketAddr> = CoapRequest::new();

    request.set_method(Method::Get);
    request.set_path("/test");

    let socket = UdpSocket::bind("127.0.0.1:0").unwrap();

    let packet = request.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], "127.0.0.1:5683")
        .expect("Could not send the data");
}
source

pub fn from_packet(packet: Packet, source: Endpoint) -> CoapRequest<Endpoint>

Creates a request from a packet.

Examples found in repository?
examples/server.rs (line 12)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let socket = UdpSocket::bind("127.0.0.1:5683").unwrap();
    let mut buf = [0; 100];
    let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");

    println!("Payload {:x?}", &buf[..size]);

    let packet = Packet::from_bytes(&buf[..size]).unwrap();
    let request = CoapRequest::from_packet(packet, src);

    let method = request.get_method().clone();
    let path = request.get_path();

    println!("Received CoAP request '{:?} {}' from {}", method, path, src);

    let mut response = request.response.unwrap();
    response.message.payload = b"OK".to_vec();

    let packet = response.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], &src)
        .expect("Could not send the data");
}
source

pub fn apply_from_error(&mut self, error: HandlingError) -> bool

Applies the given error to the request and returns true if that was successful.

source

pub fn set_method(&mut self, method: Method)

Sets the method.

Examples found in repository?
examples/client.rs (line 7)
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let mut request: CoapRequest<SocketAddr> = CoapRequest::new();

    request.set_method(Method::Get);
    request.set_path("/test");

    let socket = UdpSocket::bind("127.0.0.1:0").unwrap();

    let packet = request.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], "127.0.0.1:5683")
        .expect("Could not send the data");
}
source

pub fn get_method(&self) -> &Method

Returns the method.

Examples found in repository?
examples/server.rs (line 14)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let socket = UdpSocket::bind("127.0.0.1:5683").unwrap();
    let mut buf = [0; 100];
    let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");

    println!("Payload {:x?}", &buf[..size]);

    let packet = Packet::from_bytes(&buf[..size]).unwrap();
    let request = CoapRequest::from_packet(packet, src);

    let method = request.get_method().clone();
    let path = request.get_path();

    println!("Received CoAP request '{:?} {}' from {}", method, path, src);

    let mut response = request.response.unwrap();
    response.message.payload = b"OK".to_vec();

    let packet = response.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], &src)
        .expect("Could not send the data");
}
source

pub fn set_path(&mut self, path: &str)

Sets the path.

Examples found in repository?
examples/client.rs (line 8)
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let mut request: CoapRequest<SocketAddr> = CoapRequest::new();

    request.set_method(Method::Get);
    request.set_path("/test");

    let socket = UdpSocket::bind("127.0.0.1:0").unwrap();

    let packet = request.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], "127.0.0.1:5683")
        .expect("Could not send the data");
}
source

pub fn get_path(&self) -> String

Returns the path.

Examples found in repository?
examples/server.rs (line 15)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let socket = UdpSocket::bind("127.0.0.1:5683").unwrap();
    let mut buf = [0; 100];
    let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");

    println!("Payload {:x?}", &buf[..size]);

    let packet = Packet::from_bytes(&buf[..size]).unwrap();
    let request = CoapRequest::from_packet(packet, src);

    let method = request.get_method().clone();
    let path = request.get_path();

    println!("Received CoAP request '{:?} {}' from {}", method, path, src);

    let mut response = request.response.unwrap();
    response.message.payload = b"OK".to_vec();

    let packet = response.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], &src)
        .expect("Could not send the data");
}
source

pub fn get_path_as_vec( &self ) -> Result<Vec<String>, IncompatibleOptionValueFormat>

Returns the path as a vector (as it is encoded in CoAP rather than in HTTP-style paths).

source

pub fn get_observe_flag(&self) -> Option<Result<ObserveOption, InvalidObserve>>

Returns the flag in the Observe option or InvalidObserve if the flag was provided but not understood.

source

pub fn set_observe_flag(&mut self, flag: ObserveOption)

Sets the flag in the Observe option.

Trait Implementations§

source§

impl<Endpoint: Clone> Clone for CoapRequest<Endpoint>

source§

fn clone(&self) -> CoapRequest<Endpoint>

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

impl<Endpoint: Debug> Debug for CoapRequest<Endpoint>

source§

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

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

impl<Endpoint> Default for CoapRequest<Endpoint>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<Endpoint: Ord + Clone> From<&CoapRequest<Endpoint>> for RequestCacheKey<Endpoint>

source§

fn from(request: &CoapRequest<Endpoint>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Endpoint> RefUnwindSafe for CoapRequest<Endpoint>where Endpoint: RefUnwindSafe,

§

impl<Endpoint> Send for CoapRequest<Endpoint>where Endpoint: Send,

§

impl<Endpoint> Sync for CoapRequest<Endpoint>where Endpoint: Sync,

§

impl<Endpoint> Unpin for CoapRequest<Endpoint>where Endpoint: Unpin,

§

impl<Endpoint> UnwindSafe for CoapRequest<Endpoint>where Endpoint: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.