blob: 19b78369ddaf5b28c250f2706030563fbed100b0 [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"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tillera82950e2015-09-22 12:33:20 -070048typedef struct {
Craig Tillerbec41a22015-04-27 18:47:40 -070049 grpc_linked_mdelem status;
50 grpc_linked_mdelem details;
51} call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Craig Tillera82950e2015-09-22 12:33:20 -070053typedef struct {
yang-gc31cd862015-08-17 15:37:27 -070054 grpc_status_code error_code;
55 const char *error_message;
Craig Tiller1b22b9d2015-07-20 13:42:22 -070056} channel_data;
yangg4105e2b2015-01-09 14:19:44 -080057
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080058static void fill_metadata(grpc_call_element *elem, grpc_metadata_batch *mdb) {
59 call_data *calld = elem->call_data;
60 channel_data *chand = elem->channel_data;
61 char tmp[GPR_LTOA_MIN_BUFSIZE];
62 gpr_ltoa(chand->error_code, tmp);
Craig Tillerb2b42612015-11-20 12:02:17 -080063 calld->status.md = grpc_mdelem_from_strings("grpc-status", tmp);
64 calld->details.md =
65 grpc_mdelem_from_strings("grpc-message", chand->error_message);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080066 calld->status.prev = calld->details.next = NULL;
67 calld->status.next = &calld->details;
68 calld->details.prev = &calld->status;
69 mdb->list.head = &calld->status;
70 mdb->list.tail = &calld->details;
71 mdb->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
72}
73
Craig Tillera82950e2015-09-22 12:33:20 -070074static void lame_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
75 grpc_call_element *elem,
76 grpc_transport_stream_op *op) {
Craig Tillera82950e2015-09-22 12:33:20 -070077 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080078 if (op->recv_initial_metadata != NULL) {
79 fill_metadata(elem, op->recv_initial_metadata);
80 } else if (op->recv_trailing_metadata != NULL) {
81 fill_metadata(elem, op->recv_trailing_metadata);
Craig Tillera82950e2015-09-22 12:33:20 -070082 }
Craig Tillerf51457b2016-05-03 17:06:32 -070083 grpc_transport_stream_op_finish_with_failure(
84 exec_ctx, op, GRPC_ERROR_CREATE("lame client channel"));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085}
86
Craig Tillera82950e2015-09-22 12:33:20 -070087static char *lame_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Craig Tiller7b435612015-11-24 08:15:05 -080088 return NULL;
Craig Tiller1b22b9d2015-07-20 13:42:22 -070089}
90
Craig Tillera82950e2015-09-22 12:33:20 -070091static void lame_start_transport_op(grpc_exec_ctx *exec_ctx,
92 grpc_channel_element *elem,
93 grpc_transport_op *op) {
94 if (op->on_connectivity_state_change) {
Craig Tiller48ed92e2016-06-02 11:07:12 -070095 GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_SHUTDOWN);
96 *op->connectivity_state = GRPC_CHANNEL_SHUTDOWN;
Craig Tiller332f1b32016-05-24 13:21:21 -070097 grpc_exec_ctx_sched(exec_ctx, op->on_connectivity_state_change,
Craig Tiller77c983d2016-05-24 13:23:14 -070098 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -070099 }
100 if (op->on_consumed != NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700101 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700102 }
Craig Tiller79310ab2016-04-15 15:09:48 -0700103 if (op->send_ping != NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700104 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
Craig Tiller77c983d2016-05-24 13:23:14 -0700105 GRPC_ERROR_CREATE("lame client channel"), NULL);
Craig Tiller79310ab2016-04-15 15:09:48 -0700106 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700107 GRPC_ERROR_UNREF(op->disconnect_with_error);
nnoble0c475f02014-12-05 15:37:39 -0800108}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109
Mark D. Roth76d24422016-06-23 13:22:10 -0700110static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth0badbe82016-06-23 10:15:12 -0700111 grpc_call_element *elem,
112 grpc_call_element_args *args) {
113 return GRPC_ERROR_NONE;
114}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115
Craig Tiller2c8063c2016-03-22 22:12:15 -0700116static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700117 const grpc_call_final_info *final_info,
Craig Tiller2c8063c2016-03-22 22:12:15 -0700118 void *and_free_memory) {
119 gpr_free(and_free_memory);
120}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
Craig Tillera82950e2015-09-22 12:33:20 -0700122static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800123 grpc_channel_element *elem,
124 grpc_channel_element_args *args) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800125 GPR_ASSERT(args->is_first);
126 GPR_ASSERT(args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127}
128
Craig Tillera82950e2015-09-22 12:33:20 -0700129static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
130 grpc_channel_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
Craig Tiller178edfa2016-02-17 20:54:46 -0800132const grpc_channel_filter grpc_lame_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700133 lame_start_transport_stream_op,
134 lame_start_transport_op,
135 sizeof(call_data),
136 init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700137 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700138 destroy_call_elem,
139 sizeof(channel_data),
140 init_channel_elem,
141 destroy_channel_elem,
142 lame_get_peer,
143 "lame-client",
Craig Tiller87d5b192015-04-16 14:37:57 -0700144};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145
yang-gc31cd862015-08-17 15:37:27 -0700146#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
147
Craig Tillera82950e2015-09-22 12:33:20 -0700148grpc_channel *grpc_lame_client_channel_create(const char *target,
149 grpc_status_code error_code,
150 const char *error_message) {
Craig Tiller178edfa2016-02-17 20:54:46 -0800151 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
yang-gc31cd862015-08-17 15:37:27 -0700152 grpc_channel_element *elem;
153 channel_data *chand;
Craig Tiller178edfa2016-02-17 20:54:46 -0800154 grpc_channel *channel = grpc_channel_create(&exec_ctx, target, NULL,
155 GRPC_CLIENT_LAME_CHANNEL, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700156 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700157 GRPC_API_TRACE(
158 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700159 "error_message=%s)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700160 3, (target, (int)error_code, error_message));
Craig Tiller178edfa2016-02-17 20:54:46 -0800161 GPR_ASSERT(elem->filter == &grpc_lame_filter);
Craig Tillera82950e2015-09-22 12:33:20 -0700162 chand = (channel_data *)elem->channel_data;
yang-gc31cd862015-08-17 15:37:27 -0700163 chand->error_code = error_code;
164 chand->error_message = error_message;
Craig Tillera82950e2015-09-22 12:33:20 -0700165 grpc_exec_ctx_finish(&exec_ctx);
yang-gc31cd862015-08-17 15:37:27 -0700166 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800167}