feat: implemented basic session flow on server

Co-authored-by: f-tlm <f-tlm@users.noreply.github.com>
This commit is contained in:
əlemi 2022-07-13 01:58:34 +02:00
parent 7272879180
commit 420ca3e224

View file

@ -1,60 +1,127 @@
use std::{collections::HashMap, sync::Arc};
use state::{StateManager, AlterState};
use tonic::Streaming;
use tonic::codegen::futures_core::Stream;
use tonic::{transport::Server, Request, Response, Status}; use tonic::{transport::Server, Request, Response, Status};
pub mod proto_core { pub mod proto {
tonic::include_proto!("core"); tonic::include_proto!("workspace");
} }
use proto_core::session_server::{Session, SessionServer}; use tokio::sync::{mpsc, watch};
use proto_core::{SessionRequest, SessionResponse};
use proto::workspace_server::{Workspace, WorkspaceServer};
use proto::{SessionRequest, SessionResponse};
#[derive(Debug, Default)] use crate::workspace::Workspace as WorkspaceInstance; // TODO fuck!
pub struct TestSession {}
pub mod workspace;
pub mod state;
#[derive(Debug)]
pub struct WorkspaceService {
tx: mpsc::Sender<AlterState>,
rx: watch::Receiver<HashMap<String, Arc<WorkspaceInstance>>>
}
#[tonic::async_trait] #[tonic::async_trait]
impl Session for TestSession { impl Workspace for WorkspaceService {
async fn create( async fn create(
&self, &self,
request: Request<SessionRequest>, request: Request<SessionRequest>,
) -> Result<Response<SessionResponse>, Status> { ) -> Result<Response<SessionResponse>, Status> {
println!("Got a request: {:?}", request); println!("Got a request: {:?}", request);
let r = request.into_inner();
let reply = proto_core::SessionResponse { let w = WorkspaceInstance::new(r.session_key.clone(), r.content.unwrap_or("".to_string()));
session_id: request.into_inner().session_id,
let reply = proto::SessionResponse {
session_key: r.session_key.clone(),
accepted: true,
content: Some(w.content.clone()),
hash: None,
};
self.tx.send(AlterState::ADD{key: r.session_key.clone(), w}).await.unwrap();
Ok(Response::new(reply))
}
async fn sync(
&self,
request: Request<SessionRequest>,
) -> Result<Response<SessionResponse>, Status> {
println!("Got a request: {:?}", request);
let r = request.into_inner();
if let Some(w) = self.rx.borrow().get(&r.session_key) {
let reply = proto::SessionResponse {
session_key: r.session_key,
accepted: true,
content: Some(w.content.clone()),
hash: None,
};
Ok(Response::new(reply))
} else {
Err(Status::out_of_range("fuck you".to_string()))
}
}
// TODO make it do something
async fn join(
&self,
request: Request<SessionRequest>,
) -> Result<Response<SessionResponse>, Status> {
println!("Got a request: {:?}", request);
let reply = proto::SessionResponse {
session_key: request.into_inner().session_key,
accepted: true,
content: None,
hash: None,
}; };
Ok(Response::new(reply)) Ok(Response::new(reply))
} }
async fn leave(
&self,
request: Request<SessionRequest>,
) -> Result<Response<SessionResponse>, Status> {
println!("Got a request: {:?}", request);
let r = request.into_inner();
let mut removed = false;
if self.rx.borrow().get(&r.session_key).is_some() {
self.tx.send(AlterState::REMOVE { 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,
content: None,
hash: None,
};
Ok(Response::new(reply))
}
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?; let addr = "[::1]:50051".parse()?;
let greeter = TestSession::default();
let (tx, rx) = state::run_state_manager();
let greeter = WorkspaceService { tx, rx };
Server::builder() Server::builder()
.add_service(SessionServer::new(greeter)) .add_service(WorkspaceServer::new(greeter))
.serve(addr) .serve(addr)
.await?; .await?;
Ok(()) Ok(())
} }
/*
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}
*/