diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-03-21 16:33:37 +0100 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-03-21 16:33:37 +0100 |
| commit | 435ffba7c3005e643a8d6f7fed54d0f556ee2ad7 (patch) | |
| tree | 115e7af89ed5a83ed5433bbc5d306a4e263fdbd1 | |
| parent | f1af8679481cfc4199b6423d723f67188145ea46 (diff) | |
| download | BT-Programming-Assignment-435ffba7c3005e643a8d6f7fed54d0f556ee2ad7.tar.gz BT-Programming-Assignment-435ffba7c3005e643a8d6f7fed54d0f556ee2ad7.zip | |
get response for every operation and print it
Diffstat (limited to '')
| -rw-r--r-- | src/client/client.cpp | 17 | ||||
| -rw-r--r-- | src/server/hashtable.h | 13 | ||||
| -rw-r--r-- | src/server/shared_memory_server.h | 44 |
3 files changed, 56 insertions, 18 deletions
diff --git a/src/client/client.cpp b/src/client/client.cpp index d88df5f..6f6f152 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -25,6 +25,7 @@ void Client::start_client() while (true) { char operation; int k, v; + int index = -1; std::cout << "Choose the operation (i: Insert, g: Get, r: Remove, p: Print, e: Exit)" << '\n'; std::cin >> operation; @@ -39,7 +40,7 @@ void Client::start_client() std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); break; }; - send_request( + index = send_request( shared_memory, INSERT, std::optional(serialize(k)), std::optional(serialize(v))); break; case 'g': { @@ -50,9 +51,7 @@ void Client::start_client() std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); break; }; - int index = send_request(shared_memory, GET, std::optional(serialize(k)), std::nullopt); - std::string response = process_result(shared_memory, index); - std::cout << "Got: " << response << '\n'; + index = send_request(shared_memory, GET, std::optional(serialize(k)), std::nullopt); break; } case 'r': @@ -63,14 +62,19 @@ void Client::start_client() std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); break; }; - send_request(shared_memory, DELETE, std::optional(serialize(k)), std::nullopt); + index = send_request(shared_memory, DELETE, std::optional(serialize(k)), std::nullopt); break; case 'p': - send_request(shared_memory, PRINT, std::nullopt, std::nullopt); + 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::cout << response << '\n'; + } std::cout << '\n'; } } @@ -123,6 +127,7 @@ int Client::send_request( std::string Client::process_result(SharedMemory* shared_memory, int index) { + std::cout << "process result" << '\n'; pthread_mutex_lock(&shared_memory->mutex); while (!request_processed(shared_memory, index)) { diff --git a/src/server/hashtable.h b/src/server/hashtable.h index 786d8f5..2538e76 100644 --- a/src/server/hashtable.h +++ b/src/server/hashtable.h @@ -6,6 +6,7 @@ #include <mutex> #include <optional> #include <shared_mutex> +#include <sstream> #include <vector> template <typename K, typename V> @@ -65,18 +66,22 @@ public: return false; } - void print() + std::string string() { + std::ostringstream output; + size_t index { 0 }; for (auto bucket : table) { - std::cout << "Bucket " << index << ": ["; + output << "Bucket " << index << ": ["; std::shared_lock<std::shared_mutex> lock(bucket_mutexes.at(index)); for (auto pair : bucket) { - std::cout << "(" << pair.first << ", " << pair.second << ")"; + output << "(" << pair.first << ", " << pair.second << ")"; } - std::cout << "]" << "\n"; + output << "]" << "\n"; ++index; } + + return output.str(); } private: diff --git a/src/server/shared_memory_server.h b/src/server/shared_memory_server.h index 74abedf..d50797c 100644 --- a/src/server/shared_memory_server.h +++ b/src/server/shared_memory_server.h @@ -57,33 +57,61 @@ 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 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); } } |