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 #pragma once 18 19 #include <functional> 20 #include <vector> 21 22 #include <grpc++/grpc++.h> 23 #include <module.h> 24 25 namespace bluetooth { 26 namespace grpc { 27 28 class GrpcFacadeModule; 29 30 class GrpcModule : public ::bluetooth::Module { 31 public: 32 static const ModuleFactory Factory; 33 34 void StartServer(const std::string& address, int port); 35 36 void StopServer(); 37 38 void Register(GrpcFacadeModule* facade); 39 40 void Unregister(GrpcFacadeModule* facade); 41 42 // Blocks for incoming gRPC requests 43 void RunGrpcLoop(); 44 45 protected: 46 void ListDependencies(ModuleList* list) override; 47 48 void Start() override; 49 50 void Stop() override; 51 52 std::string ToString() const override; 53 54 private: 55 bool started_; 56 std::unique_ptr<::grpc::Server> server_ = nullptr; 57 std::unique_ptr<::grpc::ServerCompletionQueue> completion_queue_ = nullptr; 58 std::vector<GrpcFacadeModule*> facades_; 59 }; 60 61 class GrpcFacadeModule : public ::bluetooth::Module { 62 friend GrpcModule; 63 protected: 64 void ListDependencies(ModuleList* list) override; 65 66 void Start() override; 67 68 void Stop() override; 69 70 virtual ::grpc::Service* GetService() const = 0; 71 72 virtual void OnServerStarted() {} 73 74 virtual void OnServerStopped() {} 75 76 std::string ToString() const override; 77 }; 78 79 } // namespace grpc 80 } // namespace bluetooth 81