-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
66 lines (54 loc) · 1.3 KB
/
main.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
#include <iostream>
#include <rest_rpc/server.hpp>
#include "registry.hpp"
#include "load_blancer.hpp"
struct configure
{
int port;
int thread_num;
bool nodelay;
META(port, thread_num, nodelay);
};
configure get_config()
{
std::ifstream in("server.cfg");
std::stringstream ss;
ss << in.rdbuf();
configure cfg = {};
DeSerializer dr;
try
{
dr.Parse(ss.str());
dr.Deserialize(cfg);
}
catch (const std::exception& e)
{
timax::SPD_LOG_ERROR(e.what());
}
return cfg;
}
int main()
{
using namespace timax::rpc;
using timax::rpc::server;
using codec_type = msgpack_codec;
timax::log::get().init("rest_rpc_server.lg");
auto cfg = get_config();
int port = 9000;
int thread_num = std::thread::hardware_concurrency();
if (cfg.port != 0)
{
port = cfg.port;
thread_num = cfg.thread_num;
}
auto sp = std::make_shared<server<codec_type>>(port, thread_num);
registry reg;
sp->register_handler("register_service", ®istry::register_service, ®, [®](auto conn, auto r) { reg.set_conn(conn, r); });
sp->register_handler("unregister_service", ®istry::unregister_service, ®);
sp->set_disconnect_handler(std::bind(®istry::handle_disconnect, ®, std::placeholders::_1));
load_blancer bl(reg);
sp->register_handler("fetch", &load_blancer::fetch, &bl);
sp->run();
getchar();
return 0;
}