about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorckrinitsin <101062646+ckrinitsin@users.noreply.github.com>2025-03-23 20:55:14 +0100
committerGitHub <noreply@github.com>2025-03-23 20:55:14 +0100
commit66d87280633590818261b89c50fe1830092174e4 (patch)
treed9231a5f2d52249657c7523af9c2ff11bdbe4196
parent3939238f3fe46ed36919f29cbebe824341689960 (diff)
parentd6ea41e1bef4a312a8a1d6683bb29aad777ec9ae (diff)
downloadBT-Programming-Assignment-main.tar.gz
BT-Programming-Assignment-main.zip
Merge pull request #5 from ckrinitsin/fixes HEAD main
Fixes
-rw-r--r--README.md2
-rw-r--r--src/client/client.cpp24
-rw-r--r--src/client/client.h1
-rw-r--r--src/common/shared_memory.h7
-rw-r--r--src/server/hashtable.h5
-rw-r--r--src/server/main.cpp24
-rw-r--r--src/server/server.h (renamed from src/server/shared_memory_server.h)27
7 files changed, 57 insertions, 33 deletions
diff --git a/README.md b/README.md
index 6d41bc7..66ef50c 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,7 @@ $ ./server <number-of-buckets>
 
 ### Client
 
-2. Execute the client:
+2. Execute the client(s):
 ```bash
 $ ./client
 ```
diff --git a/src/client/client.cpp b/src/client/client.cpp
index cbaddeb..1c0727f 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -91,20 +91,7 @@ bool Client::request_processed(SharedMemory* shared_memory, int index)
         return false;
     }
 
-    if (shared_memory->full) {
-        return false;
-    }
-    if (shared_memory->tail == shared_memory->head && !shared_memory->full) {
-        return true;
-    }
-
-    for (int i = shared_memory->head - 1; i != shared_memory->tail; i = (i - 1) % QUEUE_SIZE) {
-        if (i == index) {
-            return false;
-        }
-    }
-
-    return shared_memory->tail != index;
+    return shared_memory->request[index].status == PROCESSED;
 }
 
 int Client::send_request(
@@ -117,17 +104,17 @@ int Client::send_request(
 
     pthread_mutex_lock(&shared_memory->mutex);
 
-    while (shared_memory->full) {
+    while (shared_memory->request[shared_memory->tail].status != FREE) {
         pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex);
     }
 
-    index = shared_memory->head;
+    index = shared_memory->tail;
     Request* request = &shared_memory->request[index];
     request->type = type;
+    request->status = SENT;
     strncpy(request->key, k.value_or("null").c_str(), MAX_KEY_SIZE);
     strncpy(request->value, v.value_or("null").c_str(), MAX_VALUE_SIZE);
-    shared_memory->head = (1 + shared_memory->head) % QUEUE_SIZE;
-    shared_memory->full = shared_memory->head == shared_memory->tail;
+    shared_memory->tail = (1 + shared_memory->tail) % QUEUE_SIZE;
     pthread_cond_signal(&shared_memory->cond_var);
 
     pthread_mutex_unlock(&shared_memory->mutex);
@@ -147,6 +134,7 @@ std::string Client::process_respond(SharedMemory* shared_memory, int index)
         pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex);
     }
     std::string result(shared_memory->request[index].response);
+    shared_memory->request[index].status = FREE;
     pthread_mutex_unlock(&shared_memory->mutex);
     return result;
 }
diff --git a/src/client/client.h b/src/client/client.h
index bc2d702..556e9bf 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -8,7 +8,6 @@
 #include <string>
 #include <sys/mman.h>
 #include <unistd.h>
-#include <utility>
 
 #include "shared_memory.h"
 
diff --git a/src/common/shared_memory.h b/src/common/shared_memory.h
index fe101ea..465e79f 100644
--- a/src/common/shared_memory.h
+++ b/src/common/shared_memory.h
@@ -15,10 +15,16 @@
 enum Operations { INSERT, DELETE, GET, PRINT };
 
 /**
+ * @brief Possible statuses of a request.
+ */
+enum Status { FREE, SENT, PROCESSED };
+
+/**
  * @brief One request constists out of the operation, the arguments and the response.
  */
 struct Request {
     Operations type;
+    Status status = FREE;
     char key[MAX_KEY_SIZE];
     char value[MAX_VALUE_SIZE];
     char response[MAX_VALUE_SIZE];
@@ -38,7 +44,6 @@ struct SharedMemory {
 
     int tail;
     int head;
-    bool full;
 };
 
 /**
diff --git a/src/server/hashtable.h b/src/server/hashtable.h
index e09fcd4..334a62e 100644
--- a/src/server/hashtable.h
+++ b/src/server/hashtable.h
@@ -29,6 +29,11 @@ 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 d724bd5..42b7668 100644
--- a/src/server/main.cpp
+++ b/src/server/main.cpp
@@ -1,14 +1,30 @@
+#include <csignal>
 #include <cstdint>
 #include <iostream>
 #include <stdexcept>
 #include <string>
 
-#include "shared_memory_server.h"
+#include "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';
+        exit(0);
+    }
+}
 
 int main(int argc, char* argv[])
 {
     if (argc != 2) {
-        std::cout << "One argument required" << '\n';
+        std::cout << "Usage: " << argv[0] << " <number-of-buckets>\n";
         return 1;
     }
 
@@ -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/server.h
index 6c97e64..4c019f1 100644
--- a/src/server/shared_memory_server.h
+++ b/src/server/server.h
@@ -2,6 +2,7 @@
 
 #include "hashtable.h"
 #include "shared_memory.h"
+#include <csignal>
 #include <cstring>
 #include <fcntl.h>
 #include <optional>
@@ -18,13 +19,14 @@ 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);
+        shm_fd = shm_open(SHM_NAME, O_EXCL | O_CREAT | O_RDWR, 0666);
+        if (shm_fd == -1) {
+            std::cout << "Server is already running!" << '\n';
+            exit(-1);
+        }
 
         ftruncate(shm_fd, sizeof(SharedMemory));
 
@@ -55,6 +57,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.
@@ -64,11 +73,11 @@ public:
         while (true) {
             pthread_mutex_lock(&shared_memory->mutex);
 
-            if (shared_memory->tail == shared_memory->head && !shared_memory->full) {
+            if (shared_memory->request[shared_memory->head].status != SENT) {
                 pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex);
             }
 
-            Request* request = &shared_memory->request[shared_memory->tail];
+            Request* request = &shared_memory->request[shared_memory->head];
 
             K key = deserialize<K>(request->key);
             V value = deserialize<V>(request->value);
@@ -130,8 +139,8 @@ public:
             default:
                 break;
             }
-            shared_memory->tail = (1 + shared_memory->tail) % QUEUE_SIZE;
-            shared_memory->full = false;
+            shared_memory->request[shared_memory->head].status = PROCESSED;
+            shared_memory->head = (1 + shared_memory->head) % QUEUE_SIZE;
             pthread_cond_signal(&shared_memory->cond_var);
             pthread_mutex_unlock(&shared_memory->mutex);
         }