Skip to content

Commit

Permalink
Implemented configurable delay for feedback messages (Downloading and…
Browse files Browse the repository at this point in the history
… Installing) (#49)

* Implemented configurable delay for feedback messages (Downloading and Installing)

* Fix review findings

* Reduce feedback interval to 0 for tests
  • Loading branch information
OleksandrChaika authored Oct 25, 2023
1 parent c413945 commit 70df8c1
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .github/actions/build-native-binary/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ runs:
if: ${{ inputs.arch == 'amd64' }}
run: |
cd dist_${{ inputs.arch }}/utest
LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent > ../../unit_tests_report_${{ inputs.arch }}.txt --gtest_output=xml:../../unit_tests_report_${{ inputs.arch }}.xml
LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent > ../../unit_tests_report_${{ inputs.arch }}.txt --gtest_output=xml:../../unit_tests_report_${{ inputs.arch }}.xml
shell: bash

- uses: uraimo/run-on-arch-action@v2
Expand All @@ -157,13 +157,13 @@ runs:
echo "\nextra/selfupdateagent.crt" | tee -a /etc/ca-certificates.conf > /dev/null
update-ca-certificates
cd /sua/dist_arm64/utest
LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent > ../../unit_tests_report_arm64.txt --gtest_output=xml:../../unit_tests_report_arm64.xml
LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent > ../../unit_tests_report_arm64.txt --gtest_output=xml:../../unit_tests_report_arm64.xml
- name: Collect code-coverage for amd64
if: ${{ inputs.arch == 'amd64' }}
run: |
cd dist_${{ inputs.arch }}_codecov/utest
LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent
LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent
shell: bash

- name: Generate code-coverage report
Expand Down
1 change: 1 addition & 0 deletions src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace sua {
std::string caFilepath = SUA_DEFAULT_CA_FILEPATH;
bool downloadMode = true;
bool fallbackMode = false;
int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds

DesiredState desiredState;
CurrentState currentState;
Expand Down
17 changes: 9 additions & 8 deletions src/Defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

#include "Defaults.h"

const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp";
const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto";
const int SUA_DEFAULT_MQTT_PORT = 1883;
const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883";
const std::string SUA_DEFAULT_MODE = "download";
const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates";
const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs";
const std::string SUA_DEFAULT_CA_FILEPATH = "";
const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp";
const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto";
const int SUA_DEFAULT_MQTT_PORT = 1883;
const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883";
const std::string SUA_DEFAULT_MODE = "download";
const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates";
const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs";
const std::string SUA_DEFAULT_CA_FILEPATH = "";
const int SUA_DEFAULT_FEEDBACK_INTERVAL = 5;
1 change: 1 addition & 0 deletions src/Defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ extern const std::string SUA_DEFAULT_MODE;
extern const std::string SUA_DEFAULT_TEMP_DIRECTORY;
extern const std::string SUA_DEFAULT_CA_DIRECTORY;
extern const std::string SUA_DEFAULT_CA_FILEPATH;
extern const int SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds

#endif
11 changes: 10 additions & 1 deletion src/FSM/States/Downloading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "FotaEvent.h"
#include "Logger.h"

#include <chrono>

namespace sua {

Downloading::Downloading()
Expand Down Expand Up @@ -49,8 +51,15 @@ namespace sua {
ctx.desiredState.downloadBytesDownloaded = std::stoll(payload.at("downloaded"));
ctx.desiredState.downloadProgressPercentage = std::stoi(payload.at("percentage"));

const auto now = std::chrono::system_clock::now().time_since_epoch();
const auto now_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();

Logger::info("Download progress: {}%", ctx.desiredState.downloadProgressPercentage);
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading);

if((ctx.desiredState.downloadProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) {
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading);
_timeLastUpdate = now_in_seconds;
}
});

if (true == ctx.downloadMode) {
Expand Down
3 changes: 3 additions & 0 deletions src/FSM/States/Downloading.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace sua {
void onEnter(Context& ctx) override;

FotaEvent body(Context& ctx) override;

private:
long long _timeLastUpdate = 0;
};

} // namespace sua
Expand Down
11 changes: 10 additions & 1 deletion src/FSM/States/Installing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "Context.h"
#include "Logger.h"

#include <chrono>

namespace sua {

Installing::Installing()
Expand All @@ -37,7 +39,14 @@ namespace sua {
subscribe(Installer::EVENT_INSTALLING, [this, &ctx](const std::map<std::string, std::string>& payload) {
ctx.desiredState.installProgressPercentage = std::stoi(payload.at("percentage"));
Logger::info("Install progress: {}%", ctx.desiredState.installProgressPercentage);
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing);

const auto now = std::chrono::system_clock::now().time_since_epoch();
const auto now_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();

if((ctx.desiredState.installProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) {
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing);
_timeLastUpdate = now_in_seconds;
}
});

std::string install_input;
Expand Down
3 changes: 3 additions & 0 deletions src/FSM/States/Installing.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace sua {
void onEnter(Context& ctx) override;

FotaEvent body(Context& ctx) override;

private:
long long _timeLastUpdate = 0;
};

} // namespace sua
Expand Down
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ int main(int argc, char* argv[])
std::string caDirectory = SUA_DEFAULT_CA_DIRECTORY;
std::string caFilepath = SUA_DEFAULT_CA_FILEPATH;
std::string help_string = fmt::format(help_string_template, server, installer, hostPathToSelfupdateDir, server, caDirectory, caFilepath);
int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL;

const char * env_server = std::getenv("SUA_SERVER");
if(env_server) {
server = env_server;
}

const char * env_interval = std::getenv("SUA_MESSAGE_DELAY");
if(env_interval) {
feedbackInterval = std::stoi(env_interval);
}

if(argc > 1) {
for(int i = 1; i < argc; i++) {
const std::string opt(argv[i]);
Expand Down Expand Up @@ -223,6 +229,7 @@ int main(int argc, char* argv[])
ctx.messagingProtocol = std::make_shared<sua::MqttMessagingProtocolJSON>();
ctx.bundleChecker = std::make_shared<sua::BundleChecker>();
ctx.mqttProcessor = std::make_shared<sua::MqttProcessor>(ctx);
ctx.feedbackInterval = feedbackInterval;

sua::Logger::info("SUA build number : '{}'", SUA_BUILD_NUMBER );
sua::Logger::info("SUA commit hash : '{}'", SUA_COMMIT_HASH );
Expand Down
2 changes: 2 additions & 0 deletions utest/TestSelfUpdateScenarios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ namespace {

ctx().messagingProtocol = std::make_shared<sua::MqttMessagingProtocolJSON>();
ctx().bundleChecker = std::make_shared<sua::BundleChecker>();

ctx().feedbackInterval = 0;
}

void TearDown() override {
Expand Down

0 comments on commit 70df8c1

Please sign in to comment.