-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e82289
commit 4c8bee1
Showing
6 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use gamercade_interface::game::{game_service_client::GameServiceClient, UpdateGameRequest}; | ||
use tokio::sync::OnceCell; | ||
use tonic::{transport::Channel, Request}; | ||
|
||
use crate::urls::SERVICE_IP_GRPC; | ||
|
||
use super::{TaskManager, TaskRequest}; | ||
|
||
pub type GameManager = TaskManager<GameManagerState, GameRequest>; | ||
|
||
async fn init_game_client() -> GameServiceClient<Channel> { | ||
GameServiceClient::connect(SERVICE_IP_GRPC).await.unwrap() | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct GameManagerState { | ||
client: OnceCell<GameServiceClient<Channel>>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum GameRequest { | ||
CreateGame(UpdateGameRequest), | ||
UpdateGame(UpdateGameRequest), | ||
} | ||
|
||
impl TaskRequest<GameManagerState> for GameRequest { | ||
async fn handle_request( | ||
self, | ||
sender: &tokio::sync::mpsc::Sender<super::TaskNotification>, | ||
state: &tokio::sync::Mutex<GameManagerState>, | ||
) { | ||
let mut lock = state.lock().await; | ||
lock.client.get_or_init(init_game_client).await; | ||
let client = lock.client.get_mut().unwrap(); | ||
|
||
let result = match self { | ||
GameRequest::CreateGame(request) => client.create_game(request).await, | ||
GameRequest::UpdateGame(request) => client.update_game(request).await, | ||
}; | ||
|
||
match result { | ||
Ok(game_info) => println!("Got game info: {game_info:?}"), | ||
Err(e) => println!("{e}"), | ||
} | ||
} | ||
} | ||
|
||
impl GameManager { | ||
pub fn send(&mut self, request: GameRequest) { | ||
self.sender.try_send(request).unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
use gamercade_interface::{Session, SESSION_METADATA_KEY}; | ||
use tonic::{metadata::MetadataValue, Request}; | ||
|
||
pub const SERVICE_IP_GRPC: &str = "http://127.0.0.1:50051"; | ||
pub const SERVICE_IP_HTTP: &str = "http://127.0.0.1:3000"; | ||
|
||
pub fn download_release_url(game_id: u64, release_id: u64) -> String { | ||
format!("{SERVICE_IP_HTTP}/games/{game_id}/releases/{release_id}") | ||
} | ||
|
||
pub fn authorized_request<T>(request: T, session: Session) -> Request<T> { | ||
let mut request = Request::new(request); | ||
request.metadata_mut().insert_bin( | ||
SESSION_METADATA_KEY, | ||
MetadataValue::from_bytes(session.bytes()), | ||
); | ||
request | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters