Skip to content

Commit 70df8c1

Browse files
Implemented configurable delay for feedback messages (Downloading and Installing) (#49)
* Implemented configurable delay for feedback messages (Downloading and Installing) * Fix review findings * Reduce feedback interval to 0 for tests
1 parent c413945 commit 70df8c1

File tree

10 files changed

+49
-13
lines changed

10 files changed

+49
-13
lines changed

.github/actions/build-native-binary/action.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ runs:
135135
if: ${{ inputs.arch == 'amd64' }}
136136
run: |
137137
cd dist_${{ inputs.arch }}/utest
138-
LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent > ../../unit_tests_report_${{ inputs.arch }}.txt --gtest_output=xml:../../unit_tests_report_${{ inputs.arch }}.xml
138+
LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent > ../../unit_tests_report_${{ inputs.arch }}.txt --gtest_output=xml:../../unit_tests_report_${{ inputs.arch }}.xml
139139
shell: bash
140140

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

169169
- name: Generate code-coverage report

src/Context.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace sua {
6262
std::string caFilepath = SUA_DEFAULT_CA_FILEPATH;
6363
bool downloadMode = true;
6464
bool fallbackMode = false;
65+
int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds
6566

6667
DesiredState desiredState;
6768
CurrentState currentState;

src/Defaults.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
#include "Defaults.h"
1818

19-
const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp";
20-
const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto";
21-
const int SUA_DEFAULT_MQTT_PORT = 1883;
22-
const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883";
23-
const std::string SUA_DEFAULT_MODE = "download";
24-
const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates";
25-
const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs";
26-
const std::string SUA_DEFAULT_CA_FILEPATH = "";
19+
const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp";
20+
const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto";
21+
const int SUA_DEFAULT_MQTT_PORT = 1883;
22+
const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883";
23+
const std::string SUA_DEFAULT_MODE = "download";
24+
const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates";
25+
const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs";
26+
const std::string SUA_DEFAULT_CA_FILEPATH = "";
27+
const int SUA_DEFAULT_FEEDBACK_INTERVAL = 5;

src/Defaults.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ extern const std::string SUA_DEFAULT_MODE;
2727
extern const std::string SUA_DEFAULT_TEMP_DIRECTORY;
2828
extern const std::string SUA_DEFAULT_CA_DIRECTORY;
2929
extern const std::string SUA_DEFAULT_CA_FILEPATH;
30+
extern const int SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds
3031

3132
#endif

src/FSM/States/Downloading.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "FotaEvent.h"
2323
#include "Logger.h"
2424

25+
#include <chrono>
26+
2527
namespace sua {
2628

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

54+
const auto now = std::chrono::system_clock::now().time_since_epoch();
55+
const auto now_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();
56+
5257
Logger::info("Download progress: {}%", ctx.desiredState.downloadProgressPercentage);
53-
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading);
58+
59+
if((ctx.desiredState.downloadProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) {
60+
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading);
61+
_timeLastUpdate = now_in_seconds;
62+
}
5463
});
5564

5665
if (true == ctx.downloadMode) {

src/FSM/States/Downloading.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace sua {
3232
void onEnter(Context& ctx) override;
3333

3434
FotaEvent body(Context& ctx) override;
35+
36+
private:
37+
long long _timeLastUpdate = 0;
3538
};
3639

3740
} // namespace sua

src/FSM/States/Installing.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "Context.h"
2222
#include "Logger.h"
2323

24+
#include <chrono>
25+
2426
namespace sua {
2527

2628
Installing::Installing()
@@ -37,7 +39,14 @@ namespace sua {
3739
subscribe(Installer::EVENT_INSTALLING, [this, &ctx](const std::map<std::string, std::string>& payload) {
3840
ctx.desiredState.installProgressPercentage = std::stoi(payload.at("percentage"));
3941
Logger::info("Install progress: {}%", ctx.desiredState.installProgressPercentage);
40-
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing);
42+
43+
const auto now = std::chrono::system_clock::now().time_since_epoch();
44+
const auto now_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();
45+
46+
if((ctx.desiredState.installProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) {
47+
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing);
48+
_timeLastUpdate = now_in_seconds;
49+
}
4150
});
4251

4352
std::string install_input;

src/FSM/States/Installing.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace sua {
3232
void onEnter(Context& ctx) override;
3333

3434
FotaEvent body(Context& ctx) override;
35+
36+
private:
37+
long long _timeLastUpdate = 0;
3538
};
3639

3740
} // namespace sua

src/main.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,18 @@ int main(int argc, char* argv[])
6868
std::string caDirectory = SUA_DEFAULT_CA_DIRECTORY;
6969
std::string caFilepath = SUA_DEFAULT_CA_FILEPATH;
7070
std::string help_string = fmt::format(help_string_template, server, installer, hostPathToSelfupdateDir, server, caDirectory, caFilepath);
71+
int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL;
7172

7273
const char * env_server = std::getenv("SUA_SERVER");
7374
if(env_server) {
7475
server = env_server;
7576
}
7677

78+
const char * env_interval = std::getenv("SUA_MESSAGE_DELAY");
79+
if(env_interval) {
80+
feedbackInterval = std::stoi(env_interval);
81+
}
82+
7783
if(argc > 1) {
7884
for(int i = 1; i < argc; i++) {
7985
const std::string opt(argv[i]);
@@ -223,6 +229,7 @@ int main(int argc, char* argv[])
223229
ctx.messagingProtocol = std::make_shared<sua::MqttMessagingProtocolJSON>();
224230
ctx.bundleChecker = std::make_shared<sua::BundleChecker>();
225231
ctx.mqttProcessor = std::make_shared<sua::MqttProcessor>(ctx);
232+
ctx.feedbackInterval = feedbackInterval;
226233

227234
sua::Logger::info("SUA build number : '{}'", SUA_BUILD_NUMBER );
228235
sua::Logger::info("SUA commit hash : '{}'", SUA_COMMIT_HASH );

utest/TestSelfUpdateScenarios.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ namespace {
191191

192192
ctx().messagingProtocol = std::make_shared<sua::MqttMessagingProtocolJSON>();
193193
ctx().bundleChecker = std::make_shared<sua::BundleChecker>();
194+
195+
ctx().feedbackInterval = 0;
194196
}
195197

196198
void TearDown() override {

0 commit comments

Comments
 (0)