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/coaplite.rs (line 7)
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
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();
handler.build_response(&mut response.message, extracted);
let packet = response.message.to_bytes().unwrap();
socket
.send_to(&packet[..], &src)
.expect("Could not send the data");
}
}
examples/coap_crate.rs (line 13)
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
fn main() {
let addr = "localhost:5683";
let log = Some(coap_message_demos::log::Log::start_once());
Runtime::new().unwrap().block_on(async move {
let mut server = Server::new(addr).unwrap();
println!("Server up on {}", addr);
info!("Server up on {}", addr);
use coap_handler::Handler;
let mut handler = coap_message_demos::full_application_tree(log);
server
.run(move |mut request| {
// Having a shared reference to a single handler works for this server because it
// allows the handler to be FnMut and not Sync -- essentially, because the server
// promises not to become multithreaded.
let extracted = handler.extract_request_data(&request.message);
if let Some(r) = request.response.as_mut() {
handler.build_response(&mut r.message, extracted);
}
// By running all the processing in the sync part of the function (because the
// coap-handler doesn't cater for async right now), the &mut handler reference we use
// does not need to live across possible await points, and is thus usable.
async { request.response }
})
.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.