• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 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 #ifndef GRPC_SRC_CPP_EXT_PROTO_SERVER_REFLECTION_H
20 #define GRPC_SRC_CPP_EXT_PROTO_SERVER_REFLECTION_H
21 
22 #include <memory>
23 #include <string>
24 #include <unordered_set>
25 #include <utility>
26 #include <vector>
27 
28 #include <grpcpp/grpcpp.h>
29 #include <grpcpp/impl/codegen/config_protobuf.h>
30 #include <grpcpp/support/config.h>
31 #include <grpcpp/support/status.h>
32 #include <grpcpp/support/sync_stream.h>
33 
34 #include "src/proto/grpc/reflection/v1/reflection.grpc.pb.h"
35 #include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h"
36 
37 namespace grpc {
38 
39 class ProtoServerReflectionBackend {
40  public:
ProtoServerReflectionBackend()41   ProtoServerReflectionBackend()
42       : descriptor_pool_(protobuf::DescriptorPool::generated_pool()) {}
43 
SetServiceList(const std::vector<std::string> * services)44   void SetServiceList(const std::vector<std::string>* services) {
45     services_ = services;
46   }
47 
48   template <typename Request, typename Response>
49   Status ServerReflectionInfo(
50       ServerReaderWriter<Response, Request>* stream) const;
51 
52  private:
53   template <typename Response>
54   Status ListService(Response* response) const;
55 
56   template <typename Response>
57   Status GetFileByName(const std::string& file_name, Response* response) const;
58 
59   template <typename Response>
60   Status GetFileContainingSymbol(const std::string& symbol,
61                                  Response* response) const;
62 
63   template <typename Request, typename Response>
64   Status GetFileContainingExtension(const Request* request,
65                                     Response* response) const;
66 
67   template <typename Response>
68   Status GetAllExtensionNumbers(const std::string& type,
69                                 Response* response) const;
70 
71   template <typename Response>
72   void FillFileDescriptorResponse(
73       const protobuf::FileDescriptor* file_desc, Response* response,
74       std::unordered_set<std::string>* seen_files) const;
75 
76   template <typename Response>
77   void FillErrorResponse(const Status& status, Response* error_response) const;
78 
79   const protobuf::DescriptorPool* descriptor_pool_;
80   const std::vector<string>* services_;
81 };
82 
83 class ProtoServerReflection final
84     : public reflection::v1alpha::ServerReflection::Service {
85  public:
ProtoServerReflection()86   ProtoServerReflection()
87       : grpc::ProtoServerReflection(
88             std::make_shared<ProtoServerReflectionBackend>()) {}
89 
ProtoServerReflection(std::shared_ptr<ProtoServerReflectionBackend> backend)90   explicit ProtoServerReflection(
91       std::shared_ptr<ProtoServerReflectionBackend> backend)
92       : backend_(std::move(backend)) {}
93 
94   // Add the full names of registered services
SetServiceList(const std::vector<std::string> * services)95   void SetServiceList(const std::vector<std::string>* services) {
96     backend_->SetServiceList(services);
97   }
98 
99   // implementation of ServerReflectionInfo(stream ServerReflectionRequest) rpc
100   // in ServerReflection service
101   Status ServerReflectionInfo(
102       ServerContext* context,
103       ServerReaderWriter<reflection::v1alpha::ServerReflectionResponse,
104                          reflection::v1alpha::ServerReflectionRequest>* stream)
105       override;
106 
107   std::shared_ptr<ProtoServerReflectionBackend> backend_;
108 };
109 
110 class ProtoServerReflectionV1 final
111     : public reflection::v1::ServerReflection::Service {
112  public:
ProtoServerReflectionV1(std::shared_ptr<ProtoServerReflectionBackend> backend)113   explicit ProtoServerReflectionV1(
114       std::shared_ptr<ProtoServerReflectionBackend> backend)
115       : backend_(std::move(backend)) {}
116 
117   // implementation of ServerReflectionInfo(stream ServerReflectionRequest) rpc
118   // in ServerReflection service
119   Status ServerReflectionInfo(
120       ServerContext* /* context */,
121       ServerReaderWriter<reflection::v1::ServerReflectionResponse,
122                          reflection::v1::ServerReflectionRequest>* stream)
123       override;
124 
125  private:
126   std::shared_ptr<ProtoServerReflectionBackend> backend_;
127 };
128 
129 }  // namespace grpc
130 
131 #endif  // GRPC_SRC_CPP_EXT_PROTO_SERVER_REFLECTION_H
132