blob: dfa36044d9e69876617c1e3f02112f5f9a3653ff [file] [log] [blame]
Yuchen Zengc84ed682016-05-04 16:30:11 -07001/*
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 Zeng7ae31a82016-06-06 14:21:11 -070033#ifndef GRPC_TEST_CPP_PROTO_SERVER_REFLECTION_DATABSE_H
34#define GRPC_TEST_CPP_PROTO_SERVER_REFLECTION_DATABSE_H
Yuchen Zengc84ed682016-05-04 16:30:11 -070035
Yuchen Zengc92fe252016-05-25 07:18:57 -070036#include <mutex>
Yuchen Zengc84ed682016-05-04 16:30:11 -070037#include <unordered_map>
38#include <unordered_set>
39#include <vector>
40
Yuchen Zeng6b88b222016-07-19 19:12:46 -070041// GRPC_NO_GENERATED_CODE indicates generated pb files should not be used
Yuchen Zeng13184792016-07-18 13:07:42 -070042#ifdef GRPC_NO_GENERATED_CODE
43#include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h"
44#else
Yuchen Zeng0601df32016-06-06 13:08:06 -070045#include <grpc++/ext/reflection.grpc.pb.h>
Yuchen Zeng13184792016-07-18 13:07:42 -070046#endif // GRPC_NO_GENERATED_CODE
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070047#include <grpc++/grpc++.h>
yang-g56fb16d2016-08-23 12:40:02 -070048#include <grpc++/impl/codegen/config_protobuf.h>
49
Yuchen Zengc84ed682016-05-04 16:30:11 -070050namespace grpc {
51
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070052// 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 Zeng13184792016-07-18 13:07:42 -070055class ProtoReflectionDescriptorDatabase : public protobuf::DescriptorDatabase {
Yuchen Zengc84ed682016-05-04 16:30:11 -070056 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 Zeng7ae31a82016-06-06 14:21:11 -070065 // 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 Zengc84ed682016-05-04 16:30:11 -070069 bool FindFileByName(const string& filename,
Yuchen Zeng13184792016-07-18 13:07:42 -070070 protobuf::FileDescriptorProto* output) GRPC_OVERRIDE;
Yuchen Zengc84ed682016-05-04 16:30:11 -070071
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070072 // 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 Zengc84ed682016-05-04 16:30:11 -070075 bool FindFileContainingSymbol(const string& symbol_name,
Yuchen Zeng13184792016-07-18 13:07:42 -070076 protobuf::FileDescriptorProto* output)
Yuchen Zengc84ed682016-05-04 16:30:11 -070077 GRPC_OVERRIDE;
78
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070079 // 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 Zengc84ed682016-05-04 16:30:11 -070083 bool FindFileContainingExtension(
84 const string& containing_type, int field_number,
Yuchen Zeng13184792016-07-18 13:07:42 -070085 protobuf::FileDescriptorProto* output) GRPC_OVERRIDE;
Yuchen Zengc84ed682016-05-04 16:30:11 -070086
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070087 // 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 Zengc84ed682016-05-04 16:30:11 -070094 bool FindAllExtensionNumbers(const string& extendee_type,
95 std::vector<int>* output) GRPC_OVERRIDE;
96
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070097 // Provide a list of full names of registered services
David Garcia Quintasf0424812016-10-04 13:29:52 -070098 bool GetServices(std::vector<grpc::string>* output);
Yuchen Zengc84ed682016-05-04 16:30:11 -070099
Yuchen Zengc84ed682016-05-04 16:30:11 -0700100 private:
Yuchen Zeng0333a492016-05-04 19:14:10 -0700101 typedef ClientReaderWriter<
Yuchen Zengace49862016-05-19 15:10:22 -0700102 grpc::reflection::v1alpha::ServerReflectionRequest,
103 grpc::reflection::v1alpha::ServerReflectionResponse>
Yuchen Zeng0333a492016-05-04 19:14:10 -0700104 ClientStream;
Yuchen Zengc84ed682016-05-04 16:30:11 -0700105
Yuchen Zeng13184792016-07-18 13:07:42 -0700106 const protobuf::FileDescriptorProto ParseFileDescriptorProtoResponse(
David Garcia Quintasf0424812016-10-04 13:29:52 -0700107 const grpc::string& byte_fd_proto);
Yuchen Zeng0333a492016-05-04 19:14:10 -0700108
Yuchen Zengace49862016-05-19 15:10:22 -0700109 void AddFileFromResponse(
110 const grpc::reflection::v1alpha::FileDescriptorResponse& response);
111
Yuchen Zeng0333a492016-05-04 19:14:10 -0700112 const std::shared_ptr<ClientStream> GetStream();
113
Yuchen Zeng27170352016-09-23 14:14:18 -0700114 bool DoOneRequest(
Yuchen Zeng7ae31a82016-06-06 14:21:11 -0700115 const grpc::reflection::v1alpha::ServerReflectionRequest& request,
116 grpc::reflection::v1alpha::ServerReflectionResponse& response);
Yuchen Zeng1495cb52016-06-06 10:58:06 -0700117
Yuchen Zeng0333a492016-05-04 19:14:10 -0700118 std::shared_ptr<ClientStream> stream_;
119 grpc::ClientContext ctx_;
Yuchen Zengc84ed682016-05-04 16:30:11 -0700120 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 Zengc92fe252016-05-25 07:18:57 -0700125 std::mutex stream_mutex_;
Yuchen Zengc84ed682016-05-04 16:30:11 -0700126
Yuchen Zeng13184792016-07-18 13:07:42 -0700127 protobuf::SimpleDescriptorDatabase cached_db_;
Yuchen Zengc84ed682016-05-04 16:30:11 -0700128};
129
130} // namespace grpc
Yuchen Zeng7ae31a82016-06-06 14:21:11 -0700131
132#endif // GRPC_TEST_CPP_METRICS_SERVER_H