gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
client_context.h
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/support/log.h>
42 #include <grpc/support/time.h>
43 #include <grpc++/config.h>
44 #include <grpc++/time.h>
45 
46 struct grpc_call;
48 
49 namespace grpc {
50 
51 class CallOpBuffer;
52 class ChannelInterface;
53 class CompletionQueue;
54 class Credentials;
55 class RpcMethod;
56 class Status;
57 template <class R>
59 template <class W>
61 template <class R, class W>
63 template <class R>
65 template <class W>
67 template <class R, class W>
69 template <class R>
71 
73  public:
74  ClientContext();
75  ~ClientContext();
76 
77  void AddMetadata(const grpc::string& meta_key,
78  const grpc::string& meta_value);
79 
80  const std::multimap<grpc::string, grpc::string>& GetServerInitialMetadata() {
81  GPR_ASSERT(initial_metadata_received_);
82  return recv_initial_metadata_;
83  }
84 
85  const std::multimap<grpc::string, grpc::string>& GetServerTrailingMetadata() {
86  // TODO(yangg) check finished
87  return trailing_metadata_;
88  }
89 
90  template <typename T>
91  void set_deadline(const T& deadline) {
92  TimePoint<T> deadline_tp(deadline);
93  deadline_ = deadline_tp.raw_time();
94  }
95 
96 #ifndef GRPC_CXX0X_NO_CHRONO
97  std::chrono::system_clock::time_point deadline() {
98  return Timespec2Timepoint(deadline_);
99  }
100 #endif // !GRPC_CXX0X_NO_CHRONO
101 
102  gpr_timespec raw_deadline() { return deadline_; }
103 
104  void set_authority(const grpc::string& authority) { authority_ = authority; }
105 
106  // Set credentials for the rpc.
107  void set_credentials(const std::shared_ptr<Credentials>& creds) {
108  creds_ = creds;
109  }
110 
111  void TryCancel();
112 
113  private:
114  // Disallow copy and assign.
116  ClientContext& operator=(const ClientContext&);
117 
118  friend class CallOpBuffer;
119  friend class Channel;
120  template <class R>
121  friend class ::grpc::ClientReader;
122  template <class W>
123  friend class ::grpc::ClientWriter;
124  template <class R, class W>
125  friend class ::grpc::ClientReaderWriter;
126  template <class R>
127  friend class ::grpc::ClientAsyncReader;
128  template <class W>
129  friend class ::grpc::ClientAsyncWriter;
130  template <class R, class W>
131  friend class ::grpc::ClientAsyncReaderWriter;
132  template <class R>
133  friend class ::grpc::ClientAsyncResponseReader;
134 
135  grpc_call* call() { return call_; }
136  void set_call(grpc_call* call,
137  const std::shared_ptr<ChannelInterface>& channel);
138 
139  grpc_completion_queue* cq() { return cq_; }
140  void set_cq(grpc_completion_queue* cq) { cq_ = cq; }
141 
142  grpc::string authority() { return authority_; }
143 
144  bool initial_metadata_received_;
145  std::shared_ptr<ChannelInterface> channel_;
146  grpc_call* call_;
148  gpr_timespec deadline_;
149  grpc::string authority_;
150  std::shared_ptr<Credentials> creds_;
151  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
152  std::multimap<grpc::string, grpc::string> recv_initial_metadata_;
153  std::multimap<grpc::string, grpc::string> trailing_metadata_;
154 };
155 
156 } // namespace grpc
157 
158 #endif // GRPCXX_CLIENT_CONTEXT_H
Definition: client_context.h:60
Definition: _channel.h:40
Definition: client_context.h:62
Definition: _completion_queue.h:40
Definition: client_context.h:70
Definition: client_context.h:72
Definition: client_context.h:58
Definition: client_context.h:64
Definition: completion_queue.c:61
Definition: client_context.h:68
Definition: call.h:53
Definition: time.h:48
Definition: client_context.h:66
Definition: call.c:128
Definition: time.h:53