Skip to content

Commit

Permalink
Add signal handling for orderly shutdown
Browse files Browse the repository at this point in the history
- Add 'signalHandler' function to handle SIGINT for graceful shutdown
- Update 'consoleHandler' and WebSocket handler loop to check for a 'running' atomic flag
- Signal processing in 'main.cpp' to intercept SIGINT and set the 'running' flag to false
- Add '<csignal>' and '<atomic>' headers for signal handling and atomic operations

These changes ensure that the application can be terminated properly when interrupted by SIGINT (Ctrl+C).
  • Loading branch information
arkadiusz committed Jul 26, 2024
1 parent 5fcb351 commit 3920485
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/handler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "handler.hpp"
#include <iostream>
#include <thread>
#include <csignal>

// A list of colors
const std::array<std::array<float, 3>, 7> predefinedColors = {{
Expand Down Expand Up @@ -35,7 +36,7 @@ void initDevices() {

void consoleHandler(bool &flagPaused, std::set<std::string>& selected_serials) {
std::string input;
while (true) {
while (running) {
std::cout << "Enter command: ";
std::getline(std::cin, input);
if (input == "Search") {
Expand Down Expand Up @@ -90,4 +91,11 @@ void consoleHandler(bool &flagPaused, std::set<std::string>& selected_serials) {
}
}
}
}
}

void signalHandler(int signal) {
if (signal == SIGINT) {
std::cout << "\nSIGINT received, shutting down gracefully...\n";
running = false;
}
}
3 changes: 3 additions & 0 deletions src/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <set>
#include <atomic>

// global variables
inline OmniscopeDeviceManager deviceManager{};
inline std::vector<std::shared_ptr<OmniscopeDevice>> devices;
inline std::map<Omniscope::Id, std::array<float, 3>> colorMap;
inline std::optional<OmniscopeSampler> sampler{};
inline std::map<Omniscope::Id, std::vector<std::pair<double, double>>> captureData;
inline std::atomic<bool> running{true};

void initDevices();
void consoleHandler(bool &flagPaused, std::set<std::string>& selected_serials);
void signalHandler(int signal);

#endif
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "handler.hpp"
#include <thread>
#include <CLI/CLI.hpp>
#include <csignal>

int main(int argc, char** argv) {

Expand All @@ -17,12 +18,15 @@ int main(int argc, char** argv) {

WebSocketHandler wsHandler(wsURI);

// Signal handler for SIGINT
std::signal(SIGINT, signalHandler);

// Start console handler thread
std::thread consoleThread(consoleHandler, std::ref(flagPaused), std::ref(selected_serials));

// WebSocket handler thread
std::thread webSocketThread([&]() {
while (true) {
while (running) {
if (sampler.has_value() && !flagPaused) {
sampler->copyOut(captureData);
wsHandler.send(captureData, selected_serials);
Expand Down

0 comments on commit 3920485

Please sign in to comment.