-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbi.cpp
103 lines (90 loc) · 3.75 KB
/
bi.cpp
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
#include <functional>
#include <string>
#include <spdlog/spdlog.h>
#include "fantasy.hpp"
inline std::string addr{"tcp://127.0.0.1:5878"};
void start(std::function<void()> func) {
std::thread(std::move(func)).detach();
}
auto create_bank_info() {
fantasy::BankInfo bank_info;
bank_info.name = "xiaoli";
bank_info.type = fantasy::TestType::EnumOne;
bank_info.test_one = 100;
bank_info.test_two = 101;
bank_info.test_map_one.emplace("fantasy", fantasy::TestType::EnumOne);
bank_info.test_map.emplace(false, 555);
bank_info.test_vector.emplace_back("vector");
bank_info.info.name = "rpc";
bank_info.date_time = frpc::getFrpcDateTime();
return bank_info;
}
struct Handler final : public fantasy::HelloWorldServerHandler {
virtual void hello_world(fantasy::BankInfo bank_info,
std::string bank_name,
uint64_t blance,
std::optional<std::string> date,
frpc::DateTime date_time,
std::function<void(std::string, fantasy::Info, uint64_t, std::optional<std::string>)> cb) noexcept override {
spdlog::info("fantasy::HelloWorldServer server recv: {}, bank_name: {}, blance: {}, date: {}, date_time: {}",
fantasy::toString(bank_info), bank_name, blance, date.has_value() ? date.value() : "nullopt", frpc::toString(date_time));
fantasy::Info info;
info.name = "test";
cb("hello world", std::move(info), 789, "2024");
}
};
void start_server() {
frpc::ChannelConfig bi_config{};
bi_config.addr = addr;
auto server = fantasy::HelloWorldServer::create(
bi_config,
std::make_shared<Handler>(),
[](std::string error) {
spdlog::error("fantasy::HelloWorldServer error: {}", error);
});
auto monitor = std::make_unique<frpc::Monitor>(*(server->context()), *(server->socket()));
auto event_cb = [](std::optional<std::tuple<zmq_event_t, std::string>> data) {
if (!data.has_value())
return;
auto& [event, point] = data.value();
spdlog::info("monitor: {} {}", frpc::getEventName(event.event), point);
};
monitor->start(event_cb, ZMQ_EVENT_ACCEPTED | ZMQ_EVENT_DISCONNECTED);
server->start();
std::this_thread::sleep_for(std::chrono::seconds(8));
}
int main() {
start(start_server);
frpc::ChannelConfig bi_config{};
bi_config.addr = addr;
auto client = fantasy::HelloWorldClient::create(bi_config, [](std::string error) {
spdlog::error("fantasy::HelloWorldClient error: {}", error);
});
client->start();
client->hello_world(
create_bank_info(),
std::string{"fantasy-bank"},
999,
"option",
frpc::getFrpcDateTime(),
[](std::string reply, fantasy::Info info, uint64_t count, std::optional<std::string> date) {
spdlog::info("fantasy::HelloWorldClient::hello_world recv: {},{},{},{}", reply, fantasy::toString(info),
count, date.has_value() ? date.value() : "nullopt");
});
client->hello_world(
create_bank_info(),
std::string{"fantasy-bank-1"},
999,
"option",
frpc::getFrpcDateTime(),
[](std::string reply, fantasy::Info info, uint64_t count, std::optional<std::string> date) {
spdlog::info("fantasy::HelloWorldClient::hello_world(timeout) recv: {},{},{},{}", reply, fantasy::toString(info), count,
date.has_value() ? date.value() : "nullopt");
},
std::chrono::milliseconds(200),
[] {
spdlog::info("fantasy::HelloWorldClient::timeout timeout!!!");
});
std::this_thread::sleep_for(std::chrono::seconds(10));
return 0;
}