|
1 | 1 | // Copyright (c) 2020 by Chrono |
2 | 2 | // |
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 |
6 | 6 |
|
7 | 7 | //#include <iostream> |
8 | 8 |
|
|
14 | 14 | // you should put json.hpp in ../common |
15 | 15 | #include "json.hpp" |
16 | 16 |
|
| 17 | +#include <cpr/cpr.h> |
| 18 | + |
17 | 19 | USING_NAMESPACE(std); |
18 | 20 | USING_NAMESPACE(cpp_study); |
19 | 21 |
|
|
44 | 46 | std::atomic_int count {0}; |
45 | 47 |
|
46 | 48 | // todo: try-catch |
47 | | - auto cycle = [&](const auto& addr) |
| 49 | + auto recv_cycle = [&](const auto& addr) |
48 | 50 | { |
49 | 51 | using zmq_ctx = ZmqContext<1>; |
50 | 52 |
|
|
89 | 91 | } |
90 | 92 | }; |
91 | 93 |
|
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, |
93 | 126 | conf.get<string>("config.zmq_ipc_addr")); |
94 | 127 |
|
95 | 128 | fu.wait(); |
|
0 commit comments