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 | 2c8063c | 2016-03-22 22:12:15 -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 | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 83 | grpc_transport_stream_op_finish_with_failure( |
| 84 | exec_ctx, op, GRPC_ERROR_CREATE("lame client channel")); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | 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] | 88 | return NULL; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | static 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 Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 95 | GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_SHUTDOWN); |
| 96 | *op->connectivity_state = GRPC_CHANNEL_SHUTDOWN; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 97 | grpc_exec_ctx_sched(exec_ctx, op->on_connectivity_state_change, |
Craig Tiller | 77c983d | 2016-05-24 13:23:14 -0700 | [diff] [blame] | 98 | GRPC_ERROR_NONE, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | } |
| 100 | if (op->on_consumed != NULL) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 101 | grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | } |
Craig Tiller | 79310ab | 2016-04-15 15:09:48 -0700 | [diff] [blame] | 103 | if (op->send_ping != NULL) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 104 | grpc_exec_ctx_sched(exec_ctx, op->send_ping, |
Craig Tiller | 77c983d | 2016-05-24 13:23:14 -0700 | [diff] [blame] | 105 | GRPC_ERROR_CREATE("lame client channel"), NULL); |
Craig Tiller | 79310ab | 2016-04-15 15:09:48 -0700 | [diff] [blame] | 106 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 107 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 108 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | |
Mark D. Roth | 76d2442 | 2016-06-23 13:22:10 -0700 | [diff] [blame] | 110 | static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 111 | grpc_call_element *elem, |
| 112 | grpc_call_element_args *args) { |
| 113 | return GRPC_ERROR_NONE; |
| 114 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 116 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 117 | const grpc_call_final_info *final_info, |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 118 | void *and_free_memory) { |
| 119 | gpr_free(and_free_memory); |
| 120 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 123 | grpc_channel_element *elem, |
| 124 | grpc_channel_element_args *args) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 125 | GPR_ASSERT(args->is_first); |
| 126 | GPR_ASSERT(args->is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 129 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 130 | grpc_channel_element *elem) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 132 | const grpc_channel_filter grpc_lame_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 133 | lame_start_transport_stream_op, |
| 134 | lame_start_transport_op, |
| 135 | sizeof(call_data), |
| 136 | init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 137 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 138 | destroy_call_elem, |
| 139 | sizeof(channel_data), |
| 140 | init_channel_elem, |
| 141 | destroy_channel_elem, |
| 142 | lame_get_peer, |
| 143 | "lame-client", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 144 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 146 | #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
| 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | grpc_channel *grpc_lame_client_channel_create(const char *target, |
| 149 | grpc_status_code error_code, |
| 150 | const char *error_message) { |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 151 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 152 | grpc_channel_element *elem; |
| 153 | channel_data *chand; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 154 | grpc_channel *channel = grpc_channel_create(&exec_ctx, target, NULL, |
| 155 | GRPC_CLIENT_LAME_CHANNEL, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 157 | GRPC_API_TRACE( |
| 158 | "grpc_lame_client_channel_create(target=%s, error_code=%d, " |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 159 | "error_message=%s)", |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 160 | 3, (target, (int)error_code, error_message)); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 161 | GPR_ASSERT(elem->filter == &grpc_lame_filter); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | chand = (channel_data *)elem->channel_data; |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 163 | chand->error_code = error_code; |
| 164 | chand->error_message = error_message; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | grpc_exec_ctx_finish(&exec_ctx); |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 166 | return channel; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 167 | } |