Branch data Line data Source code
1 : : #include <csignal>
2 : : #include <cstdint>
3 : : #include <iostream>
4 : : #include <zmq.hpp>
5 : :
6 : : #include "mq/src/ZeroMQSocket.hpp"
7 : :
8 : : namespace {
9 : : // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
10 : : volatile std::sig_atomic_t interrupted{0};
11 : : } // namespace
12 : :
13 : 0 : void signalHandler(int signum) {
14 : : (void)signum; // ignore unused variable
15 : 0 : interrupted = 1;
16 : 0 : }
17 : :
18 : 0 : void catchSignals() {
19 : 0 : (void)std::signal(SIGINT, signalHandler);
20 : 0 : (void)std::signal(SIGTERM, signalHandler);
21 : 0 : (void)std::signal(SIGSEGV, signalHandler);
22 : 0 : (void)std::signal(SIGABRT, signalHandler);
23 : 0 : }
24 : :
25 : 0 : auto main() -> int {
26 : : // Connect to zmq
27 : 0 : zmq::context_t context(1);
28 : 0 : MQ::ZeroMQSocket publisher{context, zmq::socket_type::pub};
29 : 0 : if (!publisher.connect("ipc:///tmp/speed.ipc")) {
30 : 0 : return 1;
31 : : }
32 : :
33 : 0 : uint8_t temperature = 0;
34 : :
35 : 0 : catchSignals();
36 : 0 : while (temperature < 110) {
37 : : try {
38 : 0 : publisher.send({3, temperature++});
39 : 0 : } catch (zmq::error_t &e) {
40 : 0 : if (ETERM == e.num()) {
41 : 0 : break;
42 : : }
43 : 0 : }
44 : :
45 : 0 : if (static_cast<bool>(interrupted)) {
46 : 0 : std::cout << "interrupt received, killing program...\n";
47 : 0 : break;
48 : : }
49 : 0 : sleep(10);
50 : : }
51 : :
52 : 0 : return 0;
53 : 0 : }
|