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 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 45 | typedef struct { |
| 46 | void *unused; |
| 47 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 49 | typedef struct { |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 50 | grpc_mdelem *status; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 51 | grpc_mdelem *message; |
| 52 | } channel_data; |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 53 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 54 | static void call_op(grpc_call_element *elem, grpc_call_element *from_elem, |
| 55 | grpc_call_op *op) { |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 56 | channel_data *channeld = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 58 | |
| 59 | switch (op->type) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 60 | case GRPC_SEND_START: |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 61 | grpc_call_recv_metadata(elem, grpc_mdelem_ref(channeld->status)); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 62 | grpc_call_recv_metadata(elem, grpc_mdelem_ref(channeld->message)); |
| 63 | grpc_call_stream_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | break; |
| 65 | case GRPC_SEND_METADATA: |
| 66 | grpc_mdelem_unref(op->data.metadata); |
| 67 | break; |
| 68 | default: |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | op->done_cb(op->user_data, GRPC_OP_ERROR); |
| 73 | } |
| 74 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 75 | static void channel_op(grpc_channel_element *elem, |
| 76 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 77 | switch (op->type) { |
| 78 | case GRPC_CHANNEL_GOAWAY: |
| 79 | gpr_slice_unref(op->data.goaway.message); |
| 80 | break; |
Craig Tiller | 8aa09c3 | 2015-02-05 10:17:03 -0800 | [diff] [blame] | 81 | case GRPC_CHANNEL_DISCONNECT: |
| 82 | grpc_client_channel_closed(elem); |
| 83 | break; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 84 | default: |
| 85 | break; |
| 86 | } |
| 87 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | |
| 89 | static void init_call_elem(grpc_call_element *elem, |
| 90 | const void *transport_server_data) {} |
| 91 | |
| 92 | static void destroy_call_elem(grpc_call_element *elem) {} |
| 93 | |
| 94 | static void init_channel_elem(grpc_channel_element *elem, |
| 95 | const grpc_channel_args *args, grpc_mdctx *mdctx, |
| 96 | int is_first, int is_last) { |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 97 | channel_data *channeld = elem->channel_data; |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 98 | char status[12]; |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 99 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | GPR_ASSERT(is_first); |
| 101 | GPR_ASSERT(is_last); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 102 | |
| 103 | channeld->message = grpc_mdelem_from_strings(mdctx, "grpc-message", |
| 104 | "Rpc sent on a lame channel."); |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 105 | gpr_ltoa(GRPC_STATUS_UNKNOWN, status); |
| 106 | channeld->status = grpc_mdelem_from_strings(mdctx, "grpc-status", status); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | } |
| 108 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 109 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 110 | channel_data *channeld = elem->channel_data; |
| 111 | |
| 112 | grpc_mdelem_unref(channeld->message); |
Craig Tiller | a847a8f | 2015-02-11 21:20:25 -0800 | [diff] [blame] | 113 | grpc_mdelem_unref(channeld->status); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 114 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | |
| 116 | static const grpc_channel_filter lame_filter = { |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 117 | call_op, channel_op, sizeof(call_data), |
| 118 | init_call_elem, destroy_call_elem, sizeof(channel_data), |
| 119 | init_channel_elem, destroy_channel_elem, "lame-client", }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | grpc_channel *grpc_lame_client_channel_create(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | static const grpc_channel_filter *filters[] = {&lame_filter}; |
| 123 | return grpc_channel_create_from_filters(filters, 1, NULL, grpc_mdctx_create(), |
| 124 | 1); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 125 | } |