Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 34 | #include "src/core/surface/lame_client.h" |
| 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 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include "src/core/channel/channel_stack.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 41 | #include "src/core/support/string.h" |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 42 | #include "src/core/surface/api_trace.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #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 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 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 102 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | 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] | 105 | grpc_call_element_args *args) {} |
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 destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 108 | grpc_call_element *elem) {} |
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 init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 111 | grpc_channel_element *elem, |
| 112 | grpc_channel_element_args *args) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 113 | GPR_ASSERT(args->is_first); |
| 114 | GPR_ASSERT(args->is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 118 | grpc_channel_element *elem) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 119 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 120 | const grpc_channel_filter grpc_lame_filter = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 121 | lame_start_transport_stream_op, lame_start_transport_op, sizeof(call_data), |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 122 | 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 Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 125 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 127 | #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
| 128 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 129 | grpc_channel *grpc_lame_client_channel_create(const char *target, |
| 130 | grpc_status_code error_code, |
| 131 | const char *error_message) { |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 132 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 133 | grpc_channel_element *elem; |
| 134 | channel_data *chand; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 135 | grpc_channel *channel = grpc_channel_create(&exec_ctx, target, NULL, |
| 136 | GRPC_CLIENT_LAME_CHANNEL, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 138 | GRPC_API_TRACE( |
| 139 | "grpc_lame_client_channel_create(target=%s, error_code=%d, " |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 140 | "error_message=%s)", |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 141 | 3, (target, (int)error_code, error_message)); |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 142 | GPR_ASSERT(elem->filter == &grpc_lame_filter); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 143 | chand = (channel_data *)elem->channel_data; |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 144 | chand->error_code = error_code; |
| 145 | chand->error_message = error_message; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | grpc_exec_ctx_finish(&exec_ctx); |
yang-g | c31cd86 | 2015-08-17 15:37:27 -0700 | [diff] [blame] | 147 | return channel; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 148 | } |