1 /*
2 * Copyright 2019 The Android Open Source Project
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
17 #include "grpc/grpc_module.h"
18
19 #include <bluetooth/log.h>
20
21 #include "os/log.h"
22
23 using ::grpc::Server;
24 using ::grpc::ServerBuilder;
25
26 namespace bluetooth {
27 namespace grpc {
28
ListDependencies(ModuleList *) const29 void GrpcModule::ListDependencies(ModuleList* /* list */) const {}
30
Start()31 void GrpcModule::Start() {
32 log::assert_that(!started_, "assert failed: !started_");
33 }
34
Stop()35 void GrpcModule::Stop() {
36 log::assert_that(!started_, "assert failed: !started_");
37 }
38
StartServer(const std::string & address,int port)39 void GrpcModule::StartServer(const std::string& address, int port) {
40 log::assert_that(!started_, "assert failed: !started_");
41 started_ = true;
42
43 std::string listening_port = address + ":" + std::to_string(port);
44 ServerBuilder builder;
45
46 for (const auto& facade : facades_) {
47 builder.RegisterService(facade->GetService());
48 }
49
50 builder.AddListeningPort(listening_port, ::grpc::InsecureServerCredentials());
51 completion_queue_ = builder.AddCompletionQueue();
52 server_ = builder.BuildAndStart();
53 log::assert_that(server_ != nullptr, "assert failed: server_ != nullptr");
54 log::info("gRPC server started on {}", listening_port);
55
56 for (const auto& facade : facades_) {
57 facade->OnServerStarted();
58 }
59 }
60
StopServer()61 void GrpcModule::StopServer() {
62 log::assert_that(started_, "assert failed: started_");
63
64 server_->Shutdown();
65 completion_queue_->Shutdown();
66
67 for (const auto& facade : facades_) {
68 facade->OnServerStopped();
69 }
70
71 started_ = false;
72 }
73
Register(GrpcFacadeModule * facade)74 void GrpcModule::Register(GrpcFacadeModule* facade) {
75 log::assert_that(!started_, "assert failed: !started_");
76
77 facades_.push_back(facade);
78 }
79
Unregister(GrpcFacadeModule * facade)80 void GrpcModule::Unregister(GrpcFacadeModule* facade) {
81 log::assert_that(!started_, "assert failed: !started_");
82
83 for (auto it = facades_.begin(); it != facades_.end(); it++) {
84 if (*it == facade) {
85 facades_.erase(it);
86 return;
87 }
88 }
89
90 log::fatal("module not found");
91 }
92
RunGrpcLoop()93 void GrpcModule::RunGrpcLoop() {
94 void* tag;
95 bool ok;
96 while (true) {
97 if (!completion_queue_->Next(&tag, &ok)) {
98 log::info("gRPC is shutdown");
99 break;
100 }
101 }
102 }
103
ToString() const104 std::string GrpcModule::ToString() const {
105 return "Grpc Module";
106 }
107
__anonfa9c74fc0102() 108 const ::bluetooth::ModuleFactory GrpcModule::Factory = ::bluetooth::ModuleFactory([]() { return new GrpcModule(); });
109
ListDependencies(ModuleList * list) const110 void GrpcFacadeModule::ListDependencies(ModuleList* list) const {
111 list->add<GrpcModule>();
112 }
113
Start()114 void GrpcFacadeModule::Start() {
115 GetDependency<GrpcModule>()->Register(this);
116 }
117
Stop()118 void GrpcFacadeModule::Stop() {
119 GetDependency<GrpcModule>()->Unregister(this);
120 }
121
ToString() const122 std::string GrpcFacadeModule::ToString() const {
123 return "Grpc Facade Module";
124 }
125
126 } // namespace grpc
127 } // namespace bluetooth
128