Skip to content

Commit 353b024

Browse files
committed
[refactoring][workflow]: Refactor quote/echo workflow and scheduler usage for improved clarity and control
Removed unnecessary inclusion of QuoteBuffer and buffer clearing from EchoReceivedHandler. Simplified get_quote_of_the_day(): now always requests a new quote if not already in progress, and clears the buffer after initiating a connection. Refactored get_echo() to use a new echo_connected flag and send quotes only when data is available, improving connection logic. Split scheduler into scheduler0 (Core 0) and scheduler1 (Core 1) for clearer timing management; updated all related code to use the correct scheduler. Adjusted timing intervals for scheduled tasks to new values. Improved comments and removed redundant checks for buffer emptiness before requesting new quotes. Signed-off-by: Goran Mišković <[email protected]>
1 parent 000881f commit 353b024

File tree

2 files changed

+45
-63
lines changed

2 files changed

+45
-63
lines changed

src/EchoReceivedHandler.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313

1414
#include "EchoReceivedHandler.hpp"
15-
#include "QuoteBuffer.hpp" // for END_OF_QUOTE_MARKER
1615
#include <string>
1716

1817
namespace e5 {
@@ -38,7 +37,6 @@ namespace e5 {
3837
m_io.peekConsume(available);
3938
auto quote = std::make_unique<std::string>(chunk);
4039
m_serial_printer.print(std::move(quote));
41-
m_qotd_buffer.set("");
4240
}
4341

4442
} // namespace e5

src/main.cpp

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ using namespace async_tcp;
3939
*
4040
* When false: 8KB stack is split between cores (4KB each)
4141
* When true: Each core gets its own 8KB stack
42-
*
43-
* Required for reliable dual-core operation with network stack
44-
* and temperature monitoring running on separate cores.
4542
*/
4643
bool core1_separate_stack = true;
4744

4845
volatile bool operational = false; // Global flag for core synchronization
4946
volatile bool ctx1_ready = false; // For loop() to wait for setup1()
5047
volatile bool qotd_in_progress = false;
48+
volatile bool echo_connected = false;
5149
// WiFi credentials from secrets.h
5250
const auto *ssid = STASSID;
5351
const auto *password = STAPSK;
@@ -56,8 +54,8 @@ WiFiMulti multi;
5654
// Server details
5755
const auto *qotd_host = QOTD_HOST;
5856
const auto *echo_host = ECHO_HOST;
59-
constexpr uint16_t qotd_port = QOTD_PORT; // QOTD service port
60-
constexpr uint16_t echo_port = ECHO_PORT; // Echo service port
57+
constexpr uint16_t qotd_port = QOTD_PORT;
58+
constexpr uint16_t echo_port = ECHO_PORT;
6159

6260
// TCP clients
6361
TcpClient qotd_client;
@@ -68,8 +66,8 @@ IPAddress qotd_ip_address;
6866
IPAddress echo_ip_address;
6967

7068
// Global asynchronous context managers for each core
71-
static AsyncCtx ctx0 = {}; // Core 0
72-
static AsyncCtx ctx1 = {}; // Core 1
69+
static AsyncCtx ctx0 = {}; // TCP Client Core 0
70+
static AsyncCtx ctx1 = {}; // SerialPrinter and QuoteBuffer on Core 1
7371

7472
// Thread-safe buffer for storing the quote
7573
e5::QuoteBuffer qotd_buffer(ctx1);
@@ -78,7 +76,8 @@ e5::QuoteBuffer qotd_buffer(ctx1);
7876
e5::SerialPrinter serial_printer(ctx1);
7977

8078
// Timing variables
81-
static e5::LoopScheduler scheduler;
79+
static e5::LoopScheduler scheduler0; // For Core 0
80+
static e5::LoopScheduler scheduler1; // For Core 1
8281
static const std::string qotd = "qotd";
8382
static const std::string echo = "echo";
8483
static const std::string stack_0 = "stack_0";
@@ -88,61 +87,46 @@ static const std::string heap = "heap";
8887
/**
8988
* @brief Connects to the "quote of the day" server and initiates a connection.
9089
*
91-
* This function only connects to the QOTD server if the quote buffer is empty,
92-
* ensuring that we don't get a new quote until the current one has been
93-
* fully processed by the echo server.
90+
* Connects to the QOTD server if there isn't another active connection.
9491
*/
9592
void get_quote_of_the_day() {
96-
// Check if the buffer is empty before getting a new quote
97-
const std::string buffer_content = qotd_buffer.get();
98-
99-
if (buffer_content.empty()) {
100-
// Check if we're already connected first
101-
if (qotd_in_progress) {
102-
DEBUGWIRE("QOTD client already connected, skipping new connection\n");
103-
return;
104-
}
105-
// Buffer is empty, safe to get a new quote
106-
DEBUGWIRE(
107-
"Quote buffer is empty, connecting to QOTD server for new quote\n");
108-
qotd_in_progress = true;
109-
if (!qotd_client.connect(qotd_ip_address, qotd_port)) {
110-
qotd_in_progress = false;
111-
DEBUGV("Failed to connect to QOTD server.\n");
112-
}
113-
} else {
114-
// Buffer still has content, wait until it's processed
115-
DEBUGWIRE("Quote buffer still has content (%d bytes), skipping QOTD "
116-
"request\n",
117-
buffer_content.size());
93+
// Check if we're already connected first
94+
if (qotd_in_progress) {
95+
DEBUGWIRE("QOTD client already connected, skipping new connection\n");
96+
return;
97+
}
98+
DEBUGWIRE(
99+
"Connecting to QOTD server for new quote\n");
100+
qotd_in_progress = true;
101+
if (!qotd_client.connect(qotd_ip_address, qotd_port)) {
102+
qotd_in_progress = false;
103+
DEBUGV("Failed to connect to QOTD server.\n");
118104
}
105+
qotd_buffer.clear();
119106
}
120107

121108
/**
122109
* @brief Connects to the echo server and sends data if available.
123110
*
124111
* This function checks if the echo client is connected. If not, it attempts to
125-
* connect. If connected and there is data in the transmission buffer, it checks
126-
* if the data contains the "End of Quote" marker indicating a complete quote
127-
* before sending.
112+
* connect. If connected and there is data in the transmission buffer.
128113
*/
129114
void get_echo() {
130-
if (!echo_client.connected()) {
131-
if (0 == echo_client.connect(echo_ip_address, echo_port)) {
132-
DEBUGV("Failed to connect to echo server..\n");
133-
}
134-
} else {
135-
const std::string buffer_content = qotd_buffer.get();
136-
137-
// Only process non-empty buffers
138-
if (!buffer_content.empty()) {
139-
// Send full quote via standard TcpClient interface
140-
// This will automatically delegate to the registered TcpWriter
141-
DEBUGWIRE("Sending quote to echo server (%d bytes)\n", buffer_content.size());
142-
size_t sent = echo_client.write(reinterpret_cast<const uint8_t*>(buffer_content.c_str()), buffer_content.size());
143-
if (sent < buffer_content.size()) {
144-
DEBUGWIRE("[ERROR] echo_client.write sent only %u/%u bytes\n", sent, buffer_content.size());
115+
const std::string buffer_content = qotd_buffer.get();
116+
if (!buffer_content.empty()) {
117+
if (!echo_connected) {
118+
if (0 == echo_client.connect(echo_ip_address, echo_port)) {
119+
DEBUGV("Failed to connect to echo server..\n");
120+
return;
145121
}
122+
echo_connected = true;
123+
}
124+
DEBUGWIRE("Sending quote to echo server (%d bytes)\n", buffer_content.size());
125+
if (const size_t sent = echo_client.write(
126+
reinterpret_cast<const uint8_t *>(buffer_content.c_str()),
127+
buffer_content.size());
128+
sent < buffer_content.size()) {
129+
DEBUGWIRE("[ERROR] echo_client.write sent only %u/%u bytes\n", sent, buffer_content.size());
146130
}
147131
}
148132
}
@@ -238,11 +222,9 @@ void setup() {
238222
qotd_closed_handler->initialisePerpetualBridge();
239223
qotd_client.setOnClosedCallback(std::move(qotd_closed_handler));
240224

241-
scheduler.setEntry(qotd, 7070);
242-
scheduler.setEntry(echo, 1111);
243-
scheduler.setEntry(stack_0, 7070707);
244-
scheduler.setEntry(stack_1, 9090909);
245-
scheduler.setEntry(heap, 8080808);
225+
scheduler0.setEntry(qotd, 1010);
226+
scheduler0.setEntry(echo, 101);
227+
scheduler0.setEntry(stack_0, 707070);
246228

247229
operational = true;
248230
}
@@ -260,6 +242,8 @@ void setup1() {
260242
panic_compact("CTX init failed on Core 1\n");
261243
}
262244

245+
scheduler1.setEntry(stack_1, 909090);
246+
scheduler1.setEntry(heap, 808080);
263247
ctx1_ready = true;
264248
}
265249

@@ -274,16 +258,16 @@ void loop() {
274258
return;
275259
}
276260

277-
if (scheduler.timeToRun(qotd)) get_quote_of_the_day();
278-
if (scheduler.timeToRun(echo)) get_echo();
279-
if (scheduler.timeToRun(stack_0)) print_stack_stats();
261+
if (scheduler0.timeToRun(qotd)) get_quote_of_the_day();
262+
if (scheduler0.timeToRun(echo)) get_echo();
263+
if (scheduler0.timeToRun(stack_0)) print_stack_stats();
280264
}
281265

282266
/**
283267
* @brief Loop function for Core 1.
284268
* Currently empties as all work is handled through the async context.
285269
*/
286270
void loop1() {
287-
if (scheduler.timeToRun(stack_1)) print_stack_stats();
288-
if (scheduler.timeToRun(heap)) print_heap_stats();
271+
if (scheduler1.timeToRun(stack_1)) print_stack_stats();
272+
if (scheduler1.timeToRun(heap)) print_heap_stats();
289273
}

0 commit comments

Comments
 (0)