2018-12-20 12:20:12 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
#include "test_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;
|
|
|
|
|
test_client client(io_service);
|
|
|
|
|
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;
|
|
|
|
|
test_client client(io_service);
|
|
|
|
|
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;
|
|
|
|
|
test_client client(io_service);
|
|
|
|
|
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;
|
|
|
|
|
test_client client(io_service);
|
|
|
|
|
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;
|
|
|
|
|
test_client client(io_service);
|
|
|
|
|
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-08 11:21:15 +08:00
|
|
|
void test_async_client() {
|
|
|
|
|
async_client client("127.0.0.1", 9000);
|
|
|
|
|
client.connect();
|
|
|
|
|
|
2019-03-08 21:47:21 +08:00
|
|
|
client.call("hello", "world");
|
|
|
|
|
|
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;
|
|
|
|
|
}
|