Files
rest_rpc/examples/client/main.cpp
T

309 lines
6.4 KiB
C++
Raw Normal View History

2019-03-21 16:03:24 +08:00
#include <iostream>
2019-04-03 11:27:35 +08:00
#include <rpc_client.hpp>
#include <chrono>
2019-03-28 12:58:18 +08:00
#include <fstream>
2019-03-21 16:03:24 +08:00
#include "codec.h"
using namespace rest_rpc;
using namespace rest_rpc::rpc_service;
void test_add() {
try{
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
2019-03-21 16:03:24 +08:00
2019-04-19 15:39:23 +08:00
{
auto result = client.call<int>("add", 1, 2);
std::cout << result << std::endl;
}
{
auto result = client.call<2000, int>("add", 1, 2);
std::cout << result << std::endl;
}
2019-03-21 16:03:24 +08:00
}
catch (const std::exception& e){
std::cout << e.what() << std::endl;
}
}
void test_translate() {
try {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
2019-03-21 16:03:24 +08:00
auto result = client.call<std::string>("translate", "hello");
std::cout << result << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void test_hello() {
try {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
2019-03-21 16:03:24 +08:00
client.call("hello", "purecpp");
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
struct person {
int id;
std::string name;
int age;
MSGPACK_DEFINE(id, name, age);
};
void test_get_person_name() {
try {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
2019-03-21 16:03:24 +08:00
auto result = client.call<std::string>("get_person_name", person{ 1, "tom", 20 });
std::cout << result << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void test_get_person() {
try {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
2019-03-21 16:03:24 +08:00
auto result = client.call<person>("get_person");
std::cout << result.name << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void test_async_client() {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
client.async_connect();
2019-03-27 10:00:16 +08:00
bool r = client.wait_conn(1);
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
2019-03-21 16:03:24 +08:00
client.set_error_callback([] (boost::system::error_code ec){
std::cout << ec.message() << std::endl;
});
auto f = client.async_call("get_person");
2019-04-10 14:15:29 +08:00
if (f.wait_for(std::chrono::milliseconds(50)) == std::future_status::timeout) {
2019-03-21 16:03:24 +08:00
std::cout << "timeout" << std::endl;
}
else {
auto p = f.get().as<person>();
std::cout << p.name << std::endl;
}
auto fu = client.async_call("hello", "purecpp");
fu.get().as(); //no return
//sync call
client.call("hello", "purecpp");
auto p = client.call<person>("get_person");
std::string str;
std::cin >> str;
}
2019-03-28 12:58:18 +08:00
void test_upload() {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
client.async_connect();
2019-03-28 12:58:18 +08:00
bool r = client.wait_conn(1);
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
std::ifstream file("E:/acl.7z", std::ios::binary);
file.seekg(0, std::ios::end);
size_t file_len = file.tellg();
file.seekg(0, std::ios::beg);
std::string conent;
conent.resize(file_len);
file.read(&conent[0], file_len);
std::cout << file_len << std::endl;
{
auto f = client.async_call("upload", "test", conent);
2019-04-10 14:15:29 +08:00
if (f.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) {
2019-03-28 12:58:18 +08:00
std::cout << "timeout" << std::endl;
}
else {
f.get().as();
std::cout << "ok" << std::endl;
}
}
{
auto f = client.async_call("upload", "test1", conent);
2019-04-10 14:15:29 +08:00
if (f.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) {
2019-03-28 12:58:18 +08:00
std::cout << "timeout" << std::endl;
}
else {
f.get().as();
std::cout << "ok" << std::endl;
}
}
}
void test_download() {
2019-04-03 11:27:35 +08:00
rpc_client client("127.0.0.1", 9000);
client.async_connect();
2019-03-28 12:58:18 +08:00
bool r = client.wait_conn(1);
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
auto f = client.async_call("download", "test");
2019-04-10 14:15:29 +08:00
if (f.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) {
2019-03-28 12:58:18 +08:00
std::cout << "timeout" << std::endl;
}
else {
auto content = f.get().as<std::string>();
std::cout << content.size() << std::endl;
std::ofstream file("test", std::ios::binary);
file.write(content.data(), content.size());
}
}
2019-04-03 11:27:35 +08:00
void test_sync_client() {
test_add();
test_translate();
test_hello();
test_get_person_name();
test_get_person();
}
2019-04-04 16:55:48 +08:00
void test_performance1() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
person p{ 1, "tom", 20 };
for (size_t i = 0; i < 100000000; i++) {
auto future = client.async_call("get_name", p);
2019-04-10 14:15:29 +08:00
auto status = future.wait_for(std::chrono::seconds(2));
2019-04-04 16:55:48 +08:00
if (status == std::future_status::deferred) {
std::cout << "deferred\n";
}
else if (status == std::future_status::timeout) {
std::cout << "timeout\n";
}
else if (status == std::future_status::ready) {
}
}
std::cout << "finish\n";
std::string str;
std::cin >> str;
}
void test_performance2() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
person p{ 1, "tom", 20 };
try {
for (size_t i = 0; i < 100000000; i++) {
client.call<std::string>("get_name", p);
}
std::cout << "finish\n";
}
catch (const std::exception& ex) {
std::cout << ex.what() << '\n';
}
std::string str;
std::cin >> str;
}
2019-04-19 15:39:23 +08:00
void test_call_with_timeout() {
rpc_client client;
client.async_connect("127.0.0.1", 9000);
try {
auto result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
2019-04-19 17:48:48 +08:00
client.close();
bool r = client.connect();
result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
2019-04-19 15:39:23 +08:00
}
catch (const std::exception& ex) {
std::cout << ex.what() << std::endl;
}
std::string str;
std::cin >> str;
}
2019-04-26 15:42:34 +08:00
void test_connect(){
rpc_client client;
client.set_error_callback([&client] (boost::system::error_code ex) {
client.async_reconnect();
});
bool r = client.connect("127.0.0.1", 9000);
if (!r) {
client.async_reconnect();
}
std::string str;
std::cin >> str;
}
2019-03-21 16:03:24 +08:00
int main() {
2019-04-26 15:42:34 +08:00
test_connect();
2019-04-19 17:48:48 +08:00
test_call_with_timeout();
2019-04-03 11:27:35 +08:00
test_sync_client();
test_async_client();
2019-04-19 17:48:48 +08:00
2019-03-28 12:58:18 +08:00
//test_upload();
//test_download();
2019-04-04 17:25:03 +08:00
//test_performance1();
2019-03-21 16:03:24 +08:00
return 0;
}