From a4c4f02a63eb70fcd21a102cde9aa536dcd17f67 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Fri, 21 Aug 2015 00:05:42 -0700 Subject: Added C API functions for compression args handling (w/ tests) --- src/core/channel/channel_args.c | 62 +++++++++++++++++++++++++++++++++++++++++ src/core/channel/channel_args.h | 17 +++++++++++ 2 files changed, 79 insertions(+) (limited to 'src/core/channel') diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index c430b56fa2..dc66de7dd6 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -37,6 +37,7 @@ #include #include +#include #include @@ -146,3 +147,64 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm( tmp.value.integer = algorithm; return grpc_channel_args_copy_and_add(a, &tmp, 1); } + +/** Returns 1 if the argument for compression algorithm's enabled states bitset + * was found in \a a, returning the arg's value in \a states. Otherwise, returns + * 0. */ +static int find_compression_algorithm_states_bitset( + const grpc_channel_args *a, int **states_arg) { + if (a != NULL) { + size_t i; + for (i = 0; i < a->num_args; ++i) { + if (a->args[i].type == GRPC_ARG_INTEGER && + !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) { + *states_arg = &a->args[i].value.integer; + return 1; /* GPR_TRUE */ + } + } + } + return 0; /* GPR_FALSE */ +} + +grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( + grpc_channel_args *a, + grpc_compression_algorithm algorithm, + int state) { + int *states_arg; + grpc_channel_args *result = a; + const int states_arg_found = + find_compression_algorithm_states_bitset(a, &states_arg); + + if (states_arg_found) { + if (state != 0) { + GPR_BITSET(states_arg, algorithm); + } else { + GPR_BITCLEAR(states_arg, algorithm); + } + } else { + /* create a new arg */ + grpc_arg tmp; + tmp.type = GRPC_ARG_INTEGER; + tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG; + /* all enabled by default */ + tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; + if (state != 0) { + GPR_BITSET(&tmp.value.integer, algorithm); + } else { + GPR_BITCLEAR(&tmp.value.integer, algorithm); + } + result = grpc_channel_args_copy_and_add(a, &tmp, 1); + grpc_channel_args_destroy(a); + } + return result; +} + +int grpc_channel_args_compression_algorithm_get_states( + const grpc_channel_args *a) { + int *states_arg; + if (find_compression_algorithm_states_bitset(a, &states_arg)) { + return *states_arg; + } else { + return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */ + } +} diff --git a/src/core/channel/channel_args.h b/src/core/channel/channel_args.h index 7e6ddd3997..e557f9a9d9 100644 --- a/src/core/channel/channel_args.h +++ b/src/core/channel/channel_args.h @@ -67,4 +67,21 @@ grpc_compression_algorithm grpc_channel_args_get_compression_algorithm( grpc_channel_args *grpc_channel_args_set_compression_algorithm( grpc_channel_args *a, grpc_compression_algorithm algorithm); +/** Sets the support for the given compression algorithm. By default, all + * compression algorithms are enabled. It's an error to disable an algorithm set + * by grpc_channel_args_set_compression_algorithm. + * */ +grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( + grpc_channel_args *a, + grpc_compression_algorithm algorithm, + int enabled); + +/** Returns the bitset representing the support state (true for enabled, false + * for disabled) for compression algorithms. + * + * The i-th bit of the returned bitset corresponds to the i-th entry in the + * grpc_compression_algorithm enum. */ +int grpc_channel_args_compression_algorithm_get_states( + const grpc_channel_args *a); + #endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H */ -- cgit v1.2.3 From fe5f25490d4e290ecf2fc52de64c1230429fd0a3 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Mon, 24 Aug 2015 12:33:05 -0700 Subject: Improvements to the grpc_channel_args_compression_algorithm_set_state api --- src/core/channel/channel_args.c | 11 ++++++----- src/core/channel/channel_args.h | 7 +++++-- test/core/channel/channel_args_test.c | 29 +++++++++++++++++------------ 3 files changed, 28 insertions(+), 19 deletions(-) (limited to 'src/core/channel') diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index dc66de7dd6..54ee75af28 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -167,13 +167,13 @@ static int find_compression_algorithm_states_bitset( } grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( - grpc_channel_args *a, + grpc_channel_args **a, grpc_compression_algorithm algorithm, int state) { int *states_arg; - grpc_channel_args *result = a; + grpc_channel_args *result = *a; const int states_arg_found = - find_compression_algorithm_states_bitset(a, &states_arg); + find_compression_algorithm_states_bitset(*a, &states_arg); if (states_arg_found) { if (state != 0) { @@ -193,8 +193,9 @@ grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( } else { GPR_BITCLEAR(&tmp.value.integer, algorithm); } - result = grpc_channel_args_copy_and_add(a, &tmp, 1); - grpc_channel_args_destroy(a); + result = grpc_channel_args_copy_and_add(*a, &tmp, 1); + grpc_channel_args_destroy(*a); + *a = result; } return result; } diff --git a/src/core/channel/channel_args.h b/src/core/channel/channel_args.h index e557f9a9d9..06a6012dee 100644 --- a/src/core/channel/channel_args.h +++ b/src/core/channel/channel_args.h @@ -70,9 +70,12 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm( /** Sets the support for the given compression algorithm. By default, all * compression algorithms are enabled. It's an error to disable an algorithm set * by grpc_channel_args_set_compression_algorithm. - * */ + * + * Returns an instance will the updated algorithm states. The \a a pointer is + * modified to point to the returned instance (which may be different from the + * input value of \a a). */ grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( - grpc_channel_args *a, + grpc_channel_args **a, grpc_compression_algorithm algorithm, int enabled); diff --git a/test/core/channel/channel_args_test.c b/test/core/channel/channel_args_test.c index 227cc1f415..87f006acde 100644 --- a/test/core/channel/channel_args_test.c +++ b/test/core/channel/channel_args_test.c @@ -45,7 +45,7 @@ static void test_create(void) { grpc_arg arg_string; grpc_arg to_add[2]; grpc_channel_args *ch_args; - + arg_int.key = "int_arg"; arg_int.type = GRPC_ARG_INTEGER; arg_int.value.integer = 123; @@ -57,7 +57,7 @@ static void test_create(void) { to_add[0] = arg_int; to_add[1] = arg_string; ch_args = grpc_channel_args_copy_and_add(NULL, to_add, 2); - + GPR_ASSERT(ch_args->num_args == 2); GPR_ASSERT(strcmp(ch_args->args[0].key, arg_int.key) == 0); GPR_ASSERT(ch_args->args[0].type == arg_int.type); @@ -84,7 +84,7 @@ static void test_set_compression_algorithm(void) { } static void test_compression_algorithm_states(void) { - grpc_channel_args *ch_args; + grpc_channel_args *ch_args, *ch_args_wo_gzip, *ch_args_wo_gzip_deflate; int states_bitset; size_t i; @@ -97,12 +97,15 @@ static void test_compression_algorithm_states(void) { } /* disable gzip and deflate */ - ch_args = grpc_channel_args_compression_algorithm_set_state( - ch_args, GRPC_COMPRESS_GZIP, 0); - ch_args = grpc_channel_args_compression_algorithm_set_state( - ch_args, GRPC_COMPRESS_DEFLATE, 0); - - states_bitset = grpc_channel_args_compression_algorithm_get_states(ch_args); + ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state( + &ch_args, GRPC_COMPRESS_GZIP, 0); + GPR_ASSERT(ch_args == ch_args_wo_gzip); + ch_args_wo_gzip_deflate = grpc_channel_args_compression_algorithm_set_state( + &ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0); + GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate); + + states_bitset = grpc_channel_args_compression_algorithm_get_states( + ch_args_wo_gzip_deflate); for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE) { GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0); @@ -112,10 +115,12 @@ static void test_compression_algorithm_states(void) { } /* re-enabled gzip only */ - ch_args = grpc_channel_args_compression_algorithm_set_state( - ch_args, GRPC_COMPRESS_GZIP, 1); + ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state( + &ch_args_wo_gzip_deflate, GRPC_COMPRESS_GZIP, 1); + GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate); - states_bitset = grpc_channel_args_compression_algorithm_get_states(ch_args); + states_bitset = + grpc_channel_args_compression_algorithm_get_states(ch_args_wo_gzip); for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { if (i == GRPC_COMPRESS_DEFLATE) { GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0); -- cgit v1.2.3