blob: f31d102a9e6fb7700c5dc6674f8ded00e7f07cdf [file] [log] [blame]
Yuchen Zengc84ed682016-05-04 16:30:11 -07001/*
2 *
3 * Copyright 2015, 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 */
33
Yuchen Zeng0601df32016-06-06 13:08:06 -070034#include <grpc++/ext/proto_server_reflection_plugin.h>
Yuchen Zengc84ed682016-05-04 16:30:11 -070035#include <grpc++/impl/server_builder_plugin.h>
36#include <grpc++/impl/server_initializer.h>
37#include <grpc++/server.h>
38
Yuchen Zeng0601df32016-06-06 13:08:06 -070039#include "src/cpp/ext/proto_server_reflection.h"
Yuchen Zengc84ed682016-05-04 16:30:11 -070040
41namespace grpc {
42namespace reflection {
43
44ProtoServerReflectionPlugin::ProtoServerReflectionPlugin()
Yuchen Zengc8074522016-06-03 14:36:11 -070045 : reflection_service_(new grpc::ProtoServerReflection()) {}
Yuchen Zengc84ed682016-05-04 16:30:11 -070046
Yuchen Zengc8074522016-06-03 14:36:11 -070047grpc::string ProtoServerReflectionPlugin::name() {
48 return "proto_server_reflection";
49}
Yuchen Zengc84ed682016-05-04 16:30:11 -070050
51void ProtoServerReflectionPlugin::InitServer(grpc::ServerInitializer* si) {
Yuchen Zengc8074522016-06-03 14:36:11 -070052 si->RegisterService(reflection_service_);
Yuchen Zengc84ed682016-05-04 16:30:11 -070053}
54
55void ProtoServerReflectionPlugin::Finish(grpc::ServerInitializer* si) {
Yuchen Zengc8074522016-06-03 14:36:11 -070056 reflection_service_->SetServiceList(si->GetServiceList());
Yuchen Zengc84ed682016-05-04 16:30:11 -070057}
58
59void ProtoServerReflectionPlugin::ChangeArguments(const grpc::string& name,
60 void* value) {}
61
62bool ProtoServerReflectionPlugin::has_sync_methods() const {
Yuchen Zengc8074522016-06-03 14:36:11 -070063 if (reflection_service_ != nullptr) {
64 return reflection_service_->has_synchronous_methods();
Yuchen Zengc84ed682016-05-04 16:30:11 -070065 }
66 return false;
67}
68
69bool ProtoServerReflectionPlugin::has_async_methods() const {
Yuchen Zengc8074522016-06-03 14:36:11 -070070 if (reflection_service_ != nullptr) {
71 return reflection_service_->has_async_methods();
Yuchen Zengc84ed682016-05-04 16:30:11 -070072 }
73 return false;
74}
75
Yuchen Zeng1495cb52016-06-06 10:58:06 -070076static std::unique_ptr<::grpc::ServerBuilderPlugin> CreateProtoReflection() {
Yuchen Zengace49862016-05-19 15:10:22 -070077 return std::unique_ptr<::grpc::ServerBuilderPlugin>(
78 new ProtoServerReflectionPlugin());
79}
80
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070081void InitProtoReflectionServerBuilderPlugin() {
Yuchen Zengc84ed682016-05-04 16:30:11 -070082 static bool already_here = false;
83 if (already_here) return;
84 already_here = true;
85 ::grpc::ServerBuilder::InternalAddPluginFactory(&CreateProtoReflection);
86}
87
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070088// Force InitProtoReflectionServerBuilderPlugin() to be called at static
89// initialization time.
Yuchen Zeng1495cb52016-06-06 10:58:06 -070090struct StaticProtoReflectionPluginInitializer {
91 StaticProtoReflectionPluginInitializer() {
Yuchen Zeng7ae31a82016-06-06 14:21:11 -070092 InitProtoReflectionServerBuilderPlugin();
Yuchen Zeng1495cb52016-06-06 10:58:06 -070093 }
94} static_proto_reflection_plugin_initializer;
95
Yuchen Zengc84ed682016-05-04 16:30:11 -070096} // namespace reflection
97} // namespace grpc