implement into_join_handle for RemoteBotHandle

This commit is contained in:
Ilion Beyst 2022-09-28 07:12:36 +02:00
parent f777baa4f2
commit c4f78f2dad

View file

@ -13,6 +13,7 @@ use std::time::Duration;
use runner::match_context::{EventBus, PlayerHandle, RequestError, RequestMessage}; use runner::match_context::{EventBus, PlayerHandle, RequestError, RequestMessage};
use runner::match_log::MatchLogger; use runner::match_log::MatchLogger;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
use tonic; use tonic;
use tonic::transport::Server; use tonic::transport::Server;
@ -252,13 +253,18 @@ impl runner::BotSpec for RemoteBotSpec {
let client_messages_future = let client_messages_future =
tokio::time::timeout(Duration::from_secs(10), client_messages_promise.get_value()); tokio::time::timeout(Duration::from_secs(10), client_messages_promise.get_value());
if let Ok(Ok(client_messages)) = client_messages_future.await { let join_handle = if let Ok(Ok(client_messages)) = client_messages_future.await {
tokio::spawn(handle_bot_messages( tokio::spawn(handle_bot_messages(
player_id, player_id,
event_bus.clone(), event_bus.clone(),
client_messages, client_messages,
)); ))
} } else {
// since we don't have a mechanism for bot failure yet,
// this hack will have to do for now.
// TODO: FIXME
tokio::spawn(futures::future::ready(()))
};
// ensure router cleanup // ensure router cleanup
self.router.take(&self.player_key); self.router.take(&self.player_key);
@ -271,6 +277,7 @@ impl runner::BotSpec for RemoteBotSpec {
sender: server_msg_snd, sender: server_msg_snd,
player_id, player_id,
event_bus, event_bus,
join_handle,
}) })
} }
} }
@ -299,6 +306,7 @@ struct RemoteBotHandle {
sender: mpsc::UnboundedSender<Result<pb::PlayerApiServerMessage, Status>>, sender: mpsc::UnboundedSender<Result<pb::PlayerApiServerMessage, Status>>,
player_id: u32, player_id: u32,
event_bus: Arc<Mutex<EventBus>>, event_bus: Arc<Mutex<EventBus>>,
join_handle: JoinHandle<()>,
} }
impl PlayerHandle for RemoteBotHandle { impl PlayerHandle for RemoteBotHandle {
@ -335,6 +343,10 @@ impl PlayerHandle for RemoteBotHandle {
} }
} }
} }
fn into_join_handle(self: Box<Self>) -> JoinHandle<()> {
self.join_handle
}
} }
// TODO: this will spawn a task for every request, which might not be ideal. // TODO: this will spawn a task for every request, which might not be ideal.