-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheartbeatsender.cpp
More file actions
65 lines (51 loc) · 1.45 KB
/
Copy pathheartbeatsender.cpp
File metadata and controls
65 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "heartbeatsender.h"
#include "LogHelper.h"
HeartbeatSender::HeartbeatSender(QObject *parent)
: QObject{parent},
watchdogPath_("./watchdog/watchdog"),
heartbeatTimer_(std::make_unique<QTimer>(this))
{
setConnections();
}
HeartbeatSender::~HeartbeatSender()
{
sharedMemory_.detach();
}
void HeartbeatSender::setConnections()
{
connect(heartbeatTimer_.get(), &QTimer::timeout, this, &HeartbeatSender::sendHeartbeat, Qt::DirectConnection);
}
void HeartbeatSender::sendHeartbeat()
{
if (sharedMemory_.lock()) {
char* data = (char*)sharedMemory_.data();
QString heartbeat = "heartbeat";
memcpy(data, heartbeat.toStdString().c_str(), heartbeat.length() + 1);
sharedMemory_.unlock();
} else {
qDebug() << "Failed to lock shared memory.";
}
}
bool HeartbeatSender::init()
{
if (!QFile::exists(watchdogPath_)) {
TRACE << "Error: Watchdog executable does not exist at" << watchdogPath_;
return false;
}
if (!watchdogProcess_.startDetached(watchdogPath_)) {
TRACE << "Failed to start Watchdog process!";
return false;
}
sharedMemory_.setKey("MainAppHeartbeat");
if (!sharedMemory_.create(1024))
sharedMemory_.attach();
return true;
}
bool HeartbeatSender::runWatchdogProcess()
{
if(!init())
return false;
heartbeatTimer_->start(1000);
qDebug() << "Watchdog process started successfully.";
return true;
}