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 | */ |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 33 | #include <grpc++/support/channel_arguments.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 34 | |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 35 | #include <sstream> |
| 36 | |
Craig Tiller | ed91324 | 2016-11-04 16:31:30 -0700 | [diff] [blame] | 37 | #include <grpc++/grpc++.h> |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 38 | #include <grpc++/resource_quota.h> |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 39 | #include <grpc/impl/codegen/grpc_types.h> |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 41 | extern "C" { |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 43 | #include "src/core/lib/iomgr/exec_ctx.h" |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 44 | #include "src/core/lib/iomgr/socket_mutator.h" |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 45 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | namespace grpc { |
| 47 | |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 48 | ChannelArguments::ChannelArguments() { |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 49 | // This will be ignored if used on the server side. |
Craig Tiller | ed91324 | 2016-11-04 16:31:30 -0700 | [diff] [blame] | 50 | SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, "grpc-c++/" + Version()); |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 53 | ChannelArguments::ChannelArguments(const ChannelArguments& other) |
| 54 | : strings_(other.strings_) { |
| 55 | args_.reserve(other.args_.size()); |
| 56 | auto list_it_dst = strings_.begin(); |
| 57 | auto list_it_src = other.strings_.begin(); |
| 58 | for (auto a = other.args_.begin(); a != other.args_.end(); ++a) { |
| 59 | grpc_arg ap; |
| 60 | ap.type = a->type; |
| 61 | GPR_ASSERT(list_it_src->c_str() == a->key); |
| 62 | ap.key = const_cast<char*>(list_it_dst->c_str()); |
| 63 | ++list_it_src; |
| 64 | ++list_it_dst; |
| 65 | switch (a->type) { |
| 66 | case GRPC_ARG_INTEGER: |
| 67 | ap.value.integer = a->value.integer; |
| 68 | break; |
| 69 | case GRPC_ARG_STRING: |
| 70 | GPR_ASSERT(list_it_src->c_str() == a->value.string); |
| 71 | ap.value.string = const_cast<char*>(list_it_dst->c_str()); |
| 72 | ++list_it_src; |
| 73 | ++list_it_dst; |
| 74 | break; |
| 75 | case GRPC_ARG_POINTER: |
| 76 | ap.value.pointer = a->value.pointer; |
Craig Tiller | edc2fff | 2016-01-13 06:54:27 -0800 | [diff] [blame] | 77 | ap.value.pointer.p = a->value.pointer.vtable->copy(ap.value.pointer.p); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | args_.push_back(ap); |
| 81 | } |
| 82 | } |
| 83 | |
Yuchen Zeng | 77532e8 | 2017-02-27 11:52:39 -0800 | [diff] [blame] | 84 | ChannelArguments::~ChannelArguments() { |
| 85 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 86 | for (auto it = args_.begin(); it != args_.end(); ++it) { |
| 87 | if (it->type == GRPC_ARG_POINTER) { |
| 88 | it->value.pointer.vtable->destroy(&exec_ctx, it->value.pointer.p); |
| 89 | } |
| 90 | } |
| 91 | grpc_exec_ctx_finish(&exec_ctx); |
| 92 | } |
| 93 | |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 94 | void ChannelArguments::Swap(ChannelArguments& other) { |
| 95 | args_.swap(other.args_); |
| 96 | strings_.swap(other.strings_); |
| 97 | } |
| 98 | |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 99 | void ChannelArguments::SetCompressionAlgorithm( |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 100 | grpc_compression_algorithm algorithm) { |
David Garcia Quintas | 183ba02 | 2016-05-12 17:08:19 -0700 | [diff] [blame] | 101 | SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, algorithm); |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 104 | void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) { |
| 105 | if (!mutator) { |
| 106 | return; |
| 107 | } |
| 108 | grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator); |
| 109 | bool replaced = false; |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 110 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 111 | for (auto it = args_.begin(); it != args_.end(); ++it) { |
| 112 | if (it->type == mutator_arg.type && |
| 113 | grpc::string(it->key) == grpc::string(mutator_arg.key)) { |
Yuchen Zeng | 4564b8c | 2017-03-14 01:21:57 -0700 | [diff] [blame] | 114 | GPR_ASSERT(!replaced); |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 115 | it->value.pointer.vtable->destroy(&exec_ctx, it->value.pointer.p); |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 116 | it->value.pointer = mutator_arg.value.pointer; |
Yuchen Zeng | 4564b8c | 2017-03-14 01:21:57 -0700 | [diff] [blame] | 117 | replaced = true; |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 120 | grpc_exec_ctx_finish(&exec_ctx); |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 121 | if (!replaced) { |
| 122 | args_.push_back(mutator_arg); |
| 123 | } |
| 124 | } |
| 125 | |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 126 | // Note: a second call to this will add in front the result of the first call. |
yang-g | 9384dd9 | 2016-02-17 14:25:23 -0800 | [diff] [blame] | 127 | // An example is calling this on a copy of ChannelArguments which already has a |
| 128 | // prefix. The user can build up a prefix string by calling this multiple times, |
| 129 | // each with more significant identifier. |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 130 | void ChannelArguments::SetUserAgentPrefix( |
| 131 | const grpc::string& user_agent_prefix) { |
| 132 | if (user_agent_prefix.empty()) { |
| 133 | return; |
| 134 | } |
| 135 | bool replaced = false; |
Yuchen Zeng | f5b6b5b | 2017-03-22 12:08:38 -0700 | [diff] [blame] | 136 | auto strings_it = strings_.begin(); |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 137 | for (auto it = args_.begin(); it != args_.end(); ++it) { |
| 138 | const grpc_arg& arg = *it; |
Yuchen Zeng | f5b6b5b | 2017-03-22 12:08:38 -0700 | [diff] [blame] | 139 | ++strings_it; |
| 140 | if (arg.type == GRPC_ARG_STRING) { |
| 141 | if (grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) { |
| 142 | GPR_ASSERT(arg.value.string == strings_it->c_str()); |
| 143 | *(strings_it) = user_agent_prefix + " " + arg.value.string; |
| 144 | it->value.string = const_cast<char*>(strings_it->c_str()); |
| 145 | replaced = true; |
| 146 | break; |
| 147 | } |
| 148 | ++strings_it; |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | if (!replaced) { |
| 152 | SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix); |
| 153 | } |
| 154 | } |
| 155 | |
Craig Tiller | afcc875 | 2016-10-18 16:10:06 -0700 | [diff] [blame] | 156 | void ChannelArguments::SetResourceQuota( |
| 157 | const grpc::ResourceQuota& resource_quota) { |
Craig Tiller | 153eaa7 | 2016-10-21 13:52:36 -0700 | [diff] [blame] | 158 | SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA, |
| 159 | resource_quota.c_resource_quota(), |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 160 | grpc_resource_quota_arg_vtable()); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Mark D. Roth | f0e1778 | 2017-01-19 08:41:15 -0800 | [diff] [blame] | 163 | void ChannelArguments::SetMaxReceiveMessageSize(int size) { |
| 164 | SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, size); |
| 165 | } |
| 166 | |
| 167 | void ChannelArguments::SetMaxSendMessageSize(int size) { |
| 168 | SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, size); |
| 169 | } |
| 170 | |
Mark D. Roth | 7190b0c | 2016-10-26 09:02:37 -0700 | [diff] [blame] | 171 | void ChannelArguments::SetLoadBalancingPolicyName( |
| 172 | const grpc::string& lb_policy_name) { |
| 173 | SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name); |
| 174 | } |
| 175 | |
Mark D. Roth | dc5d60b | 2016-11-09 14:43:07 -0800 | [diff] [blame] | 176 | void ChannelArguments::SetServiceConfigJSON( |
| 177 | const grpc::string& service_config_json) { |
| 178 | SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json); |
| 179 | } |
| 180 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 181 | void ChannelArguments::SetInt(const grpc::string& key, int value) { |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 182 | grpc_arg arg; |
| 183 | arg.type = GRPC_ARG_INTEGER; |
| 184 | strings_.push_back(key); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 185 | arg.key = const_cast<char*>(strings_.back().c_str()); |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 186 | arg.value.integer = value; |
| 187 | |
| 188 | args_.push_back(arg); |
| 189 | } |
| 190 | |
yang-g | 5270559 | 2015-11-25 11:45:33 -0800 | [diff] [blame] | 191 | void ChannelArguments::SetPointer(const grpc::string& key, void* value) { |
Craig Tiller | edc2fff | 2016-01-13 06:54:27 -0800 | [diff] [blame] | 192 | static const grpc_arg_pointer_vtable vtable = { |
David Garcia Quintas | 4105505 | 2016-02-17 18:01:18 -0800 | [diff] [blame] | 193 | &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy, |
| 194 | &PointerVtableMembers::Compare}; |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 195 | SetPointerWithVtable(key, value, &vtable); |
| 196 | } |
| 197 | |
| 198 | void ChannelArguments::SetPointerWithVtable( |
| 199 | const grpc::string& key, void* value, |
| 200 | const grpc_arg_pointer_vtable* vtable) { |
yang-g | 5270559 | 2015-11-25 11:45:33 -0800 | [diff] [blame] | 201 | grpc_arg arg; |
| 202 | arg.type = GRPC_ARG_POINTER; |
| 203 | strings_.push_back(key); |
| 204 | arg.key = const_cast<char*>(strings_.back().c_str()); |
Yuchen Zeng | 4564b8c | 2017-03-14 01:21:57 -0700 | [diff] [blame] | 205 | arg.value.pointer.p = vtable->copy(value); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 206 | arg.value.pointer.vtable = vtable; |
yang-g | 5270559 | 2015-11-25 11:45:33 -0800 | [diff] [blame] | 207 | args_.push_back(arg); |
| 208 | } |
| 209 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 210 | void ChannelArguments::SetString(const grpc::string& key, |
| 211 | const grpc::string& value) { |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 212 | grpc_arg arg; |
| 213 | arg.type = GRPC_ARG_STRING; |
| 214 | strings_.push_back(key); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 215 | arg.key = const_cast<char*>(strings_.back().c_str()); |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 216 | strings_.push_back(value); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 217 | arg.value.string = const_cast<char*>(strings_.back().c_str()); |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 218 | |
| 219 | args_.push_back(arg); |
| 220 | } |
| 221 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 222 | void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const { |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 223 | channel_args->num_args = args_.size(); |
| 224 | if (channel_args->num_args > 0) { |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 225 | channel_args->args = const_cast<grpc_arg*>(&args_[0]); |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 226 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 229 | } // namespace grpc |