blob: c277d7ebe84905948227bd68087c1a54404c1f1b [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 */
33
34#include <grpc++/client_context.h>
35
Craig Tillerf40df232016-03-25 13:38:14 -070036#include <grpc++/security/credentials.h>
37#include <grpc++/server_context.h>
38#include <grpc++/support/time.h>
David Garcia Quintasb523c732016-01-25 18:22:28 -080039#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/grpc.h>
yang-gc07d9bf2015-07-28 09:19:22 -070041#include <grpc/support/alloc.h>
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070042#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/channel/compress_filter.h"
yang-g3abe60b2015-07-06 14:00:36 -070045#include "src/cpp/common/create_auth_context.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
47namespace grpc {
48
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080049class DefaultGlobalClientCallbacks GRPC_FINAL
50 : public ClientContext::GlobalCallbacks {
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080051 public:
Bogdan Drutu618051c2016-01-14 08:54:43 -080052 ~DefaultGlobalClientCallbacks() GRPC_OVERRIDE {}
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080053 void DefaultConstructor(ClientContext* context) GRPC_OVERRIDE {}
Bogdan Drutu42ab3fa2015-12-11 19:48:19 -080054 void Destructor(ClientContext* context) GRPC_OVERRIDE {}
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080055};
56
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080057static DefaultGlobalClientCallbacks g_default_client_callbacks;
58static ClientContext::GlobalCallbacks* g_client_callbacks =
59 &g_default_client_callbacks;
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080060
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061ClientContext::ClientContext()
Craig Tillercf133f42015-02-26 14:05:56 -080062 : initial_metadata_received_(false),
Craig Tiller399b3c42016-04-01 12:24:18 -070063 fail_fast_(true),
64 idempotent_(false),
Craig Tillercf133f42015-02-26 14:05:56 -080065 call_(nullptr),
yang-ga89bf502015-11-17 14:19:17 -080066 call_canceled_(false),
Craig Tillerbb536142015-08-05 15:18:19 -070067 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080068 propagate_from_call_(nullptr) {
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080069 g_client_callbacks->DefaultConstructor(this);
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080070}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
72ClientContext::~ClientContext() {
73 if (call_) {
74 grpc_call_destroy(call_);
75 }
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080076 g_client_callbacks->Destructor(this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077}
78
Craig Tiller2c3be1d2015-08-05 15:59:27 -070079std::unique_ptr<ClientContext> ClientContext::FromServerContext(
80 const ServerContext& context, PropagationOptions options) {
81 std::unique_ptr<ClientContext> ctx(new ClientContext);
82 ctx->propagate_from_call_ = context.call_;
83 ctx->propagation_options_ = options;
Craig Tillerbb536142015-08-05 15:18:19 -070084 return ctx;
85}
86
Yang Gao6baa9b62015-03-17 10:49:39 -070087void ClientContext::AddMetadata(const grpc::string& meta_key,
88 const grpc::string& meta_value) {
Yang Gao968ca532015-02-11 16:23:47 -080089 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090}
91
Yang Gaoa8938922015-05-14 11:51:07 -070092void ClientContext::set_call(grpc_call* call,
yang-g8c2be9f2015-08-19 16:28:09 -070093 const std::shared_ptr<Channel>& channel) {
yang-ga89bf502015-11-17 14:19:17 -080094 grpc::unique_lock<grpc::mutex> lock(mu_);
Yang Gaoa8938922015-05-14 11:51:07 -070095 GPR_ASSERT(call_ == nullptr);
96 call_ = call;
97 channel_ = channel;
98 if (creds_ && !creds_->ApplyToCall(call_)) {
99 grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200100 "Failed to set credentials to rpc.", nullptr);
Yang Gaoa8938922015-05-14 11:51:07 -0700101 }
yang-ga89bf502015-11-17 14:19:17 -0800102 if (call_canceled_) {
103 grpc_call_cancel(call_, nullptr);
104 }
Yang Gaoa8938922015-05-14 11:51:07 -0700105}
106
Craig Tillerbf6abee2015-07-19 22:31:04 -0700107void ClientContext::set_compression_algorithm(
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700108 grpc_compression_algorithm algorithm) {
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200109 char* algorithm_name = nullptr;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700110 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
111 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
112 algorithm);
113 abort();
114 }
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200115 GPR_ASSERT(algorithm_name != nullptr);
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700116 AddMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name);
117}
118
yang-g85c04f92015-07-07 17:47:31 -0700119std::shared_ptr<const AuthContext> ClientContext::auth_context() const {
120 if (auth_context_.get() == nullptr) {
121 auth_context_ = CreateAuthContext(call_);
122 }
123 return auth_context_;
yang-g3abe60b2015-07-06 14:00:36 -0700124}
125
Yang Gao406b32f2015-02-13 16:25:33 -0800126void ClientContext::TryCancel() {
yang-ga89bf502015-11-17 14:19:17 -0800127 grpc::unique_lock<grpc::mutex> lock(mu_);
Yang Gao406b32f2015-02-13 16:25:33 -0800128 if (call_) {
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200129 grpc_call_cancel(call_, nullptr);
yang-ga89bf502015-11-17 14:19:17 -0800130 } else {
131 call_canceled_ = true;
Yang Gao406b32f2015-02-13 16:25:33 -0800132 }
133}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134
yang-gc07d9bf2015-07-28 09:19:22 -0700135grpc::string ClientContext::peer() const {
136 grpc::string peer;
137 if (call_) {
138 char* c_peer = grpc_call_get_peer(call_);
139 peer = c_peer;
140 gpr_free(c_peer);
141 }
142 return peer;
143}
144
Bogdan Drutu6af31bd2015-12-12 15:39:40 -0800145void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
146 GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks);
147 GPR_ASSERT(client_callbacks != NULL);
148 GPR_ASSERT(client_callbacks != &g_default_client_callbacks);
149 g_client_callbacks = client_callbacks;
Bogdan Drutu38d2ad62015-12-11 19:26:52 -0800150}
151
Craig Tiller190d3602015-02-18 09:23:38 -0800152} // namespace grpc