about summary refs log tree commit diff stats
path: root/src/client
diff options
context:
space:
mode:
authorChristian Krinitsin <mail@krinitsin.com>2025-03-21 22:12:49 +0100
committerChristian Krinitsin <mail@krinitsin.com>2025-03-21 22:12:49 +0100
commitb67a507e2ef0db4970f04df5c5a52d9b9e9dd74d (patch)
tree514254f28a3230d4a6289e160c7df0e5c74cde26 /src/client
parent435ffba7c3005e643a8d6f7fed54d0f556ee2ad7 (diff)
downloadBT-Programming-Assignment-b67a507e2ef0db4970f04df5c5a52d9b9e9dd74d.tar.gz
BT-Programming-Assignment-b67a507e2ef0db4970f04df5c5a52d9b9e9dd74d.zip
renaming and refactoring
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.cpp21
-rw-r--r--src/client/client.h2
-rw-r--r--src/client/main.cpp2
3 files changed, 18 insertions, 7 deletions
diff --git a/src/client/client.cpp b/src/client/client.cpp
index 6f6f152..cbaddeb 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -7,7 +7,7 @@ Client::Client()
     shm_fd = shm_open(SHM_NAME, O_RDWR, 0666);
     if (shm_fd == -1) {
         std::cout << "Server not running" << '\n';
-        return;
+        exit(-1);
     }
 
     shared_memory =
@@ -29,9 +29,11 @@ void Client::start_client()
         std::cout << "Choose the operation (i: Insert, g: Get, r: Remove, p: Print, e: Exit)"
                   << '\n';
         std::cin >> operation;
+
         switch (operation) {
         case 'e':
             return;
+
         case 'i':
             std::cout << "Insert: Enter the k-v pair(<int> <int>):" << '\n';
             if (!(std::cin >> k >> v)) {
@@ -43,6 +45,7 @@ void Client::start_client()
             index = send_request(
                 shared_memory, INSERT, std::optional(serialize(k)), std::optional(serialize(v)));
             break;
+
         case 'g': {
             std::cout << "Get: Enter the key(<int>):" << '\n';
             if (!(std::cin >> k)) {
@@ -54,6 +57,7 @@ void Client::start_client()
             index = send_request(shared_memory, GET, std::optional(serialize(k)), std::nullopt);
             break;
         }
+
         case 'r':
             std::cout << "Remove: Enter key(<int>):" << '\n';
             if (!(std::cin >> k)) {
@@ -64,15 +68,17 @@ void Client::start_client()
             };
             index = send_request(shared_memory, DELETE, std::optional(serialize(k)), std::nullopt);
             break;
+
         case 'p':
             index = send_request(shared_memory, PRINT, std::nullopt, std::nullopt);
             break;
+
         default:
             break;
         }
 
         if (index != -1) {
-            std::string response = process_result(shared_memory, index);
+            std::string response = process_respond(shared_memory, index);
             std::cout << response << '\n';
         }
         std::cout << '\n';
@@ -81,6 +87,10 @@ void Client::start_client()
 
 bool Client::request_processed(SharedMemory* shared_memory, int index)
 {
+    if (index >= QUEUE_SIZE || index < 0) {
+        return false;
+    }
+
     if (shared_memory->full) {
         return false;
     }
@@ -125,9 +135,12 @@ int Client::send_request(
     return index;
 }
 
-std::string Client::process_result(SharedMemory* shared_memory, int index)
+std::string Client::process_respond(SharedMemory* shared_memory, int index)
 {
-    std::cout << "process result" << '\n';
+    if (index >= QUEUE_SIZE || index < 0) {
+        return std::string();
+    }
+
     pthread_mutex_lock(&shared_memory->mutex);
 
     while (!request_processed(shared_memory, index)) {
diff --git a/src/client/client.h b/src/client/client.h
index 9ff5b47..852ade3 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -28,5 +28,5 @@ private:
         std::optional<const std::string> k,
         std::optional<const std::string> v);
     bool request_processed(SharedMemory* shared_memory, int index);
-    std::string process_result(SharedMemory* shared_memory, int index);
+    std::string process_respond(SharedMemory* shared_memory, int index);
 };
diff --git a/src/client/main.cpp b/src/client/main.cpp
index ca2f4a3..7e61ffe 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -1,9 +1,7 @@
 #include "client.h"
 
-
 int main()
 {
     Client client;
-
     client.start_client();
 }