wrap bot api in oneof for extendability

This commit is contained in:
Ilion Beyst 2022-07-20 23:21:06 +02:00
parent f058000072
commit 73c536b4a6
2 changed files with 38 additions and 23 deletions

View file

@ -66,12 +66,12 @@ impl PlayerRouter {
#[tonic::async_trait] #[tonic::async_trait]
impl pb::bot_api_service_server::BotApiService for BotApiServer { impl pb::bot_api_service_server::BotApiService for BotApiServer {
type ConnectBotStream = UnboundedReceiverStream<Result<pb::PlayerRequest, Status>>; type ConnectPlayerStream = UnboundedReceiverStream<Result<pb::ServerMessage, Status>>;
async fn connect_bot( async fn connect_player(
&self, &self,
req: Request<Streaming<pb::PlayerRequestResponse>>, req: Request<Streaming<pb::ClientMessage>>,
) -> Result<Response<Self::ConnectBotStream>, Status> { ) -> Result<Response<Self::ConnectPlayerStream>, Status> {
// TODO: clean up errors // TODO: clean up errors
let player_key = req let player_key = req
.metadata() .metadata()
@ -141,8 +141,8 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer {
// TODO: please rename me // TODO: please rename me
struct SyncThingData { struct SyncThingData {
tx: oneshot::Sender<Streaming<pb::PlayerRequestResponse>>, tx: oneshot::Sender<Streaming<pb::ClientMessage>>,
server_messages: mpsc::UnboundedReceiver<Result<pb::PlayerRequest, Status>>, server_messages: mpsc::UnboundedReceiver<Result<pb::ServerMessage, Status>>,
} }
struct RemoteBotSpec { struct RemoteBotSpec {
@ -199,29 +199,41 @@ impl runner::BotSpec for RemoteBotSpec {
async fn handle_bot_messages( async fn handle_bot_messages(
player_id: u32, player_id: u32,
event_bus: Arc<Mutex<EventBus>>, event_bus: Arc<Mutex<EventBus>>,
mut messages: Streaming<pb::PlayerRequestResponse>, mut messages: Streaming<pb::ClientMessage>,
) { ) {
// TODO: can this be writte nmore nicely?
while let Some(message) = messages.message().await.unwrap() { while let Some(message) = messages.message().await.unwrap() {
let request_id = (player_id, message.request_id as u32); match message.client_message {
Some(pb::client_message::ClientMessage::RequestResponse(resp)) => {
let request_id = (player_id, resp.request_id as u32);
event_bus event_bus
.lock() .lock()
.unwrap() .unwrap()
.resolve_request(request_id, Ok(message.content)); .resolve_request(request_id, Ok(resp.content));
}
_ => (),
}
} }
} }
struct RemoteBotHandle { struct RemoteBotHandle {
sender: mpsc::UnboundedSender<Result<pb::PlayerRequest, Status>>, sender: mpsc::UnboundedSender<Result<pb::ServerMessage, Status>>,
player_id: u32, player_id: u32,
event_bus: Arc<Mutex<EventBus>>, event_bus: Arc<Mutex<EventBus>>,
} }
impl PlayerHandle for RemoteBotHandle { impl PlayerHandle for RemoteBotHandle {
fn send_request(&mut self, r: RequestMessage) { fn send_request(&mut self, r: RequestMessage) {
let res = self.sender.send(Ok(pb::PlayerRequest { let req = pb::PlayerRequest {
request_id: r.request_id as i32, request_id: r.request_id as i32,
content: r.content, content: r.content,
})); };
let server_message = pb::ServerMessage {
server_message: Some(pb::server_message::ServerMessage::PlayerRequest(req)),
};
let res = self.sender.send(Ok(server_message));
match res { match res {
Ok(()) => { Ok(()) => {
// schedule a timeout. See comments at method implementation // schedule a timeout. See comments at method implementation

View file

@ -2,19 +2,22 @@ syntax = "proto3";
package grpc.planetwars.bot_api; package grpc.planetwars.bot_api;
message Hello { message ServerMessage {
string hello_message = 1; oneof server_message {
PlayerRequest player_request = 1;
}
} }
message HelloResponse {
string response = 1;
}
message PlayerRequest { message PlayerRequest {
int32 request_id = 1; int32 request_id = 1;
bytes content = 2; bytes content = 2;
} }
message ClientMessage {
oneof client_message {
PlayerRequestResponse request_response = 1;
}
}
message PlayerRequestResponse { message PlayerRequestResponse {
int32 request_id = 1; int32 request_id = 1;
bytes content = 2; bytes content = 2;
@ -32,5 +35,5 @@ message CreatedMatch {
service BotApiService { service BotApiService {
rpc CreateMatch(MatchRequest) returns (CreatedMatch); rpc CreateMatch(MatchRequest) returns (CreatedMatch);
// server sends requests to the player, player responds // server sends requests to the player, player responds
rpc ConnectBot(stream PlayerRequestResponse) returns (stream PlayerRequest); rpc ConnectPlayer(stream ClientMessage) returns (stream ServerMessage);
} }