GRPC C++  0.10.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
client_context.h
Go to the documentation of this file.
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 #ifndef GRPCXX_CLIENT_CONTEXT_H
35 #define GRPCXX_CLIENT_CONTEXT_H
36 
37 #include <map>
38 #include <memory>
39 #include <string>
40 
41 #include <grpc/compression.h>
42 #include <grpc/support/log.h>
43 #include <grpc/support/time.h>
44 #include <grpc++/auth_context.h>
45 #include <grpc++/config.h>
46 #include <grpc++/status.h>
47 #include <grpc++/time.h>
48 
49 struct grpc_call;
50 struct grpc_completion_queue;
51 struct census_context;
52 
53 namespace grpc {
54 
55 class ChannelInterface;
56 class CompletionQueue;
57 class Credentials;
58 class RpcMethod;
59 template <class R>
61 template <class W>
63 template <class R, class W>
65 template <class R>
67 template <class W>
69 template <class R, class W>
71 template <class R>
73 
75  public:
76  ClientContext();
78 
79  void AddMetadata(const grpc::string& meta_key,
80  const grpc::string& meta_value);
81 
82  const std::multimap<grpc::string, grpc::string>& GetServerInitialMetadata() {
83  GPR_ASSERT(initial_metadata_received_);
84  return recv_initial_metadata_;
85  }
86 
87  const std::multimap<grpc::string, grpc::string>& GetServerTrailingMetadata() {
88  // TODO(yangg) check finished
89  return trailing_metadata_;
90  }
91 
92  template <typename T>
93  void set_deadline(const T& deadline) {
94  TimePoint<T> deadline_tp(deadline);
95  deadline_ = deadline_tp.raw_time();
96  }
97 
98 #ifndef GRPC_CXX0X_NO_CHRONO
99  std::chrono::system_clock::time_point deadline() {
100  return Timespec2Timepoint(deadline_);
101  }
102 #endif // !GRPC_CXX0X_NO_CHRONO
103 
104  gpr_timespec raw_deadline() { return deadline_; }
105 
106  void set_authority(const grpc::string& authority) { authority_ = authority; }
107 
108  // Set credentials for the rpc.
109  void set_credentials(const std::shared_ptr<Credentials>& creds) {
110  creds_ = creds;
111  }
112 
113  grpc_compression_algorithm compression_algorithm() const {
114  return compression_algorithm_;
115  }
116 
117  void set_compression_algorithm(grpc_compression_algorithm algorithm);
118 
119  std::shared_ptr<const AuthContext> auth_context() const;
120 
121  // Return the peer uri in a string.
122  // WARNING: this value is never authenticated or subject to any security
123  // related code. It must not be used for any authentication related
124  // functionality. Instead, use auth_context.
125  grpc::string peer() const;
126 
127  // Get and set census context
128  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
129  struct census_context* census_context() const { return census_context_; }
130 
131  void TryCancel();
132 
133  private:
134  // Disallow copy and assign.
136  ClientContext& operator=(const ClientContext&);
137 
140  friend class Channel;
141  template <class R>
142  friend class ::grpc::ClientReader;
143  template <class W>
144  friend class ::grpc::ClientWriter;
145  template <class R, class W>
146  friend class ::grpc::ClientReaderWriter;
147  template <class R>
148  friend class ::grpc::ClientAsyncReader;
149  template <class W>
150  friend class ::grpc::ClientAsyncWriter;
151  template <class R, class W>
152  friend class ::grpc::ClientAsyncReaderWriter;
153  template <class R>
154  friend class ::grpc::ClientAsyncResponseReader;
155  template <class InputMessage, class OutputMessage>
156  friend Status BlockingUnaryCall(ChannelInterface* channel,
157  const RpcMethod& method,
158  ClientContext* context,
159  const InputMessage& request,
160  OutputMessage* result);
161 
162  grpc_call* call() { return call_; }
163  void set_call(grpc_call* call,
164  const std::shared_ptr<ChannelInterface>& channel);
165 
166  grpc_completion_queue* cq() { return cq_; }
167  void set_cq(grpc_completion_queue* cq) { cq_ = cq; }
168 
169  grpc::string authority() { return authority_; }
170 
171  bool initial_metadata_received_;
172  std::shared_ptr<ChannelInterface> channel_;
173  grpc_call* call_;
174  grpc_completion_queue* cq_;
175  gpr_timespec deadline_;
176  grpc::string authority_;
177  std::shared_ptr<Credentials> creds_;
178  mutable std::shared_ptr<const AuthContext> auth_context_;
179  struct census_context* census_context_;
180  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
181  std::multimap<grpc::string, grpc::string> recv_initial_metadata_;
182  std::multimap<grpc::string, grpc::string> trailing_metadata_;
183 
184  grpc_compression_algorithm compression_algorithm_;
185 };
186 
187 } // namespace grpc
188 
189 #endif // GRPCXX_CLIENT_CONTEXT_H
Definition: client_context.h:70
Definition: client_context.h:60
std::string string
Definition: config.h:112
std::chrono::system_clock::time_point deadline()
Definition: client_context.h:99
const std::multimap< grpc::string, grpc::string > & GetServerTrailingMetadata()
Definition: client_context.h:87
Definition: call.h:431
void set_deadline(const T &deadline)
Definition: client_context.h:93
grpc::string peer() const
Definition: client_context.cc:108
std::shared_ptr< const AuthContext > auth_context() const
Definition: client_context.cc:95
gpr_timespec raw_time()
Definition: time.h:56
Definition: time.h:53
Definition: async_unary_call.h:57
Definition: client_context.h:74
ClientContext()
Definition: client_context.cc:47
struct census_context * census_context() const
Definition: client_context.h:129
void TryCancel()
Definition: client_context.cc:102
void set_census_context(struct census_context *ccp)
Definition: client_context.h:128
grpc_compression_algorithm compression_algorithm() const
Definition: client_context.h:113
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
Definition: time.cc:81
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:51
Definition: channel_interface.h:52
Definition: client_context.h:68
Definition: rpc_method.h:39
void set_authority(const grpc::string &authority)
Definition: client_context.h:106
gpr_timespec raw_deadline()
Definition: client_context.h:104
Definition: status.h:42
const std::multimap< grpc::string, grpc::string > & GetServerInitialMetadata()
Definition: client_context.h:82
Definition: client_context.h:64
Definition: client_context.h:66
Definition: client_context.h:62
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Definition: client_context.cc:83
Definition: call.h:402
void set_credentials(const std::shared_ptr< Credentials > &creds)
Definition: client_context.h:109
void AddMetadata(const grpc::string &meta_key, const grpc::string &meta_value)
Definition: client_context.cc:67
Definition: channel.h:53
~ClientContext()
Definition: client_context.cc:53