proto_reflection_descriptor_database.h 4.3 KB

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