Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 34 | #include <grpc++/channel.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 36 | #include <memory> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <grpc++/client_context.h> |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 39 | #include <grpc++/completion_queue.h> |
Craig Tiller | 20f4af2 | 2015-02-10 09:52:15 -0800 | [diff] [blame] | 40 | #include <grpc++/impl/call.h> |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/codegen/completion_queue_tag.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/grpc_library.h> |
| 43 | #include <grpc++/impl/rpc_method.h> |
| 44 | #include <grpc++/security/credentials.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 45 | #include <grpc++/support/channel_arguments.h> |
| 46 | #include <grpc++/support/config.h> |
| 47 | #include <grpc++/support/status.h> |
| 48 | #include <grpc++/support/time.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 49 | #include <grpc/grpc.h> |
Craig Tiller | b37d53e | 2016-10-26 16:16:35 -0700 | [diff] [blame] | 50 | #include <grpc/slice.h> |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 51 | #include <grpc/support/alloc.h> |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 52 | #include <grpc/support/log.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 53 | #include "src/core/lib/profiling/timers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | |
| 55 | namespace grpc { |
| 56 | |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 57 | static internal::GrpcLibraryInitializer g_gli_initializer; |
yang-g | d556da9 | 2015-07-31 15:59:04 -0700 | [diff] [blame] | 58 | Channel::Channel(const grpc::string& host, grpc_channel* channel) |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 59 | : host_(host), c_channel_(channel) { |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 60 | g_gli_initializer.summon(); |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 61 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | |
| 63 | Channel::~Channel() { grpc_channel_destroy(c_channel_); } |
| 64 | |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 65 | namespace { |
| 66 | |
| 67 | grpc::string GetChannelInfoField(grpc_channel* channel, |
| 68 | grpc_channel_info* channel_info, |
| 69 | char*** channel_info_field) { |
| 70 | char* value = NULL; |
| 71 | memset(channel_info, 0, sizeof(*channel_info)); |
| 72 | *channel_info_field = &value; |
| 73 | grpc_channel_get_info(channel, channel_info); |
| 74 | if (value == NULL) return ""; |
| 75 | grpc::string result = value; |
| 76 | gpr_free(value); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | grpc::string Channel::GetLoadBalancingPolicyName() const { |
| 83 | grpc_channel_info channel_info; |
| 84 | return GetChannelInfoField(c_channel_, &channel_info, |
| 85 | &channel_info.lb_policy_name); |
| 86 | } |
| 87 | |
| 88 | grpc::string Channel::GetServiceConfigJSON() const { |
| 89 | grpc_channel_info channel_info; |
| 90 | return GetChannelInfoField(c_channel_, &channel_info, |
| 91 | &channel_info.service_config_json); |
| 92 | } |
| 93 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 94 | Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, |
| 95 | CompletionQueue* cq) { |
yang-g | 37ce034 | 2015-08-12 11:46:26 -0700 | [diff] [blame] | 96 | const bool kRegistered = method.channel_tag() && context->authority().empty(); |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 97 | grpc_call* c_call = NULL; |
yang-g | 37ce034 | 2015-08-12 11:46:26 -0700 | [diff] [blame] | 98 | if (kRegistered) { |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 99 | c_call = grpc_channel_create_registered_call( |
| 100 | c_channel_, context->propagate_from_call_, |
| 101 | context->propagation_options_.c_bitmask(), cq->cq(), |
Nicolas "Pixel" Noble | d53b389 | 2015-08-13 19:20:39 +0200 | [diff] [blame] | 102 | method.channel_tag(), context->raw_deadline(), nullptr); |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 103 | } else { |
| 104 | const char* host_str = NULL; |
| 105 | if (!context->authority().empty()) { |
yang-g | 2f543f2 | 2015-08-19 15:14:17 -0700 | [diff] [blame] | 106 | host_str = context->authority_.c_str(); |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 107 | } else if (!host_.empty()) { |
| 108 | host_str = host_.c_str(); |
| 109 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 110 | grpc_slice method_slice = SliceFromCopiedString(method.name()); |
| 111 | grpc_slice host_slice; |
| 112 | if (host_str != nullptr) { |
| 113 | host_slice = SliceFromCopiedString(host_str); |
| 114 | } |
| 115 | c_call = grpc_channel_create_call( |
| 116 | c_channel_, context->propagate_from_call_, |
| 117 | context->propagation_options_.c_bitmask(), cq->cq(), method_slice, |
| 118 | host_str == nullptr ? nullptr : &host_slice, context->raw_deadline(), |
| 119 | nullptr); |
| 120 | grpc_slice_unref(method_slice); |
| 121 | if (host_str != nullptr) { |
| 122 | grpc_slice_unref(host_slice); |
| 123 | } |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 124 | } |
yang-g | 9a009f2 | 2015-07-29 11:38:18 -0700 | [diff] [blame] | 125 | grpc_census_call_set_context(c_call, context->census_context()); |
Craig Tiller | 1fb9955 | 2015-04-27 08:55:08 -0700 | [diff] [blame] | 126 | context->set_call(c_call, shared_from_this()); |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 127 | return Call(c_call, this, cq); |
| 128 | } |
| 129 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 130 | void Channel::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) { |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 131 | static const size_t MAX_OPS = 8; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 132 | size_t nops = 0; |
| 133 | grpc_op cops[MAX_OPS]; |
Craig Tiller | 66051c6 | 2017-03-31 09:16:35 -0700 | [diff] [blame^] | 134 | ops->FillOps(call->call(), cops, &nops); |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 135 | GPR_ASSERT(GRPC_CALL_OK == |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 136 | grpc_call_start_batch(call->call(), cops, nops, ops, nullptr)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Craig Tiller | 277d3cf | 2015-04-14 14:04:51 -0700 | [diff] [blame] | 139 | void* Channel::RegisterMethod(const char* method) { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 140 | return grpc_channel_register_call( |
| 141 | c_channel_, method, host_.empty() ? NULL : host_.c_str(), nullptr); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | } |
| 143 | |
yang-g | a73dc1c | 2015-08-05 14:24:00 -0700 | [diff] [blame] | 144 | grpc_connectivity_state Channel::GetState(bool try_to_connect) { |
| 145 | return grpc_channel_check_connectivity_state(c_channel_, try_to_connect); |
| 146 | } |
| 147 | |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 148 | namespace { |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 149 | class TagSaver final : public CompletionQueueTag { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 150 | public: |
| 151 | explicit TagSaver(void* tag) : tag_(tag) {} |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 152 | ~TagSaver() override {} |
| 153 | bool FinalizeResult(void** tag, bool* status) override { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 154 | *tag = tag_; |
| 155 | delete this; |
| 156 | return true; |
| 157 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 158 | |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 159 | private: |
| 160 | void* tag_; |
| 161 | }; |
| 162 | |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 163 | } // namespace |
| 164 | |
| 165 | void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed, |
| 166 | gpr_timespec deadline, |
| 167 | CompletionQueue* cq, void* tag) { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 168 | TagSaver* tag_saver = new TagSaver(tag); |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 169 | grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline, |
| 170 | cq->cq(), tag_saver); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 171 | } |
| 172 | |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 173 | bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed, |
| 174 | gpr_timespec deadline) { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 175 | CompletionQueue cq; |
| 176 | bool ok = false; |
| 177 | void* tag = NULL; |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 178 | NotifyOnStateChangeImpl(last_observed, deadline, &cq, NULL); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 179 | cq.Next(&tag, &ok); |
| 180 | GPR_ASSERT(tag == NULL); |
| 181 | return ok; |
| 182 | } |
| 183 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 184 | } // namespace grpc |