blob: 7d97b7955311eb6539f1a0a6b2bd44fbbb1cbe3d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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 */
33
34#include <grpc/grpc.h>
35#include "src/core/channel/channel_args.h"
Craig Tiller485d7762015-01-23 12:54:05 -080036#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
38#include <grpc/support/alloc.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070039#include <grpc/support/string_util.h>
David Garcia Quintasb58b3462015-08-09 13:52:19 -070040#include <grpc/support/useful.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
42#include <string.h>
43
44static grpc_arg copy_arg(const grpc_arg *src) {
45 grpc_arg dst;
46 dst.type = src->type;
47 dst.key = gpr_strdup(src->key);
48 switch (dst.type) {
49 case GRPC_ARG_STRING:
50 dst.value.string = gpr_strdup(src->value.string);
51 break;
52 case GRPC_ARG_INTEGER:
53 dst.value.integer = src->value.integer;
54 break;
55 case GRPC_ARG_POINTER:
56 dst.value.pointer = src->value.pointer;
Craig Tiller94022a72015-01-23 08:18:47 -080057 dst.value.pointer.p = src->value.pointer.copy
58 ? src->value.pointer.copy(src->value.pointer.p)
59 : src->value.pointer.p;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060 break;
61 }
62 return dst;
63}
64
65grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src,
Craig Tillerd9a50882015-06-29 15:57:36 -070066 const grpc_arg *to_add,
67 size_t num_to_add) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068 grpc_channel_args *dst = gpr_malloc(sizeof(grpc_channel_args));
69 size_t i;
70 size_t src_num_args = (src == NULL) ? 0 : src->num_args;
71 if (!src && !to_add) {
72 dst->num_args = 0;
73 dst->args = NULL;
74 return dst;
75 }
Craig Tillerd9a50882015-06-29 15:57:36 -070076 dst->num_args = src_num_args + num_to_add;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077 dst->args = gpr_malloc(sizeof(grpc_arg) * dst->num_args);
78 for (i = 0; i < src_num_args; i++) {
79 dst->args[i] = copy_arg(&src->args[i]);
80 }
Craig Tillerd9a50882015-06-29 15:57:36 -070081 for (i = 0; i < num_to_add; i++) {
82 dst->args[i + src_num_args] = copy_arg(&to_add[i]);
83 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084 return dst;
85}
86
87grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src) {
Craig Tillerd9a50882015-06-29 15:57:36 -070088 return grpc_channel_args_copy_and_add(src, NULL, 0);
89}
90
91grpc_channel_args *grpc_channel_args_merge(const grpc_channel_args *a,
92 const grpc_channel_args *b) {
93 return grpc_channel_args_copy_and_add(a, b->args, b->num_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094}
95
96void grpc_channel_args_destroy(grpc_channel_args *a) {
97 size_t i;
98 for (i = 0; i < a->num_args; i++) {
99 switch (a->args[i].type) {
100 case GRPC_ARG_STRING:
101 gpr_free(a->args[i].value.string);
102 break;
103 case GRPC_ARG_INTEGER:
104 break;
105 case GRPC_ARG_POINTER:
Craig Tillerc6dffe52015-01-21 13:00:10 -0800106 if (a->args[i].value.pointer.destroy) {
107 a->args[i].value.pointer.destroy(a->args[i].value.pointer.p);
108 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 break;
110 }
111 gpr_free(a->args[i].key);
112 }
113 gpr_free(a->args);
114 gpr_free(a);
115}
116
117int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) {
David Garcia Quintas20afd462015-07-06 23:01:39 -0700118 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119 if (a == NULL) return 0;
120 for (i = 0; i < a->num_args; i++) {
121 if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_CENSUS)) {
122 return a->args[i].value.integer != 0;
123 }
124 }
125 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800126}
David Garcia Quintascc6c43c2015-06-16 11:35:41 -0700127
David Garcia Quintascadbf222015-07-17 15:33:13 -0700128grpc_compression_algorithm grpc_channel_args_get_compression_algorithm(
David Garcia Quintascc6c43c2015-06-16 11:35:41 -0700129 const grpc_channel_args *a) {
130 size_t i;
David Garcia Quintas20afd462015-07-06 23:01:39 -0700131 if (a == NULL) return 0;
132 for (i = 0; i < a->num_args; ++i) {
133 if (a->args[i].type == GRPC_ARG_INTEGER &&
David Garcia Quintascadbf222015-07-17 15:33:13 -0700134 !strcmp(GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key)) {
David Garcia Quintas20afd462015-07-06 23:01:39 -0700135 return a->args[i].value.integer;
136 break;
David Garcia Quintascc6c43c2015-06-16 11:35:41 -0700137 }
138 }
David Garcia Quintascadbf222015-07-17 15:33:13 -0700139 return GRPC_COMPRESS_NONE;
David Garcia Quintascc6c43c2015-06-16 11:35:41 -0700140}
141
David Garcia Quintascadbf222015-07-17 15:33:13 -0700142grpc_channel_args *grpc_channel_args_set_compression_algorithm(
143 grpc_channel_args *a, grpc_compression_algorithm algorithm) {
David Garcia Quintascc6c43c2015-06-16 11:35:41 -0700144 grpc_arg tmp;
145 tmp.type = GRPC_ARG_INTEGER;
David Garcia Quintascadbf222015-07-17 15:33:13 -0700146 tmp.key = GRPC_COMPRESSION_ALGORITHM_ARG;
147 tmp.value.integer = algorithm;
David Garcia Quintas17bb6492015-07-08 15:16:22 -0700148 return grpc_channel_args_copy_and_add(a, &tmp, 1);
David Garcia Quintascc6c43c2015-06-16 11:35:41 -0700149}
David Garcia Quintasb58b3462015-08-09 13:52:19 -0700150
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700151/** Returns the compression algorithm's enabled states bitset from \a a. If not
152 * found, return a biset will all algorithms enabled */
David Garcia Quintasb58b3462015-08-09 13:52:19 -0700153static gpr_uint32 find_compression_algorithm_states_bitset(
154 const grpc_channel_args *a) {
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700155 gpr_uint32 states_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
156 if (a != NULL) {
157 size_t i;
158 for (i = 0; i < a->num_args; ++i) {
159 if (a->args[i].type == GRPC_ARG_INTEGER &&
160 !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) {
161 states_bitset = a->args[i].value.integer;
162 break;
163 }
David Garcia Quintasb58b3462015-08-09 13:52:19 -0700164 }
165 }
166 return states_bitset;
167}
168
169grpc_channel_args *grpc_channel_args_compression_algorithm_set_state(
170 grpc_channel_args *a,
171 grpc_compression_algorithm algorithm,
172 int state) {
173 gpr_uint32 states_bitset = find_compression_algorithm_states_bitset(a);
174 grpc_arg tmp;
175
176 if (state != 0) {
177 GPR_BITSET(&states_bitset, algorithm);
178 } else {
179 GPR_BITCLEAR(&states_bitset, algorithm);
180 }
181
182 tmp.type = GRPC_ARG_INTEGER;
183 tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG;
184 tmp.value.integer = states_bitset;
185 return grpc_channel_args_copy_and_add(a, &tmp, 1);
186}
187
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700188int grpc_channel_args_compression_algorithm_get_states(
189 const grpc_channel_args *a) {
190 return find_compression_algorithm_states_bitset(a);
David Garcia Quintasb58b3462015-08-09 13:52:19 -0700191}