Files
rest_rpc/client/main.cpp
T

163 lines
3.2 KiB
C++
Raw Normal View History

2018-12-20 12:20:12 +08:00
#include <iostream>
2019-03-12 16:27:24 +08:00
#include "sync_client.hpp"
2019-03-08 11:21:15 +08:00
#include "async_client.hpp"
2018-12-20 12:20:12 +08:00
#include "../codec.h"
using namespace rest_rpc;
using namespace rest_rpc::rpc_service;
void test_add() {
try{
boost::asio::io_service io_service;
2019-03-12 16:16:11 +08:00
sync_client client(io_service);
2018-12-20 12:20:12 +08:00
client.connect("127.0.0.1", "9000");
auto result = client.call<int>("add", 1, 2);
std::cout << result << std::endl;
}
catch (const std::exception& e){
std::cout << e.what() << std::endl;
}
}
void test_translate() {
try {
boost::asio::io_service io_service;
2019-03-12 16:16:11 +08:00
sync_client client(io_service);
2018-12-20 12:20:12 +08:00
client.connect("127.0.0.1", "9000");
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 {
boost::asio::io_service io_service;
2019-03-12 16:16:11 +08:00
sync_client client(io_service);
2018-12-20 12:20:12 +08:00
client.connect("127.0.0.1", "9000");
client.call("hello", "purecpp");
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
2019-01-09 15:08:25 +08:00
struct person {
int id;
std::string name;
int age;
MSGPACK_DEFINE(id, name, age);
};
void test_get_person_name() {
try {
boost::asio::io_service io_service;
2019-03-12 16:16:11 +08:00
sync_client client(io_service);
2019-01-09 15:08:25 +08:00
client.connect("127.0.0.1", "9000");
2019-03-08 11:21:15 +08:00
auto result = client.call<std::string>("get_person_name", person{ 1, "tom", 20 });
2019-01-09 15:08:25 +08:00
std::cout << result << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
2019-01-10 06:03:42 +08:00
void test_get_person() {
try {
boost::asio::io_service io_service;
2019-03-12 16:16:11 +08:00
sync_client client(io_service);
2019-01-10 06:03:42 +08:00
client.connect("127.0.0.1", "9000");
auto result = client.call<person>("get_person");
std::cout << result.name << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
2019-03-18 15:25:24 +08:00
struct dummy {
void foo(boost::system::error_code ec, std::string_view data) {
if (ec) {
std::cout << "ec" << std::endl;
return;
}
std::cout << data.size() << std::endl;
}
};
2019-03-08 11:21:15 +08:00
void test_async_client() {
async_client client("127.0.0.1", 9000);
client.connect();
2019-03-12 15:21:41 +08:00
std::string str1;
std::cin >> str1;
2019-03-08 11:21:15 +08:00
2019-03-18 00:23:41 +08:00
client.set_error_callback([] (boost::system::error_code ec){
2019-03-12 15:21:41 +08:00
std::cout << ec.message() << std::endl;
});
2019-03-18 15:25:24 +08:00
dummy dm;
client.call("hello", "purecpp", std::bind(&dummy::foo, &dm, std::placeholders::_1,
std::placeholders::_2), 2000);
2019-03-18 00:23:41 +08:00
client.call("hello", "purecpp", [](auto ec, auto data) {
if (ec) {
std::cout << "ec" << std::endl;
2019-03-12 16:09:50 +08:00
return;
}
2019-03-18 15:25:24 +08:00
if (has_error(data)) {
std::cout << "rpc error" << std::endl;
return;
}
2019-03-18 00:23:41 +08:00
std::cout << data.size() << std::endl;
}, 2000);
2019-03-12 16:09:50 +08:00
2019-03-18 00:23:41 +08:00
client.call("hello", "purecpp", [](auto ec, auto data) {
if (ec) {
std::cout << "ec" << std::endl;
return;
}
std::cout << data.size() << std::endl;
2019-03-12 15:21:41 +08:00
});
2019-03-18 00:23:41 +08:00
client.call("get_person", [](auto ec, auto data) {
if (ec) {
std::cout << "ec" << std::endl;
return;
2019-03-12 16:09:50 +08:00
}
2019-03-18 00:23:41 +08:00
std::cout << data.size() << std::endl;
2019-03-12 15:21:41 +08:00
});
2019-03-18 00:23:41 +08:00
client.call("get_person", [](auto ec, auto data) {
if (ec) {
std::cout << "ec" << std::endl;
return;
}
std::cout << data.size() << std::endl;
}, 2000);
2019-03-12 16:09:50 +08:00
client.call("get_person");
2019-03-08 21:47:21 +08:00
2019-03-08 11:21:15 +08:00
std::string str;
std::cin >> str;
}
2018-12-20 12:20:12 +08:00
int main() {
2019-03-08 11:21:15 +08:00
test_async_client();
2019-01-10 06:03:42 +08:00
test_get_person();
2019-01-09 15:08:25 +08:00
test_get_person_name();
2018-12-20 12:20:12 +08:00
test_hello();
test_add();
test_translate();
return 0;
}