-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simulator: new command to run script and play a scenario
- New commands: PLAY script, BREAK, and PLAY EXIT, and the commands to resume (RUN, PLAY) are invalid in script mode. All others are usable in script, even LOAD gpx and RUN trkid.
- Loading branch information
Showing
9 changed files
with
409 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (C) 2024 | ||
* Jean-Luc Barriere <jlbarriere68@gmail.com> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "scriptrunner.h" | ||
#include "simulator.h" | ||
|
||
#include <QByteArray> | ||
#include <QEventLoop> | ||
#include <cassert> | ||
|
||
#define LINE_MAXSIZE 1024 | ||
|
||
ScriptRunner::~ScriptRunner() | ||
{ | ||
if (isRunning()) | ||
{ | ||
this->requestInterruption(); | ||
this->wait(); | ||
} | ||
if (_file) | ||
delete _file; | ||
} | ||
|
||
bool ScriptRunner::configureScript(const QString &filepath) | ||
{ | ||
QFile file(filepath); | ||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) | ||
{ | ||
file.close(); | ||
if (_file) | ||
delete _file; | ||
_file = new QFile(filepath); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
QString ScriptRunner::filepath() const | ||
{ | ||
if (_file) | ||
return _file->fileName(); | ||
return QString(); | ||
} | ||
|
||
void ScriptRunner::run() | ||
{ | ||
if (_file == nullptr) | ||
return; | ||
if (!_file->isOpen() && !_file->open(QIODevice::ReadOnly | QIODevice::Text)) | ||
return; | ||
_aborted = false; | ||
while (processCommand()) | ||
{ | ||
// check for thread interrupted, not this | ||
if (QThread::currentThread()->isInterruptionRequested()) | ||
{ | ||
_aborted = true; | ||
break; | ||
} | ||
// now the command is completed, therefore clear recover | ||
_redo = false; | ||
} | ||
if (!_aborted) | ||
_file->close(); | ||
} | ||
|
||
void ScriptRunner::recover() | ||
{ | ||
qDebug("rollback %s : %s", filepath().toStdString().c_str(), _command.constData()); | ||
_redo = true; | ||
} | ||
|
||
bool ScriptRunner::processCommand() | ||
{ | ||
assert(_file->isOpen() == true); | ||
// redo last aborted command or process next | ||
if (!_redo) | ||
_command = _file->readLine(LINE_MAXSIZE); | ||
|
||
if (_command.isEmpty() || _command.back() != '\n') | ||
return false; | ||
fprintf(stdout, "PLAY: %s", _command.constData()); | ||
_simulator.onCommand(QString(_command.mid(0, _command.size()-1))); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2024 | ||
* Jean-Luc Barriere <jlbarriere68@gmail.com> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef SCRIPTRUNNER_H | ||
#define SCRIPTRUNNER_H | ||
|
||
#include <QObject> | ||
#include <QString> | ||
#include <QThread> | ||
#include <QFile> | ||
#include <QByteArray> | ||
|
||
class Simulator; | ||
|
||
class ScriptRunner : public QThread | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit ScriptRunner(Simulator& s) | ||
: _simulator(s) { } | ||
~ScriptRunner(); | ||
|
||
bool configureScript(const QString& filepath); | ||
bool isRunAborted() { return (_file ? _aborted : false); } | ||
QString filepath() const; | ||
|
||
void run(); | ||
void recover(); | ||
bool isRecovery() const { return _redo; } | ||
|
||
private: | ||
bool processCommand(); | ||
|
||
bool _aborted = false; | ||
bool _redo = false; | ||
bool _finished = false; | ||
Simulator& _simulator; | ||
QFile * _file = nullptr; | ||
QByteArray _command; | ||
}; | ||
|
||
#endif // SCRIPTRUNNER_H |
Oops, something went wrong.