Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | |
| 34 | #include <grpc/grpc.h> |
| 35 | #include "src/core/channel/channel_args.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 36 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
| 38 | #include <grpc/support/alloc.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 39 | #include <grpc/support/string_util.h> |
David Garcia Quintas | b58b346 | 2015-08-09 13:52:19 -0700 | [diff] [blame] | 40 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
| 42 | #include <string.h> |
| 43 | |
| 44 | static 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 Tiller | 94022a7 | 2015-01-23 08:18:47 -0800 | [diff] [blame] | 57 | dst.value.pointer.p = src->value.pointer.copy |
| 58 | ? src->value.pointer.copy(src->value.pointer.p) |
| 59 | : src->value.pointer.p; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | return dst; |
| 63 | } |
| 64 | |
| 65 | grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src, |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 66 | const grpc_arg *to_add, |
| 67 | size_t num_to_add) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | 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 Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 76 | dst->num_args = src_num_args + num_to_add; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | 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 Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 81 | for (i = 0; i < num_to_add; i++) { |
| 82 | dst->args[i + src_num_args] = copy_arg(&to_add[i]); |
| 83 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | return dst; |
| 85 | } |
| 86 | |
| 87 | grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src) { |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 88 | return grpc_channel_args_copy_and_add(src, NULL, 0); |
| 89 | } |
| 90 | |
| 91 | grpc_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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void 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 Tiller | c6dffe5 | 2015-01-21 13:00:10 -0800 | [diff] [blame] | 106 | if (a->args[i].value.pointer.destroy) { |
| 107 | a->args[i].value.pointer.destroy(a->args[i].value.pointer.p); |
| 108 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | gpr_free(a->args[i].key); |
| 112 | } |
| 113 | gpr_free(a->args); |
| 114 | gpr_free(a); |
| 115 | } |
| 116 | |
| 117 | int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) { |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 118 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 119 | 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 126 | } |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 127 | |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 128 | grpc_compression_algorithm grpc_channel_args_get_compression_algorithm( |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 129 | const grpc_channel_args *a) { |
| 130 | size_t i; |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 131 | 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 Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 134 | !strcmp(GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key)) { |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 135 | return a->args[i].value.integer; |
| 136 | break; |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 139 | return GRPC_COMPRESS_NONE; |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 140 | } |
| 141 | |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 142 | grpc_channel_args *grpc_channel_args_set_compression_algorithm( |
| 143 | grpc_channel_args *a, grpc_compression_algorithm algorithm) { |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 144 | grpc_arg tmp; |
| 145 | tmp.type = GRPC_ARG_INTEGER; |
David Garcia Quintas | cadbf22 | 2015-07-17 15:33:13 -0700 | [diff] [blame] | 146 | tmp.key = GRPC_COMPRESSION_ALGORITHM_ARG; |
| 147 | tmp.value.integer = algorithm; |
David Garcia Quintas | 17bb649 | 2015-07-08 15:16:22 -0700 | [diff] [blame] | 148 | return grpc_channel_args_copy_and_add(a, &tmp, 1); |
David Garcia Quintas | cc6c43c | 2015-06-16 11:35:41 -0700 | [diff] [blame] | 149 | } |
David Garcia Quintas | b58b346 | 2015-08-09 13:52:19 -0700 | [diff] [blame] | 150 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 151 | /** Returns the compression algorithm's enabled states bitset from \a a. If not |
| 152 | * found, return a biset will all algorithms enabled */ |
David Garcia Quintas | b58b346 | 2015-08-09 13:52:19 -0700 | [diff] [blame] | 153 | static gpr_uint32 find_compression_algorithm_states_bitset( |
| 154 | const grpc_channel_args *a) { |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 155 | 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 Quintas | b58b346 | 2015-08-09 13:52:19 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | return states_bitset; |
| 167 | } |
| 168 | |
| 169 | grpc_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 Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 188 | int grpc_channel_args_compression_algorithm_get_states( |
| 189 | const grpc_channel_args *a) { |
| 190 | return find_compression_algorithm_states_bitset(a); |
David Garcia Quintas | b58b346 | 2015-08-09 13:52:19 -0700 | [diff] [blame] | 191 | } |