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