2022-07-31 13:47:18 +02:00
|
|
|
pub mod proto {
|
|
|
|
tonic::include_proto!("session");
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-08-03 18:51:56 +02:00
|
|
|
use tracing::debug;
|
2022-07-31 13:47:18 +02:00
|
|
|
|
2022-08-03 18:51:56 +02:00
|
|
|
use tonic::{Request, Response, Status};
|
2022-07-31 13:47:18 +02:00
|
|
|
|
2022-08-03 18:51:56 +02:00
|
|
|
use proto::session_server::Session;
|
2022-07-31 13:47:18 +02:00
|
|
|
use proto::{SessionRequest, SessionResponse};
|
|
|
|
|
|
|
|
use crate::actor::{
|
|
|
|
state::{AlterState, StateManager},
|
|
|
|
workspace::Workspace as WorkspaceInstance, // TODO fuck x2!
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SessionService {
|
|
|
|
state: Arc<StateManager>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tonic::async_trait]
|
|
|
|
impl Session for SessionService {
|
|
|
|
async fn create(
|
|
|
|
&self,
|
|
|
|
request: Request<SessionRequest>,
|
|
|
|
) -> Result<Response<SessionResponse>, Status> {
|
|
|
|
debug!("create request: {:?}", request);
|
|
|
|
let r = request.into_inner();
|
|
|
|
|
2022-08-03 18:51:56 +02:00
|
|
|
let _w = WorkspaceInstance::new(r.session_key.clone());
|
2022-07-31 13:47:18 +02:00
|
|
|
|
|
|
|
let reply = proto::SessionResponse {
|
|
|
|
session_key: r.session_key.clone(),
|
|
|
|
accepted: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// self.tx.send(AlterState::ADD{key: r.session_key.clone(), w}).await.unwrap();
|
|
|
|
|
|
|
|
Ok(Response::new(reply))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn join(
|
|
|
|
&self,
|
|
|
|
request: Request<SessionRequest>,
|
|
|
|
) -> Result<Response<SessionResponse>, Status> {
|
|
|
|
debug!("join request: {:?}", request);
|
|
|
|
|
|
|
|
let reply = proto::SessionResponse {
|
|
|
|
session_key: request.into_inner().session_key,
|
|
|
|
accepted: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Response::new(reply))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn leave(
|
|
|
|
&self,
|
|
|
|
request: Request<SessionRequest>,
|
|
|
|
) -> Result<Response<SessionResponse>, Status> {
|
|
|
|
debug!("leave request: {:?}", request);
|
|
|
|
let r = request.into_inner();
|
|
|
|
let mut removed = false;
|
|
|
|
|
2022-08-03 18:51:56 +02:00
|
|
|
if self.state.workspaces_ref().get(&r.session_key).is_some() {
|
2022-07-31 13:47:18 +02:00
|
|
|
self.state
|
2022-08-03 18:51:56 +02:00
|
|
|
.op(AlterState::REMOVE {
|
2022-07-31 13:47:18 +02:00
|
|
|
key: r.session_key.clone(),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
removed = true; // TODO this is a lie! Verify it
|
|
|
|
}
|
|
|
|
|
|
|
|
let reply = proto::SessionResponse {
|
|
|
|
session_key: r.session_key,
|
|
|
|
accepted: removed,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Response::new(reply))
|
|
|
|
}
|
|
|
|
}
|