about summary refs log tree commit diff stats
path: root/src/client
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 /src/client
parent3939238f3fe46ed36919f29cbebe824341689960 (diff)
parentd6ea41e1bef4a312a8a1d6683bb29aad777ec9ae (diff)
downloadBT-Programming-Assignment-66d87280633590818261b89c50fe1830092174e4.tar.gz
BT-Programming-Assignment-66d87280633590818261b89c50fe1830092174e4.zip
Merge pull request #5 from ckrinitsin/fixes HEAD main
Fixes
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.cpp24
-rw-r--r--src/client/client.h1
2 files changed, 6 insertions, 19 deletions
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"