engula_cooperator/
server.rs

1// Copyright 2022 The Engula Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use engula_apis::*;
16use engula_supervisor::Supervisor;
17use tonic::{Request, Response, Status};
18
19use crate::{apis::*, Universe};
20
21#[derive(Clone)]
22pub struct Server {
23    uv: Universe,
24}
25
26impl Server {
27    pub fn new(supervisor: Supervisor) -> Self {
28        Self {
29            uv: Universe::new(supervisor),
30        }
31    }
32
33    pub fn into_service(self) -> cooperator_server::CooperatorServer<Self> {
34        cooperator_server::CooperatorServer::new(self)
35    }
36}
37
38#[tonic::async_trait]
39impl cooperator_server::Cooperator for Server {
40    async fn txn(&self, req: Request<TxnRequest>) -> Result<Response<TxnResponse>, Status> {
41        let req = req.into_inner();
42        let res = self.uv.execute(req).await?;
43        Ok(Response::new(res))
44    }
45}