2020-06-04 03:05:45 +00:00
|
|
|
use crate::config::DefaultSection;
|
2020-05-30 21:37:17 +00:00
|
|
|
use crate::tile::TileData;
|
2020-06-04 23:47:16 +00:00
|
|
|
use futures::channel::mpsc::Receiver;
|
|
|
|
use futures::StreamExt;
|
2020-05-30 21:37:17 +00:00
|
|
|
use tokio::io::{self, AsyncWriteExt};
|
|
|
|
|
2020-08-07 19:00:03 +00:00
|
|
|
pub async fn launch<E>(
|
2020-06-04 03:05:45 +00:00
|
|
|
num_tiles: usize,
|
2020-08-07 19:00:03 +00:00
|
|
|
mut receiver: Receiver<Result<TileData, E>>,
|
2020-06-04 03:05:45 +00:00
|
|
|
_default: DefaultSection,
|
2020-08-08 15:32:50 +00:00
|
|
|
) -> io::Result<!>
|
2020-08-07 19:00:03 +00:00
|
|
|
where
|
|
|
|
E: Send + std::fmt::Debug,
|
|
|
|
{
|
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 {
|
2021-08-27 18:37:40 +00:00
|
|
|
match receiver.next().await.unwrap() {
|
|
|
|
Ok(message) => {
|
|
|
|
if message.sender_id < num_tiles {
|
|
|
|
blocks[message.sender_id] = Some(message.block);
|
|
|
|
} else {
|
|
|
|
eprintln!("Invalid message with sender id {}", message.sender_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut serialized = serde_json::to_vec(&blocks).unwrap();
|
|
|
|
serialized.extend_from_slice(b",\n");
|
|
|
|
stdout.write_all(&serialized).await?;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Error in tile: {:?}", err);
|
|
|
|
}
|
2020-05-30 21:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|