Skip to content

Commit bab65d9

Browse files
committed
feature: Add quote completion tracking to QuoteBuffer
This commit enhances the QuoteBuffer class with quote completion state management: - Added `m_quote_complete` flag to track when quotes are fully received. - Implemented new methods: - `setComplete()` to mark quotes as fully received. - `isComplete()` to check completion status. - `resetBuffer()` to clear buffer and reset completion state. - Added corresponding BufferPayload operations (SET_COMPLETE, IS_COMPLETE, RESET_COMPLETE). - Fixed parameter passing for set/append methods to use const references. - Improved error handling with explicit GET case and proper error code for invalid operations. These changes enable better state tracking for quote processing, allowing consumers to know when a complete quote is available for processing. Signed-off-by: Goran Mišković <schkovich@users.noreply.github.com>
1 parent 2a8562c commit bab65d9

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

include/QuoteBuffer.hpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace e5 {
3838
class QuoteBuffer final : public SyncBridge {
3939

4040
std::string m_buffer; ///< The internal string buffer
41+
bool m_quote_complete = false; ///< Flag indicating if current quote is complete
4142

4243
/**
4344
* @struct BufferPayload
@@ -52,6 +53,9 @@ namespace e5 {
5253
SET, ///< Set the buffer content
5354
GET, ///< Get the buffer content
5455
APPEND, ///< Append to the buffer content
56+
SET_COMPLETE, ///< Mark quote as complete
57+
IS_COMPLETE, ///< Check if quote is complete
58+
RESET_COMPLETE, ///< Reset quote completion flag
5559
};
5660

5761
Operation op; ///< The operation to perform
@@ -93,7 +97,7 @@ namespace e5 {
9397
*
9498
* @param data String to set as the buffer content
9599
*/
96-
void set(std::string data);
100+
void set(const std::string &data);
97101

98102
/**
99103
* @brief Gets the buffer content
@@ -115,7 +119,7 @@ namespace e5 {
115119
*
116120
* @param data String to append to the buffer content
117121
*/
118-
void append(std::string data);
122+
void append(const std::string &data);
119123

120124
/**
121125
* @brief Returns true if the buffer is empty, false otherwise.
@@ -126,6 +130,29 @@ namespace e5 {
126130
* @brief Clears the buffer content.
127131
*/
128132
void clear();
133+
134+
/**
135+
* @brief Marks the current quote as complete.
136+
*
137+
* This method signals that the quote is fully received and ready
138+
* for consumption by other components (e.g., echo server).
139+
*/
140+
void setComplete();
141+
142+
/**
143+
* @brief Checks if the current quote is complete.
144+
*
145+
* @return true if quote is complete and ready for consumption, false otherwise
146+
*/
147+
bool isComplete();
148+
149+
/**
150+
* @brief Resets the completion flag to false.
151+
*
152+
* This method explicitly resets the completion flag, typically called
153+
* when starting to receive a new quote.
154+
*/
155+
void resetBuffer();
129156
};
130157

131158
} // namespace e5

src/QuoteBuffer.cpp

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace e5 {
3636
uint32_t QuoteBuffer::onExecute(const SyncPayloadPtr payload) {
3737

3838
switch (const auto *buffer_payload =
39-
static_cast<BufferPayload *>(payload.get());
39+
static_cast<BufferPayload *>(payload.get()); // NOLINT
4040
buffer_payload->op) {
4141
case BufferPayload::SET:
4242
m_buffer = buffer_payload->data;
@@ -46,12 +46,29 @@ namespace e5 {
4646
m_buffer += buffer_payload->data;
4747
return PICO_OK;
4848

49-
default:
49+
case BufferPayload::SET_COMPLETE:
50+
m_quote_complete = true;
51+
return PICO_OK;
52+
53+
case BufferPayload::RESET_COMPLETE:
54+
m_quote_complete = false;
55+
m_buffer.clear();
56+
return PICO_OK;
57+
58+
case BufferPayload::IS_COMPLETE:
59+
if (buffer_payload->result_ptr) {
60+
*buffer_payload->result_ptr = m_quote_complete ? "1" : "0";
61+
}
62+
return PICO_OK;
63+
64+
case BufferPayload::GET:
5065
if (buffer_payload->result_ptr) {
5166
*buffer_payload->result_ptr = m_buffer;
5267
return PICO_OK;
5368
}
5469
return PICO_ERROR_NO_DATA;
70+
default:
71+
return PICO_ERROR_INVALID_ARG;
5572
}
5673
}
5774

@@ -64,7 +81,7 @@ namespace e5 {
6481
*
6582
* @param data String to set as the buffer content
6683
*/
67-
void QuoteBuffer::set(const std::string data) { // NOLINT
84+
void QuoteBuffer::set(const std::string &data) { // NOLINT
6885
auto payload = std::make_unique<BufferPayload>();
6986
payload->op = BufferPayload::SET;
7087
payload->data = data;
@@ -110,7 +127,7 @@ namespace e5 {
110127
*
111128
* @param data String to append to the buffer content
112129
*/
113-
void QuoteBuffer::append(const std::string data) { // NOLINT
130+
void QuoteBuffer::append(const std::string &data) { // NOLINT
114131
auto payload = std::make_unique<BufferPayload>();
115132
payload->op = BufferPayload::APPEND;
116133
payload->data = data;
@@ -138,8 +155,9 @@ namespace e5 {
138155
payload->result_ptr = &result_string;
139156
if (const auto result = execute(std::move(payload));
140157
result != PICO_OK) {
141-
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::empty() returned error %d.\n",
142-
rp2040.cpuid(), time_us_64(), result);
158+
DEBUGV(
159+
"[c%d][%llu][ERROR] QuoteBuffer::empty() returned error %d.\n",
160+
rp2040.cpuid(), time_us_64(), result);
143161
}
144162
return result_string.empty();
145163
}
@@ -157,7 +175,63 @@ namespace e5 {
157175
payload->data = "";
158176
if (const auto result = execute(std::move(payload));
159177
result != PICO_OK) {
160-
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::clear() returned error %d.\n",
178+
DEBUGV(
179+
"[c%d][%llu][ERROR] QuoteBuffer::clear() returned error %d.\n",
180+
rp2040.cpuid(), time_us_64(), result);
181+
}
182+
}
183+
184+
/**
185+
* @brief Marks the current quote as complete
186+
*
187+
* This method signals that the quote is fully received and ready for
188+
* consumption by other components (e.g., echo server).
189+
*/
190+
void QuoteBuffer::setComplete() {
191+
auto payload = std::make_unique<BufferPayload>();
192+
payload->op = BufferPayload::SET_COMPLETE;
193+
if (const auto result = execute(std::move(payload));
194+
result != PICO_OK) {
195+
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::setComplete() returned "
196+
"error %d.\n",
197+
rp2040.cpuid(), time_us_64(), result);
198+
}
199+
}
200+
201+
/**
202+
* @brief Checks if the current quote is complete
203+
*
204+
* @return true if quote is complete and ready for consumption, false
205+
* otherwise
206+
*/
207+
bool QuoteBuffer::isComplete() {
208+
std::string result_string;
209+
auto payload = std::make_unique<BufferPayload>();
210+
payload->op = BufferPayload::IS_COMPLETE;
211+
payload->result_ptr = &result_string;
212+
if (const auto result = execute(std::move(payload));
213+
result != PICO_OK) {
214+
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::isComplete() returned "
215+
"error %d.\n",
216+
rp2040.cpuid(), time_us_64(), result);
217+
return false;
218+
}
219+
return result_string == "1";
220+
}
221+
222+
/**
223+
* @brief Resets the completion flag to false
224+
*
225+
* This method explicitly resets the completion flag and clears the buffer.
226+
* Called when connection to QOTD server is open.
227+
*/
228+
void QuoteBuffer::resetBuffer() {
229+
auto payload = std::make_unique<BufferPayload>();
230+
payload->op = BufferPayload::RESET_COMPLETE;
231+
if (const auto result = execute(std::move(payload));
232+
result != PICO_OK) {
233+
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::resetBuffer() returned "
234+
"error %d.\n",
161235
rp2040.cpuid(), time_us_64(), result);
162236
}
163237
}

0 commit comments

Comments
 (0)