-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
194 lines (184 loc) · 7 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SYSTEM INCLUDES
#include <QBasicTimer>
#include <QStyleFactory>
#include <range/v3/all.hpp>
#include <private/qringbuffer_p.h>
// APPLICATION INCLUDES
#include "MainWindow.h"
#include "ui_MainWindow.h"
// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// DEFINES
// NAMESPACE USAGE
// STATIC VARIABLE INITIALIZATIONS
namespace {
qint64 gCounter = 0;
qint64 gUnderruns = 0;
qint64 gErrorCounter = 0;
qint64 gReadBlockCounter = 0;
qint64 gWriteBlockCounter = 0;
qint64 gBytesWritten = 0;
qint64 gBytesRead = 0;
qint64 gBuffersWritten = 0;
qint64 gBuffersRead = 0;
constexpr qint64 gTestBufferLength = 1500;
const std::vector<char> gFixedTestBuffer =
ranges::views::closed_iota('A', 'Z')
| ranges::views::cycle
| ranges::views::take(gTestBufferLength + 500)
| ranges::to<std::vector<char>>();
std::vector<char> readBuffer(gFixedTestBuffer.size());
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, mpTimer{ std::make_unique<QBasicTimer>() }
, mpBuffer{ std::make_unique<QRingBuffer>() }
{
ui->setupUi(this);
// enable dark and light mode support to follow the windows settings.
qApp->setStyle(QStyleFactory::create("Fusion"));
gErrorCounter = gWriteBlockCounter = gReadBlockCounter = gUnderruns = 0;
resetFormFields();
}
void
MainWindow::resetFormFields() const
{
ui->errorCounter->setText(QString::number(gErrorCounter));
ui->writeBlockCounter->setText(QString::number(gWriteBlockCounter));
ui->readBlockCounter->setText(QString::number(gReadBlockCounter));
ui->bufferSize->setText(QString("%L1 bytes").arg(mpBuffer->size()));
ui->bytesWritten->setText(QString("%L1 bytes").arg(gBytesWritten));
ui->bytesRead->setText(QString("%L1 bytes").arg(gBytesRead));
ui->basicBlockSize->setValue(mpBuffer->chunkSize());
ui->underruns->setText(QString("%L1").arg(gUnderruns));
}
MainWindow::~MainWindow()
{
delete ui;
}
void
MainWindow::timerEvent(QTimerEvent* event)
{
if (event->timerId() == mpTimer->timerId()) {
// min 1, max 5
const auto writeReadRatio = ui->writeReadRatio->value();
// more writes than reads 4:1
if (++gCounter % (writeReadRatio+1) != 0) {
// write multiple blocks into buffer
qint64 offset = 0;
const auto blockLen = ui->writeBlockSize->value();
auto nBlocksWritten = 0;
while (offset < gTestBufferLength) {
const auto nRemaining = gTestBufferLength - offset;
const auto nBytesToWrite = std::min<qint64>(
nRemaining, blockLen);
// this API does not fail - pattern slightly
// different for each gBuffersWritten
// must make sure that we wrap around after 500 reads
// or the offset will push us off the edge
mpBuffer->append(gFixedTestBuffer.data() +
offset + (gBuffersWritten % 500), nBytesToWrite);
offset += nBytesToWrite;
gBytesWritten += nBytesToWrite;
nBlocksWritten++;
}
gWriteBlockCounter += nBlocksWritten;
// assuming ascii pattern buffer - show buffer data preview
ui->writePreview->setText(QByteArray(gFixedTestBuffer.data() +
(gBuffersWritten % 500), 25)/*.toHex(',')*/ + "...");
gBuffersWritten++;
ui->writeBlockCounter->setText(QString(
"%L1").arg(gWriteBlockCounter));
ui->bytesWritten->setText(QString(
"%L1 bytes").arg(gBytesWritten));
} else {
// read multiple blocks into readBuffer
qint64 offset = 0;
const auto blockLen = ui->readBlockSize->value();
auto nBlocksRead = 0;
while (offset < gTestBufferLength) {
const auto nRemaining = gTestBufferLength - offset;
const auto nBytesToRead = std::min<qint64>(
nRemaining, blockLen);
const auto nBytesRead = mpBuffer->read(
readBuffer.data() + offset, nBytesToRead);
if (nBytesRead > 0) {
offset += nBytesRead;
gBytesRead += nBytesRead;
nBlocksRead++;
} else if (offset < gTestBufferLength) {
// cannot read from empty buffer
++gUnderruns;
ui->underruns->setText(QString(
"%L1").arg(gUnderruns));
break;
}
}
gReadBlockCounter += nBlocksRead;
ui->readBlockCounter->setText(QString(
"%L1").arg(gReadBlockCounter));
ui->bytesRead->setText(QString(
"%L1 bytes").arg(gBytesRead));
// check to ensure we read a full buffer
// before comparing written to read data
if (offset == gTestBufferLength) {
// compare sliding window cyclic buffer pattern
// must make sure that we wrap around after 500 reads
// or the offset will push us off the edge
if (memcmp(readBuffer.data(),
gFixedTestBuffer.data() + (gBuffersRead % 500),
gTestBufferLength) != 0) {
ui->errorCounter->setText(QString(
"%L1").arg(++gErrorCounter));
}
// assuming ascii pattern buffer - show buffer data preview
ui->readPreview->setText(QByteArray(
readBuffer.data(), 25)/*.toHex(',')*/ + "...");
gBuffersRead++;
}
// reset the buffer
std::ranges::fill(readBuffer, 0);
}
ui->bufferSize->setText(QString(
"%L1 bytes").arg(mpBuffer->size()));
} else { // pass unhandled timer event up the chain
QObject::timerEvent(event);
}
}
void
MainWindow::on_start_clicked()
{
// if timer is already running, stop it
if (ui->start->text() == "Start") {
// reset stats
gErrorCounter = 0;
gWriteBlockCounter = 0;
gReadBlockCounter = 0;
gBytesWritten = 0;
gBytesRead = 0;
mpBuffer->clear();
mpBuffer->setChunkSize(ui->basicBlockSize->value());
// not sure if this can change while running
ui->basicBlockSize->setEnabled(false);
resetFormFields();
// disable button until protocol started
ui->start->setText("Stop");
mpTimer->start(ui->timerPeriod->value(), Qt::PreciseTimer, this);
} else {
if (mpTimer->isActive()) {
mpTimer->stop();
}
// not sure if this can change while running
ui->basicBlockSize->setEnabled(true);
ui->start->setText("Start");
}
}
void
MainWindow::on_timerPeriod_valueChanged(int value) {
if (mpTimer->isActive()) {
mpTimer->stop();
mpTimer->start(value, Qt::PreciseTimer, this);
}
}