Skip to content

Commit 5ee2cdd

Browse files
committed
Refactor: Split noctx code into more files
1 parent 7ef3711 commit 5ee2cdd

30 files changed

+1444
-1224
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ tests/validation/logs
7070
html/
7171
test.*
7272
build/
73-
core*
7473
out*
7574
assets/
7675
*.pkg

tests/integration_tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ sources = files('tests.cpp', 'st_test.cpp', 'st20_test.cpp', 'st22_test.cpp',
88

99
subdir('noctx')
1010
sources += noctx_sources
11+
test_inc = include_directories('.', 'noctx')
1112

1213
ufd_sources = files('ufd_test.cpp', 'ufd_loop_test.cpp', 'test_util.cpp')
1314

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
#pragma once
3+
4+
#ifndef SESSION_SKIP_PORT
5+
#define SESSION_SKIP_PORT -1
6+
#endif
7+
8+
#ifndef VIDEO_CLOCK_HZ
9+
#define VIDEO_CLOCK_HZ 90000
10+
#endif
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include "handler_base.hpp"
4+
5+
#include <cstdio>
6+
#include <stdexcept>
7+
8+
Handlers::Handlers(st_tests_context* ctx, FrameTestStrategy* frameTestStrategy)
9+
: ctx(ctx), frameTestStrategy(frameTestStrategy) {
10+
}
11+
12+
Handlers::~Handlers() {
13+
session.stop();
14+
}
15+
16+
void Handlers::startSession(
17+
std::vector<std::function<void(std::atomic<bool>&)>> threadFunctions) {
18+
for (auto& func : threadFunctions) {
19+
session.addThread(func);
20+
}
21+
}
22+
23+
void Handlers::stopSession() {
24+
session.stop();
25+
}
26+
27+
void Handlers::setSessionPortsTx(struct st_tx_port* port, int txPortIdx,
28+
int txPortRedundantIdx) {
29+
if (!ctx) {
30+
throw std::runtime_error("setSessionPortsTx no ctx (ctx is null)");
31+
} else if (txPortIdx >= (int)ctx->para.num_ports) {
32+
throw std::runtime_error("setSessionPortsTx txPortIdx out of range");
33+
} else if (txPortRedundantIdx >= (int)ctx->para.num_ports) {
34+
throw std::runtime_error("setSessionPortsTx txPortRedundantIdx out of range");
35+
}
36+
37+
if (txPortIdx >= 0) {
38+
snprintf(port->port[MTL_SESSION_PORT_P], MTL_PORT_MAX_LEN, "%s",
39+
ctx->para.port[txPortIdx]);
40+
int num_ports = 1;
41+
42+
if (txPortRedundantIdx >= 0) {
43+
snprintf(port->port[MTL_SESSION_PORT_R], MTL_PORT_MAX_LEN, "%s",
44+
ctx->para.port[txPortRedundantIdx]);
45+
num_ports = 2;
46+
}
47+
48+
port->num_port = num_ports;
49+
}
50+
}
51+
52+
void Handlers::setSessionPortsRx(struct st_rx_port* port, int rxPortIdx,
53+
int rxPortRedundantIdx) {
54+
if (!ctx) {
55+
throw std::runtime_error("setSessionPortsRx no ctx (ctx is null)");
56+
} else if (rxPortIdx >= (int)ctx->para.num_ports) {
57+
throw std::runtime_error("setSessionPortsRx rxPortIdx out of range");
58+
} else if (rxPortRedundantIdx >= (int)ctx->para.num_ports) {
59+
throw std::runtime_error("setSessionPortsRx rxPortRedundantIdx out of range");
60+
}
61+
62+
if (rxPortIdx >= 0) {
63+
snprintf(port->port[MTL_SESSION_PORT_P], MTL_PORT_MAX_LEN, "%s",
64+
ctx->para.port[rxPortIdx]);
65+
int num_ports = 1;
66+
67+
if (rxPortRedundantIdx >= 0) {
68+
snprintf(port->port[MTL_SESSION_PORT_R], MTL_PORT_MAX_LEN, "%s",
69+
ctx->para.port[rxPortRedundantIdx]);
70+
num_ports = 2;
71+
}
72+
port->num_port = num_ports;
73+
}
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
#pragma once
3+
4+
#include <atomic>
5+
#include <functional>
6+
#include <vector>
7+
8+
#include "session.hpp"
9+
#include "strategy.hpp"
10+
#include "tests.hpp"
11+
12+
class Handlers {
13+
public:
14+
explicit Handlers(st_tests_context* ctx,
15+
FrameTestStrategy* frameTestStrategy = nullptr);
16+
virtual ~Handlers();
17+
18+
void startSession(
19+
std::vector<std::function<void(std::atomic<bool>&)>> threadFunctions = {});
20+
void stopSession();
21+
22+
void setSessionPortsRx(struct st_rx_port* port, int rxPortIdx, int rxPortRedundantIdx);
23+
void setSessionPortsTx(struct st_tx_port* port, int txPortIdx, int txPortRedundantIdx);
24+
25+
Session session;
26+
st_tests_context* ctx;
27+
FrameTestStrategy* frameTestStrategy;
28+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include "session.hpp"
4+
5+
void Session::addThread(std::function<void(std::atomic<bool>&)> func) {
6+
if (!func) {
7+
return;
8+
}
9+
10+
if (threads_.empty()) {
11+
stopFlag_.store(false);
12+
}
13+
14+
threads_.emplace_back(func, std::ref(stopFlag_));
15+
}
16+
17+
bool Session::isRunning() const {
18+
if (threads_.empty()) {
19+
return false;
20+
}
21+
22+
for (const auto& thread : threads_) {
23+
if (!thread.joinable()) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
31+
void Session::stop() {
32+
stopFlag_ = true;
33+
for (auto& thread : threads_) {
34+
if (thread.joinable()) {
35+
thread.join();
36+
}
37+
}
38+
threads_.clear();
39+
}
40+
41+
Session::~Session() {
42+
stop();
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
#pragma once
3+
4+
#include <atomic>
5+
#include <functional>
6+
#include <thread>
7+
#include <vector>
8+
9+
/**
10+
* @brief Helper that owns the background threads used by TX/RX handlers.
11+
*/
12+
class Session {
13+
public:
14+
void addThread(std::function<void(std::atomic<bool>&)> func);
15+
bool isRunning() const;
16+
void stop();
17+
~Session();
18+
19+
private:
20+
std::vector<std::thread> threads_;
21+
std::atomic<bool> stopFlag_{false};
22+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include "strategy.hpp"
4+
5+
#include <utility>
6+
7+
void StrategySharedState::setPacingParameters(double tr_offset_ns, double trs_ns,
8+
uint32_t vrx_pkts) {
9+
std::lock_guard<std::mutex> lock(pacing_mutex_);
10+
pacing_.tr_offset_ns = tr_offset_ns;
11+
pacing_.trs_ns = trs_ns;
12+
pacing_.vrx_pkts = vrx_pkts;
13+
pacing_.has_value = true;
14+
}
15+
16+
StrategySharedState::PacingParameters StrategySharedState::getPacingParameters() const {
17+
std::lock_guard<std::mutex> lock(pacing_mutex_);
18+
return pacing_;
19+
}
20+
21+
FrameTestStrategy::FrameTestStrategy(Handlers* parent, bool enable_tx_modifier,
22+
bool enable_rx_modifier)
23+
: parent(parent),
24+
idx_tx(0),
25+
idx_rx(0),
26+
expect_fps(0.0),
27+
enable_tx_modifier(enable_tx_modifier),
28+
enable_rx_modifier(enable_rx_modifier),
29+
shared_state_(nullptr) {
30+
}
31+
32+
FrameTestStrategy::~FrameTestStrategy() {
33+
parent = nullptr;
34+
}
35+
36+
void FrameTestStrategy::setSharedState(std::shared_ptr<StrategySharedState> state) {
37+
shared_state_ = std::move(state);
38+
}
39+
40+
std::shared_ptr<StrategySharedState> FrameTestStrategy::sharedState() const {
41+
return shared_state_;
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
#pragma once
3+
4+
#include <cstddef>
5+
#include <cstdint>
6+
#include <memory>
7+
#include <mutex>
8+
9+
class Handlers;
10+
11+
class StrategySharedState {
12+
public:
13+
struct PacingParameters {
14+
double tr_offset_ns = 0.0;
15+
double trs_ns = 0.0;
16+
uint32_t vrx_pkts = 0;
17+
bool has_value = false;
18+
};
19+
20+
void setPacingParameters(double tr_offset_ns, double trs_ns, uint32_t vrx_pkts);
21+
PacingParameters getPacingParameters() const;
22+
23+
private:
24+
mutable std::mutex pacing_mutex_;
25+
PacingParameters pacing_;
26+
};
27+
28+
class FrameTestStrategy {
29+
public:
30+
FrameTestStrategy(Handlers* parent = nullptr, bool enable_tx_modifier = false,
31+
bool enable_rx_modifier = false);
32+
virtual ~FrameTestStrategy();
33+
34+
void setSharedState(std::shared_ptr<StrategySharedState> state);
35+
std::shared_ptr<StrategySharedState> sharedState() const;
36+
37+
virtual void txTestFrameModifier(void* frame, size_t frame_size) {
38+
}
39+
virtual void rxTestFrameModifier(void* frame, size_t frame_size) {
40+
}
41+
42+
Handlers* parent;
43+
uint32_t idx_tx;
44+
uint32_t idx_rx;
45+
double expect_fps;
46+
bool enable_tx_modifier;
47+
bool enable_rx_modifier;
48+
49+
private:
50+
std::shared_ptr<StrategySharedState> shared_state_;
51+
};

0 commit comments

Comments
 (0)