2023-12-14 05:34:58 +00:00
|
|
|
use crate::tile::{Block, TileData};
|
|
|
|
use eyre::OptionExt;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::{
|
|
|
|
io::{self, AsyncWriteExt},
|
|
|
|
sync::mpsc,
|
|
|
|
};
|
|
|
|
use uuid::Uuid;
|
2020-05-30 21:37:17 +00:00
|
|
|
|
2023-12-14 05:34:58 +00:00
|
|
|
pub struct OutputChannel {
|
|
|
|
sender: mpsc::Sender<TileData>,
|
|
|
|
sender_id: usize,
|
|
|
|
instance: Arc<str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutputChannel {
|
|
|
|
pub fn with_random_uuid(sender: mpsc::Sender<TileData>, sender_id: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
sender,
|
|
|
|
sender_id,
|
|
|
|
instance: Uuid::new_v4().to_string().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send(&self, mut block: Block) -> Result<(), mpsc::error::SendError<TileData>> {
|
|
|
|
block.instance = self.instance.clone();
|
|
|
|
self.sender
|
|
|
|
.send(TileData {
|
|
|
|
block,
|
|
|
|
sender_id: self.sender_id,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(num_tiles: usize, mut receiver: mpsc::Receiver<TileData>) -> eyre::Result<()> {
|
2020-05-30 21:37:17 +00:00
|
|
|
let mut stdout = io::stdout();
|
|
|
|
stdout.write_all(b"{ \"version\": 1 }\n[").await?;
|
|
|
|
|
|
|
|
let mut blocks = Vec::new();
|
|
|
|
blocks.resize_with(num_tiles, Default::default);
|
|
|
|
loop {
|
2023-12-14 05:34:58 +00:00
|
|
|
let message = receiver
|
|
|
|
.recv()
|
|
|
|
.await
|
|
|
|
.ok_or_eyre("No more messages to recieve")?;
|
|
|
|
|
|
|
|
if message.sender_id < num_tiles {
|
|
|
|
blocks[message.sender_id] = Some(message.block);
|
|
|
|
} else {
|
|
|
|
eprintln!("Invalid message with sender id {}", message.sender_id);
|
|
|
|
continue;
|
2020-05-30 21:37:17 +00:00
|
|
|
}
|
2023-12-14 05:34:58 +00:00
|
|
|
let mut serialized = serde_json::to_vec(&blocks).unwrap();
|
|
|
|
serialized.extend_from_slice(b",\n");
|
|
|
|
stdout.write_all(&serialized).await?;
|
2020-05-30 21:37:17 +00:00
|
|
|
}
|
|
|
|
}
|