Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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++/client_context.h> |
| 35 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 36 | #include <grpc++/security/credentials.h> |
| 37 | #include <grpc++/server_context.h> |
| 38 | #include <grpc++/support/time.h> |
David Garcia Quintas | b523c73 | 2016-01-25 18:22:28 -0800 | [diff] [blame] | 39 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/grpc.h> |
yang-g | c07d9bf | 2015-07-28 09:19:22 -0700 | [diff] [blame] | 41 | #include <grpc/support/alloc.h> |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 42 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 44 | #include "src/core/lib/channel/compress_filter.h" |
yang-g | 3abe60b | 2015-07-06 14:00:36 -0700 | [diff] [blame] | 45 | #include "src/cpp/common/create_auth_context.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
| 47 | namespace grpc { |
| 48 | |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 49 | class DefaultGlobalClientCallbacks GRPC_FINAL |
| 50 | : public ClientContext::GlobalCallbacks { |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 51 | public: |
Bogdan Drutu | 618051c | 2016-01-14 08:54:43 -0800 | [diff] [blame] | 52 | ~DefaultGlobalClientCallbacks() GRPC_OVERRIDE {} |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 53 | void DefaultConstructor(ClientContext* context) GRPC_OVERRIDE {} |
Bogdan Drutu | 42ab3fa | 2015-12-11 19:48:19 -0800 | [diff] [blame] | 54 | void Destructor(ClientContext* context) GRPC_OVERRIDE {} |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 57 | static DefaultGlobalClientCallbacks g_default_client_callbacks; |
| 58 | static ClientContext::GlobalCallbacks* g_client_callbacks = |
| 59 | &g_default_client_callbacks; |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 60 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | ClientContext::ClientContext() |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 62 | : initial_metadata_received_(false), |
Craig Tiller | 399b3c4 | 2016-04-01 12:24:18 -0700 | [diff] [blame] | 63 | fail_fast_(true), |
| 64 | idempotent_(false), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 65 | call_(nullptr), |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 66 | call_canceled_(false), |
Craig Tiller | bb53614 | 2015-08-05 15:18:19 -0700 | [diff] [blame] | 67 | deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)), |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 68 | propagate_from_call_(nullptr) { |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 69 | g_client_callbacks->DefaultConstructor(this); |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 70 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
| 72 | ClientContext::~ClientContext() { |
| 73 | if (call_) { |
| 74 | grpc_call_destroy(call_); |
| 75 | } |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 76 | g_client_callbacks->Destructor(this); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 79 | std::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 Tiller | bb53614 | 2015-08-05 15:18:19 -0700 | [diff] [blame] | 84 | return ctx; |
| 85 | } |
| 86 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 87 | void ClientContext::AddMetadata(const grpc::string& meta_key, |
| 88 | const grpc::string& meta_value) { |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 89 | send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 92 | void ClientContext::set_call(grpc_call* call, |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 93 | const std::shared_ptr<Channel>& channel) { |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 94 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 95 | 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" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 100 | "Failed to set credentials to rpc.", nullptr); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 101 | } |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 102 | if (call_canceled_) { |
| 103 | grpc_call_cancel(call_, nullptr); |
| 104 | } |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 107 | void ClientContext::set_compression_algorithm( |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 108 | grpc_compression_algorithm algorithm) { |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 109 | char* algorithm_name = nullptr; |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 110 | 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" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 115 | GPR_ASSERT(algorithm_name != nullptr); |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 116 | AddMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name); |
| 117 | } |
| 118 | |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 119 | std::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-g | 3abe60b | 2015-07-06 14:00:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 126 | void ClientContext::TryCancel() { |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 127 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 128 | if (call_) { |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 129 | grpc_call_cancel(call_, nullptr); |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 130 | } else { |
| 131 | call_canceled_ = true; |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 134 | |
yang-g | c07d9bf | 2015-07-28 09:19:22 -0700 | [diff] [blame] | 135 | grpc::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 Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 145 | void 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 Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 152 | } // namespace grpc |