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-06-03 02:38:26 +00:00
|
|
|
use std::convert::Infallible;
|
2020-05-30 21:37:17 +00:00
|
|
|
use tokio::io::{self, AsyncWriteExt};
|
|
|
|
|
2020-06-04 03:05:45 +00:00
|
|
|
pub async fn launch(
|
|
|
|
num_tiles: usize,
|
|
|
|
mut receiver: Receiver<TileData>,
|
|
|
|
_default: DefaultSection,
|
|
|
|
) -> io::Result<Infallible> {
|
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 {
|
2020-06-04 23:47:16 +00:00
|
|
|
let message = receiver.next().await.unwrap();
|
2020-05-30 21:37:17 +00:00
|
|
|
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 serialized = serde_json::to_vec(&blocks).unwrap();
|
|
|
|
stdout.write_all(&serialized).await?;
|
|
|
|
stdout.write_all(b",\n").await?;
|
|
|
|
}
|
|
|
|
}
|