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 } 29 Start()30void GrpcModule::Start() { 31 ASSERT(!started_); 32 } 33 Stop()34void GrpcModule::Stop() { 35 ASSERT(!started_); 36 } 37 StartServer(const std::string & address,int port)38void GrpcModule::StartServer(const std::string& address, int port) { 39 ASSERT(!started_); 40 started_ = true; 41 42 std::string listening_port = address + ":" + std::to_string(port); 43 ServerBuilder builder; 44 45 for (const auto& facade : facades_) { 46 builder.RegisterService(facade->GetService()); 47 } 48 49 builder.AddListeningPort(listening_port, ::grpc::InsecureServerCredentials()); 50 completion_queue_ = builder.AddCompletionQueue(); 51 server_ = builder.BuildAndStart(); 52 ASSERT(server_ != nullptr); 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 __anonec2fe2dd0102() 106const ::bluetooth::ModuleFactory GrpcModule::Factory = ::bluetooth::ModuleFactory([]() { 107 return new GrpcModule(); 108 }); 109 110 ListDependencies(ModuleList * list)111void GrpcFacadeModule::ListDependencies(ModuleList* list) { 112 list->add<GrpcModule>(); 113 } 114 Start()115void GrpcFacadeModule::Start() { 116 GetDependency<GrpcModule>()->Register(this); 117 } 118 Stop()119void GrpcFacadeModule::Stop() { 120 GetDependency<GrpcModule>()->Unregister(this); 121 } 122 ToString() const123std::string GrpcFacadeModule::ToString() const { 124 return "Grpc Facade Module"; 125 } 126 127 } // namespace grpc 128 } // namespace bluetooth 129