Skip to content

Commit

Permalink
reject if app already connected
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertBodziony committed Aug 8, 2023
1 parent 8f9ea03 commit 8c86605
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sdk/apps/base/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Base App tests', () => {
assert(baseApp2.sessionId == sessionId)
})
test('#on("userConnected")', async () => {
const baseApp = await BaseApp.build(testAppBaseInitialize)
const baseApp = await BaseApp.build({ ...testAppBaseInitialize, persistent: false })
expect(baseApp).toBeDefined()
assert(baseApp.sessionId !== '')
const userConnectedFn = vi.fn()
Expand Down
5 changes: 5 additions & 0 deletions sdk/apps/base/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export class BaseApp extends EventEmitter<BaseEvents> {
case 'UserConnectedEvent': {
baseApp.connectedPublicKeys = response.publicKeys
baseApp.emit('userConnected', response)
break
}
case 'AlreadyConnected': {
reject(new Error('Already connected'))
break
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions sdk/bindings/AlreadyConnected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface AlreadyConnected { sessionId: string, }
3 changes: 2 additions & 1 deletion sdk/bindings/ServerToApp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { AckMessage } from "./AckMessage";
import type { AlreadyConnected } from "./AlreadyConnected";
import type { ErrorMessage } from "./ErrorMessage";
import type { InitializeResponse } from "./InitializeResponse";
import type { ResponsePayload } from "./ResponsePayload";
import type { UserConnectedEvent } from "./UserConnectedEvent";
import type { UserDisconnectedEvent } from "./UserDisconnectedEvent";

export type ServerToApp = { type: "InitializeResponse" } & InitializeResponse | { type: "UserConnectedEvent" } & UserConnectedEvent | { type: "UserDisconnectedEvent" } & UserDisconnectedEvent | { type: "ResponsePayload" } & ResponsePayload | { type: "ErrorMessage" } & ErrorMessage | { type: "AckMessage" } & AckMessage;
export type ServerToApp = { type: "InitializeResponse" } & InitializeResponse | { type: "UserConnectedEvent" } & UserConnectedEvent | { type: "UserDisconnectedEvent" } & UserDisconnectedEvent | { type: "ResponsePayload" } & ResponsePayload | { type: "ErrorMessage" } & ErrorMessage | { type: "AckMessage" } & AckMessage | { type: "AlreadyConnected" } & AlreadyConnected;
3 changes: 3 additions & 0 deletions server/bindings/AlreadyConnected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface AlreadyConnected { sessionId: string, }
7 changes: 1 addition & 6 deletions server/bindings/InitializeResponse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface InitializeResponse {
responseId: string
sessionId: string
createdNew: boolean
publicKeys: Array<string>
}
export interface InitializeResponse { responseId: string, sessionId: string, createdNew: boolean, publicKeys: Array<string>, }
3 changes: 2 additions & 1 deletion server/bindings/ServerToApp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { AckMessage } from "./AckMessage";
import type { AlreadyConnected } from "./AlreadyConnected";
import type { ErrorMessage } from "./ErrorMessage";
import type { InitializeResponse } from "./InitializeResponse";
import type { ResponsePayload } from "./ResponsePayload";
import type { UserConnectedEvent } from "./UserConnectedEvent";
import type { UserDisconnectedEvent } from "./UserDisconnectedEvent";

export type ServerToApp = { type: "InitializeResponse" } & InitializeResponse | { type: "UserConnectedEvent" } & UserConnectedEvent | { type: "UserDisconnectedEvent" } & UserDisconnectedEvent | { type: "ResponsePayload" } & ResponsePayload | { type: "ErrorMessage" } & ErrorMessage | { type: "AckMessage" } & AckMessage;
export type ServerToApp = { type: "InitializeResponse" } & InitializeResponse | { type: "UserConnectedEvent" } & UserConnectedEvent | { type: "UserDisconnectedEvent" } & UserDisconnectedEvent | { type: "ResponsePayload" } & ResponsePayload | { type: "ErrorMessage" } & ErrorMessage | { type: "AckMessage" } & AckMessage | { type: "AlreadyConnected" } & AlreadyConnected;
26 changes: 22 additions & 4 deletions server/src/app/app_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use axum::{
},
response::Response,
};
use futures::StreamExt;
use futures::{SinkExt, StreamExt};
use log::warn;

use crate::{
state::{ClientSockets, ClientToSessions, ModifySession, SendToClient, Sessions},
structs::{
app_messages::{
already_connected::AlreadyConnected,
app_messages::{AppToServer, ServerToApp},
initialize::InitializeResponse,
},
Expand Down Expand Up @@ -45,7 +47,7 @@ pub async fn app_handler(
client_sockets: ClientSockets,
client_to_sessions: ClientToSessions,
) {
let (sender, mut receiver) = socket.split();
let (mut sender, mut receiver) = socket.split();
// Handle the new app connection here
// Wait for initialize message
let session_id = loop {
Expand Down Expand Up @@ -85,8 +87,24 @@ pub async fn app_handler(
let mut sessions = sessions.write().await;
match sessions.get_mut(session_id.as_str()) {
Some(mut session) => {
// Close previous app socket
session.close_app_socket().await;
// Do not allow to connect to the same session twice
if session.app_state.app_socket.is_some() {
warn!("App tried to connect to the same session twice");
let response =
ServerToApp::AlreadyConnected(AlreadyConnected {
session_id: session_id.clone(),
});
sender
.send(Message::Text(
serde_json::to_string(&response)
.expect("Serialization should work"),
))
.await
.unwrap_or_default();
sender.close().await.unwrap_or_default();
return;
}

session.update_status(SessionStatus::AppConnected);
session.app_state = AppState {
metadata: init_data.app_metadata,
Expand Down
9 changes: 9 additions & 0 deletions server/src/structs/app_messages/already_connected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};
use ts_rs::TS;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct AlreadyConnected {
#[serde(rename = "sessionId")]
pub session_id: String,
}
2 changes: 2 additions & 0 deletions server/src/structs/app_messages/app_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ts_rs::TS;
use crate::structs::common::{AckMessage, ErrorMessage};

use super::{
already_connected::AlreadyConnected,
initialize::{InitializeRequest, InitializeResponse},
payload::{RequestPayload, ResponsePayload},
user_connected_event::UserConnectedEvent,
Expand All @@ -27,4 +28,5 @@ pub enum ServerToApp {
ResponsePayload(ResponsePayload),
ErrorMessage(ErrorMessage),
AckMessage(AckMessage),
AlreadyConnected(AlreadyConnected),
}
1 change: 1 addition & 0 deletions server/src/structs/app_messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod already_connected;
pub mod app_messages;
pub mod initialize;
pub mod payload;
Expand Down

0 comments on commit 8c86605

Please sign in to comment.