1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Haichao.chc |
17 | * \date Oct 2020 |
18 | * \brief Grpc server module of proxima search engine |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <thread> |
24 | #include <brpc/server.h> |
25 | #include "common/macro_define.h" |
26 | #include "proxima_request_handler.h" |
27 | |
28 | namespace proxima { |
29 | namespace be { |
30 | namespace server { |
31 | |
32 | class GrpcServer; |
33 | using GrpcServerUPtr = std::unique_ptr<GrpcServer>; |
34 | |
35 | /* |
36 | * GrpcServer provides grpc service with protobuf protocol |
37 | */ |
38 | class GrpcServer { |
39 | public: |
40 | PROXIMA_DISALLOW_COPY_AND_ASSIGN(GrpcServer); |
41 | |
42 | //! Constructor |
43 | GrpcServer() = default; |
44 | |
45 | //! Destructor |
46 | ~GrpcServer(); |
47 | |
48 | //! Constructor |
49 | static GrpcServerUPtr Create(); |
50 | |
51 | public: |
52 | //! Start grpc server |
53 | int bind_and_start(const agent::IndexAgentPtr &index_agent, |
54 | const query::QueryAgentPtr &query_agent, |
55 | const admin::AdminAgentPtr &admin_agent, |
56 | const std::string &version); |
57 | |
58 | //! Stop grpc server |
59 | int stop(); |
60 | |
61 | //! Check if server is running |
62 | bool is_running(); |
63 | |
64 | private: |
65 | int start_server(); |
66 | |
67 | int stop_server(); |
68 | |
69 | private: |
70 | brpc::Server server_{}; |
71 | std::unique_ptr<std::thread> thread_{}; |
72 | }; |
73 | |
74 | |
75 | } // end namespace server |
76 | } // namespace be |
77 | } // end namespace proxima |
78 | |