diff options
author | yang-g <yangg@google.com> | 2016-02-10 12:42:53 -0800 |
---|---|---|
committer | yang-g <yangg@google.com> | 2016-02-10 12:42:53 -0800 |
commit | d59ad7ef393c7624c7035a09b488f630cbd96730 (patch) | |
tree | 6a3a233679828c63716d3e33314db93136c9d20e /src | |
parent | 852acc7deed2fb4587d930fb5c15bd72abc39c26 (diff) |
Provide explicit API for user to set user agent string prefix
Diffstat (limited to 'src')
-rw-r--r-- | src/cpp/client/create_channel.cc | 8 | ||||
-rw-r--r-- | src/cpp/common/channel_arguments.cc | 33 |
2 files changed, 33 insertions, 8 deletions
diff --git a/src/cpp/client/create_channel.cc b/src/cpp/client/create_channel.cc index fdaa28ffef..76a1b31e2f 100644 --- a/src/cpp/client/create_channel.cc +++ b/src/cpp/client/create_channel.cc @@ -32,7 +32,6 @@ */ #include <memory> -#include <sstream> #include <grpc++/channel.h> #include <grpc++/create_channel.h> @@ -56,13 +55,8 @@ std::shared_ptr<Channel> CreateCustomChannel( const ChannelArguments& args) { internal::GrpcLibrary init_lib; // We need to call init in case of a bad creds. - ChannelArguments cp_args = args; - std::ostringstream user_agent_prefix; - user_agent_prefix << "grpc-c++/" << grpc_version_string(); - cp_args.SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, - user_agent_prefix.str()); return creds - ? creds->CreateChannel(target, cp_args) + ? creds->CreateChannel(target, args) : CreateChannelInternal("", grpc_lame_client_channel_create( NULL, GRPC_STATUS_INVALID_ARGUMENT, "Invalid credentials.")); diff --git a/src/cpp/common/channel_arguments.cc b/src/cpp/common/channel_arguments.cc index 90cd5136af..e23c964797 100644 --- a/src/cpp/common/channel_arguments.cc +++ b/src/cpp/common/channel_arguments.cc @@ -30,14 +30,23 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ - #include <grpc++/support/channel_arguments.h> +#include <sstream> + +#include <grpc/impl/codegen/grpc_types.h> #include <grpc/support/log.h> #include "src/core/channel/channel_args.h" namespace grpc { +ChannelArguments::ChannelArguments() { + std::ostringstream user_agent_prefix; + user_agent_prefix << "grpc-c++/" << grpc_version_string(); + // This will be ignored if used on the server side. + SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix.str()); +} + ChannelArguments::ChannelArguments(const ChannelArguments& other) : strings_(other.strings_) { args_.reserve(other.args_.size()); @@ -81,6 +90,28 @@ void ChannelArguments::SetCompressionAlgorithm( SetInt(GRPC_COMPRESSION_ALGORITHM_ARG, algorithm); } +// Note: a second call to this will add in front the result of the first call. +void ChannelArguments::SetUserAgentPrefix( + const grpc::string& user_agent_prefix) { + if (user_agent_prefix.empty()) { + return; + } + bool replaced = false; + for (auto it = args_.begin(); it != args_.end(); ++it) { + const grpc_arg& arg = *it; + if (arg.type == GRPC_ARG_STRING && + grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) { + strings_.push_back(user_agent_prefix + " " + arg.value.string); + it->value.string = const_cast<char*>(strings_.back().c_str()); + replaced = true; + break; + } + } + if (!replaced) { + SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix); + } +} + void ChannelArguments::SetInt(const grpc::string& key, int value) { grpc_arg arg; arg.type = GRPC_ARG_INTEGER; |