Skip to content

Commit

Permalink
resolve the corresponding issue trpc-group#136
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalis committed Jul 4, 2024
1 parent 81bf717 commit 7bce783
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 43 deletions.
18 changes: 16 additions & 2 deletions examples/features/trpc_json/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int Run() {

auto proxy = ::trpc::GetTrpcClient()->GetProxy<::trpc::RpcServiceProxy>(FLAGS_target, option);

std::string req_json_str = "{\"age\":18,\"height\":180}";
std::string req_json_str = "{\"name\":\"issueshooter\",\"age\":18,\"hobby\":[\"opensource project\",\"movies\",\"books\"]}";

rapidjson::Document hello_req;
hello_req.Parse(req_json_str.c_str());
Expand All @@ -67,7 +67,21 @@ int Run() {
}

for (rapidjson::Value::ConstMemberIterator iter = hello_rsp.MemberBegin(); iter != hello_rsp.MemberEnd(); ++iter) {
std::cout << "json name: " << iter->name.GetString() << ", value: " << iter->value.GetInt() << std::endl;
std::cout << "json name: " << iter->name.GetString() << ", value: ";
if (iter->value.IsInt()) {
std::cout << iter->value.GetInt() << std::endl;
} else if (iter->value.IsString()){
std::cout << iter->value.GetString() << std::endl;
}else if (iter->value.IsArray()){
std::string array_values;
for (auto& v : iter->value.GetArray()){
if (!array_values.empty()){
array_values += ",";
}
array_values += v.GetString();
}
std::cout << array_values << std::endl;
}
}

return 0;
Expand Down
30 changes: 28 additions & 2 deletions examples/features/trpc_json/server/demo_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,35 @@ class DemoServiceImpl : public ::trpc::RpcServiceImpl {
const rapidjson::Document* request,
rapidjson::Document* reply) {
for (rapidjson::Value::ConstMemberIterator iter = request->MemberBegin(); iter != request->MemberEnd(); ++iter) {
TRPC_FMT_INFO("json name: {}, value: {}", iter->name.GetString(), iter->value.GetInt());
if (iter->value.IsInt()) {
TRPC_FMT_INFO("json name: {}, value: {}", iter->name.GetString(), iter->value.GetInt());
} else if (iter->value.IsArray()) {
std::string array_values;
for (auto& v : iter->value.GetArray()) {
if (!array_values.empty()){
array_values += ",";
}
array_values += v.GetString();
}
TRPC_FMT_INFO("json name: {}, values: {}", iter->name.GetString(), array_values);
} else if (iter->value.IsString()) {
TRPC_FMT_INFO("json name: {}, value: {}", iter->name.GetString(), iter->value.GetString());
}
}
reply->CopyFrom(*request, const_cast<rapidjson::Document*>(request)->GetAllocator());

// Copy request to reply to start with the same JSON
reply->CopyFrom(*request, reply->GetAllocator());
// Check and modify the "name" field if it exists
if (reply->HasMember("name") && (*reply)["name"].IsString()) {
std::string modifiedName = "hello " + std::string((*reply)["name"].GetString());
(*reply)["name"].SetString(modifiedName.c_str(), modifiedName.length(), reply->GetAllocator());
}
// Check and increment the "age" field if it exists
if (reply->HasMember("age") && (*reply)["age"].IsInt()) {
int incrementedAge = (*reply)["age"].GetInt() + 1;
(*reply)["age"].SetInt(incrementedAge);
}

return ::trpc::kSuccStatus;
}
};
Expand Down
16 changes: 14 additions & 2 deletions examples/features/trpc_noop/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <atomic>
#include <csignal>
#include <cstdint>
#include <nlohmann/json.hpp>

#include "gflags/gflags.h"

Expand All @@ -27,7 +28,13 @@ DEFINE_string(addr, "127.0.0.1:12351", "ip:port");
DEFINE_string(client_config, "trpc_cpp_fiber.yaml", "");

void SendStringMsg(const std::shared_ptr<::trpc::RpcServiceProxy>& proxy) {
std::string req_msg("hello world");
nlohmann::json json_req_msg = {
{"name","issueshooter"},
{"age",18},
{"hobby",{"opensource project","movies","books"}}
};
//Convert JSON to string
std::string req_msg = json_req_msg.dump();
std::string rsp_msg;

::trpc::ClientContextPtr context = ::trpc::MakeClientContext(proxy);
Expand All @@ -44,7 +51,12 @@ void SendStringMsg(const std::shared_ptr<::trpc::RpcServiceProxy>& proxy) {
}

void SendNoncontiguousBufferMsg(const std::shared_ptr<::trpc::RpcServiceProxy>& proxy) {
::trpc::NoncontiguousBuffer req_msg = ::trpc::CreateBufferSlow("hello world");
nlohmann::json json_req_msg = {
{"name","issueshooter"},
{"age",18},
{"hobby",{"opensource project","movies","books"}}
};
::trpc::NoncontiguousBuffer req_msg = ::trpc::CreateBufferSlow(json_req_msg.dump());
::trpc::NoncontiguousBuffer rsp_msg;

::trpc::ClientContextPtr context = ::trpc::MakeClientContext(proxy);
Expand Down
30 changes: 26 additions & 4 deletions examples/features/trpc_noop/server/demo_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <functional>
#include <memory>
#include <string>
#include <nlohmann/json.hpp>

#include "trpc/common/trpc_app.h"
#include "trpc/log/trpc_log.h"
Expand All @@ -40,16 +41,37 @@ class DemoServiceImpl : public ::trpc::RpcServiceImpl {
::trpc::Status NoopSayHello1(const ::trpc::ServerContextPtr& context,
const std::string* request,
std::string* reply) {
TRPC_FMT_INFO("request msg: {}", *request);
*reply = *request;
//Parse the JSON string back to JSON.
nlohmann::json req_json = nlohmann::json::parse(*request);
TRPC_FMT_INFO("request msg: {}", req_json.dump());

if (req_json.contains("name") && req_json["name"].is_string()) {
req_json["name"] = "hello " + req_json["name"].get<std::string>();
}
if (req_json.contains("age") && req_json["age"].is_number_integer()) {
req_json["age"] = req_json["age"].get<int>() + 1;
}

//Convert modified JSON to string to send back.
*reply = req_json.dump();
return ::trpc::kSuccStatus;
}

::trpc::Status NoopSayHello2(const ::trpc::ServerContextPtr& context,
const ::trpc::NoncontiguousBuffer* request,
::trpc::NoncontiguousBuffer* reply) {
TRPC_FMT_INFO("request msg: {}", ::trpc::FlattenSlow(*request));
*reply = *request;
nlohmann::json req_json = nlohmann::json::parse(::trpc::FlattenSlow(*request));
TRPC_FMT_INFO("request msg: {}", req_json.dump());

if (req_json.contains("age") && req_json["age"].is_number_integer()) {
req_json["age"] = req_json["age"].get<int>() + 2;
}
if (req_json.contains("name") && req_json["name"].is_string()) {
req_json["name"] = "bye " + req_json["name"].get<std::string>();
}

std::string mod_req = req_json.dump();
*reply = ::trpc::CreateBufferSlow(mod_req.data(), mod_req.size());
return ::trpc::kSuccStatus;
}
};
Expand Down
26 changes: 19 additions & 7 deletions examples/helloworld/greeter_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ ::trpc::Status GreeterServiceImpl::SayHello(::trpc::ServerContextPtr context,
const ::trpc::test::helloworld::HelloRequest* request,
::trpc::test::helloworld::HelloReply* reply) {
// Your can access more information from rpc context, eg: remote ip and port
TRPC_FMT_INFO("remote address: {}:{}", context->GetIp(), context->GetPort());
TRPC_FMT_INFO("request message: {}", request->msg());

std::string response = "Hello, " + request->msg();
reply->set_msg(response);

return ::trpc::kSuccStatus;
TRPC_FMT_INFO("remote address: {}:{}", context->GetIp(), context->GetPort());
TRPC_FMT_INFO("request name: {}", request->name());
TRPC_FMT_INFO("request age: {}", request->age());
std::string hobbies_str;
for (const auto& hobby : request->hobby()) {
if (!hobbies_str.empty()) {
hobbies_str += ", ";
}
hobbies_str += hobby;
}
TRPC_FMT_INFO("request hobby: {}", hobbies_str);

reply->set_name("hello " + request->name());
reply->set_age(request->age() + 1);
for (const auto& hobby : request->hobby()) {
reply->add_hobby(hobby);
}

return ::trpc::kSuccStatus;
}

} // namespace helloworld
Expand Down
15 changes: 12 additions & 3 deletions examples/helloworld/greeter_service_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ ::trpc::ServerContextPtr MakeServerContext() {

TEST(GreeterServiceTest, SayHelloOK) {
auto server_context = MakeServerContext();

::trpc::test::helloworld::HelloRequest request;
request.set_msg("tRPC");
request.set_name("issueshooter");
request.set_age(18);
request.add_hobby("opensource project");
request.add_hobby("movies");
request.add_hobby("books");

::trpc::test::helloworld::HelloReply response;

test::helloworld::GreeterServiceImpl svc_impl;
::trpc::Status status = svc_impl.SayHello(server_context, &request, &response);

ASSERT_TRUE(status.OK());
ASSERT_EQ(response.msg(), "Hello, tRPC");
ASSERT_EQ(response.name(), "hello issueshooter");
ASSERT_EQ(response.age(), 19);
ASSERT_EQ(response.hobby_size(), 3);
ASSERT_EQ(response.hobby(0), "opensource project");
ASSERT_EQ(response.hobby(1), "movies");
ASSERT_EQ(response.hobby(2), "books");
}

} // namespace testing
Expand Down
10 changes: 7 additions & 3 deletions examples/helloworld/helloworld.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ service Greeter {
}

message HelloRequest {
string msg = 1;
string name = 1;
int32 age = 2;
repeated string hobby = 3;
}

message HelloReply {
string msg = 1;
}
string name = 1;
int32 age = 2;
repeated string hobby = 3;
}
36 changes: 26 additions & 10 deletions examples/helloworld/test/fiber_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,32 @@ DEFINE_string(service_name, "trpc.test.helloworld.Greeter", "callee service name

int DoRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProxy>& proxy) {
::trpc::ClientContextPtr client_ctx = ::trpc::MakeClientContext(proxy);
::trpc::test::helloworld::HelloRequest req;
req.set_msg("fiber");
::trpc::test::helloworld::HelloReply rsp;
::trpc::Status status = proxy->SayHello(client_ctx, req, &rsp);
if (!status.OK()) {
std::cerr << "get rpc error: " << status.ErrorMessage() << std::endl;
return -1;
}
std::cout << "get rsp msg: " << rsp.msg() << std::endl;
return 0;

::trpc::test::helloworld::HelloRequest req;
req.set_name("issueshooter");
req.set_age(18);
req.add_hobby("opensource project");
req.add_hobby("movies");
req.add_hobby("books");

::trpc::test::helloworld::HelloReply rsp;
::trpc::Status status = proxy->SayHello(client_ctx, req, &rsp);
if (!status.OK()) {
std::cerr << "get rpc error: " << status.ErrorMessage() << std::endl;
return -1;
}
std::cout << "get rsp: \n name: " << rsp.name() << std::endl;
std::cout << " age: " << rsp.age() << std::endl;
std::cout << " hobby: ";
std::string hobbies_str;
for (const auto& hobby : rsp.hobby()) {
if (!hobbies_str.empty()) {
hobbies_str += ", ";
}
hobbies_str += hobby;
}
std::cout << hobbies_str << std::endl;
return 0;
}

int Run() {
Expand Down
37 changes: 27 additions & 10 deletions examples/helloworld/test/future_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,37 @@ DEFINE_string(service_name, "trpc.test.helloworld.Greeter", "callee service name

int DoAsyncRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProxy>& proxy) {
::trpc::ClientContextPtr client_ctx = ::trpc::MakeClientContext(proxy);

::trpc::test::helloworld::HelloRequest req;
req.set_msg("future");
req.set_name("issueshooter");
req.set_age(18);
req.add_hobby("opensource project");
req.add_hobby("movies");
req.add_hobby("books");

bool succ = true;
auto fut = proxy->AsyncSayHello(client_ctx, req)
.Then([&succ](::trpc::Future<::trpc::test::helloworld::HelloReply>&& fut) {
if (fut.IsReady()) {
auto rsp = fut.GetValue0();
std::cout << "get rsp msg: " << rsp.msg() << std::endl;
} else {
auto exception = fut.GetException();
succ = false;
std::cerr << "get rpc error: " << exception.what() << std::endl;
}
return ::trpc::MakeReadyFuture<>();
if (fut.IsReady()) {
auto rsp = fut.GetValue0();
std::cout << "get rsp:" << std::endl;
std::cout << " name: " << rsp.name() << std::endl;
std::cout << " age: " << rsp.age() << std::endl;
std::cout << " hobby: ";
std::string hobbies_str;
for (const auto& hobby : rsp.hobby()) {
if (!hobbies_str.empty()) {
hobbies_str += ", ";
}
hobbies_str += hobby;
}
std::cout << hobbies_str << std::endl;
} else {
auto exception = fut.GetException();
succ = false;
std::cerr << "get rpc error: " << exception.what() << std::endl;
}
return ::trpc::MakeReadyFuture<>();
});
::trpc::future::BlockingGet(std::move(fut));
return succ ? 0 : -1;
Expand Down

0 comments on commit 7bce783

Please sign in to comment.