Skip to content

Commit c135fe0

Browse files
committed
srv.cpp
1 parent 107343f commit c135fe0

3 files changed

Lines changed: 42 additions & 9 deletions

File tree

section5/Config.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Config final
4545
}
4646

4747
template<typename T>
48-
T get(string_view_type key)
48+
T get(string_view_type key) const
4949
{
5050
if (!std::regex_match(key, m_what, m_reg)) {
5151
throw std::runtime_error("config key error");
@@ -64,8 +64,8 @@ class Config final
6464
private:
6565
vm_type m_vm {luaL_newstate(), lua_close};
6666

67-
regex_type m_reg {R"(^(\w+)\.(\w+)$)"};
68-
match_type m_what;
67+
regex_type m_reg {R"(^(\w+)\.(\w+)$)"};
68+
mutable match_type m_what;
6969
};
7070

7171
END_NAMESPACE(cpp_study)

section5/conf.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ config = {
1818

1919
best_n = 3,
2020

21-
time_interval = 1,
21+
time_interval = 1, -- seconds
2222

2323
}
2424

section5/srv.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) 2020 by Chrono
22
//
3-
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -o a.out;./a.out
4-
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -g -O0 -o a.out
5-
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -g -O0 -o a.out;./a.out
3+
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -lcpr -lcurl -o a.out;./a.out
4+
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -lcpr -lcurl -g -O0 -o a.out
5+
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -lcpr -lcurl -g -O0 -o a.out;./a.out
66

77
//#include <iostream>
88

@@ -14,6 +14,8 @@
1414
// you should put json.hpp in ../common
1515
#include "json.hpp"
1616

17+
#include <cpr/cpr.h>
18+
1719
USING_NAMESPACE(std);
1820
USING_NAMESPACE(cpp_study);
1921

@@ -44,7 +46,7 @@ try
4446
std::atomic_int count {0};
4547

4648
// todo: try-catch
47-
auto cycle = [&](const auto& addr)
49+
auto recv_cycle = [&](const auto& addr)
4850
{
4951
using zmq_ctx = ZmqContext<1>;
5052

@@ -89,7 +91,38 @@ try
8991
}
9092
};
9193

92-
auto fu = std::async(cycle,
94+
auto log_cycle = [&]()
95+
{
96+
auto http_addr = conf.get<string>("config.http_addr");
97+
auto time_interval = conf.get<int>("config.time_interval");
98+
99+
for(;;) {
100+
std::this_thread::sleep_for(time_interval * 1s);
101+
//cout << "log_cycle" << endl;
102+
103+
using json_t = nlohmann::json;
104+
105+
json_t j;
106+
107+
j["count"] = static_cast<int>(count);
108+
109+
auto res = cpr::Post(
110+
cpr::Url{http_addr},
111+
cpr::Body{j.dump()},
112+
cpr::Timeout{200ms}
113+
);
114+
115+
if (res.status_code != 200) {
116+
cout << "http post failed" << endl;
117+
}
118+
}
119+
};
120+
121+
// launch log_cycle
122+
std::async(std::launch::async, log_cycle);
123+
124+
// launch recv_cycle then wait
125+
auto fu = std::async(recv_cycle,
93126
conf.get<string>("config.zmq_ipc_addr"));
94127

95128
fu.wait();

0 commit comments

Comments
 (0)