Imported Upstream version 1.33.1
[platform/upstream/grpc.git] / src / cpp / server / channelz / channelz_service_plugin.cc
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <grpcpp/ext/channelz_service_plugin.h>
22 #include <grpcpp/impl/server_builder_plugin.h>
23 #include <grpcpp/impl/server_initializer.h>
24 #include <grpcpp/server.h>
25
26 #include "src/cpp/server/channelz/channelz_service.h"
27
28 namespace grpc {
29 namespace channelz {
30 namespace experimental {
31
32 class ChannelzServicePlugin : public ::grpc::ServerBuilderPlugin {
33  public:
34   ChannelzServicePlugin() : channelz_service_(new grpc::ChannelzService()) {}
35
36   std::string name() override { return "channelz_service"; }
37
38   void InitServer(grpc::ServerInitializer* si) override {
39     si->RegisterService(channelz_service_);
40   }
41
42   void Finish(grpc::ServerInitializer* /*si*/) override {}
43
44   void ChangeArguments(const std::string& /*name*/, void* /*value*/) override {}
45
46   bool has_sync_methods() const override {
47     if (channelz_service_) {
48       return channelz_service_->has_synchronous_methods();
49     }
50     return false;
51   }
52
53   bool has_async_methods() const override {
54     if (channelz_service_) {
55       return channelz_service_->has_async_methods();
56     }
57     return false;
58   }
59
60  private:
61   std::shared_ptr<grpc::ChannelzService> channelz_service_;
62 };
63
64 static std::unique_ptr< ::grpc::ServerBuilderPlugin>
65 CreateChannelzServicePlugin() {
66   return std::unique_ptr< ::grpc::ServerBuilderPlugin>(
67       new ChannelzServicePlugin());
68 }
69
70 }  // namespace experimental
71 }  // namespace channelz
72 }  // namespace grpc
73 namespace grpc_impl {
74 namespace channelz {
75 namespace experimental {
76
77 void InitChannelzService() {
78   static struct Initializer {
79     Initializer() {
80       ::grpc::ServerBuilder::InternalAddPluginFactory(
81           &grpc::channelz::experimental::CreateChannelzServicePlugin);
82     }
83   } initialize;
84 }
85
86 }  // namespace experimental
87 }  // namespace channelz
88 }  // namespace grpc_impl