about summary refs log tree commit diff stats
path: root/src/server/shared_memory_server.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/shared_memory_server.h')
-rw-r--r--src/server/shared_memory_server.h84
1 files changed, 72 insertions, 12 deletions
diff --git a/src/server/shared_memory_server.h b/src/server/shared_memory_server.h
index 74abedf..4580d1a 100644
--- a/src/server/shared_memory_server.h
+++ b/src/server/shared_memory_server.h
@@ -8,10 +8,20 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
+/**
+ * @class Server
+ * @brief Represents the server, which performs operations on the hashtable based on the requests of
+ * the client.
+ */
 template <typename K, typename V>
-class SharedMemoryServer {
+class Server {
 public:
-    SharedMemoryServer(size_t size)
+    /**
+     * @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)
     {
         shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
@@ -34,13 +44,21 @@ public:
         pthread_cond_init(&shared_memory->cond_var, &cond_attr);
     }
 
-    ~SharedMemoryServer()
+    /**
+     * @brief Unmaps and unlinks the shared memory.
+     */
+    ~Server()
     {
         munmap(shared_memory, sizeof(SharedMemory));
         close(shm_fd);
         shm_unlink(SHM_NAME);
     }
 
+    /**
+     * @brief The main loop of the server.
+     *
+     * @details The server checks the shared memory for new requests and executes them.
+     */
     void process_requests()
     {
         while (true) {
@@ -57,40 +75,82 @@ public:
 
             switch (request->type) {
             case INSERT:
-                std::cout << "Inserting" << '\n';
-                hash_table.insert(key, value);
+                std::cout << "Insert operation" << '\n';
+                if (hash_table.insert(key, value)) {
+                    strncpy(
+                        request->response,
+                        serialize<std::string>("Inserted successfully").c_str(),
+                        MAX_VALUE_SIZE);
+                } else {
+                    strncpy(
+                        request->response,
+                        serialize<std::string>("Key is already available").c_str(),
+                        MAX_VALUE_SIZE);
+                }
                 break;
+
             case GET: {
-                std::cout << "Getting" << '\n';
+                std::cout << "Get operation" << '\n';
                 hash_table.insert(key, value);
                 std::optional<V> result = hash_table.get(key);
                 if (result.has_value()) {
                     std::string response = serialize<V>(result.value());
                     strncpy(request->response, response.c_str(), MAX_VALUE_SIZE);
-                    pthread_cond_signal(&shared_memory->cond_var);
+                } else {
+                    strncpy(
+                        request->response,
+                        serialize<std::string>("Couldn't get any value").c_str(),
+                        MAX_VALUE_SIZE);
                 }
                 break;
             }
+
             case DELETE:
-                std::cout << "Deleting" << '\n';
-                hash_table.remove(key);
+                std::cout << "Remove operation" << '\n';
+                if (hash_table.remove(key)) {
+                    strncpy(
+                        request->response,
+                        serialize<std::string>("Key successfully deleted").c_str(),
+                        MAX_VALUE_SIZE);
+                } else {
+                    strncpy(
+                        request->response,
+                        serialize<std::string>("Couldn't find the key").c_str(),
+                        MAX_VALUE_SIZE);
+                }
                 break;
+
             case PRINT:
-                std::cout << "Printing" << '\n';
-                hash_table.print();
+                std::cout << "Print operation" << '\n';
+                strncpy(
+                    request->response,
+                    serialize<std::string>(hash_table.string()).c_str(),
+                    MAX_VALUE_SIZE);
                 break;
+
             default:
                 break;
             }
             shared_memory->tail = (1 + shared_memory->tail) % QUEUE_SIZE;
             shared_memory->full = false;
+            pthread_cond_signal(&shared_memory->cond_var);
             pthread_mutex_unlock(&shared_memory->mutex);
         }
     }
 
 private:
+    /**
+     * @brief The hashtable.
+     */
     HashTable<K, V> hash_table;
 
-    int shm_fd;
+    /**
+     * @brief Memory which is shared with the client.
+     */
     SharedMemory* shared_memory;
+
+    /**
+     * @brief File descriptor for the shared memory, used to unmap and close the memory at the end.
+     */
+    int shm_fd;
 };