Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 33 | #ifndef GRPC_TEST_CPP_PROTO_SERVER_REFLECTION_DATABSE_H |
| 34 | #define GRPC_TEST_CPP_PROTO_SERVER_REFLECTION_DATABSE_H |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 35 | |
Yuchen Zeng | c92fe25 | 2016-05-25 07:18:57 -0700 | [diff] [blame] | 36 | #include <mutex> |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 37 | #include <unordered_map> |
| 38 | #include <unordered_set> |
| 39 | #include <vector> |
| 40 | |
Yuchen Zeng | 6b88b22 | 2016-07-19 19:12:46 -0700 | [diff] [blame] | 41 | // GRPC_NO_GENERATED_CODE indicates generated pb files should not be used |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 42 | #ifdef GRPC_NO_GENERATED_CODE |
| 43 | #include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h" |
| 44 | #else |
Yuchen Zeng | 0601df3 | 2016-06-06 13:08:06 -0700 | [diff] [blame] | 45 | #include <grpc++/ext/reflection.grpc.pb.h> |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 46 | #endif // GRPC_NO_GENERATED_CODE |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 47 | #include <grpc++/grpc++.h> |
yang-g | 56fb16d | 2016-08-23 12:40:02 -0700 | [diff] [blame] | 48 | #include <grpc++/impl/codegen/config_protobuf.h> |
| 49 | |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 50 | namespace grpc { |
| 51 | |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 52 | // ProtoReflectionDescriptorDatabase takes a stub of ServerReflection and |
| 53 | // provides the methods defined by DescriptorDatabase interfaces. It can be used |
| 54 | // to feed a DescriptorPool instance. |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 55 | class ProtoReflectionDescriptorDatabase : public protobuf::DescriptorDatabase { |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 56 | public: |
| 57 | explicit ProtoReflectionDescriptorDatabase( |
| 58 | std::unique_ptr<reflection::v1alpha::ServerReflection::Stub> stub); |
| 59 | |
| 60 | explicit ProtoReflectionDescriptorDatabase( |
| 61 | std::shared_ptr<grpc::Channel> channel); |
| 62 | |
| 63 | virtual ~ProtoReflectionDescriptorDatabase(); |
| 64 | |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 65 | // The following four methods implement DescriptorDatabase interfaces. |
| 66 | // |
| 67 | // Find a file by file name. Fills in in *output and returns true if found. |
| 68 | // Otherwise, returns false, leaving the contents of *output undefined. |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 69 | bool FindFileByName(const string& filename, |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 70 | protobuf::FileDescriptorProto* output) GRPC_OVERRIDE; |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 71 | |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 72 | // Find the file that declares the given fully-qualified symbol name. |
| 73 | // If found, fills in *output and returns true, otherwise returns false |
| 74 | // and leaves *output undefined. |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 75 | bool FindFileContainingSymbol(const string& symbol_name, |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 76 | protobuf::FileDescriptorProto* output) |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 77 | GRPC_OVERRIDE; |
| 78 | |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 79 | // Find the file which defines an extension extending the given message type |
| 80 | // with the given field number. If found, fills in *output and returns true, |
| 81 | // otherwise returns false and leaves *output undefined. containing_type |
| 82 | // must be a fully-qualified type name. |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 83 | bool FindFileContainingExtension( |
| 84 | const string& containing_type, int field_number, |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 85 | protobuf::FileDescriptorProto* output) GRPC_OVERRIDE; |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 86 | |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 87 | // Finds the tag numbers used by all known extensions of |
| 88 | // extendee_type, and appends them to output in an undefined |
| 89 | // order. This method is best-effort: it's not guaranteed that the |
| 90 | // database will find all extensions, and it's not guaranteed that |
| 91 | // FindFileContainingExtension will return true on all of the found |
| 92 | // numbers. Returns true if the search was successful, otherwise |
| 93 | // returns false and leaves output unchanged. |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 94 | bool FindAllExtensionNumbers(const string& extendee_type, |
| 95 | std::vector<int>* output) GRPC_OVERRIDE; |
| 96 | |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 97 | // Provide a list of full names of registered services |
David Garcia Quintas | f042481 | 2016-10-04 13:29:52 -0700 | [diff] [blame] | 98 | bool GetServices(std::vector<grpc::string>* output); |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 99 | |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 100 | private: |
Yuchen Zeng | 0333a49 | 2016-05-04 19:14:10 -0700 | [diff] [blame] | 101 | typedef ClientReaderWriter< |
Yuchen Zeng | ace4986 | 2016-05-19 15:10:22 -0700 | [diff] [blame] | 102 | grpc::reflection::v1alpha::ServerReflectionRequest, |
| 103 | grpc::reflection::v1alpha::ServerReflectionResponse> |
Yuchen Zeng | 0333a49 | 2016-05-04 19:14:10 -0700 | [diff] [blame] | 104 | ClientStream; |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 105 | |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 106 | const protobuf::FileDescriptorProto ParseFileDescriptorProtoResponse( |
David Garcia Quintas | f042481 | 2016-10-04 13:29:52 -0700 | [diff] [blame] | 107 | const grpc::string& byte_fd_proto); |
Yuchen Zeng | 0333a49 | 2016-05-04 19:14:10 -0700 | [diff] [blame] | 108 | |
Yuchen Zeng | ace4986 | 2016-05-19 15:10:22 -0700 | [diff] [blame] | 109 | void AddFileFromResponse( |
| 110 | const grpc::reflection::v1alpha::FileDescriptorResponse& response); |
| 111 | |
Yuchen Zeng | 0333a49 | 2016-05-04 19:14:10 -0700 | [diff] [blame] | 112 | const std::shared_ptr<ClientStream> GetStream(); |
| 113 | |
Yuchen Zeng | 2717035 | 2016-09-23 14:14:18 -0700 | [diff] [blame] | 114 | bool DoOneRequest( |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 115 | const grpc::reflection::v1alpha::ServerReflectionRequest& request, |
| 116 | grpc::reflection::v1alpha::ServerReflectionResponse& response); |
Yuchen Zeng | 1495cb5 | 2016-06-06 10:58:06 -0700 | [diff] [blame] | 117 | |
Yuchen Zeng | 0333a49 | 2016-05-04 19:14:10 -0700 | [diff] [blame] | 118 | std::shared_ptr<ClientStream> stream_; |
| 119 | grpc::ClientContext ctx_; |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 120 | std::unique_ptr<grpc::reflection::v1alpha::ServerReflection::Stub> stub_; |
| 121 | std::unordered_set<string> known_files_; |
| 122 | std::unordered_set<string> missing_symbols_; |
| 123 | std::unordered_map<string, std::unordered_set<int>> missing_extensions_; |
| 124 | std::unordered_map<string, std::vector<int>> cached_extension_numbers_; |
Yuchen Zeng | c92fe25 | 2016-05-25 07:18:57 -0700 | [diff] [blame] | 125 | std::mutex stream_mutex_; |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 126 | |
Yuchen Zeng | 1318479 | 2016-07-18 13:07:42 -0700 | [diff] [blame] | 127 | protobuf::SimpleDescriptorDatabase cached_db_; |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } // namespace grpc |
Yuchen Zeng | 7ae31a8 | 2016-06-06 14:21:11 -0700 | [diff] [blame] | 131 | |
| 132 | #endif // GRPC_TEST_CPP_METRICS_SERVER_H |