• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/cpp/server/channelz/channelz_service.h"
22 
23 #include <google/protobuf/text_format.h>
24 #include <google/protobuf/util/json_util.h>
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 
29 namespace grpc {
30 
GetTopChannels(ServerContext * unused,const channelz::v1::GetTopChannelsRequest * request,channelz::v1::GetTopChannelsResponse * response)31 Status ChannelzService::GetTopChannels(
32     ServerContext* unused, const channelz::v1::GetTopChannelsRequest* request,
33     channelz::v1::GetTopChannelsResponse* response) {
34   char* json_str = grpc_channelz_get_top_channels(request->start_channel_id());
35   if (json_str == nullptr) {
36     return Status(INTERNAL, "grpc_channelz_get_top_channels returned null");
37   }
38   google::protobuf::util::Status s =
39       google::protobuf::util::JsonStringToMessage(json_str, response);
40   gpr_free(json_str);
41   if (s != google::protobuf::util::Status::OK) {
42     return Status(INTERNAL, s.ToString());
43   }
44   return Status::OK;
45 }
46 
GetServers(ServerContext * unused,const channelz::v1::GetServersRequest * request,channelz::v1::GetServersResponse * response)47 Status ChannelzService::GetServers(
48     ServerContext* unused, const channelz::v1::GetServersRequest* request,
49     channelz::v1::GetServersResponse* response) {
50   char* json_str = grpc_channelz_get_servers(request->start_server_id());
51   if (json_str == nullptr) {
52     return Status(INTERNAL, "grpc_channelz_get_servers returned null");
53   }
54   google::protobuf::util::Status s =
55       google::protobuf::util::JsonStringToMessage(json_str, response);
56   gpr_free(json_str);
57   if (s != google::protobuf::util::Status::OK) {
58     return Status(INTERNAL, s.ToString());
59   }
60   return Status::OK;
61 }
62 
GetChannel(ServerContext * unused,const channelz::v1::GetChannelRequest * request,channelz::v1::GetChannelResponse * response)63 Status ChannelzService::GetChannel(
64     ServerContext* unused, const channelz::v1::GetChannelRequest* request,
65     channelz::v1::GetChannelResponse* response) {
66   char* json_str = grpc_channelz_get_channel(request->channel_id());
67   if (json_str == nullptr) {
68     return Status(NOT_FOUND, "No object found for that ChannelId");
69   }
70   google::protobuf::util::Status s =
71       google::protobuf::util::JsonStringToMessage(json_str, response);
72   gpr_free(json_str);
73   if (s != google::protobuf::util::Status::OK) {
74     return Status(INTERNAL, s.ToString());
75   }
76   return Status::OK;
77 }
78 
GetSubchannel(ServerContext * unused,const channelz::v1::GetSubchannelRequest * request,channelz::v1::GetSubchannelResponse * response)79 Status ChannelzService::GetSubchannel(
80     ServerContext* unused, const channelz::v1::GetSubchannelRequest* request,
81     channelz::v1::GetSubchannelResponse* response) {
82   char* json_str = grpc_channelz_get_subchannel(request->subchannel_id());
83   if (json_str == nullptr) {
84     return Status(NOT_FOUND, "No object found for that SubchannelId");
85   }
86   google::protobuf::util::Status s =
87       google::protobuf::util::JsonStringToMessage(json_str, response);
88   gpr_free(json_str);
89   if (s != google::protobuf::util::Status::OK) {
90     return Status(INTERNAL, s.ToString());
91   }
92   return Status::OK;
93 }
94 
95 }  // namespace grpc
96