Created
August 11, 2017 09:02
-
-
Save odeblic/f741ad9c6dd0e2dedadb6dbe6d05ca16 to your computer and use it in GitHub Desktop.
Monitoring and heartbeat
This file contains hidden or 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
#include <thread> | |
#include <chrono> | |
class Monitor | |
{ | |
public: | |
void onHeartbeat(int id, int status) | |
{ | |
lastTimestamp = std::chrono::system_clock::now(); | |
} | |
private: | |
std::chrono::system_clock::time_point firstTimestamp; | |
std::chrono::system_clock::time_point lastTimestamp; | |
}; | |
class Monitored | |
{ | |
public: | |
Monitored(int id) : _id(id) | |
{ | |
_thread = std::thread([this]{ | |
while(true) | |
{ | |
std::this_thread::sleep_for(std::chrono::milliseconds(500)); | |
sendHeartbeat(_id, _status, _sequenceNumber); | |
} | |
}); | |
} | |
~Monitored() | |
{ | |
_thread.join(); | |
} | |
void setStatus(int status) | |
{ | |
_status = status; | |
} | |
private: | |
int _id; | |
std::thread _thread; | |
std::atomic<int> _status; | |
int _sequenceNumber{0}; | |
}; | |
int main() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment