-
Notifications
You must be signed in to change notification settings - Fork 81
/
client.cc
289 lines (249 loc) · 9.37 KB
/
client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#include <iostream>
#include <string>
#include <vector>
#include "gflags/gflags.h"
#include "rapidjson/error/en.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "trpc/client/http/http_service_proxy.h"
#include "trpc/client/make_client_context.h"
#include "trpc/client/trpc_client.h"
#include "trpc/common/config/trpc_config.h"
#include "trpc/common/runtime_manager.h"
#include "trpc/common/status.h"
#include "trpc/common/trpc_plugin.h"
#include "trpc/coroutine/fiber.h"
#include "trpc/coroutine/fiber_latch.h"
#include "trpc/util/log/logging.h"
DEFINE_string(service_name, "http_client", "callee service name");
DEFINE_string(client_config, "trpc_cpp_fiber.yaml", "");
DEFINE_string(addr, "127.0.0.1:24856", "ip:port");
namespace http::demo {
constexpr std::string_view kGreetings{"hello world!"};
using HttpServiceProxyPtr = std::shared_ptr<::trpc::http::HttpServiceProxy>;
namespace {
//// HTTP GET.
bool GetString(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
std::string rsp_str;
auto status = proxy->GetString(ctx, "http://example.com/foo", &rsp_str);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
TRPC_FMT_INFO("response content: {}", rsp_str);
if (rsp_str != kGreetings) return false;
return true;
}
bool GetJson(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
rapidjson::Document rsp_json;
auto status = proxy->Get(ctx, "http://example.com/hello", &rsp_json);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
rsp_json.Accept(writer);
auto rsp_str = buffer.GetString();
TRPC_FMT_INFO("response content: {}", rsp_str);
if (rsp_json["msg"].GetString() == kGreetings && rsp_json["age"].GetInt() == 18) {
return true;
}
return false;
}
bool GetHttpResponse(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
::trpc::http::HttpResponse http_rsp;
auto status = proxy->Get2(ctx, "http://example.com/hello", &http_rsp);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
TRPC_FMT_INFO("response content: {}", http_rsp.GetContent());
const std::string rsp_json = R"({"msg": "hello world!", "age": 18, "height": 180})";
if (http_rsp.GetContent() != rsp_json) return false;
return true;
}
bool GetHttpResponseNotFound(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
::trpc::http::HttpResponse http_rsp;
auto status = proxy->Get2(ctx, "http://example.com/foo-not-found", &http_rsp);
if (!status.OK() && status.GetFuncRetCode() == 404) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return true;
}
return false;
}
} // namespace
namespace {
//// HTTP HEAD.
bool HeadHttpResponse(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
::trpc::http::HttpResponse http_rsp;
auto status = proxy->Head(ctx, "http://example.com/foo", &http_rsp);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
TRPC_FMT_INFO("response content: {}", http_rsp.GetContent());
if (!http_rsp.GetContent().empty()) return false;
if (http_rsp.GetHeader("Content-Length") != std::to_string(kGreetings.size())) return false;
return true;
}
} // namespace
namespace {
//// HTTP POST.
bool PostString(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
std::string req_str{kGreetings};
std::string rsp_str;
auto status = proxy->Post(ctx, "http://example.com/foo", std::move(req_str), &rsp_str);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
TRPC_FMT_INFO("response content: {}", rsp_str);
if (rsp_str != kGreetings) return false;
return true;
}
bool PostJson(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
rapidjson::Document req_json;
req_json.Parse(R"({"msg": "hello world!"})");
rapidjson::Document rsp_json;
auto status = proxy->Post(ctx, "http://example.com/foo", req_json, &rsp_json);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
rsp_json.Accept(writer);
auto rsp_str = buffer.GetString();
TRPC_FMT_INFO("response content: {}", rsp_str);
if (req_json["msg"] != rsp_json["msg"]) {
return false;
}
return true;
}
bool PostStringAndWaitHttpResponse(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
std::string req_str{kGreetings};
::trpc::http::HttpResponse http_rsp;
auto status = proxy->Post2(ctx, "http://example.com/foo", std::move(req_str), &http_rsp);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
TRPC_FMT_INFO("response content: {}", http_rsp.GetContent());
if (http_rsp.GetContent() != kGreetings) return false;
return true;
}
} // namespace
namespace {
//// HTTP unary invoke.
bool HttpUnaryInvoke(const HttpServiceProxyPtr& proxy) {
auto ctx = ::trpc::MakeClientContext(proxy);
::trpc::http::HttpRequest http_req;
http_req.SetMethod("POST");
http_req.SetUrl("/foo");
std::string req_content = R"({"msg": "hello world!"})";
http_req.SetHeader("Content-Type", "application/json");
http_req.SetHeader("Content-Length", std::to_string(req_content.size()));
http_req.SetContent(req_content);
::trpc::http::HttpResponse http_rsp;
auto status = proxy->HttpUnaryInvoke<::trpc::http::HttpRequest, ::trpc::http::HttpResponse>(ctx, http_req, &http_rsp);
if (!status.OK()) {
TRPC_FMT_ERROR("status: {}", status.ToString());
return false;
}
TRPC_FMT_INFO("response content: {}", http_rsp.GetContent());
if (http_rsp.GetContent() != req_content) return false;
return true;
}
} // namespace
int Run() {
bool final_ok{true};
struct http_calling_args_t {
std::string calling_name;
std::function<bool()> calling_executor;
bool ok;
};
::trpc::ServiceProxyOption option;
option.name = FLAGS_service_name;
option.codec_name = "http";
option.network = "tcp";
option.conn_type = "long";
option.timeout = 1000;
option.selector_name = "direct";
option.target = FLAGS_addr;
auto http_client = ::trpc::GetTrpcClient()->GetProxy<::trpc::http::HttpServiceProxy>(FLAGS_service_name, option);
std::vector<http_calling_args_t> callings{
// GET.
{"GET string", [&http_client]() { return GetString(http_client); }, false},
{"GET json", [&http_client]() { return GetJson(http_client); }, false},
{"GET http response", [&http_client]() { return GetHttpResponse(http_client); }, false},
{"GET http response(not found)", [&http_client]() { return GetHttpResponseNotFound(http_client); }, false},
// HEAD.
{"HEAD string", [&http_client]() { return HeadHttpResponse(http_client); }, false},
// POST.
{"POST string", [&http_client]() { return PostString(http_client); }, false},
{"POST json", [&http_client]() { return PostJson(http_client); }, false},
{"POST string, then wait http response", [&http_client]() { return PostStringAndWaitHttpResponse(http_client); },
false},
// HTTP unary invoke.
{"UNARY invoking", [&http_client]() { return HttpUnaryInvoke(http_client); }, false},
};
auto latch_count = static_cast<std::ptrdiff_t>(callings.size());
::trpc::FiberLatch callings_latch{latch_count};
for (auto& c : callings) {
::trpc::StartFiberDetached([&callings_latch, &c]() {
c.ok = c.calling_executor();
callings_latch.CountDown();
});
}
callings_latch.Wait();
for (const auto& c : callings) {
final_ok &= c.ok;
std::cout << "name: " << c.calling_name << ", ok: " << c.ok << std::endl;
}
std::cout << "final result of http calling: " << final_ok << std::endl;
return final_ok ? 0 : -1;
}
} // namespace http::demo
void ParseClientConfig(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
google::CommandLineFlagInfo info;
if (GetCommandLineFlagInfo("client_config", &info) && info.is_default) {
std::cerr << "start client with client_config, for example: " << argv[0]
<< " --client_config=/client/client_config/filepath" << std::endl;
exit(-1);
}
std::cout << "FLAGS_service_name: " << FLAGS_service_name << std::endl;
std::cout << "FLAGS_client_config: " << FLAGS_client_config << std::endl;
std::cout << "FLAGS_addr: " << FLAGS_addr << std::endl;
}
int main(int argc, char* argv[]) {
ParseClientConfig(argc, argv);
if (::trpc::TrpcConfig::GetInstance()->Init(FLAGS_client_config) != 0) {
std::cerr << "load client_config failed." << std::endl;
exit(-1);
}
// If the business code is running in trpc pure client mode,
// the business code needs to be running in the `RunInTrpcRuntime` function
return ::trpc::RunInTrpcRuntime([]() { return http::demo::Run(); });
}