diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-03-21 22:12:49 +0100 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-03-21 22:12:49 +0100 |
| commit | b67a507e2ef0db4970f04df5c5a52d9b9e9dd74d (patch) | |
| tree | 514254f28a3230d4a6289e160c7df0e5c74cde26 /src | |
| parent | 435ffba7c3005e643a8d6f7fed54d0f556ee2ad7 (diff) | |
| download | BT-Programming-Assignment-b67a507e2ef0db4970f04df5c5a52d9b9e9dd74d.tar.gz BT-Programming-Assignment-b67a507e2ef0db4970f04df5c5a52d9b9e9dd74d.zip | |
renaming and refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/client.cpp | 21 | ||||
| -rw-r--r-- | src/client/client.h | 2 | ||||
| -rw-r--r-- | src/client/main.cpp | 2 | ||||
| -rw-r--r-- | src/server/main.cpp | 2 | ||||
| -rw-r--r-- | src/server/shared_memory_server.h | 12 |
5 files changed, 27 insertions, 12 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(); } diff --git a/src/server/main.cpp b/src/server/main.cpp index d4890aa..d724bd5 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) return 1; } - SharedMemoryServer<int, int> shm(size); + Server<int, int> shm(size); shm.process_requests(); diff --git a/src/server/shared_memory_server.h b/src/server/shared_memory_server.h index d50797c..aa0e261 100644 --- a/src/server/shared_memory_server.h +++ b/src/server/shared_memory_server.h @@ -9,9 +9,9 @@ #include <unistd.h> template <typename K, typename V> -class SharedMemoryServer { +class Server { public: - SharedMemoryServer(size_t size) + Server(size_t size) : hash_table(size) { shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666); @@ -34,7 +34,7 @@ public: pthread_cond_init(&shared_memory->cond_var, &cond_attr); } - ~SharedMemoryServer() + ~Server() { munmap(shared_memory, sizeof(SharedMemory)); close(shm_fd); @@ -70,6 +70,7 @@ public: MAX_VALUE_SIZE); } break; + case GET: { std::cout << "Get operation" << '\n'; hash_table.insert(key, value); @@ -85,6 +86,7 @@ public: } break; } + case DELETE: std::cout << "Remove operation" << '\n'; if (hash_table.remove(key)) { @@ -95,10 +97,11 @@ public: } else { strncpy( request->response, - serialize<std::string>("Couldn't find key'").c_str(), + serialize<std::string>("Couldn't find the key").c_str(), MAX_VALUE_SIZE); } break; + case PRINT: std::cout << "Print operation" << '\n'; strncpy( @@ -106,6 +109,7 @@ public: serialize<std::string>(hash_table.string()).c_str(), MAX_VALUE_SIZE); break; + default: break; } |