blob: c32c9af50e58574b9957848aadb22766713152a8 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Craig Tiller42bc87c2015-02-23 08:50:19 -080019#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080020
yangg4105e2b2015-01-09 14:19:44 -080021#include <string.h>
22
Craig Tiller2c8063c2016-03-22 22:12:15 -070023#include <grpc/support/alloc.h>
24#include <grpc/support/log.h>
Craig Tiller3bf289d2017-03-31 14:32:51 -070025
Craig Tiller85db7792017-04-11 10:46:31 -070026#include "src/core/lib/support/atomic.h"
27
Craig Tiller9533d042016-03-25 17:11:06 -070028#include "src/core/lib/channel/channel_stack.h"
29#include "src/core/lib/support/string.h"
30#include "src/core/lib/surface/api_trace.h"
31#include "src/core/lib/surface/call.h"
32#include "src/core/lib/surface/channel.h"
Craig Tiller3bf289d2017-03-31 14:32:51 -070033#include "src/core/lib/surface/lame_client.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080034#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Craig Tiller3bf289d2017-03-31 14:32:51 -070036namespace grpc_core {
37
38namespace {
39
40struct CallData {
Craig Tillerbaa14a92017-11-03 09:09:36 -070041 grpc_call_combiner* call_combiner;
Craig Tillerbec41a22015-04-27 18:47:40 -070042 grpc_linked_mdelem status;
43 grpc_linked_mdelem details;
Craig Tiller85db7792017-04-11 10:46:31 -070044 grpc_core::atomic<bool> filled_metadata;
Craig Tiller3bf289d2017-03-31 14:32:51 -070045};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Craig Tiller3bf289d2017-03-31 14:32:51 -070047struct ChannelData {
yang-gc31cd862015-08-17 15:37:27 -070048 grpc_status_code error_code;
Craig Tillerbaa14a92017-11-03 09:09:36 -070049 const char* error_message;
Craig Tiller3bf289d2017-03-31 14:32:51 -070050};
yangg4105e2b2015-01-09 14:19:44 -080051
Craig Tillerbaa14a92017-11-03 09:09:36 -070052static void fill_metadata(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
53 grpc_metadata_batch* mdb) {
54 CallData* calld = reinterpret_cast<CallData*>(elem->call_data);
Craig Tiller3bf289d2017-03-31 14:32:51 -070055 bool expected = false;
56 if (!calld->filled_metadata.compare_exchange_strong(
Craig Tiller85db7792017-04-11 10:46:31 -070057 expected, true, grpc_core::memory_order_relaxed,
58 grpc_core::memory_order_relaxed)) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080059 return;
60 }
Craig Tillerbaa14a92017-11-03 09:09:36 -070061 ChannelData* chand = reinterpret_cast<ChannelData*>(elem->channel_data);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080062 char tmp[GPR_LTOA_MIN_BUFSIZE];
63 gpr_ltoa(chand->error_code, tmp);
Craig Tiller7c70b6c2017-01-23 07:48:42 -080064 calld->status.md = grpc_mdelem_from_slices(
65 exec_ctx, GRPC_MDSTR_GRPC_STATUS, grpc_slice_from_copied_string(tmp));
66 calld->details.md = grpc_mdelem_from_slices(
67 exec_ctx, GRPC_MDSTR_GRPC_MESSAGE,
68 grpc_slice_from_copied_string(chand->error_message));
Craig Tiller4782d922017-11-10 09:53:21 -080069 calld->status.prev = calld->details.next = nullptr;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080070 calld->status.next = &calld->details;
71 calld->details.prev = &calld->status;
72 mdb->list.head = &calld->status;
73 mdb->list.tail = &calld->details;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080074 mdb->list.count = 2;
Craig Tiller89c14282017-07-19 15:32:27 -070075 mdb->deadline = GRPC_MILLIS_INF_FUTURE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080076}
77
Craig Tillere1b51da2017-03-31 15:44:33 -070078static void lame_start_transport_stream_op_batch(
Craig Tillerbaa14a92017-11-03 09:09:36 -070079 grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
80 grpc_transport_stream_op_batch* op) {
81 CallData* calld = reinterpret_cast<CallData*>(elem->call_data);
Craig Tiller72920cc2017-03-10 10:20:17 -080082 if (op->recv_initial_metadata) {
83 fill_metadata(exec_ctx, elem,
84 op->payload->recv_initial_metadata.recv_initial_metadata);
85 } else if (op->recv_trailing_metadata) {
86 fill_metadata(exec_ctx, elem,
87 op->payload->recv_trailing_metadata.recv_trailing_metadata);
Craig Tillera82950e2015-09-22 12:33:20 -070088 }
Craig Tillera0f3abd2017-03-31 15:42:16 -070089 grpc_transport_stream_op_batch_finish_with_failure(
Mark D. Roth76e264b2017-08-25 09:03:33 -070090 exec_ctx, op, GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"),
91 calld->call_combiner);
Craig Tiller1b22b9d2015-07-20 13:42:22 -070092}
93
Craig Tillerbaa14a92017-11-03 09:09:36 -070094static void lame_get_channel_info(grpc_exec_ctx* exec_ctx,
95 grpc_channel_element* elem,
96 const grpc_channel_info* channel_info) {}
Mark D. Rothb2d24882016-10-27 15:44:07 -070097
Craig Tillerbaa14a92017-11-03 09:09:36 -070098static void lame_start_transport_op(grpc_exec_ctx* exec_ctx,
99 grpc_channel_element* elem,
100 grpc_transport_op* op) {
Craig Tillera82950e2015-09-22 12:33:20 -0700101 if (op->on_connectivity_state_change) {
Craig Tiller48ed92e2016-06-02 11:07:12 -0700102 GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_SHUTDOWN);
103 *op->connectivity_state = GRPC_CHANNEL_SHUTDOWN;
ncteisen274bbbe2017-06-08 14:57:11 -0700104 GRPC_CLOSURE_SCHED(exec_ctx, op->on_connectivity_state_change,
Craig Tiller91031da2016-12-28 15:44:25 -0800105 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700106 }
Craig Tiller4782d922017-11-10 09:53:21 -0800107 if (op->send_ping != nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700108 GRPC_CLOSURE_SCHED(
ncteisen4b36a3d2017-03-13 19:08:06 -0700109 exec_ctx, op->send_ping,
110 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
Craig Tiller79310ab2016-04-15 15:09:48 -0700111 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700112 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tiller4782d922017-11-10 09:53:21 -0800113 if (op->on_consumed != nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700114 GRPC_CLOSURE_SCHED(exec_ctx, op->on_consumed, GRPC_ERROR_NONE);
Craig Tiller57726ca2016-09-12 11:59:45 -0700115 }
nnoble0c475f02014-12-05 15:37:39 -0800116}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
Craig Tillerbaa14a92017-11-03 09:09:36 -0700118static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx,
119 grpc_call_element* elem,
120 const grpc_call_element_args* args) {
121 CallData* calld = reinterpret_cast<CallData*>(elem->call_data);
Mark D. Roth76e264b2017-08-25 09:03:33 -0700122 calld->call_combiner = args->call_combiner;
Mark D. Roth0badbe82016-06-23 10:15:12 -0700123 return GRPC_ERROR_NONE;
124}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125
Craig Tillerbaa14a92017-11-03 09:09:36 -0700126static void destroy_call_elem(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
127 const grpc_call_final_info* final_info,
128 grpc_closure* then_schedule_closure) {
ncteisen274bbbe2017-06-08 14:57:11 -0700129 GRPC_CLOSURE_SCHED(exec_ctx, then_schedule_closure, GRPC_ERROR_NONE);
Craig Tiller2c8063c2016-03-22 22:12:15 -0700130}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
Craig Tillerbaa14a92017-11-03 09:09:36 -0700132static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx,
133 grpc_channel_element* elem,
134 grpc_channel_element_args* args) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800135 GPR_ASSERT(args->is_first);
136 GPR_ASSERT(args->is_last);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800137 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138}
139
Craig Tillerbaa14a92017-11-03 09:09:36 -0700140static void destroy_channel_elem(grpc_exec_ctx* exec_ctx,
141 grpc_channel_element* elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142
Craig Tiller3bf289d2017-03-31 14:32:51 -0700143} // namespace
144
145} // namespace grpc_core
146
ncteisenadbfbd52017-11-16 15:35:45 -0800147const grpc_channel_filter grpc_lame_filter = {
Craig Tillereb364372017-04-07 17:08:42 -0700148 grpc_core::lame_start_transport_stream_op_batch,
Craig Tiller3bf289d2017-03-31 14:32:51 -0700149 grpc_core::lame_start_transport_op,
150 sizeof(grpc_core::CallData),
151 grpc_core::init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700152 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tiller3bf289d2017-03-31 14:32:51 -0700153 grpc_core::destroy_call_elem,
154 sizeof(grpc_core::ChannelData),
155 grpc_core::init_channel_elem,
156 grpc_core::destroy_channel_elem,
Craig Tiller3bf289d2017-03-31 14:32:51 -0700157 grpc_core::lame_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -0700158 "lame-client",
Craig Tiller87d5b192015-04-16 14:37:57 -0700159};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160
Craig Tillerbaa14a92017-11-03 09:09:36 -0700161#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack*)((c) + 1))
yang-gc31cd862015-08-17 15:37:27 -0700162
Craig Tillerbaa14a92017-11-03 09:09:36 -0700163grpc_channel* grpc_lame_client_channel_create(const char* target,
Craig Tillera82950e2015-09-22 12:33:20 -0700164 grpc_status_code error_code,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700165 const char* error_message) {
Craig Tiller178edfa2016-02-17 20:54:46 -0800166 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700167 grpc_channel_element* elem;
Craig Tillerbe98d242017-11-10 15:26:57 -0800168 grpc_channel* channel = grpc_channel_create(
169 &exec_ctx, target, nullptr, GRPC_CLIENT_LAME_CHANNEL, nullptr);
Craig Tillera82950e2015-09-22 12:33:20 -0700170 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700171 GRPC_API_TRACE(
172 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700173 "error_message=%s)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700174 3, (target, (int)error_code, error_message));
Craig Tiller178edfa2016-02-17 20:54:46 -0800175 GPR_ASSERT(elem->filter == &grpc_lame_filter);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700176 auto chand = reinterpret_cast<grpc_core::ChannelData*>(elem->channel_data);
yang-gc31cd862015-08-17 15:37:27 -0700177 chand->error_code = error_code;
178 chand->error_message = error_message;
Craig Tillera82950e2015-09-22 12:33:20 -0700179 grpc_exec_ctx_finish(&exec_ctx);
yang-gc31cd862015-08-17 15:37:27 -0700180 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800181}