summaryrefslogtreecommitdiff
path: root/system/gd/grpc/grpc_module.cc
blob: 3dd40ab811273830fa3dc469ce453e6e3d8b6a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "grpc/grpc_module.h"

#include "os/log.h"

using ::grpc::Server;
using ::grpc::ServerBuilder;

namespace bluetooth {
namespace grpc {

void GrpcModule::ListDependencies(ModuleList* list) const {}

void GrpcModule::Start() {
  ASSERT(!started_);
}

void GrpcModule::Stop() {
  ASSERT(!started_);
}

void GrpcModule::StartServer(const std::string& address, int port) {
  ASSERT(!started_);
  started_ = true;

  std::string listening_port = address + ":" + std::to_string(port);
  ServerBuilder builder;

  for (const auto& facade : facades_) {
    builder.RegisterService(facade->GetService());
  }

  builder.AddListeningPort(listening_port, ::grpc::InsecureServerCredentials());
  completion_queue_ = builder.AddCompletionQueue();
  server_ = builder.BuildAndStart();
  ASSERT(server_ != nullptr);
  LOG_INFO("gRPC server started on %s", listening_port.c_str());

  for (const auto& facade : facades_) {
    facade->OnServerStarted();
  }
}

void GrpcModule::StopServer() {
  ASSERT(started_);

  server_->Shutdown();
  completion_queue_->Shutdown();

  for (const auto& facade : facades_) {
    facade->OnServerStopped();
  }

  started_ = false;
}

void GrpcModule::Register(GrpcFacadeModule* facade) {
  ASSERT(!started_);

  facades_.push_back(facade);
}

void GrpcModule::Unregister(GrpcFacadeModule* facade) {
  ASSERT(!started_);

  for (auto it = facades_.begin(); it != facades_.end(); it++) {
    if (*it == facade) {
      facades_.erase(it);
      return;
    }
  }

  ASSERT(false);
}

void GrpcModule::RunGrpcLoop() {
  void* tag;
  bool ok;
  while (true) {
    if (!completion_queue_->Next(&tag, &ok)) {
      LOG_INFO("gRPC is shutdown");
      break;
    }
  }
}

std::string GrpcModule::ToString() const {
  return "Grpc Module";
}

const ::bluetooth::ModuleFactory GrpcModule::Factory = ::bluetooth::ModuleFactory([]() { return new GrpcModule(); });

void GrpcFacadeModule::ListDependencies(ModuleList* list) const {
  list->add<GrpcModule>();
}

void GrpcFacadeModule::Start() {
  GetDependency<GrpcModule>()->Register(this);
}

void GrpcFacadeModule::Stop() {
  GetDependency<GrpcModule>()->Unregister(this);
}

std::string GrpcFacadeModule::ToString() const {
  return "Grpc Facade Module";
}

}  // namespace grpc
}  // namespace bluetooth