1 // 2 // 3 // Copyright 2016 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 #ifndef GRPC_TEST_CPP_UTIL_PROTO_REFLECTION_DESCRIPTOR_DATABASE_H 19 #define GRPC_TEST_CPP_UTIL_PROTO_REFLECTION_DESCRIPTOR_DATABASE_H 20 21 #include <mutex> 22 #include <unordered_map> 23 #include <unordered_set> 24 #include <vector> 25 26 #include <grpcpp/grpcpp.h> 27 #include <grpcpp/impl/codegen/config_protobuf.h> 28 29 #include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h" 30 31 namespace grpc { 32 33 // ProtoReflectionDescriptorDatabase takes a stub of ServerReflection and 34 // provides the methods defined by DescriptorDatabase interfaces. It can be used 35 // to feed a DescriptorPool instance. 36 class ProtoReflectionDescriptorDatabase : public protobuf::DescriptorDatabase { 37 public: 38 explicit ProtoReflectionDescriptorDatabase( 39 std::unique_ptr<reflection::v1alpha::ServerReflection::Stub> stub); 40 41 explicit ProtoReflectionDescriptorDatabase( 42 const std::shared_ptr<grpc::ChannelInterface>& channel); 43 44 ~ProtoReflectionDescriptorDatabase() override; 45 46 // The following four methods implement DescriptorDatabase interfaces. 47 // 48 // Find a file by file name. Fills in *output and returns true if found. 49 // Otherwise, returns false, leaving the contents of *output undefined. 50 bool FindFileByName(const string& filename, 51 protobuf::FileDescriptorProto* output) override; 52 53 // Find the file that declares the given fully-qualified symbol name. 54 // If found, fills in *output and returns true, otherwise returns false 55 // and leaves *output undefined. 56 bool FindFileContainingSymbol(const string& symbol_name, 57 protobuf::FileDescriptorProto* output) override; 58 59 // Find the file which defines an extension extending the given message type 60 // with the given field number. If found, fills in *output and returns true, 61 // otherwise returns false and leaves *output undefined. containing_type 62 // must be a fully-qualified type name. 63 bool FindFileContainingExtension( 64 const string& containing_type, int field_number, 65 protobuf::FileDescriptorProto* output) override; 66 67 // Finds the tag numbers used by all known extensions of 68 // extendee_type, and appends them to output in an undefined 69 // order. This method is best-effort: it's not guaranteed that the 70 // database will find all extensions, and it's not guaranteed that 71 // FindFileContainingExtension will return true on all of the found 72 // numbers. Returns true if the search was successful, otherwise 73 // returns false and leaves output unchanged. 74 bool FindAllExtensionNumbers(const string& extendee_type, 75 std::vector<int>* output) override; 76 77 // Provide a list of full names of registered services 78 bool GetServices(std::vector<std::string>* output); 79 80 private: 81 typedef ClientReaderWriter< 82 grpc::reflection::v1alpha::ServerReflectionRequest, 83 grpc::reflection::v1alpha::ServerReflectionResponse> 84 ClientStream; 85 86 protobuf::FileDescriptorProto ParseFileDescriptorProtoResponse( 87 const std::string& byte_fd_proto); 88 89 void AddFileFromResponse( 90 const grpc::reflection::v1alpha::FileDescriptorResponse& response); 91 92 std::shared_ptr<ClientStream> GetStream(); 93 94 bool DoOneRequest( 95 const grpc::reflection::v1alpha::ServerReflectionRequest& request, 96 grpc::reflection::v1alpha::ServerReflectionResponse& response); 97 98 std::shared_ptr<ClientStream> stream_; 99 grpc::ClientContext ctx_; 100 std::unique_ptr<grpc::reflection::v1alpha::ServerReflection::Stub> stub_; 101 std::unordered_set<string> known_files_; 102 std::unordered_set<string> missing_symbols_; 103 std::unordered_map<string, std::unordered_set<int>> missing_extensions_; 104 std::unordered_map<string, std::vector<int>> cached_extension_numbers_; 105 std::mutex stream_mutex_; 106 107 protobuf::SimpleDescriptorDatabase cached_db_; 108 }; 109 110 } // namespace grpc 111 112 #endif // GRPC_TEST_CPP_UTIL_PROTO_REFLECTION_DESCRIPTOR_DATABASE_H 113