2023-07-18 05:53:51 +00:00
|
|
|
use std::sync::Arc;
|
2023-05-10 03:44:41 +00:00
|
|
|
|
|
|
|
use serai_client::primitives::NetworkId;
|
|
|
|
use processor_messages::{ProcessorMessage, CoordinatorMessage};
|
|
|
|
|
2023-07-18 05:53:51 +00:00
|
|
|
use message_queue::{Service, Metadata, client::MessageQueue};
|
|
|
|
|
2023-05-10 03:44:41 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Message {
|
|
|
|
pub id: u64,
|
|
|
|
pub network: NetworkId,
|
|
|
|
pub msg: ProcessorMessage,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
pub trait Processors: 'static + Send + Sync + Clone {
|
2023-09-29 08:19:59 +00:00
|
|
|
async fn send(&self, network: NetworkId, msg: impl Send + Into<CoordinatorMessage>);
|
2023-09-27 16:20:57 +00:00
|
|
|
async fn recv(&mut self, network: NetworkId) -> Message;
|
2023-05-10 03:44:41 +00:00
|
|
|
async fn ack(&mut self, msg: Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2023-07-18 05:53:51 +00:00
|
|
|
impl Processors for Arc<MessageQueue> {
|
2023-09-29 08:19:59 +00:00
|
|
|
async fn send(&self, network: NetworkId, msg: impl Send + Into<CoordinatorMessage>) {
|
|
|
|
let msg: CoordinatorMessage = msg.into();
|
2023-07-18 05:53:51 +00:00
|
|
|
let metadata =
|
|
|
|
Metadata { from: self.service, to: Service::Processor(network), intent: msg.intent() };
|
|
|
|
let msg = serde_json::to_string(&msg).unwrap();
|
|
|
|
self.queue(metadata, msg.into_bytes()).await;
|
2023-05-10 03:44:41 +00:00
|
|
|
}
|
2023-09-27 16:20:57 +00:00
|
|
|
async fn recv(&mut self, network: NetworkId) -> Message {
|
|
|
|
let msg = self.next(Service::Processor(network)).await;
|
|
|
|
assert_eq!(msg.from, Service::Processor(network));
|
2023-07-18 05:53:51 +00:00
|
|
|
|
|
|
|
let id = msg.id;
|
|
|
|
|
|
|
|
// Deserialize it into a ProcessorMessage
|
|
|
|
let msg: ProcessorMessage =
|
|
|
|
serde_json::from_slice(&msg.msg).expect("message wasn't a JSON-encoded ProcessorMessage");
|
|
|
|
|
|
|
|
return Message { id, network, msg };
|
2023-05-10 03:44:41 +00:00
|
|
|
}
|
2023-07-18 05:53:51 +00:00
|
|
|
async fn ack(&mut self, msg: Message) {
|
2023-09-27 16:20:57 +00:00
|
|
|
MessageQueue::ack(self, Service::Processor(msg.network), msg.id).await
|
2023-05-10 03:44:41 +00:00
|
|
|
}
|
|
|
|
}
|