25 class TestHttpBackend :
public RemoteHandlerBackend
28 std::shared_ptr<RemoteHandlerBackend> bkd2;
30 nng_http_server *http_server;
31 nng_http_handler *handler_api;
32 nng_http_handler *handler_apiG;
33 nng_http_handler *handler_apiP;
38 static void http_handle(nng_aio *aio)
40 nlohmann::json output;
42 nng_http_handler *handler = (nng_http_handler *)nng_aio_get_input(aio, 1);
43 TestHttpBackend *tthis = (TestHttpBackend *)nng_http_handler_get_data(handler);
45 output = tthis->bkd2->get_cfg_list();
47 std::string jsonstr = output.dump();
50 nng_http_res_alloc(&res);
51 nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size());
52 nng_http_res_set_header(res,
"Content-Type",
"application/json; charset=utf-8");
53 nng_aio_set_output(aio, 0, res);
54 nng_aio_finish(aio, 0);
58 static void http_handleG(nng_aio *aio)
60 nng_http_req *msg = (nng_http_req *)nng_aio_get_input(aio, 0);
61 nng_http_handler *handler = (nng_http_handler *)nng_aio_get_input(aio, 1);
62 TestHttpBackend *tthis = (TestHttpBackend *)nng_http_handler_get_data(handler);
66 nng_http_req_get_data(msg, &ptr, &ptrl);
69 nlohmann::ordered_json output;
71 output = tthis->bkd2->get_cfg(std::string((
char *)ptr, ptrl));
73 std::string jsonstr = output.dump();
76 nng_http_res_alloc(&res);
77 nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size());
78 nng_http_res_set_header(res,
"Content-Type",
"application/json; charset=utf-8");
79 nng_aio_set_output(aio, 0, res);
80 nng_aio_finish(aio, 0);
84 static void http_handleP(nng_aio *aio)
86 nng_http_req *msg = (nng_http_req *)nng_aio_get_input(aio, 0);
87 nng_http_handler *handler = (nng_http_handler *)nng_aio_get_input(aio, 1);
88 TestHttpBackend *tthis = (TestHttpBackend *)nng_http_handler_get_data(handler);
92 nng_http_req_get_data(msg, &ptr, &ptrl);
96 nlohmann::ordered_json output;
100 input = nlohmann::json::parse(std::string((
char *)ptr, ptrl));
101 output[
"res"] = tthis->bkd2->set_cfg(input);
103 catch (std::exception &e)
106 output[
"err"] = e.what();
107 output[
"input"] = std::string((
char *)ptr, ptrl);
110 std::string jsonstr = output.dump();
113 nng_http_res_alloc(&res);
114 nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size());
115 nng_http_res_set_header(res,
"Content-Type",
"application/json; charset=utf-8");
116 nng_aio_set_output(aio, 0, res);
117 nng_aio_finish(aio, 0);
121 TestHttpBackend(std::shared_ptr<RemoteHandlerBackend> bkd2) : bkd2(bkd2)
123 bkd2->set_stream_rx_handler(
124 [
this](std::string v, uint8_t *dat,
size_t sz)
127 push_stream_data(v, dat, sz);
131 std::vector<uint8_t> send_buf;
132 send_buf.push_back(v.size());
133 send_buf.insert(send_buf.end(), v.begin(), v.begin() + v.size());
134 send_buf.push_back((fftsz >> 24) & 0xFF);
135 send_buf.push_back((fftsz >> 16) & 0xFF);
136 send_buf.push_back((fftsz >> 8) & 0xFF);
137 send_buf.push_back((fftsz >> 0) & 0xFF);
138 send_buf.insert(send_buf.end(), dat, dat + sz);
139 nng_send(socket, send_buf.data(), send_buf.size(), NNG_FLAG_NONBLOCK);
142 std::string http_server_url =
"http://0.0.0.0:8080";
146 nng_url_parse(&url, http_server_url.c_str());
147 nng_http_server_hold(&http_server, url);
151 nng_http_handler_alloc(&handler_api,
"/list", http_handle);
152 nng_http_handler_set_data(handler_api,
this, 0);
153 nng_http_handler_set_method(handler_api,
"GET");
154 nng_http_server_add_handler(http_server, handler_api);
158 nng_http_handler_alloc(&handler_apiP,
"/get", http_handleG);
159 nng_http_handler_set_data(handler_apiP,
this, 0);
160 nng_http_handler_set_method(handler_apiP,
"POST");
161 nng_http_server_add_handler(http_server, handler_apiP);
165 nng_http_handler_alloc(&handler_apiP,
"/set", http_handleP);
166 nng_http_handler_set_data(handler_apiP,
this, 0);
167 nng_http_handler_set_method(handler_apiP,
"POST");
168 nng_http_server_add_handler(http_server, handler_apiP);
174 if (rv = nng_bus0_open(&socket); rv != 0)
176 printf(
"pair open error\n");
179 if (rv = nng_listen(socket,
"ws://0.0.0.0:8080/ws",
nullptr, 0); rv != 0)
181 printf(
"server listen error\n");
187 nng_http_server_start(http_server);
190 ~TestHttpBackend() {}
192 nlohmann::ordered_json _get_cfg_list() {
return bkd2->get_cfg_list(); }
193 nlohmann::ordered_json _get_cfg(std::string key) {
return bkd2->get_cfg(key); }
194 cfg_res_t _set_cfg(std::string key, nlohmann::ordered_json v) {
return bkd2->set_cfg(key, v); }