Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Zeng | 0601df3 | 2016-06-06 13:08:06 -0700 | [diff] [blame^] | 34 | #include <grpc++/ext/proto_server_reflection_plugin.h> |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 35 | #include <grpc++/impl/server_builder_plugin.h> |
| 36 | #include <grpc++/impl/server_initializer.h> |
| 37 | #include <grpc++/server.h> |
| 38 | |
Yuchen Zeng | 0601df3 | 2016-06-06 13:08:06 -0700 | [diff] [blame^] | 39 | #include "src/cpp/ext/proto_server_reflection.h" |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 40 | |
| 41 | namespace grpc { |
| 42 | namespace reflection { |
| 43 | |
| 44 | ProtoServerReflectionPlugin::ProtoServerReflectionPlugin() |
Yuchen Zeng | c807452 | 2016-06-03 14:36:11 -0700 | [diff] [blame] | 45 | : reflection_service_(new grpc::ProtoServerReflection()) {} |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 46 | |
Yuchen Zeng | c807452 | 2016-06-03 14:36:11 -0700 | [diff] [blame] | 47 | grpc::string ProtoServerReflectionPlugin::name() { |
| 48 | return "proto_server_reflection"; |
| 49 | } |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 50 | |
| 51 | void ProtoServerReflectionPlugin::InitServer(grpc::ServerInitializer* si) { |
Yuchen Zeng | c807452 | 2016-06-03 14:36:11 -0700 | [diff] [blame] | 52 | si->RegisterService(reflection_service_); |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void ProtoServerReflectionPlugin::Finish(grpc::ServerInitializer* si) { |
Yuchen Zeng | c807452 | 2016-06-03 14:36:11 -0700 | [diff] [blame] | 56 | reflection_service_->SetServiceList(si->GetServiceList()); |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void ProtoServerReflectionPlugin::ChangeArguments(const grpc::string& name, |
| 60 | void* value) {} |
| 61 | |
| 62 | bool ProtoServerReflectionPlugin::has_sync_methods() const { |
Yuchen Zeng | c807452 | 2016-06-03 14:36:11 -0700 | [diff] [blame] | 63 | if (reflection_service_ != nullptr) { |
| 64 | return reflection_service_->has_synchronous_methods(); |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | bool ProtoServerReflectionPlugin::has_async_methods() const { |
Yuchen Zeng | c807452 | 2016-06-03 14:36:11 -0700 | [diff] [blame] | 70 | if (reflection_service_ != nullptr) { |
| 71 | return reflection_service_->has_async_methods(); |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
Yuchen Zeng | 1495cb5 | 2016-06-06 10:58:06 -0700 | [diff] [blame] | 76 | static std::unique_ptr<::grpc::ServerBuilderPlugin> CreateProtoReflection() { |
Yuchen Zeng | ace4986 | 2016-05-19 15:10:22 -0700 | [diff] [blame] | 77 | return std::unique_ptr<::grpc::ServerBuilderPlugin>( |
| 78 | new ProtoServerReflectionPlugin()); |
| 79 | } |
| 80 | |
Yuchen Zeng | 1495cb5 | 2016-06-06 10:58:06 -0700 | [diff] [blame] | 81 | static void AddProtoReflectionServerBuilderPlugin() { |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 82 | static bool already_here = false; |
| 83 | if (already_here) return; |
| 84 | already_here = true; |
| 85 | ::grpc::ServerBuilder::InternalAddPluginFactory(&CreateProtoReflection); |
| 86 | } |
| 87 | |
Yuchen Zeng | 1495cb5 | 2016-06-06 10:58:06 -0700 | [diff] [blame] | 88 | struct StaticProtoReflectionPluginInitializer { |
| 89 | StaticProtoReflectionPluginInitializer() { |
| 90 | AddProtoReflectionServerBuilderPlugin(); |
| 91 | } |
| 92 | } static_proto_reflection_plugin_initializer; |
| 93 | |
Yuchen Zeng | c84ed68 | 2016-05-04 16:30:11 -0700 | [diff] [blame] | 94 | } // namespace reflection |
| 95 | } // namespace grpc |