Skip to content
Snippets Groups Projects
Verified Commit 5fcd8a3c authored by Tobias Triffterer's avatar Tobias Triffterer :house_with_garden:
Browse files

Add Parser for setTargetState Command

This new method does the usual input validation. As only three values
are allowed for the only parameter “newstate”, they are explicitly
checked, because “never trust your clients”...
parent 3fa57b7a
No related branches found
No related tags found
No related merge requests found
Pipeline #3007 passed with stage
in 46 seconds
......@@ -77,6 +77,8 @@ Command Command::fromString(const QString& source)
return parseStoreToken(rootobj);
case Action::updateExperimentState:
return parseUpdateExperimentState(rootobj);
case Action::setTargetState:
return parseSetTargetState(rootobj);
case Action::sendHistogramUpdate:
case Action::moveTargetCloser:
case Action::moveTargetFarther:
......@@ -91,7 +93,6 @@ Command Command::fromString(const QString& source)
// Not yet implemented:
case Action::queryServerInformation:
case Action::updateHistogram:
case Action::setTargetState:
case Action::fillHistogram:
return Command();
default:
......@@ -207,6 +208,26 @@ Protocol::Command Protocol::Command::parseUpdateExperimentState(const QJsonObjec
);
}
Protocol::Command Protocol::Command::parseSetTargetState(const QJsonObject& input)
{
if (!input.keys().contains(QStringLiteral("newstate")) || !input.value(QStringLiteral("newstate")).isString()) {
logError(QStringLiteral("setTargetState command does not contain newstate."));
return Command();
}
if (input.value(QStringLiteral("newstate")).toString() != QStringLiteral("open")
&& input.value(QStringLiteral("newstate")).toString() != QStringLiteral("closed")
&& input.value(QStringLiteral("newstate")).toString() != QStringLiteral("goldfoil")) {
logError(QStringLiteral("newstate parameter of setTargetState command is invalid."));
return Command();
}
return Command(
Action::setTargetState,
Arguments{std::make_pair(QStringLiteral("newstate"), input.value(QStringLiteral("newstate")).toString())},
input.value(QStringLiteral("token")).toString()
);
}
QString Command::toString() const
{
if (action == Action::invalid)
......
......@@ -73,6 +73,7 @@ private:
static Command parseStoreToken(const QJsonObject& input);
static Command parseError(const QJsonObject& input);
static Command parseUpdateExperimentState(const QJsonObject& input);
static Command parseSetTargetState(const QJsonObject& input);
static QString getActionString(const Action action);
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment