Alice_DBMS/
grpc_server.rs

1/*                          MIT License
2
3Copyright (c) 2024 Daniil Ermolaev
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all
13copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE. */
22
23pub use tonic::{ transport::Server, Request, Response, Status };
24
25pub mod grpc_proto {
26    tonic::include_proto!("instance");
27}
28
29pub use grpc_proto:: {
30    CreateInstanceRequest  ,CreateInstanceResponse,
31    GetInstanceRequest     ,GetInstanceResponse,
32    GetAllInstancesRequest  ,GetAllInstancesResponse,
33    SignUpRequest          ,SignUpResponse,
34    Token,
35};
36
37pub use grpc_proto::instance_service_server::InstanceServiceServer;
38pub use grpc_proto::instance_service_server::InstanceService;
39
40
41use crate::json_engine::*;
42use crate::instance::InstanceManager;
43
44use std::sync::{ Arc, Mutex };
45
46
47#[derive(Debug, Default)]
48pub struct GRPCInstanceManager {
49    pub instance_manager: Arc<Mutex<InstanceManager>>,
50}
51
52#[tonic::async_trait]
53impl InstanceService for GRPCInstanceManager
54{
55    async fn create_instance(
56        &self, request: Request<CreateInstanceRequest>,
57    ) -> Result<Response<CreateInstanceResponse>, Status> {
58        let inner = request.into_inner();
59        let engine_type = inner.engine_type;
60        let name = inner.name;
61        let mut im = self.instance_manager.lock().unwrap();
62        let id = im.create_instance(&engine_type, &name).unwrap();
63
64        Ok(
65            Response::new( CreateInstanceResponse { instance: id } )
66        )
67    }
68
69    async fn get_instance(
70        &self, request: Request<GetInstanceRequest>,
71    ) -> Result<Response<GetInstanceResponse>, Status> {
72
73        let instance_name = request.into_inner().instance_name;
74        let im = self.instance_manager.lock().unwrap();
75
76        if let Some(instance) = im.get_instance(&instance_name) {
77            return Ok( Response::new(GetInstanceResponse { instance: instance.name.clone(), }));
78        }
79        Err( Status::not_found("Instance not found") )
80    }
81
82    async fn get_all_instances(
83        &self, request: Request<GetAllInstancesRequest>,
84    ) -> Result<Response<GetAllInstancesResponse>, Status> {
85
86        let im = self.instance_manager.lock().unwrap();
87        let mut re_instances: Vec<grpc_proto::Instance> = vec![];
88
89        for instance in &im.instances {
90            re_instances.push(
91                grpc_proto::Instance {
92                    name: instance.name.clone(),
93                    engine: "not implemented".into(),
94                }
95            )
96        }
97        let response = GetAllInstancesResponse { instances: re_instances };
98        Ok( Response::new( response ))
99    }
100
101    async fn sign_up(
102        &self, request: Request<SignUpRequest>,
103    ) -> Result<Response<SignUpResponse>, Status> {
104
105        let inner = request.into_inner();
106        let mut im = self.instance_manager.lock().unwrap();
107
108        let mut key: String = String::new();
109
110        if !im.authenticated_apps.contains_key(&inner.app_name) {
111            key = im.sign_up(inner.app_name);
112        } else {
113            key = "whoops...".to_string();
114        }
115        let response = SignUpResponse { secret_key: key };
116        im.get_all_apps();
117        Ok( Response::new(response) )
118    }
119}
120