SatDump 2.0.0-alpha-520736c72
Loading...
Searching...
No Matches
test_http.h
1#pragma once
2
3#include "../base/remote_handler_backend.h"
4#include "core/exception.h"
5#include "logger.h"
6#include "nlohmann/json.hpp"
7#include <cstddef>
8#include <cstdint>
9#include <exception>
10#include <functional>
11#include <memory>
12#include <mutex>
13#include <string>
14
15#include <nng/nng.h>
16#include <nng/protocol/bus0/bus.h>
17#include <nng/protocol/pair0/pair.h>
18#include <nng/supplemental/http/http.h>
19#include <nng/supplemental/util/platform.h>
20
21namespace satdump
22{
23 namespace handlers
24 {
25 class TestHttpBackend : public RemoteHandlerBackend
26 {
27 private:
28 std::shared_ptr<RemoteHandlerBackend> bkd2;
29
30 nng_http_server *http_server;
31 nng_http_handler *handler_api;
32 nng_http_handler *handler_apiG;
33 nng_http_handler *handler_apiP;
34 nng_socket socket;
35
36 private:
37 // HTTP Handler for stats
38 static void http_handle(nng_aio *aio)
39 {
40 nlohmann::json output;
41
42 nng_http_handler *handler = (nng_http_handler *)nng_aio_get_input(aio, 1);
43 TestHttpBackend *tthis = (TestHttpBackend *)nng_http_handler_get_data(handler);
44
45 output = tthis->bkd2->get_cfg_list();
46
47 std::string jsonstr = output.dump();
48
49 nng_http_res *res;
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);
55 }
56
57 // HTTP Handler for commands
58 static void http_handleG(nng_aio *aio)
59 {
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);
63
64 void *ptr;
65 size_t ptrl;
66 nng_http_req_get_data(msg, &ptr, &ptrl);
67 // logger->info("Got : %s", (char *)ptr);
68
69 nlohmann::ordered_json output;
70
71 output = tthis->bkd2->get_cfg(std::string((char *)ptr, ptrl));
72
73 std::string jsonstr = output.dump();
74
75 nng_http_res *res;
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);
81 }
82
83 // HTTP Handler for commands
84 static void http_handleP(nng_aio *aio)
85 {
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);
89
90 void *ptr;
91 size_t ptrl;
92 nng_http_req_get_data(msg, &ptr, &ptrl);
93 // logger->info("Got : %s", (char *)ptr);
94
95 nlohmann::json input;
96 nlohmann::ordered_json output;
97
98 try
99 {
100 input = nlohmann::json::parse(std::string((char *)ptr, ptrl));
101 output["res"] = tthis->bkd2->set_cfg(input);
102 }
103 catch (std::exception &e)
104 {
105 output["res"] = 2;
106 output["err"] = e.what();
107 output["input"] = std::string((char *)ptr, ptrl);
108 }
109
110 std::string jsonstr = output.dump();
111
112 nng_http_res *res;
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);
118 }
119
120 public:
121 TestHttpBackend(std::shared_ptr<RemoteHandlerBackend> bkd2) : bkd2(bkd2)
122 {
123 bkd2->set_stream_rx_handler(
124 [this](std::string v, uint8_t *dat, size_t sz)
125 {
126 // logger->info("Stream " + v);
127 push_stream_data(v, dat, sz);
128
129 uint32_t fftsz = sz;
130
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);
140 });
141
142 std::string http_server_url = "http://0.0.0.0:8080";
143
144 nng_url *url;
145 {
146 nng_url_parse(&url, http_server_url.c_str());
147 nng_http_server_hold(&http_server, url);
148 }
149
150 {
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);
155 }
156
157 {
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);
162 }
163
164 {
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);
169 }
170
171 {
172
173 int rv = 0;
174 if (rv = nng_bus0_open(&socket); rv != 0)
175 {
176 printf("pair open error\n");
177 }
178
179 if (rv = nng_listen(socket, "ws://0.0.0.0:8080/ws", nullptr, 0); rv != 0)
180 {
181 printf("server listen error\n");
182 }
183 }
184
185 nng_url_free(url);
186
187 nng_http_server_start(http_server);
188 }
189
190 ~TestHttpBackend() {}
191
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); }
195 };
196 } // namespace handlers
197} // namespace satdump