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)27void GrpcModule::ListDependencies(ModuleList* list) {} 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 53 for (const auto& facade : facades_) { 54 facade->OnServerStarted(); 55 } 56 } 57 StopServer()58void GrpcModule::StopServer() { 59 ASSERT(started_); 60 61 server_->Shutdown(); 62 completion_queue_->Shutdown(); 63 64 for (const auto& facade : facades_) { 65 facade->OnServerStopped(); 66 } 67 68 started_ = false; 69 } 70 Register(GrpcFacadeModule * facade)71void GrpcModule::Register(GrpcFacadeModule* facade) { 72 ASSERT(!started_); 73 74 facades_.push_back(facade); 75 } 76 Unregister(GrpcFacadeModule * facade)77void GrpcModule::Unregister(GrpcFacadeModule* facade) { 78 ASSERT(!started_); 79 80 for (auto it = facades_.begin(); it != facades_.end(); it++) { 81 if (*it == facade) { 82 facades_.erase(it); 83 return; 84 } 85 } 86 87 ASSERT(false); 88 } 89 RunGrpcLoop()90void GrpcModule::RunGrpcLoop() { 91 void* tag; 92 bool ok; 93 while (true) { 94 if (!completion_queue_->Next(&tag, &ok)) { 95 LOG_INFO("gRPC is shutdown"); 96 break; 97 } 98 } 99 } 100 ToString() const101std::string GrpcModule::ToString() const { 102 return "Grpc Module"; 103 } 104 __anon626f203e0102() 105const ::bluetooth::ModuleFactory GrpcModule::Factory = ::bluetooth::ModuleFactory([]() { return new GrpcModule(); }); 106 ListDependencies(ModuleList * list)107void GrpcFacadeModule::ListDependencies(ModuleList* list) { 108 list->add<GrpcModule>(); 109 } 110 Start()111void GrpcFacadeModule::Start() { 112 GetDependency<GrpcModule>()->Register(this); 113 } 114 Stop()115void GrpcFacadeModule::Stop() { 116 GetDependency<GrpcModule>()->Unregister(this); 117 } 118 ToString() const119std::string GrpcFacadeModule::ToString() const { 120 return "Grpc Facade Module"; 121 } 122 123 } // namespace grpc 124 } // namespace bluetooth 125