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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/surface/lame_client.h" |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 35 | |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 36 | #include <grpc/grpc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 38 | #include <string.h> |
| 39 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 42 | #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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | typedef struct { |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 49 | grpc_linked_mdelem status; |
| 50 | grpc_linked_mdelem details; |
| 51 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | typedef struct { |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 54 | grpc_status_code error_code; |
| 55 | const char *error_message; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 56 | } channel_data; |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 57 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 58 | static 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 Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 63 | 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 Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 66 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | static void lame_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 75 | grpc_call_element *elem, |
| 76 | grpc_transport_stream_op *op) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 78 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | } |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 83 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | static char *lame_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 87 | return NULL; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | static 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 | } |
Craig Tiller | 79310ab | 2016-04-15 15:09:48 -0700 | [diff] [blame] | 102 | if (op->send_ping != NULL) { |
| 103 | op->send_ping->cb(exec_ctx, op->send_ping->cb_arg, 0); |
| 104 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 105 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 108 | grpc_call_element_args *args) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 111 | grpc_call_element *elem) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 114 | grpc_channel_element *elem, |
| 115 | grpc_channel_element_args *args) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 116 | GPR_ASSERT(args->is_first); |
| 117 | GPR_ASSERT(args->is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 121 | grpc_channel_element *elem) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 123 | const grpc_channel_filter grpc_lame_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 124 | lame_start_transport_stream_op, |
| 125 | lame_start_transport_op, |
| 126 | sizeof(call_data), |
| 127 | init_call_elem, |
| 128 | grpc_call_stack_ignore_set_pollset, |
| 129 | destroy_call_elem, |
| 130 | sizeof(channel_data), |
| 131 | init_channel_elem, |
| 132 | destroy_channel_elem, |
| 133 | lame_get_peer, |
| 134 | "lame-client", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 135 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 137 | #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
| 138 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | grpc_channel *grpc_lame_client_channel_create(const char *target, |
| 140 | grpc_status_code error_code, |
| 141 | const char *error_message) { |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 142 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 143 | grpc_channel_element *elem; |
| 144 | channel_data *chand; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 145 | grpc_channel *channel = grpc_channel_create(&exec_ctx, target, NULL, |
| 146 | GRPC_CLIENT_LAME_CHANNEL, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 148 | GRPC_API_TRACE( |
| 149 | "grpc_lame_client_channel_create(target=%s, error_code=%d, " |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 150 | "error_message=%s)", |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 151 | 3, (target, (int)error_code, error_message)); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 152 | GPR_ASSERT(elem->filter == &grpc_lame_filter); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | chand = (channel_data *)elem->channel_data; |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 154 | chand->error_code = error_code; |
| 155 | chand->error_message = error_message; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | grpc_exec_ctx_finish(&exec_ctx); |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 157 | return channel; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 158 | } |