Home | History | Annotate | Download | only in surface
      1 /*
      2  *
      3  * Copyright 2015 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #include <grpc/support/port_platform.h>
     20 
     21 #include <grpc/grpc.h>
     22 
     23 #include <string.h>
     24 
     25 #include <grpc/support/alloc.h>
     26 #include <grpc/support/log.h>
     27 
     28 #include "src/core/lib/gprpp/atomic.h"
     29 
     30 #include "src/core/lib/channel/channel_stack.h"
     31 #include "src/core/lib/gpr/string.h"
     32 #include "src/core/lib/surface/api_trace.h"
     33 #include "src/core/lib/surface/call.h"
     34 #include "src/core/lib/surface/channel.h"
     35 #include "src/core/lib/surface/lame_client.h"
     36 #include "src/core/lib/transport/static_metadata.h"
     37 
     38 namespace grpc_core {
     39 
     40 namespace {
     41 
     42 struct CallData {
     43   grpc_call_combiner* call_combiner;
     44   grpc_linked_mdelem status;
     45   grpc_linked_mdelem details;
     46   grpc_core::atomic<bool> filled_metadata;
     47 };
     48 
     49 struct ChannelData {
     50   grpc_status_code error_code;
     51   const char* error_message;
     52 };
     53 
     54 static void fill_metadata(grpc_call_element* elem, grpc_metadata_batch* mdb) {
     55   CallData* calld = static_cast<CallData*>(elem->call_data);
     56   bool expected = false;
     57   if (!calld->filled_metadata.compare_exchange_strong(
     58           expected, true, grpc_core::memory_order_relaxed,
     59           grpc_core::memory_order_relaxed)) {
     60     return;
     61   }
     62   ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
     63   char tmp[GPR_LTOA_MIN_BUFSIZE];
     64   gpr_ltoa(chand->error_code, tmp);
     65   calld->status.md = grpc_mdelem_from_slices(
     66       GRPC_MDSTR_GRPC_STATUS, grpc_slice_from_copied_string(tmp));
     67   calld->details.md = grpc_mdelem_from_slices(
     68       GRPC_MDSTR_GRPC_MESSAGE,
     69       grpc_slice_from_copied_string(chand->error_message));
     70   calld->status.prev = calld->details.next = nullptr;
     71   calld->status.next = &calld->details;
     72   calld->details.prev = &calld->status;
     73   mdb->list.head = &calld->status;
     74   mdb->list.tail = &calld->details;
     75   mdb->list.count = 2;
     76   mdb->deadline = GRPC_MILLIS_INF_FUTURE;
     77 }
     78 
     79 static void lame_start_transport_stream_op_batch(
     80     grpc_call_element* elem, grpc_transport_stream_op_batch* op) {
     81   CallData* calld = static_cast<CallData*>(elem->call_data);
     82   if (op->recv_initial_metadata) {
     83     fill_metadata(elem,
     84                   op->payload->recv_initial_metadata.recv_initial_metadata);
     85   } else if (op->recv_trailing_metadata) {
     86     fill_metadata(elem,
     87                   op->payload->recv_trailing_metadata.recv_trailing_metadata);
     88   }
     89   grpc_transport_stream_op_batch_finish_with_failure(
     90       op, GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"),
     91       calld->call_combiner);
     92 }
     93 
     94 static void lame_get_channel_info(grpc_channel_element* elem,
     95                                   const grpc_channel_info* channel_info) {}
     96 
     97 static void lame_start_transport_op(grpc_channel_element* elem,
     98                                     grpc_transport_op* op) {
     99   if (op->on_connectivity_state_change) {
    100     GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_SHUTDOWN);
    101     *op->connectivity_state = GRPC_CHANNEL_SHUTDOWN;
    102     GRPC_CLOSURE_SCHED(op->on_connectivity_state_change, GRPC_ERROR_NONE);
    103   }
    104   if (op->send_ping.on_initiate != nullptr) {
    105     GRPC_CLOSURE_SCHED(
    106         op->send_ping.on_initiate,
    107         GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
    108   }
    109   if (op->send_ping.on_ack != nullptr) {
    110     GRPC_CLOSURE_SCHED(
    111         op->send_ping.on_ack,
    112         GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"));
    113   }
    114   GRPC_ERROR_UNREF(op->disconnect_with_error);
    115   if (op->on_consumed != nullptr) {
    116     GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE);
    117   }
    118 }
    119 
    120 static grpc_error* init_call_elem(grpc_call_element* elem,
    121                                   const grpc_call_element_args* args) {
    122   CallData* calld = static_cast<CallData*>(elem->call_data);
    123   calld->call_combiner = args->call_combiner;
    124   return GRPC_ERROR_NONE;
    125 }
    126 
    127 static void destroy_call_elem(grpc_call_element* elem,
    128                               const grpc_call_final_info* final_info,
    129                               grpc_closure* then_schedule_closure) {
    130   GRPC_CLOSURE_SCHED(then_schedule_closure, GRPC_ERROR_NONE);
    131 }
    132 
    133 static grpc_error* init_channel_elem(grpc_channel_element* elem,
    134                                      grpc_channel_element_args* args) {
    135   GPR_ASSERT(args->is_first);
    136   GPR_ASSERT(args->is_last);
    137   return GRPC_ERROR_NONE;
    138 }
    139 
    140 static void destroy_channel_elem(grpc_channel_element* elem) {}
    141 
    142 }  // namespace
    143 
    144 }  // namespace grpc_core
    145 
    146 const grpc_channel_filter grpc_lame_filter = {
    147     grpc_core::lame_start_transport_stream_op_batch,
    148     grpc_core::lame_start_transport_op,
    149     sizeof(grpc_core::CallData),
    150     grpc_core::init_call_elem,
    151     grpc_call_stack_ignore_set_pollset_or_pollset_set,
    152     grpc_core::destroy_call_elem,
    153     sizeof(grpc_core::ChannelData),
    154     grpc_core::init_channel_elem,
    155     grpc_core::destroy_channel_elem,
    156     grpc_core::lame_get_channel_info,
    157     "lame-client",
    158 };
    159 
    160 #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack*)((c) + 1))
    161 
    162 grpc_channel* grpc_lame_client_channel_create(const char* target,
    163                                               grpc_status_code error_code,
    164                                               const char* error_message) {
    165   grpc_core::ExecCtx exec_ctx;
    166   grpc_channel_element* elem;
    167   grpc_channel* channel =
    168       grpc_channel_create(target, nullptr, GRPC_CLIENT_LAME_CHANNEL, nullptr);
    169   elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
    170   GRPC_API_TRACE(
    171       "grpc_lame_client_channel_create(target=%s, error_code=%d, "
    172       "error_message=%s)",
    173       3, (target, (int)error_code, error_message));
    174   GPR_ASSERT(elem->filter == &grpc_lame_filter);
    175   auto chand = static_cast<grpc_core::ChannelData*>(elem->channel_data);
    176   chand->error_code = error_code;
    177   chand->error_message = error_message;
    178 
    179   return channel;
    180 }
    181