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>
impl<Endpoint> CoapRequest<Endpoint>
sourcepub fn new() -> CoapRequest<Endpoint>
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");
}sourcepub fn from_packet(packet: Packet, source: Endpoint) -> CoapRequest<Endpoint>
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");
}sourcepub fn apply_from_error(&mut self, error: HandlingError) -> bool
pub fn apply_from_error(&mut self, error: HandlingError) -> bool
Applies the given error to the request and returns true if that was successful.
sourcepub fn set_method(&mut self, method: Method)
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");
}sourcepub fn get_method(&self) -> &Method
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");
}sourcepub fn set_path(&mut self, path: &str)
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");
}sourcepub fn get_path(&self) -> String
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");
}sourcepub fn get_path_as_vec(
&self
) -> Result<Vec<String>, IncompatibleOptionValueFormat>
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).
sourcepub fn get_observe_flag(&self) -> Option<Result<ObserveOption, InvalidObserve>>
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.
sourcepub fn set_observe_flag(&mut self, flag: ObserveOption)
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>
impl<Endpoint: Clone> Clone for CoapRequest<Endpoint>
source§fn clone(&self) -> CoapRequest<Endpoint>
fn clone(&self) -> CoapRequest<Endpoint>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl<Endpoint: Debug> Debug for CoapRequest<Endpoint>
impl<Endpoint: Debug> Debug for CoapRequest<Endpoint>
source§impl<Endpoint> Default for CoapRequest<Endpoint>
impl<Endpoint> Default for CoapRequest<Endpoint>
source§impl<Endpoint: Ord + Clone> From<&CoapRequest<Endpoint>> for RequestCacheKey<Endpoint>
impl<Endpoint: Ord + Clone> From<&CoapRequest<Endpoint>> for RequestCacheKey<Endpoint>
source§fn from(request: &CoapRequest<Endpoint>) -> Self
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> 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
Mutably borrows from an owned value. Read more