Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 34 | #include <grpc/grpc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include "src/core/channel/channel_stack.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 39 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include "src/core/surface/channel.h" |
| 41 | #include "src/core/surface/call.h" |
| 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 45 | typedef struct { |
| 46 | grpc_linked_mdelem status; |
| 47 | grpc_linked_mdelem details; |
| 48 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 50 | typedef struct { |
| 51 | grpc_mdctx *mdctx; |
| 52 | grpc_channel *master; |
| 53 | } channel_data; |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 54 | |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 55 | static void lame_start_transport_stream_op(grpc_call_element *elem, |
| 56 | grpc_transport_stream_op *op) { |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 57 | call_data *calld = elem->call_data; |
| 58 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 60 | if (op->send_ops != NULL) { |
Yang Gao | dbf8fdc | 2015-05-28 00:52:31 -0700 | [diff] [blame] | 61 | grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 62 | op->on_done_send->cb(op->on_done_send->cb_arg, 0); |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 63 | } |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 64 | if (op->recv_ops != NULL) { |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 65 | char tmp[GPR_LTOA_MIN_BUFSIZE]; |
| 66 | grpc_metadata_batch mdb; |
| 67 | gpr_ltoa(GRPC_STATUS_UNKNOWN, tmp); |
| 68 | calld->status.md = |
| 69 | grpc_mdelem_from_strings(chand->mdctx, "grpc-status", tmp); |
| 70 | calld->details.md = grpc_mdelem_from_strings(chand->mdctx, "grpc-message", |
| 71 | "Rpc sent on a lame channel."); |
| 72 | calld->status.prev = calld->details.next = NULL; |
| 73 | calld->status.next = &calld->details; |
| 74 | calld->details.prev = &calld->status; |
| 75 | mdb.list.head = &calld->status; |
| 76 | mdb.list.tail = &calld->details; |
| 77 | mdb.garbage.head = mdb.garbage.tail = NULL; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 78 | mdb.deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 79 | grpc_sopb_add_metadata(op->recv_ops, mdb); |
| 80 | *op->recv_state = GRPC_STREAM_CLOSED; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 81 | op->on_done_recv->cb(op->on_done_recv->cb_arg, 1); |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 82 | } |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 83 | if (op->on_consumed != NULL) { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 84 | op->on_consumed->cb(op->on_consumed->cb_arg, 0); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 85 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 88 | static char *lame_get_peer(grpc_call_element *elem) { |
| 89 | channel_data *chand = elem->channel_data; |
| 90 | return grpc_channel_get_target(chand->master); |
| 91 | } |
| 92 | |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 93 | static void lame_start_transport_op(grpc_channel_element *elem, |
| 94 | grpc_transport_op *op) { |
| 95 | if (op->on_connectivity_state_change) { |
| 96 | GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE); |
| 97 | *op->connectivity_state = GRPC_CHANNEL_FATAL_FAILURE; |
| 98 | op->on_connectivity_state_change->cb( |
| 99 | op->on_connectivity_state_change->cb_arg, 1); |
| 100 | } |
| 101 | if (op->on_consumed != NULL) { |
| 102 | op->on_consumed->cb(op->on_consumed->cb_arg, 1); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | |
| 106 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 107 | const void *transport_server_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 108 | grpc_transport_stream_op *initial_op) { |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 109 | if (initial_op) { |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 110 | grpc_transport_stream_op_finish_with_failure(initial_op); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | |
| 114 | static void destroy_call_elem(grpc_call_element *elem) {} |
| 115 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 116 | static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 118 | int is_first, int is_last) { |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 119 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | GPR_ASSERT(is_first); |
| 121 | GPR_ASSERT(is_last); |
Craig Tiller | bec41a2 | 2015-04-27 18:47:40 -0700 | [diff] [blame] | 122 | chand->mdctx = mdctx; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 123 | chand->master = master; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 126 | static void destroy_channel_elem(grpc_channel_element *elem) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | |
| 128 | static const grpc_channel_filter lame_filter = { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 129 | lame_start_transport_stream_op, |
| 130 | lame_start_transport_op, |
| 131 | sizeof(call_data), |
| 132 | init_call_elem, |
| 133 | destroy_call_elem, |
| 134 | sizeof(channel_data), |
| 135 | init_channel_elem, |
| 136 | destroy_channel_elem, |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 137 | lame_get_peer, |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 138 | "lame-client", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 139 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 140 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 141 | grpc_channel *grpc_lame_client_channel_create(const char *target) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | static const grpc_channel_filter *filters[] = {&lame_filter}; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 143 | return grpc_channel_create_from_filters(target, filters, 1, NULL, |
| 144 | grpc_mdctx_create(), 1); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 145 | } |