Skip to content

Commit

Permalink
Реализованная циклическая отправка запросов по таймеру. Поток убран
Browse files Browse the repository at this point in the history
  • Loading branch information
maisvendoo committed Jun 2, 2023
1 parent c413fae commit 8b728bf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 198 deletions.
58 changes: 0 additions & 58 deletions include/data-sender.h

This file was deleted.

16 changes: 7 additions & 9 deletions include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

#include <QMainWindow>
#include <QTableWidget>
#include <QThread>
#include <QTimer>

#include "master.h"
#include "data-sender.h"

//------------------------------------------------------------------------------
//
Expand Down Expand Up @@ -86,14 +85,13 @@ class MainWindow : public QMainWindow
bool is_send_started;

/// Cyclic send flag
bool is_cyclic;

DataSender dataSender;
QThread threadCyclicSend;
bool is_cyclic;

/// Flag of close window event
bool is_close_event;

QTimer sendRequestTimer;

signals:

/// Send request from master to slave
Expand Down Expand Up @@ -137,11 +135,11 @@ private slots:
/// Check/uncheck cyclic send of data
void checkCyclicSend(int state);

/// Actions by finish sender thread
void onFinishSendThread();

/// Send started flag to data sender
void getStartedFlag(bool *started);

/// Cyclic send request by reference time interval
void slotCyclicRequestSend();
};

#endif // MAINWINDOW_H
69 changes: 0 additions & 69 deletions src/data-sender.cpp

This file was deleted.

84 changes: 22 additions & 62 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,8 @@ void MainWindow::init()
connect(ui->cCyclicSend, &QCheckBox::stateChanged,
this, &MainWindow::checkCyclicSend);

connect(&dataSender, &DataSender::sendMasterRequest,
master, &Master::sendRequest);

connect(&threadCyclicSend, &QThread::finished,
this, &MainWindow::onFinishSendThread);

connect(&dataSender, &DataSender::quit,
&threadCyclicSend, &QThread::terminate);

connect(&dataSender, &DataSender::isStarted,
this, &MainWindow::getStartedFlag);
connect(&sendRequestTimer, &QTimer::timeout,
this, &MainWindow::slotCyclicRequestSend);

is_close_event = false;
}
Expand Down Expand Up @@ -313,18 +304,7 @@ abstract_request_t *MainWindow::getRequestData()
//------------------------------------------------------------------------------
void MainWindow::closeEvent(QCloseEvent *event)
{
// Check sender thread state
if (threadCyclicSend.isRunning())
{
// Stop thread
is_send_started = false;
// Close window after terminating of sender thread
is_close_event = true;
// Ignore close event
event->ignore();
}
else
event->accept();
event->accept();
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -433,32 +413,25 @@ void MainWindow::sendButtonRelease()
return;
}

// Check sender thread state
if (!threadCyclicSend.isRunning())
if (!is_cyclic)
{
// Set started flag
is_send_started = true;

// Init data sender
dataSender.init(is_cyclic, ui->sbSendInterval->value(), *getRequestData());

// Move data sender to thread
dataSender.moveToThread(&threadCyclicSend);

// Connect start signal with thread function
connect(&threadCyclicSend, &QThread::started,
&dataSender, &DataSender::cyclicDataSend);

// Start sender thread
threadCyclicSend.start();

// Mark button as stop button
ui->bSend->setText("Stop");
emit sendMasterRequest(getRequestData());
}
else
{
// Reset started flag
is_send_started = false;
if (!sendRequestTimer.isActive())
{
sendRequestTimer.setInterval(ui->sbSendInterval->value());
sendRequestTimer.start();
ui->bSend->setText("Stop");
ui->bConnect->setEnabled(false);
}
else
{
sendRequestTimer.stop();
ui->bSend->setText("Send");
ui->bConnect->setEnabled(true);
}
}
}

Expand Down Expand Up @@ -541,28 +514,15 @@ void MainWindow::checkCyclicSend(int state)
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void MainWindow::onFinishSendThread()
void MainWindow::getStartedFlag(bool *started)
{
// Disconnect thread function
disconnect(&threadCyclicSend, &QThread::started,
&dataSender, &DataSender::cyclicDataSend);

// Mark button as send button
ui->bSend->setText("Send");

// Check close flag
if (is_close_event)
{
master->closeConnection();
QApplication::quit();
}
*started = is_send_started;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void MainWindow::getStartedFlag(bool *started)
void MainWindow::slotCyclicRequestSend()
{
*started = is_send_started;
emit sendMasterRequest(getRequestData());
}

0 comments on commit 8b728bf

Please sign in to comment.