Struct coap_message_demos::log::Log
source · pub struct Log { /* private fields */ }
Expand description
A ring-buffered log structure
Implementations§
source§impl Log
impl Log
pub fn new() -> Self
pub fn init(&'static self) -> Result<(), SetLoggerError>
sourcepub fn start_once() -> &'static Self
pub fn start_once() -> &'static Self
Allocate a log in static memory, and set it up
Examples found in repository?
examples/std_embedded_nal_minicoapserver.rs (line 17)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
fn run<S>(stack: &mut S)
where
S: UdpFullStack,
{
let mut sock = stack.socket().expect("Can't create a socket");
let log = Some(coap_message_demos::log::Log::start_once());
let mut handler = coap_message_demos::full_application_tree(log);
stack.bind(&mut sock, 5683).expect("Can't bind to port");
info!("Server is ready.");
loop {
match embedded_nal_minimal_coapserver::poll(stack, &mut sock, &mut handler) {
Err(embedded_nal::nb::Error::WouldBlock) => {
// See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
std::thread::sleep(std::time::Duration::from_millis(50));
}
e => e.expect("UDP error during send/receive"),
}
}
}
More examples
examples/std_embedded_nal_minicoaptcpserver.rs (line 15)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn run<S>(stack: &mut S)
where
S: embedded_nal::TcpFullStack + embedded_nal_tcpextensions::TcpExactStack,
{
let mut sock = stack.socket().expect("Can't create a socket");
let log = Some(coap_message_demos::log::Log::start_once());
let mut handler = coap_message_demos::full_application_tree(log);
stack.bind(&mut sock, 5683).expect("Can't bind to port");
info!("Server is ready.");
let mut pool = embedded_nal_minimal_coaptcpserver::ServerPool::<S, 4, 1152>::new(sock);
loop {
pool.poll(stack, &mut handler)
.expect("Actual error in polling (accepting?)");
// See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
std::thread::sleep(std::time::Duration::from_millis(50));
}
}
examples/std_embedded_nal_coap.rs (line 18)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
async fn run<S>(stack: &mut S)
where
S: embedded_nal_async::UdpStack,
{
let mut sock = stack
.bind_multiple(embedded_nal_async::SocketAddr::new(
"::".parse().unwrap(),
5683,
))
.await
.expect("Can't create a socket");
let log = Some(coap_message_demos::log::Log::start_once());
let mut handler = coap_message_demos::full_application_tree(log);
info!("Server is ready.");
let coap = embedded_nal_coap::CoAPShared::<3>::new();
let (client, server) = coap.split();
// going with an embassy_futures join instead of an async_std::task::spawn b/c CoAPShared is not
// Sync, and async_std expects to work in multiple threads
embassy_futures::join::join(
async {
use rand::SeedableRng;
server
.run(
&mut sock,
&mut handler,
&mut rand::rngs::StdRng::from_entropy(),
)
.await
.expect("UDP error")
},
run_client_operations(client)
)
.await;
}
examples/coaplite.rs (line 8)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() {
let log = Some(coap_message_demos::log::Log::start_once());
let mut handler = coap_message_demos::full_application_tree(log);
let socket = UdpSocket::bind("localhost:5683").unwrap();
let mut buf = [0; 1280];
loop {
let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");
let packet = Packet::from_bytes(&buf[..size]).unwrap();
let request = CoapRequest::from_packet(packet, src);
let extracted = handler.extract_request_data(&request.message);
let mut response = request.response.unwrap();
match extracted {
Ok(extracted) => {
if let Err(e2) =
handler.build_response(&mut response.message, extracted)
{
response.message.payload = Default::default();
response.message.clear_all_options();
e2.render(&mut response.message).unwrap();
}
}
Err(e) => {
e.render(&mut response.message).unwrap();
}
}
let packet = response.message.to_bytes().unwrap();
socket
.send_to(&packet[..], &src)
.expect("Could not send the data");
}
}
examples/coap_crate.rs (line 11)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let addr = "localhost:5683";
let log = coap_message_demos::log::Log::start_once();
Runtime::new().unwrap().block_on(async move {
let server = Server::new_udp(addr).unwrap();
println!("Server up on {}", addr);
info!("Server up on {}", addr);
use coap_handler::Handler;
// Unlike in coap 0.11, the run closure is now Fn, so we can't have an exclusive reference
// to a handler but need to fan out. To avoid the need for a mutex, we just use a handler
// factory.
//
// Note that this may have "funny" effects w/rt the CBOR editable items, which are shared
// only across handlers spawned from a single full_application_tree call.
let handler = || coap_message_demos::full_application_tree(Some(log));
server
.run(
move |mut request: Box<coap_lite::CoapRequest<std::net::SocketAddr>>| {
let mut handler = handler();
// We can just unwrap because the tokio based surver unwinds crashing handlers.
// We could still choose to rather render the errors, because a) it'd provide
// concrete information to the client and b) it sends a response instead of
// leaving the client hanging -- see the coap-lite example for how that is
// done.
let extracted = handler.extract_request_data(&request.message).unwrap();
if let Some(r) = request.response.as_mut() {
handler.build_response(&mut r.message, extracted).unwrap();
}
async { request }
},
)
.await
.unwrap();
});
}
sourcepub fn handler(&self) -> BufferHandler<'_, 1024>
pub fn handler(&self) -> BufferHandler<'_, 1024>
Obtain a coap_handler::Handler implementation from the Log
This is exposed as a separate type rather than on Log in order to reuse the same handler implementation for more than just the log.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Log
impl !RefUnwindSafe for Log
impl Send for Log
impl Sync for Log
impl Unpin for Log
impl UnwindSafe for Log
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