OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #include <grpc/grpc.h> |
| 35 |
| 36 #include <string.h> |
| 37 |
| 38 #include "src/core/channel/channel_stack.h" |
| 39 #include "src/core/support/string.h" |
| 40 #include "src/core/surface/api_trace.h" |
| 41 #include "src/core/surface/channel.h" |
| 42 #include "src/core/surface/call.h" |
| 43 #include <grpc/support/alloc.h> |
| 44 #include <grpc/support/log.h> |
| 45 |
| 46 typedef struct { |
| 47 grpc_linked_mdelem status; |
| 48 grpc_linked_mdelem details; |
| 49 } call_data; |
| 50 |
| 51 typedef struct { |
| 52 grpc_status_code error_code; |
| 53 const char *error_message; |
| 54 } channel_data; |
| 55 |
| 56 static void fill_metadata(grpc_call_element *elem, grpc_metadata_batch *mdb) { |
| 57 call_data *calld = elem->call_data; |
| 58 channel_data *chand = elem->channel_data; |
| 59 char tmp[GPR_LTOA_MIN_BUFSIZE]; |
| 60 gpr_ltoa(chand->error_code, tmp); |
| 61 calld->status.md = grpc_mdelem_from_strings("grpc-status", tmp); |
| 62 calld->details.md = |
| 63 grpc_mdelem_from_strings("grpc-message", chand->error_message); |
| 64 calld->status.prev = calld->details.next = NULL; |
| 65 calld->status.next = &calld->details; |
| 66 calld->details.prev = &calld->status; |
| 67 mdb->list.head = &calld->status; |
| 68 mdb->list.tail = &calld->details; |
| 69 mdb->deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
| 70 } |
| 71 |
| 72 static void lame_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 73 grpc_call_element *elem, |
| 74 grpc_transport_stream_op *op) { |
| 75 GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 76 if (op->recv_initial_metadata != NULL) { |
| 77 fill_metadata(elem, op->recv_initial_metadata); |
| 78 } else if (op->recv_trailing_metadata != NULL) { |
| 79 fill_metadata(elem, op->recv_trailing_metadata); |
| 80 } |
| 81 grpc_transport_stream_op_finish_with_failure(exec_ctx, op); |
| 82 } |
| 83 |
| 84 static char *lame_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 85 return NULL; |
| 86 } |
| 87 |
| 88 static void lame_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 89 grpc_channel_element *elem, |
| 90 grpc_transport_op *op) { |
| 91 if (op->on_connectivity_state_change) { |
| 92 GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE); |
| 93 *op->connectivity_state = GRPC_CHANNEL_FATAL_FAILURE; |
| 94 op->on_connectivity_state_change->cb( |
| 95 exec_ctx, op->on_connectivity_state_change->cb_arg, 1); |
| 96 } |
| 97 if (op->on_consumed != NULL) { |
| 98 op->on_consumed->cb(exec_ctx, op->on_consumed->cb_arg, 1); |
| 99 } |
| 100 } |
| 101 |
| 102 static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 103 grpc_call_element_args *args) {} |
| 104 |
| 105 static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 106 grpc_call_element *elem) {} |
| 107 |
| 108 static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 109 grpc_channel_element *elem, |
| 110 grpc_channel_element_args *args) { |
| 111 GPR_ASSERT(args->is_first); |
| 112 GPR_ASSERT(args->is_last); |
| 113 } |
| 114 |
| 115 static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 116 grpc_channel_element *elem) {} |
| 117 |
| 118 static const grpc_channel_filter lame_filter = { |
| 119 lame_start_transport_stream_op, lame_start_transport_op, sizeof(call_data), |
| 120 init_call_elem, grpc_call_stack_ignore_set_pollset, destroy_call_elem, |
| 121 sizeof(channel_data), init_channel_elem, destroy_channel_elem, |
| 122 lame_get_peer, "lame-client", |
| 123 }; |
| 124 |
| 125 #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
| 126 |
| 127 grpc_channel *grpc_lame_client_channel_create(const char *target, |
| 128 grpc_status_code error_code, |
| 129 const char *error_message) { |
| 130 grpc_channel *channel; |
| 131 grpc_channel_element *elem; |
| 132 channel_data *chand; |
| 133 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 134 static const grpc_channel_filter *filters[] = {&lame_filter}; |
| 135 channel = |
| 136 grpc_channel_create_from_filters(&exec_ctx, target, filters, 1, NULL, 1); |
| 137 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
| 138 GRPC_API_TRACE( |
| 139 "grpc_lame_client_channel_create(target=%s, error_code=%d, " |
| 140 "error_message=%s)", |
| 141 3, (target, (int)error_code, error_message)); |
| 142 GPR_ASSERT(elem->filter == &lame_filter); |
| 143 chand = (channel_data *)elem->channel_data; |
| 144 chand->error_code = error_code; |
| 145 chand->error_message = error_message; |
| 146 grpc_exec_ctx_finish(&exec_ctx); |
| 147 return channel; |
| 148 } |
OLD | NEW |