Add a message-queue connection to processor

Still needs love, yet should get us closer to starting testing.
This commit is contained in:
Luke Parker
2023-07-17 15:49:15 -04:00
parent 56f7037084
commit 6ccac2d0ab
17 changed files with 184 additions and 81 deletions

View File

@@ -53,6 +53,8 @@ fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>)
// Queue it
(*QUEUES).read().unwrap()[&meta.to].write().unwrap().queue_message(QueuedMessage {
from: meta.from,
// Temporary value which queue_message will override
id: u64::MAX,
msg,
sig: sig.serialize(),
});
@@ -133,6 +135,8 @@ async fn main() {
// Start server
let builder = ServerBuilder::new();
// TODO: Add middleware to check some key is present in the header, making this an authed
// connection
// TODO: Set max request/response size
// 5132 ^ ((b'M' << 8) | b'Q')
let listen_on: &[std::net::SocketAddr] = &["0.0.0.0:2287".parse().unwrap()];
@@ -152,7 +156,7 @@ async fn main() {
.unwrap();
module
.register_method("next", |args, _| {
let args = args.parse::<(Service, u64, Vec<u8>)>().unwrap();
let args = args.parse::<(Service, u64)>().unwrap();
get_next_message(args.0, args.1);
Ok(())
})

View File

@@ -14,6 +14,7 @@ pub enum Service {
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct QueuedMessage {
pub from: Service,
pub id: u64,
pub msg: Vec<u8>,
pub sig: Vec<u8>,
}

View File

@@ -33,8 +33,9 @@ impl<D: Db> Queue<D> {
fn message_key(&self, id: u64) -> Vec<u8> {
Self::key(b"message", serde_json::to_vec(&(self.1, id)).unwrap())
}
pub(crate) fn queue_message(&mut self, msg: QueuedMessage) {
pub(crate) fn queue_message(&mut self, mut msg: QueuedMessage) {
let id = self.message_count();
msg.id = id;
let msg_key = self.message_key(id);
let msg_count_key = self.message_count_key();
@@ -45,7 +46,11 @@ impl<D: Db> Queue<D> {
}
pub(crate) fn get_message(&self, id: u64) -> Option<QueuedMessage> {
self.0.get(self.message_key(id)).map(|bytes| serde_json::from_slice(&bytes).unwrap())
let msg = self.0.get(self.message_key(id)).map(|bytes| serde_json::from_slice(&bytes).unwrap());
if let Some(msg) = msg.as_ref() {
assert_eq!(msg.id, id, "message stored at {id} has ID {}", msg.id);
}
msg
}
pub(crate) fn ack_message(&mut self, id: u64) {