messages in mutex
This commit is contained in:
parent
e6bc8561d4
commit
1c0108cefa
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1103,6 +1103,7 @@ dependencies = [
|
||||
"embassy-executor",
|
||||
"embassy-net",
|
||||
"embassy-rp",
|
||||
"embassy-sync",
|
||||
"embassy-time",
|
||||
"embassy-usb-logger",
|
||||
"embedded-io-async",
|
||||
|
@ -36,6 +36,7 @@ embassy-net = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"udp",
|
||||
"dhcpv4",
|
||||
] }
|
||||
embassy-sync = { git = "https://github.com/embassy-rs/embassy" }
|
||||
cyw43-pio = { git = "https://github.com/embassy-rs/embassy" }
|
||||
cyw43 = { git = "https://github.com/embassy-rs/embassy" }
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use core::{fmt::Write, str::from_utf8};
|
||||
use core::fmt::Write;
|
||||
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, mutex::Mutex};
|
||||
use heapless::{String, Vec};
|
||||
use log::info;
|
||||
use pico_website::unwrap;
|
||||
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
|
||||
|
||||
@ -9,37 +9,15 @@ use crate::socket::{HttpRequestType, HttpResCode};
|
||||
use super::App;
|
||||
|
||||
const MEMORY_SIZE: usize = 16;
|
||||
static MESSAGES: Mutex<ThreadModeRawMutex, Messages> = Mutex::new(Messages::new());
|
||||
|
||||
pub struct ChatApp {
|
||||
res_buf: Vec<u8, 2048>,
|
||||
msgs: ConstGenericRingBuffer<Message, MEMORY_SIZE>,
|
||||
next_msg: u16,
|
||||
}
|
||||
impl ChatApp {
|
||||
pub fn new() -> Self {
|
||||
let mut msgs = ConstGenericRingBuffer::new();
|
||||
|
||||
let mut aut0 = String::new();
|
||||
write!(&mut aut0, "rael").unwrap();
|
||||
let mut msg0 = String::new();
|
||||
write!(&mut msg0, "Prout...").unwrap();
|
||||
msgs.push(Message {
|
||||
author: aut0,
|
||||
content: msg0,
|
||||
});
|
||||
let mut aut1 = String::new();
|
||||
write!(&mut aut1, "josh").unwrap();
|
||||
let mut msg1 = String::new();
|
||||
write!(&mut msg1, "Salut !\nJe m'appelle...").unwrap();
|
||||
msgs.push(Message {
|
||||
author: aut1,
|
||||
content: msg1,
|
||||
});
|
||||
|
||||
Self {
|
||||
res_buf: Vec::new(),
|
||||
msgs,
|
||||
next_msg: 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -57,7 +35,7 @@ impl App for ChatApp {
|
||||
(HttpRequestType::Get, "/" | "/index" | "/index.html" | "/chat" | "/chat.html") => {
|
||||
(HttpResCode::Ok, "html", include_bytes!("./chat.html"))
|
||||
}
|
||||
(req_type, path) => {
|
||||
(_, path) => {
|
||||
let (path, args) = path.split_once('?').unwrap_or((path, ""));
|
||||
let mut load = None;
|
||||
let mut username = None;
|
||||
@ -102,20 +80,13 @@ impl App for ChatApp {
|
||||
Ok(n) => n,
|
||||
Err(_) => return (HttpResCode::BadRequest, "", &[]),
|
||||
};
|
||||
info!("msg_id = {:?}", msg_id);
|
||||
if msg_id >= self.next_msg {
|
||||
let msgs = MESSAGES.lock().await;
|
||||
if msg_id >= msgs.next {
|
||||
return (HttpResCode::BadRequest, "", &[]);
|
||||
}
|
||||
if msg_id < self.next_msg.saturating_sub(MEMORY_SIZE as u16 + 1) {
|
||||
if msg_id < msgs.next.saturating_sub(MEMORY_SIZE as u16 + 1) {
|
||||
return (HttpResCode::NoContent, "", &[]);
|
||||
}
|
||||
let msg = match self
|
||||
.msgs
|
||||
.get_signed((msg_id as isize) + 1 - (self.next_msg as isize))
|
||||
{
|
||||
Some(msg) => msg,
|
||||
None => return (HttpResCode::NoContent, "", &[]),
|
||||
};
|
||||
self.res_buf.clear();
|
||||
unwrap(write!(&mut self.res_buf, "<div class=\"message\"")).await;
|
||||
if let Some(n) = load {
|
||||
@ -130,33 +101,16 @@ impl App for ChatApp {
|
||||
))
|
||||
.await;
|
||||
}
|
||||
let msg = match msgs.get_abs(msg_id) {
|
||||
Some(msg) => msg,
|
||||
None => return (HttpResCode::NoContent, "", &[]),
|
||||
};
|
||||
unwrap(write!(
|
||||
&mut self.res_buf,
|
||||
"><b>{}</b>: {}</div>",
|
||||
msg.author, msg.content
|
||||
))
|
||||
.await;
|
||||
// unwrap(write!(
|
||||
// &mut self.res_buf,
|
||||
// "<div class=\"message\" \
|
||||
// hx-get=\"/chat/message{}\" \
|
||||
// hx-target=\"next .message\" \
|
||||
// hx-swap=\"outerHTML\" \
|
||||
// hx-trigger=\"load\"\
|
||||
// >\
|
||||
// <b>{}</b>: {}\
|
||||
// </div>",
|
||||
// match load {
|
||||
// Some(target) => "hx-get=\"/chat/message{}\" \
|
||||
// hx-target=\"next .message\" \
|
||||
// hx-swap=\"outerHTML\" \
|
||||
// hx-trigger=\"load\"\"
|
||||
// }
|
||||
// (msg_num + 1),
|
||||
// msg.author,
|
||||
// msg.content
|
||||
// ))
|
||||
// .await;
|
||||
return (HttpResCode::Ok, "html", &self.res_buf);
|
||||
} else {
|
||||
(HttpResCode::NotFound, "", &[])
|
||||
@ -170,3 +124,20 @@ struct Message {
|
||||
author: String<16>,
|
||||
content: String<256>,
|
||||
}
|
||||
|
||||
struct Messages {
|
||||
inner: ConstGenericRingBuffer<Message, MEMORY_SIZE>,
|
||||
next: u16,
|
||||
}
|
||||
impl Messages {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
inner: ConstGenericRingBuffer::new(),
|
||||
next: 2,
|
||||
}
|
||||
}
|
||||
fn get_abs(&self, id: u16) -> Option<&Message> {
|
||||
self.inner
|
||||
.get_signed((id as isize) + 1 - (self.next as isize))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user