blob: 65f327749999493476cf9d61db3c49e67add9e3e [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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-g9e2f90c2015-08-21 15:35:03 -070033#include <grpc++/support/channel_arguments.h>
nnoble0c475f02014-12-05 15:37:39 -080034
yang-gd59ad7e2016-02-10 12:42:53 -080035#include <sstream>
36
Craig Tillered913242016-11-04 16:31:30 -070037#include <grpc++/grpc++.h>
Craig Tiller20afa3d2016-10-17 14:52:14 -070038#include <grpc++/resource_quota.h>
yang-gd59ad7e2016-02-10 12:42:53 -080039#include <grpc/impl/codegen/grpc_types.h>
Craig Tiller0dc5e6c2015-07-10 10:07:53 -070040#include <grpc/support/log.h>
Craig Tillerc5866662016-11-16 15:25:00 -080041extern "C" {
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/channel/channel_args.h"
Craig Tillerc5866662016-11-16 15:25:00 -080043#include "src/core/lib/iomgr/exec_ctx.h"
Yuchen Zengde3daf52016-10-13 17:26:26 -070044#include "src/core/lib/iomgr/socket_mutator.h"
Craig Tillerc5866662016-11-16 15:25:00 -080045}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046namespace grpc {
47
yang-gd59ad7e2016-02-10 12:42:53 -080048ChannelArguments::ChannelArguments() {
yang-gd59ad7e2016-02-10 12:42:53 -080049 // This will be ignored if used on the server side.
Craig Tillered913242016-11-04 16:31:30 -070050 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, "grpc-c++/" + Version());
yang-gd59ad7e2016-02-10 12:42:53 -080051}
52
Craig Tiller0dc5e6c2015-07-10 10:07:53 -070053ChannelArguments::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 Tilleredc2fff2016-01-13 06:54:27 -080077 ap.value.pointer.p = a->value.pointer.vtable->copy(ap.value.pointer.p);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -070078 break;
79 }
80 args_.push_back(ap);
81 }
82}
83
84void ChannelArguments::Swap(ChannelArguments& other) {
85 args_.swap(other.args_);
86 strings_.swap(other.strings_);
87}
88
Craig Tillerbf6abee2015-07-19 22:31:04 -070089void ChannelArguments::SetCompressionAlgorithm(
David Garcia Quintascadbf222015-07-17 15:33:13 -070090 grpc_compression_algorithm algorithm) {
David Garcia Quintas183ba022016-05-12 17:08:19 -070091 SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, algorithm);
David Garcia Quintascc6c43c2015-06-16 11:35:41 -070092}
93
Yuchen Zengde3daf52016-10-13 17:26:26 -070094void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) {
95 if (!mutator) {
96 return;
97 }
98 grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator);
99 bool replaced = false;
Craig Tillerc5866662016-11-16 15:25:00 -0800100 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengde3daf52016-10-13 17:26:26 -0700101 for (auto it = args_.begin(); it != args_.end(); ++it) {
102 if (it->type == mutator_arg.type &&
103 grpc::string(it->key) == grpc::string(mutator_arg.key)) {
Craig Tillerc5866662016-11-16 15:25:00 -0800104 it->value.pointer.vtable->destroy(&exec_ctx, it->value.pointer.p);
Yuchen Zengde3daf52016-10-13 17:26:26 -0700105 it->value.pointer = mutator_arg.value.pointer;
106 }
107 }
Craig Tillerc5866662016-11-16 15:25:00 -0800108 grpc_exec_ctx_finish(&exec_ctx);
Yuchen Zengde3daf52016-10-13 17:26:26 -0700109 if (!replaced) {
110 args_.push_back(mutator_arg);
111 }
112}
113
yang-gd59ad7e2016-02-10 12:42:53 -0800114// Note: a second call to this will add in front the result of the first call.
yang-g9384dd92016-02-17 14:25:23 -0800115// An example is calling this on a copy of ChannelArguments which already has a
116// prefix. The user can build up a prefix string by calling this multiple times,
117// each with more significant identifier.
yang-gd59ad7e2016-02-10 12:42:53 -0800118void ChannelArguments::SetUserAgentPrefix(
119 const grpc::string& user_agent_prefix) {
120 if (user_agent_prefix.empty()) {
121 return;
122 }
123 bool replaced = false;
124 for (auto it = args_.begin(); it != args_.end(); ++it) {
125 const grpc_arg& arg = *it;
126 if (arg.type == GRPC_ARG_STRING &&
127 grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) {
128 strings_.push_back(user_agent_prefix + " " + arg.value.string);
129 it->value.string = const_cast<char*>(strings_.back().c_str());
130 replaced = true;
131 break;
132 }
133 }
134 if (!replaced) {
135 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
136 }
137}
138
Craig Tillerafcc8752016-10-18 16:10:06 -0700139void ChannelArguments::SetResourceQuota(
140 const grpc::ResourceQuota& resource_quota) {
Craig Tiller153eaa72016-10-21 13:52:36 -0700141 SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA,
142 resource_quota.c_resource_quota(),
Craig Tiller20afa3d2016-10-17 14:52:14 -0700143 grpc_resource_quota_arg_vtable());
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700144}
145
Mark D. Rothf0e17782017-01-19 08:41:15 -0800146void ChannelArguments::SetMaxReceiveMessageSize(int size) {
147 SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, size);
148}
149
150void ChannelArguments::SetMaxSendMessageSize(int size) {
151 SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, size);
152}
153
Mark D. Roth7190b0c2016-10-26 09:02:37 -0700154void ChannelArguments::SetLoadBalancingPolicyName(
155 const grpc::string& lb_policy_name) {
156 SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name);
157}
158
Mark D. Rothdc5d60b2016-11-09 14:43:07 -0800159void ChannelArguments::SetServiceConfigJSON(
160 const grpc::string& service_config_json) {
161 SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json);
162}
163
Yang Gao6baa9b62015-03-17 10:49:39 -0700164void ChannelArguments::SetInt(const grpc::string& key, int value) {
yangg59dfc902014-12-19 14:00:14 -0800165 grpc_arg arg;
166 arg.type = GRPC_ARG_INTEGER;
167 strings_.push_back(key);
Yang Gao6baa9b62015-03-17 10:49:39 -0700168 arg.key = const_cast<char*>(strings_.back().c_str());
yangg59dfc902014-12-19 14:00:14 -0800169 arg.value.integer = value;
170
171 args_.push_back(arg);
172}
173
yang-g52705592015-11-25 11:45:33 -0800174void ChannelArguments::SetPointer(const grpc::string& key, void* value) {
Craig Tilleredc2fff2016-01-13 06:54:27 -0800175 static const grpc_arg_pointer_vtable vtable = {
David Garcia Quintas41055052016-02-17 18:01:18 -0800176 &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy,
177 &PointerVtableMembers::Compare};
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700178 SetPointerWithVtable(key, value, &vtable);
179}
180
181void ChannelArguments::SetPointerWithVtable(
182 const grpc::string& key, void* value,
183 const grpc_arg_pointer_vtable* vtable) {
yang-g52705592015-11-25 11:45:33 -0800184 grpc_arg arg;
185 arg.type = GRPC_ARG_POINTER;
186 strings_.push_back(key);
187 arg.key = const_cast<char*>(strings_.back().c_str());
188 arg.value.pointer.p = value;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700189 arg.value.pointer.vtable = vtable;
yang-g52705592015-11-25 11:45:33 -0800190 args_.push_back(arg);
191}
192
Yang Gao6baa9b62015-03-17 10:49:39 -0700193void ChannelArguments::SetString(const grpc::string& key,
194 const grpc::string& value) {
yangg59dfc902014-12-19 14:00:14 -0800195 grpc_arg arg;
196 arg.type = GRPC_ARG_STRING;
197 strings_.push_back(key);
Yang Gao6baa9b62015-03-17 10:49:39 -0700198 arg.key = const_cast<char*>(strings_.back().c_str());
yangg59dfc902014-12-19 14:00:14 -0800199 strings_.push_back(value);
Yang Gao6baa9b62015-03-17 10:49:39 -0700200 arg.value.string = const_cast<char*>(strings_.back().c_str());
yangg59dfc902014-12-19 14:00:14 -0800201
202 args_.push_back(arg);
203}
204
Yang Gao6baa9b62015-03-17 10:49:39 -0700205void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
yangg59dfc902014-12-19 14:00:14 -0800206 channel_args->num_args = args_.size();
207 if (channel_args->num_args > 0) {
Yang Gao6baa9b62015-03-17 10:49:39 -0700208 channel_args->args = const_cast<grpc_arg*>(&args_[0]);
yangg59dfc902014-12-19 14:00:14 -0800209 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210}
211
Craig Tiller190d3602015-02-18 09:23:38 -0800212} // namespace grpc