Skip to content

Commit

Permalink
feat(junowen-server): add reserved-room
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed Nov 27, 2023
1 parent 685ce2a commit cbbd719
Show file tree
Hide file tree
Showing 13 changed files with 819 additions and 5 deletions.
1 change: 1 addition & 0 deletions junowen-lib/src/signaling_server.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod custom;
pub mod reserved_room;
pub mod room;
148 changes: 148 additions & 0 deletions junowen-lib/src/signaling_server/reserved_room.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use anyhow::bail;
use anyhow::Result;
use derive_new::new;
use getset::Getters;
use http::StatusCode;
use serde::Deserialize;
use serde::Serialize;

use crate::connection::signaling::CompressedSdp;

use super::room::PostRoomKeepResponse;
use super::room::PutRoomResponse;

// PUT /reserved-room/{name}

#[derive(Debug, Deserialize, Serialize, new)]
pub struct PutReservedRoomResponseConflictBody {
opponent_offer: Option<CompressedSdp>,
}

impl PutReservedRoomResponseConflictBody {
pub fn into_offer(self) -> Option<CompressedSdp> {
self.opponent_offer
}
}

pub type PutReservedRoomResponse = PutRoomResponse<PutReservedRoomResponseConflictBody>;

// GET /reserved-room/{name}

#[derive(Debug, Deserialize, Serialize, new)]
pub struct GetReservedRoomResponseOkBody {
opponent_offer: Option<CompressedSdp>,
spectator_offer: Option<CompressedSdp>,
}

impl GetReservedRoomResponseOkBody {
pub fn opponent_offer(&self) -> Option<&CompressedSdp> {
self.opponent_offer.as_ref()
}

pub fn into_spectator_offer(self) -> Option<CompressedSdp> {
self.spectator_offer
}
}

pub enum GetReservedRoomResponse {
Ok(GetReservedRoomResponseOkBody),
NotFound,
}

impl GetReservedRoomResponse {
pub fn parse(status: StatusCode, text: Option<&str>) -> Result<Self> {
match (status, text) {
(StatusCode::OK, Some(text)) => {
if let Ok(body) = serde_json::from_str::<GetReservedRoomResponseOkBody>(text) {
return Ok(Self::Ok(body));
}
}
(StatusCode::NOT_FOUND, _) => return Ok(Self::NotFound),
_ => {}
}
bail!("invalid response")
}

pub fn status_code(&self) -> StatusCode {
match self {
Self::Ok(_) => StatusCode::OK,
Self::NotFound => StatusCode::NOT_FOUND,
}
}

pub fn to_body(&self) -> Option<String> {
match self {
Self::Ok(body) => Some(serde_json::to_string(&body).unwrap()),
Self::NotFound => None,
}
}
}

// POST /reserved-room/{name}/keep

#[derive(Deserialize, Serialize, Getters, new)]
pub struct PostReservedRoomKeepRequestBody {
key: String,
spectator_offer: Option<String>,
}

impl PostReservedRoomKeepRequestBody {
pub fn into_inner(self) -> (String, Option<String>) {
(self.key, self.spectator_offer)
}
}

#[derive(Debug, Deserialize, Serialize, new)]
pub struct PostReservedRoomKeepResponseOkOpponentAnswerBody {
opponent_answer: CompressedSdp,
}

impl PostReservedRoomKeepResponseOkOpponentAnswerBody {
pub fn into_opponent_answer(self) -> CompressedSdp {
self.opponent_answer
}
}

#[derive(Debug, Deserialize, Serialize, new)]
pub struct PostReservedRoomKeepResponseOkSpectatorAnswerBody {
spectator_answer: CompressedSdp,
}

impl PostReservedRoomKeepResponseOkSpectatorAnswerBody {
pub fn into_spectator_answer(self) -> CompressedSdp {
self.spectator_answer
}
}

#[derive(Debug, Deserialize, Serialize)]
pub enum PostReservedRoomKeepResponseOkBody {
OpponentAnswer(PostReservedRoomKeepResponseOkOpponentAnswerBody),
SpectatorAnswer(PostReservedRoomKeepResponseOkSpectatorAnswerBody),
}

impl From<PostReservedRoomKeepResponseOkOpponentAnswerBody> for PostReservedRoomKeepResponseOkBody {
fn from(body: PostReservedRoomKeepResponseOkOpponentAnswerBody) -> Self {
Self::OpponentAnswer(body)
}
}

impl From<PostReservedRoomKeepResponseOkSpectatorAnswerBody>
for PostReservedRoomKeepResponseOkBody
{
fn from(body: PostReservedRoomKeepResponseOkSpectatorAnswerBody) -> Self {
Self::SpectatorAnswer(body)
}
}

pub type PostReservedRoomKeepResponse = PostRoomKeepResponse<PostReservedRoomKeepResponseOkBody>;

impl From<PostReservedRoomKeepResponseOkBody> for PostReservedRoomKeepResponse {
fn from(body: PostReservedRoomKeepResponseOkBody) -> Self {
Self::Ok(body)
}
}

// POST /reserved-room/{name}/join

pub use super::room::PostRoomJoinRequestBody as PostReservedRoomSpectateRequestBody;
pub use super::room::PostRoomJoinResponse as PostReservedRoomSpectateResponse;
6 changes: 4 additions & 2 deletions junowen-server/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# junowen-server

## create

```
```sh
cargo lambda deploy \
--binary-name junowen-server \
--enable-function-url \
Expand All @@ -12,7 +14,7 @@ cargo lambda deploy \
## Dynamo DB definition

* env = dev | prod
* table_name = Offer | Answer
* table_name = Offer | Answer | ReservedRoom | ReservedRoomOpponentAnswer | ReservedRoomSpectatorAnswer

### {env}.{table_name}

Expand Down
73 changes: 71 additions & 2 deletions junowen-server/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use dynamodb::DynamoDB;
pub use file::File;

use anyhow::Result;
use getset::Getters;
use getset::{Getters, Setters};
use junowen_lib::connection::signaling::CompressedSdp;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -73,4 +73,73 @@ pub trait SharedRoomTables: Send + Sync + 'static {
) -> Result<Option<SharedRoomOpponentAnswer>>;
}

pub trait Database: SharedRoomTables {}
#[derive(Clone, Debug, Deserialize, Getters, Setters, Serialize, new)]
pub struct ReservedRoom {
/// primary
#[get = "pub"]
name: String,
/// ルームの所有者であることを証明する為のキー
#[get = "pub"]
key: String,
#[getset(get = "pub", set = "pub")]
opponent_offer_sdp: Option<CompressedSdp>,
#[get = "pub"]
spectator_offer_sdp: Option<CompressedSdp>,
ttl_sec: u64,
}

impl ReservedRoom {
pub fn into_opponent_offer_sdp(self) -> Option<CompressedSdp> {
self.opponent_offer_sdp
}

pub fn into_opponent_offer_sdp_spectator_offer_sdp(
self,
) -> (Option<CompressedSdp>, Option<CompressedSdp>) {
(self.opponent_offer_sdp, self.spectator_offer_sdp)
}

pub fn is_expired(&self, now_sec: u64) -> bool {
now_sec > self.ttl_sec
}
}

#[derive(Serialize, Deserialize)]
pub struct ReservedRoomOpponentAnswer(pub Answer);
#[derive(Serialize, Deserialize)]
pub struct ReservedRoomSpectatorAnswer(pub Answer);

pub trait ReservedRoomTables: Send + Sync + 'static {
async fn put_room(&self, offer: ReservedRoom) -> Result<(), PutError>;
async fn find_room(&self, name: String) -> Result<Option<ReservedRoom>>;
async fn keep_room(
&self,
name: String,
key: String,
spectator_offer_sdp: Option<String>,
ttl_sec: u64,
) -> Result<Option<ReservedRoom>>;
async fn remove_opponent_offer_sdp_in_room(&self, name: String) -> Result<bool>;
async fn remove_spectator_offer_sdp_in_room(&self, name: String) -> Result<bool>;
async fn remove_room(&self, name: String, key: Option<String>) -> Result<bool>;

async fn put_room_opponent_answer(
&self,
answer: ReservedRoomOpponentAnswer,
) -> Result<(), PutError>;
async fn remove_room_opponent_answer(
&self,
name: String,
) -> Result<Option<ReservedRoomOpponentAnswer>>;

async fn put_room_spectator_answer(
&self,
answer: ReservedRoomSpectatorAnswer,
) -> Result<(), PutError>;
async fn remove_room_spectator_answer(
&self,
name: String,
) -> Result<Option<ReservedRoomSpectatorAnswer>>;
}

pub trait Database: SharedRoomTables + ReservedRoomTables {}
13 changes: 13 additions & 0 deletions junowen-server/src/database/dynamodb.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod reserved_room;
mod shared_room;

use std::env;
Expand All @@ -16,6 +17,9 @@ pub struct DynamoDB {
client: aws_sdk_dynamodb::Client,
table_name_shared_room: String,
table_name_shared_room_opponent_answer: String,
table_name_reserved_room: String,
table_name_reserved_room_opponent_answer: String,
table_name_reserved_room_spectator_answer: String,
}

impl DynamoDB {
Expand All @@ -25,6 +29,15 @@ impl DynamoDB {
client: aws_sdk_dynamodb::Client::new(&config),
table_name_shared_room: format!("{}.Offer", env::var("ENV").unwrap()),
table_name_shared_room_opponent_answer: format!("{}.Answer", env::var("ENV").unwrap()),
table_name_reserved_room: format!("{}.ReservedRoom", env::var("ENV").unwrap()),
table_name_reserved_room_opponent_answer: format!(
"{}.ReservedRoomOpponentAnswer",
env::var("ENV").unwrap()
),
table_name_reserved_room_spectator_answer: format!(
"{}.ReservedRoomSpectatorAnswer",
env::var("ENV").unwrap()
),
}
}

Expand Down
Loading

0 comments on commit cbbd719

Please sign in to comment.