Skip to content

Commit 4a4adc2

Browse files
committed
fix: test individual module
1 parent e66f881 commit 4a4adc2

File tree

6 files changed

+42
-208
lines changed

6 files changed

+42
-208
lines changed

test/test_http_server.cpp

Lines changed: 13 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,21 @@
1-
#include <boost/asio.hpp>
2-
#include <boost/beast.hpp>
3-
#include <iostream>
4-
#include <memory>
5-
#include <string>
6-
7-
namespace asio = boost::asio;
8-
namespace beast = boost::beast;
9-
namespace http = beast::http;
10-
using tcp = asio::ip::tcp;
11-
12-
class HttpSession : public std::enable_shared_from_this<HttpSession> {
13-
public:
14-
explicit HttpSession(tcp::socket socket)
15-
: stream_(std::move(socket)) {}
16-
17-
void Start() { ReadRequest(); }
18-
19-
private:
20-
beast::tcp_stream stream_;
21-
beast::flat_buffer buffer_;
22-
http::request<http::string_body> req_;
23-
std::shared_ptr<http::response<http::string_body>> res_;
24-
25-
void ReadRequest() {
26-
auto self = shared_from_this();
27-
http::async_read(stream_, buffer_, req_,
28-
[self](beast::error_code ec, std::size_t bytes_transferred) {
29-
if (!ec) {
30-
self->HandleRequest();
31-
} else {
32-
std::cerr << "Read error: " << ec.message() << "\n";
33-
}
34-
});
35-
}
36-
37-
void HandleRequest() {
38-
// Prepare the response
39-
res_ =
40-
std::make_shared<http::response<http::string_body>>(http::status::ok, req_.version());
41-
res_->set(http::field::server, "Boost.Beast/HTTP");
42-
res_->set(http::field::content_type, "text/plain");
43-
res_->body() = "Hello, world!";
44-
res_->prepare_payload();
45-
46-
WriteResponse();
47-
}
48-
49-
void WriteResponse() {
50-
auto self = shared_from_this();
51-
http::async_write(stream_, *res_,
52-
[self](beast::error_code ec, std::size_t bytes_transferred) {
53-
if (!ec) {
54-
std::cout << "Response sent successfully\n";
55-
self->CloseConnection();
56-
} else {
57-
std::cerr << "Write error: " << ec.message() << "\n";
58-
}
59-
});
60-
}
61-
62-
void CloseConnection() {
63-
beast::error_code ec;
64-
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
65-
if (ec) {
66-
std::cerr << "Shutdown error: " << ec.message() << "\n";
67-
}
68-
}
69-
};
70-
71-
class HttpServer {
72-
public:
73-
HttpServer(asio::io_context &ioc, const tcp::endpoint &endpoint)
74-
: acceptor_(ioc, endpoint) {}
75-
76-
void Start() { AcceptConnection(); }
77-
78-
private:
79-
tcp::acceptor acceptor_;
80-
81-
void AcceptConnection() {
82-
acceptor_.async_accept([this](beast::error_code ec, tcp::socket socket) {
83-
if (!ec) {
84-
std::make_shared<HttpSession>(std::move(socket))->Start();
85-
} else {
86-
std::cerr << "Accept error: " << ec.message() << "\n";
87-
}
88-
AcceptConnection();
89-
});
90-
}
91-
};
1+
#include "signaling/http_service.h"
922

933
int main() {
94-
try {
95-
asio::io_context ioc;
4+
Args args;
965

97-
// Server listens on port 8080
98-
tcp::endpoint endpoint(tcp::v4(), 8080);
99-
HttpServer server(ioc, endpoint);
6+
boost::asio::io_context ioc;
1007

101-
std::cout << "Server is running on port 8080...\n";
8+
boost::asio::steady_timer timer(ioc, std::chrono::seconds(5));
9+
timer.async_wait([&ioc](const boost::system::error_code &ec) {
10+
if (!ec) {
11+
std::cout << "no events in io_context" << std::endl;
12+
ioc.stop();
13+
}
14+
});
10215

103-
server.Start();
104-
ioc.run();
105-
} catch (const std::exception &e) {
106-
std::cerr << "Error: " << e.what() << "\n";
107-
}
16+
auto http_service = HttpService::Create(args, nullptr, ioc);
17+
http_service->Start();
10818

19+
ioc.run();
10920
return 0;
11021
}

test/test_libcamera.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ int main(int argc, char *argv[]) {
4646
cond_var.wait(lock, [&] {
4747
return is_finished;
4848
});
49+
50+
return 0;
4951
}

test/test_mqtt.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
int main(int argc, char *argv[]) {
77
Args args{.mqtt_username = "hakunamatata", .mqtt_password = "wonderful"};
88

9-
auto mqtt = MqttService::Create(args, nullptr, nullptr);
9+
auto mqtt_service = MqttService::Create(args, nullptr);
10+
mqtt_service->Start();
1011

11-
sleep(60);
12+
return 0;
1213
}

test/test_recorder.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
#include "args.h"
2-
#include "capturer/v4l2_capturer.h"
1+
#include "capturer/libcamera_capturer.h"
32
#include "recorder/recorder_manager.h"
43

54
int main(int argc, char *argv[]) {
65
Args args{.fps = 15,
76
.width = 1280,
87
.height = 720,
98
.sample_rate = 48000,
10-
.hw_accel = true,
11-
.format = V4L2_PIX_FMT_H264,
9+
.format = V4L2_PIX_FMT_YUV420,
1210
.record_path = "./"};
1311

14-
auto video_capture = V4L2Capturer::Create(args);
12+
auto video_capture = LibcameraCapturer::Create(args);
1513
auto audio_capture = PaCapturer::Create(args);
1614
auto recorder_mgr = RecorderManager::Create(video_capture, audio_capture, args);
17-
sleep(45);
15+
sleep(5);
1816

1917
return 0;
2018
}

test/test_v4l2_capturer.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "args.h"
21
#include "capturer/v4l2_capturer.h"
32

43
#include <condition_variable>
@@ -28,11 +27,7 @@ int main(int argc, char *argv[]) {
2827
bool is_finished = false;
2928
int i = 0;
3029
int images_nb = 10;
31-
Args args{.fps = 15,
32-
.width = 1280,
33-
.height = 720,
34-
.format = V4L2_PIX_FMT_MJPEG,
35-
.device = "/dev/video0"};
30+
Args args{.fps = 15, .width = 1280, .height = 720, .cameraId = 0, .format = V4L2_PIX_FMT_MJPEG};
3631

3732
auto capturer = V4L2Capturer::Create(args);
3833
auto observer = capturer->AsRawBufferObservable();
@@ -49,4 +44,6 @@ int main(int argc, char *argv[]) {
4944
cond_var.wait(lock, [&] {
5045
return is_finished;
5146
});
47+
48+
return 0;
5249
}

test/test_websocket.cpp

Lines changed: 17 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,25 @@
1-
#include <boost/asio/ssl.hpp>
2-
#include <boost/beast/core.hpp>
3-
#include <boost/beast/ssl.hpp>
4-
#include <boost/beast/websocket.hpp>
5-
#include <iostream>
6-
#include <string>
7-
8-
#include <boost/asio/buffers_iterator.hpp>
9-
#include <nlohmann/json.hpp>
10-
11-
namespace beast = boost::beast;
12-
namespace websocket = beast::websocket;
13-
namespace net = boost::asio;
14-
using tcp = net::ip::tcp;
15-
using json = nlohmann::json;
16-
17-
void OnHandshake(boost::system::error_code ec,
18-
websocket::stream<beast::ssl_stream<tcp::socket>> &ws) {
19-
if (!ec) {
20-
printf("WebSocket is connected!\n");
21-
22-
while (true) {
23-
beast::flat_buffer buffer;
24-
try {
25-
ws.read(buffer);
26-
} catch (const std::exception &e) {
27-
std::cerr << "Error: " << e.what() << std::endl;
28-
break;
29-
}
30-
31-
if (ws.got_text()) {
32-
std::string res = beast::buffers_to_string(buffer.data());
33-
34-
json jsonObj = json::parse(res.c_str());
35-
std::string action = jsonObj["action"];
36-
std::string message = jsonObj["message"];
37-
std::cout << "Received Action: " << action << std::endl;
38-
std::cout << "Received Message: " << message << std::endl;
39-
40-
if (action == "join") {
41-
std::string response = "{\"Action\":\"offer\",\"Message\":\"test\"}";
42-
ws.write(net::buffer(response));
43-
} else {
44-
break;
45-
}
46-
} else if (ws.got_binary()) {
47-
std::vector<uint8_t> binaryData(boost::asio::buffers_begin(buffer.data()),
48-
boost::asio::buffers_end(buffer.data()));
49-
std::cout << "Received Binary Data (size: " << binaryData.size() << " bytes)"
50-
<< std::endl;
51-
}
52-
}
53-
54-
// ws.close(websocket::close_code::normal);
55-
} else {
56-
printf("Handshake Error: %s", ec.message().c_str());
57-
}
58-
}
1+
#include "signaling/websocket_service.h"
592

603
int main() {
61-
net::io_context ioc;
62-
63-
net::ssl::context ctx(net::ssl::context::tls_client);
64-
ctx.set_default_verify_paths();
65-
ctx.set_verify_mode(net::ssl::verify_peer);
4+
Args args{
5+
.ws_token =
6+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
7+
"eyJleHAiOjE3NzM4OTI2NjEsImlzcyI6IkFQSVduUVRzNHRtVVp2QSIsIm5iZiI6MTc0MjM1NjY2MSwic3ViIj"
8+
"oiMTMxZGZjMzItNjRlMi00YjZiLTllZGEtZDdjYTU5NTNjYWJlIiwidmlkZW8iOnsicm9vbSI6ImRldmljZS0x"
9+
"Iiwicm9vbUpvaW4iOnRydWV9fQ.ThQTHYd8CBR0t3epwcak6oaleeu760V96UF8GbOMUks"};
6610

67-
tcp::resolver resolver(ioc);
68-
websocket::stream<beast::ssl_stream<tcp::socket>> ws(ioc, ctx);
69-
70-
std::string host = "api.picamera.live";
71-
std::string port = "443";
72-
std::string accessToken =
73-
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
74-
"eyJleHAiOjE3NzM4OTI2NjEsImlzcyI6IkFQSVduUVRzNHRtVVp2QSIsIm5iZiI6MTc0MjM1NjY2MSwic3ViIj"
75-
"oiMTMxZGZjMzItNjRlMi00YjZiLTllZGEtZDdjYTU5NTNjYWJlIiwidmlkZW8iOnsicm9vbSI6ImRldmljZS0x"
76-
"Iiwicm9vbUpvaW4iOnRydWV9fQ.ThQTHYd8CBR0t3epwcak6oaleeu760V96UF8GbOMUks";
77-
std::string target = "/rtc?token=" + accessToken;
78-
79-
auto const results = resolver.resolve(host, port);
11+
net::io_context ioc;
8012

81-
net::async_connect(
82-
beast::get_lowest_layer(ws), results, [&](boost::system::error_code ec, tcp::endpoint) {
83-
if (ec) {
84-
std::cerr << "Connect Error: " << ec.message() << std::endl;
85-
} else {
86-
ws.next_layer().async_handshake(
87-
net::ssl::stream_base::client, [&](boost::system::error_code ec) {
88-
if (ec) {
89-
return;
90-
}
13+
boost::asio::steady_timer timer(ioc, std::chrono::seconds(5));
14+
timer.async_wait([&ioc](const boost::system::error_code &ec) {
15+
if (!ec) {
16+
std::cout << "no events in io_context" << std::endl;
17+
ioc.stop();
18+
}
19+
});
9120

92-
ws.async_handshake(host, target, [&](boost::system::error_code ec) {
93-
OnHandshake(ec, ws);
94-
});
95-
});
96-
}
97-
});
21+
auto ws_service = WebsocketService::Create(args, nullptr, ioc);
22+
ws_service->Start();
9823

9924
ioc.run();
10025
return 0;

0 commit comments

Comments
 (0)