From eef27810ce34d00d1d19a7ac6fbce794df4a9708 Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Sun, 23 Mar 2025 19:09:52 +0100 Subject: add server-shutdown with --- src/server/hashtable.h | 6 ++++++ src/server/main.cpp | 20 +++++++++++++++++++- src/server/shared_memory_server.h | 15 ++++++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/server/hashtable.h b/src/server/hashtable.h index e09fcd4..dfa9ca3 100644 --- a/src/server/hashtable.h +++ b/src/server/hashtable.h @@ -28,6 +28,12 @@ public: { } + /** + * @brief Constructs a new Hashtable. + * + */ + HashTable() {} + /** * @brief Insert a kv-pair into the hashtable. * diff --git a/src/server/main.cpp b/src/server/main.cpp index 75939e0..c20cde9 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,21 @@ #include "shared_memory_server.h" +Server shm; + +/** + * @brief Shuts the server down, when pressing . + * + * @param signal Specifies the signal, which was caught. + */ +void signal_handler(int signal) { + if (signal == SIGINT) { + std::cout << "Server shutting down" << '\n'; + shm.terminate_server(); + exit(0); + } +} + int main(int argc, char* argv[]) { if (argc != 2) { @@ -20,7 +36,9 @@ int main(int argc, char* argv[]) return 1; } - Server shm(size); + shm.initialize_hashtable(size); + + std::signal(SIGINT, signal_handler); shm.process_requests(); diff --git a/src/server/shared_memory_server.h b/src/server/shared_memory_server.h index 3ac375a..46005fc 100644 --- a/src/server/shared_memory_server.h +++ b/src/server/shared_memory_server.h @@ -2,6 +2,7 @@ #include "hashtable.h" #include "shared_memory.h" +#include #include #include #include @@ -18,11 +19,8 @@ class Server { public: /** * @brief Constructs a new hashtable and initializes a shared memory buffer. - * - * @param size The number of buckets in the hashtable. */ - Server(size_t size) - : hash_table(size) + Server() { shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666); @@ -48,13 +46,20 @@ public: /** * @brief Unmaps and unlinks the shared memory. */ - ~Server() + void terminate_server() { munmap(shared_memory, sizeof(SharedMemory)); close(shm_fd); shm_unlink(SHM_NAME); } + /** + * @brief Initializes the hashtable. + * + * @param size The number of buckets in the hashtable. + */ + void initialize_hashtable(size_t size) { hash_table = HashTable(size); } + /** * @brief The main loop of the server. * -- cgit 1.4.1