LCOV - code coverage report
Current view: top level - src/cpp/client - channel.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 43 44 97.7 %
Date: 2015-10-10 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, 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++/channel.h>
      35             : 
      36             : #include <memory>
      37             : 
      38             : #include <grpc/grpc.h>
      39             : #include <grpc/support/log.h>
      40             : #include <grpc/support/slice.h>
      41             : #include <grpc++/client_context.h>
      42             : #include <grpc++/completion_queue.h>
      43             : #include <grpc++/security/credentials.h>
      44             : #include <grpc++/impl/call.h>
      45             : #include <grpc++/impl/rpc_method.h>
      46             : #include <grpc++/support/channel_arguments.h>
      47             : #include <grpc++/support/config.h>
      48             : #include <grpc++/support/status.h>
      49             : #include <grpc++/support/time.h>
      50             : #include "src/core/profiling/timers.h"
      51             : 
      52             : namespace grpc {
      53             : 
      54         182 : Channel::Channel(const grpc::string& host, grpc_channel* channel)
      55         182 :     : host_(host), c_channel_(channel) {}
      56             : 
      57         364 : Channel::~Channel() { grpc_channel_destroy(c_channel_); }
      58             : 
      59     1297398 : Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
      60             :                          CompletionQueue* cq) {
      61     1297398 :   const bool kRegistered = method.channel_tag() && context->authority().empty();
      62     1298490 :   grpc_call* c_call = NULL;
      63     1298490 :   if (kRegistered) {
      64             :     c_call = grpc_channel_create_registered_call(
      65             :         c_channel_, context->propagate_from_call_,
      66             :         context->propagation_options_.c_bitmask(), cq->cq(),
      67     1298476 :         method.channel_tag(), context->raw_deadline(), nullptr);
      68             :   } else {
      69          14 :     const char* host_str = NULL;
      70          14 :     if (!context->authority().empty()) {
      71           1 :       host_str = context->authority_.c_str();
      72          13 :     } else if (!host_.empty()) {
      73           0 :       host_str = host_.c_str();
      74             :     }
      75             :     c_call = grpc_channel_create_call(c_channel_, context->propagate_from_call_,
      76             :                                       context->propagation_options_.c_bitmask(),
      77             :                                       cq->cq(), method.name(), host_str,
      78          14 :                                       context->raw_deadline(), nullptr);
      79             :   }
      80     1299194 :   grpc_census_call_set_context(c_call, context->census_context());
      81             :   GRPC_TIMER_MARK(GRPC_PTAG_CPP_CALL_CREATED, c_call);
      82     1299196 :   context->set_call(c_call, shared_from_this());
      83     1299195 :   return Call(c_call, this, cq);
      84             : }
      85             : 
      86     2633744 : void Channel::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
      87             :   static const size_t MAX_OPS = 8;
      88     2633744 :   size_t nops = 0;
      89             :   grpc_op cops[MAX_OPS];
      90             :   GRPC_TIMER_BEGIN(GRPC_PTAG_CPP_PERFORM_OPS, call->call());
      91     2633744 :   ops->FillOps(cops, &nops);
      92     2635472 :   GPR_ASSERT(GRPC_CALL_OK ==
      93             :              grpc_call_start_batch(call->call(), cops, nops, ops, nullptr));
      94             :   GRPC_TIMER_END(GRPC_PTAG_CPP_PERFORM_OPS, call->call());
      95     2636201 : }
      96             : 
      97         784 : void* Channel::RegisterMethod(const char* method) {
      98             :   return grpc_channel_register_call(
      99         784 :       c_channel_, method, host_.empty() ? NULL : host_.c_str(), nullptr);
     100             : }
     101             : 
     102          17 : grpc_connectivity_state Channel::GetState(bool try_to_connect) {
     103          17 :   return grpc_channel_check_connectivity_state(c_channel_, try_to_connect);
     104             : }
     105             : 
     106             : namespace {
     107             : class TagSaver GRPC_FINAL : public CompletionQueueTag {
     108             :  public:
     109          14 :   explicit TagSaver(void* tag) : tag_(tag) {}
     110          28 :   ~TagSaver() GRPC_OVERRIDE {}
     111          14 :   bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
     112          14 :     *tag = tag_;
     113          14 :     delete this;
     114          14 :     return true;
     115             :   }
     116             : 
     117             :  private:
     118             :   void* tag_;
     119             : };
     120             : 
     121             : }  // namespace
     122             : 
     123          14 : void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
     124             :                                       gpr_timespec deadline,
     125             :                                       CompletionQueue* cq, void* tag) {
     126          14 :   TagSaver* tag_saver = new TagSaver(tag);
     127             :   grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline,
     128          14 :                                         cq->cq(), tag_saver);
     129          14 : }
     130             : 
     131          12 : bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed,
     132             :                                      gpr_timespec deadline) {
     133          12 :   CompletionQueue cq;
     134          12 :   bool ok = false;
     135          12 :   void* tag = NULL;
     136          12 :   NotifyOnStateChangeImpl(last_observed, deadline, &cq, NULL);
     137          12 :   cq.Next(&tag, &ok);
     138          12 :   GPR_ASSERT(tag == NULL);
     139          12 :   return ok;
     140             : }
     141             : 
     142             : }  // namespace grpc

Generated by: LCOV version 1.10