blob: 455d1cd7f4846fb620f6fb781b9b1f7a6f1585f8 [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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/surface/lame_client.h"
Craig Tiller178edfa2016-02-17 20:54:46 -080035
Craig Tiller42bc87c2015-02-23 08:50:19 -080036#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
yangg4105e2b2015-01-09 14:19:44 -080038#include <string.h>
39
Craig Tiller2c8063c2016-03-22 22:12:15 -070040#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/channel/channel_stack.h"
43#include "src/core/lib/support/string.h"
44#include "src/core/lib/surface/api_trace.h"
45#include "src/core/lib/surface/call.h"
46#include "src/core/lib/surface/channel.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080047#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Craig Tillera82950e2015-09-22 12:33:20 -070049typedef struct {
Craig Tillerbec41a22015-04-27 18:47:40 -070050 grpc_linked_mdelem status;
51 grpc_linked_mdelem details;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080052 gpr_atm filled_metadata;
Craig Tillerbec41a22015-04-27 18:47:40 -070053} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
Craig Tillera82950e2015-09-22 12:33:20 -070055typedef struct {
yang-gc31cd862015-08-17 15:37:27 -070056 grpc_status_code error_code;
57 const char *error_message;
Craig Tiller1b22b9d2015-07-20 13:42:22 -070058} channel_data;
yangg4105e2b2015-01-09 14:19:44 -080059
Craig Tillera59c16c2016-10-31 07:25:01 -070060static void fill_metadata(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
61 grpc_metadata_batch *mdb) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080062 call_data *calld = elem->call_data;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080063 if (!gpr_atm_no_barrier_cas(&calld->filled_metadata, 0, 1)) {
64 return;
65 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080066 channel_data *chand = elem->channel_data;
67 char tmp[GPR_LTOA_MIN_BUFSIZE];
68 gpr_ltoa(chand->error_code, tmp);
Craig Tiller7c70b6c2017-01-23 07:48:42 -080069 calld->status.md = grpc_mdelem_from_slices(
70 exec_ctx, GRPC_MDSTR_GRPC_STATUS, grpc_slice_from_copied_string(tmp));
71 calld->details.md = grpc_mdelem_from_slices(
72 exec_ctx, GRPC_MDSTR_GRPC_MESSAGE,
73 grpc_slice_from_copied_string(chand->error_message));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080074 calld->status.prev = calld->details.next = NULL;
75 calld->status.next = &calld->details;
76 calld->details.prev = &calld->status;
77 mdb->list.head = &calld->status;
78 mdb->list.tail = &calld->details;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080079 mdb->list.count = 2;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080080 mdb->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
81}
82
Craig Tillera0f3abd2017-03-31 15:42:16 -070083static void lame_start_transport_stream_op_batch(grpc_exec_ctx *exec_ctx,
Craig Tillera82950e2015-09-22 12:33:20 -070084 grpc_call_element *elem,
Craig Tillera0f3abd2017-03-31 15:42:16 -070085 grpc_transport_stream_op_batch *op) {
Craig Tillera82950e2015-09-22 12:33:20 -070086 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Craig Tiller72920cc2017-03-10 10:20:17 -080087 if (op->recv_initial_metadata) {
88 fill_metadata(exec_ctx, elem,
89 op->payload->recv_initial_metadata.recv_initial_metadata);
90 } else if (op->recv_trailing_metadata) {
91 fill_metadata(exec_ctx, elem,
92 op->payload->recv_trailing_metadata.recv_trailing_metadata);
Craig Tillera82950e2015-09-22 12:33:20 -070093 }
Craig Tillera0f3abd2017-03-31 15:42:16 -070094 grpc_transport_stream_op_batch_finish_with_failure(
ncteisen4b36a3d2017-03-13 19:08:06 -070095 exec_ctx, op,
96 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097}
98
Craig Tillera82950e2015-09-22 12:33:20 -070099static char *lame_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Craig Tiller7b435612015-11-24 08:15:05 -0800100 return NULL;
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700101}
102
Mark D. Rothb2d24882016-10-27 15:44:07 -0700103static void lame_get_channel_info(grpc_exec_ctx *exec_ctx,
104 grpc_channel_element *elem,
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700105 const grpc_channel_info *channel_info) {}
Mark D. Rothb2d24882016-10-27 15:44:07 -0700106
Craig Tillera82950e2015-09-22 12:33:20 -0700107static void lame_start_transport_op(grpc_exec_ctx *exec_ctx,
108 grpc_channel_element *elem,
109 grpc_transport_op *op) {
110 if (op->on_connectivity_state_change) {
Craig Tiller48ed92e2016-06-02 11:07:12 -0700111 GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_SHUTDOWN);
112 *op->connectivity_state = GRPC_CHANNEL_SHUTDOWN;
Craig Tiller91031da2016-12-28 15:44:25 -0800113 grpc_closure_sched(exec_ctx, op->on_connectivity_state_change,
114 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700115 }
Craig Tiller79310ab2016-04-15 15:09:48 -0700116 if (op->send_ping != NULL) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700117 grpc_closure_sched(
118 exec_ctx, op->send_ping,
119 GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
Craig Tiller79310ab2016-04-15 15:09:48 -0700120 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700121 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tiller57726ca2016-09-12 11:59:45 -0700122 if (op->on_consumed != NULL) {
Craig Tiller91031da2016-12-28 15:44:25 -0800123 grpc_closure_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE);
Craig Tiller57726ca2016-09-12 11:59:45 -0700124 }
nnoble0c475f02014-12-05 15:37:39 -0800125}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Mark D. Roth76d24422016-06-23 13:22:10 -0700127static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth0badbe82016-06-23 10:15:12 -0700128 grpc_call_element *elem,
Craig Tillerc52ba3a2017-02-15 22:57:43 -0800129 const grpc_call_element_args *args) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800130 call_data *calld = elem->call_data;
131 gpr_atm_no_barrier_store(&calld->filled_metadata, 0);
Mark D. Roth0badbe82016-06-23 10:15:12 -0700132 return GRPC_ERROR_NONE;
133}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134
Craig Tiller2c8063c2016-03-22 22:12:15 -0700135static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700136 const grpc_call_final_info *final_info,
Craig Tillere7a17022017-03-13 10:20:38 -0700137 grpc_closure *then_schedule_closure) {
138 grpc_closure_sched(exec_ctx, then_schedule_closure, GRPC_ERROR_NONE);
Craig Tiller2c8063c2016-03-22 22:12:15 -0700139}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140
Mark D. Rothc1087882016-11-18 10:54:45 -0800141static grpc_error *init_channel_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800142 grpc_channel_element *elem,
143 grpc_channel_element_args *args) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800144 GPR_ASSERT(args->is_first);
145 GPR_ASSERT(args->is_last);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800146 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147}
148
Craig Tillera82950e2015-09-22 12:33:20 -0700149static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
150 grpc_channel_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800151
Craig Tiller178edfa2016-02-17 20:54:46 -0800152const grpc_channel_filter grpc_lame_filter = {
Craig Tillera0f3abd2017-03-31 15:42:16 -0700153 lame_start_transport_stream_op_batch,
Craig Tillerf40df232016-03-25 13:38:14 -0700154 lame_start_transport_op,
155 sizeof(call_data),
156 init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700157 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700158 destroy_call_elem,
159 sizeof(channel_data),
160 init_channel_elem,
161 destroy_channel_elem,
162 lame_get_peer,
Mark D. Rothb2d24882016-10-27 15:44:07 -0700163 lame_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -0700164 "lame-client",
Craig Tiller87d5b192015-04-16 14:37:57 -0700165};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166
yang-gc31cd862015-08-17 15:37:27 -0700167#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
168
Craig Tillera82950e2015-09-22 12:33:20 -0700169grpc_channel *grpc_lame_client_channel_create(const char *target,
170 grpc_status_code error_code,
171 const char *error_message) {
Craig Tiller178edfa2016-02-17 20:54:46 -0800172 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
yang-gc31cd862015-08-17 15:37:27 -0700173 grpc_channel_element *elem;
174 channel_data *chand;
Craig Tiller178edfa2016-02-17 20:54:46 -0800175 grpc_channel *channel = grpc_channel_create(&exec_ctx, target, NULL,
176 GRPC_CLIENT_LAME_CHANNEL, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700177 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700178 GRPC_API_TRACE(
179 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700180 "error_message=%s)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700181 3, (target, (int)error_code, error_message));
Craig Tiller178edfa2016-02-17 20:54:46 -0800182 GPR_ASSERT(elem->filter == &grpc_lame_filter);
Craig Tillera82950e2015-09-22 12:33:20 -0700183 chand = (channel_data *)elem->channel_data;
yang-gc31cd862015-08-17 15:37:27 -0700184 chand->error_code = error_code;
185 chand->error_message = error_message;
Craig Tillera82950e2015-09-22 12:33:20 -0700186 grpc_exec_ctx_finish(&exec_ctx);
yang-gc31cd862015-08-17 15:37:27 -0700187 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800188}