blob: 58f89946d226b0776b8144eb1fbd74c60b6c7391 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tillera93a25f2016-01-28 13:55:49 -08003 * Copyright 2015-2016, 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 Tiller178edfa2016-02-17 20:54:46 -080034#include "src/core/surface/lame_client.h"
35
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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include "src/core/channel/channel_stack.h"
Craig Tiller485d7762015-01-23 12:54:05 -080041#include "src/core/support/string.h"
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070042#include "src/core/surface/api_trace.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include "src/core/surface/channel.h"
44#include "src/core/surface/call.h"
45#include <grpc/support/alloc.h>
46#include <grpc/support/log.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 Tillera44cbfc2016-02-03 16:02:49 -080083 grpc_transport_stream_op_finish_with_failure(exec_ctx, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084}
85
Craig Tillera82950e2015-09-22 12:33:20 -070086static char *lame_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Craig Tiller7b435612015-11-24 08:15:05 -080087 return NULL;
Craig Tiller1b22b9d2015-07-20 13:42:22 -070088}
89
Craig Tillera82950e2015-09-22 12:33:20 -070090static void lame_start_transport_op(grpc_exec_ctx *exec_ctx,
91 grpc_channel_element *elem,
92 grpc_transport_op *op) {
93 if (op->on_connectivity_state_change) {
94 GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE);
95 *op->connectivity_state = GRPC_CHANNEL_FATAL_FAILURE;
96 op->on_connectivity_state_change->cb(
97 exec_ctx, op->on_connectivity_state_change->cb_arg, 1);
98 }
99 if (op->on_consumed != NULL) {
100 op->on_consumed->cb(exec_ctx, op->on_consumed->cb_arg, 1);
101 }
nnoble0c475f02014-12-05 15:37:39 -0800102}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103
Craig Tillera82950e2015-09-22 12:33:20 -0700104static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerab33b482015-11-21 08:11:04 -0800105 grpc_call_element_args *args) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
Craig Tillera82950e2015-09-22 12:33:20 -0700107static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
108 grpc_call_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109
Craig Tillera82950e2015-09-22 12:33:20 -0700110static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800111 grpc_channel_element *elem,
112 grpc_channel_element_args *args) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800113 GPR_ASSERT(args->is_first);
114 GPR_ASSERT(args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115}
116
Craig Tillera82950e2015-09-22 12:33:20 -0700117static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
118 grpc_channel_element *elem) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119
Craig Tiller178edfa2016-02-17 20:54:46 -0800120const grpc_channel_filter grpc_lame_filter = {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700121 lame_start_transport_stream_op, lame_start_transport_op, sizeof(call_data),
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800122 init_call_elem, grpc_call_stack_ignore_set_pollset, destroy_call_elem,
123 sizeof(channel_data), init_channel_elem, destroy_channel_elem,
124 lame_get_peer, "lame-client",
Craig Tiller87d5b192015-04-16 14:37:57 -0700125};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
yang-gc31cd862015-08-17 15:37:27 -0700127#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
128
Craig Tillera82950e2015-09-22 12:33:20 -0700129grpc_channel *grpc_lame_client_channel_create(const char *target,
130 grpc_status_code error_code,
131 const char *error_message) {
Craig Tiller178edfa2016-02-17 20:54:46 -0800132 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
yang-gc31cd862015-08-17 15:37:27 -0700133 grpc_channel_element *elem;
134 channel_data *chand;
Craig Tiller178edfa2016-02-17 20:54:46 -0800135 grpc_channel *channel = grpc_channel_create(&exec_ctx, target, NULL,
136 GRPC_CLIENT_LAME_CHANNEL, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700137 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700138 GRPC_API_TRACE(
139 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700140 "error_message=%s)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700141 3, (target, (int)error_code, error_message));
Craig Tiller178edfa2016-02-17 20:54:46 -0800142 GPR_ASSERT(elem->filter == &grpc_lame_filter);
Craig Tillera82950e2015-09-22 12:33:20 -0700143 chand = (channel_data *)elem->channel_data;
yang-gc31cd862015-08-17 15:37:27 -0700144 chand->error_code = error_code;
145 chand->error_message = error_message;
Craig Tillera82950e2015-09-22 12:33:20 -0700146 grpc_exec_ctx_finish(&exec_ctx);
yang-gc31cd862015-08-17 15:37:27 -0700147 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800148}