diff options
Diffstat (limited to '')
| -rw-r--r-- | src/server/hashtable.h | 6 | ||||
| -rw-r--r-- | src/server/main.cpp | 20 | ||||
| -rw-r--r-- | src/server/shared_memory_server.h | 15 |
3 files changed, 35 insertions, 6 deletions
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 @@ -29,6 +29,12 @@ public: } /** + * @brief Constructs a new Hashtable. + * + */ + HashTable() {} + + /** * @brief Insert a kv-pair into the hashtable. * * @param key The key to determine the bucket. 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 <csignal> #include <cstdint> #include <iostream> #include <stdexcept> @@ -5,6 +6,21 @@ #include "shared_memory_server.h" +Server<int, int> shm; + +/** + * @brief Shuts the server down, when pressing <Ctrl+C>. + * + * @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<int, int> 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 <csignal> #include <cstring> #include <fcntl.h> #include <optional> @@ -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,7 +46,7 @@ public: /** * @brief Unmaps and unlinks the shared memory. */ - ~Server() + void terminate_server() { munmap(shared_memory, sizeof(SharedMemory)); close(shm_fd); @@ -56,6 +54,13 @@ public: } /** + * @brief Initializes the hashtable. + * + * @param size The number of buckets in the hashtable. + */ + void initialize_hashtable(size_t size) { hash_table = HashTable<K, V>(size); } + + /** * @brief The main loop of the server. * * @details The server checks the shared memory for new requests and executes them. |