From f1af8679481cfc4199b6423d723f67188145ea46 Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Fri, 21 Mar 2025 16:17:20 +0100 Subject: client: add client class with input processing --- src/server/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/server') diff --git a/src/server/main.cpp b/src/server/main.cpp index 424326f..d4890aa 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) return 1; } - SharedMemoryServer shm(size); + SharedMemoryServer shm(size); shm.process_requests(); -- cgit 1.4.1 From 435ffba7c3005e643a8d6f7fed54d0f556ee2ad7 Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Fri, 21 Mar 2025 16:33:37 +0100 Subject: get response for every operation and print it --- src/client/client.cpp | 17 +++++++++------ src/server/hashtable.h | 13 ++++++++---- src/server/shared_memory_server.h | 44 ++++++++++++++++++++++++++++++++------- 3 files changed, 56 insertions(+), 18 deletions(-) (limited to 'src/server') 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::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::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::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 #include #include +#include #include template @@ -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 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("Inserted successfully").c_str(), + MAX_VALUE_SIZE); + } else { + strncpy( + request->response, + serialize("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 result = hash_table.get(key); if (result.has_value()) { std::string response = serialize(result.value()); strncpy(request->response, response.c_str(), MAX_VALUE_SIZE); - pthread_cond_signal(&shared_memory->cond_var); + } else { + strncpy( + request->response, + serialize("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("Key successfully deleted").c_str(), + MAX_VALUE_SIZE); + } else { + strncpy( + request->response, + serialize("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(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); } } -- cgit 1.4.1 From b67a507e2ef0db4970f04df5c5a52d9b9e9dd74d Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Fri, 21 Mar 2025 22:12:49 +0100 Subject: renaming and refactoring --- src/client/client.cpp | 21 +++++++++++++++++---- src/client/client.h | 2 +- src/client/main.cpp | 2 -- src/server/main.cpp | 2 +- src/server/shared_memory_server.h | 12 ++++++++---- 5 files changed, 27 insertions(+), 12 deletions(-) (limited to 'src/server') 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( ):" << '\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():" << '\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():" << '\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 k, std::optional 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 shm(size); + Server 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 template -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("Couldn't find key'").c_str(), + serialize("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(hash_table.string()).c_str(), MAX_VALUE_SIZE); break; + default: break; } -- cgit 1.4.1 From 7212db13b9013aa15673ae65da65eeaf97ee0d12 Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Fri, 21 Mar 2025 22:13:38 +0100 Subject: add doxygen-comments for each member --- src/client/client.h | 51 ++++++++++++++++++++++++++++++ src/common/shared_memory.h | 19 ++++++++++++ src/server/hashtable.h | 65 +++++++++++++++++++++++++++++++++++++++ src/server/shared_memory_server.h | 30 +++++++++++++++++- 4 files changed, 164 insertions(+), 1 deletion(-) (limited to 'src/server') diff --git a/src/client/client.h b/src/client/client.h index 852ade3..bc2d702 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -11,22 +11,73 @@ #include #include "shared_memory.h" + +/** + * @class Client + * @brief Represents the client, which performs actions on the hashtable of the server. + */ class Client { public: + /** + * @brief Constructs a new client and opens the shared memory. + */ Client(); + + /** + * @brief Unmaps the shared memory. + */ ~Client(); + /** + * @brief Main client program. + * + * @details The user can choose the operation and the arguments, which will be sent to the + * server. + */ void start_client(); private: + /** + * @brief Memory which is shared with the server. + */ SharedMemory* shared_memory; + + /** + * @brief File descriptor for the shared memory, used to unmap and close the memory at the end. + */ int shm_fd; + /** + * @brief Sends a request to the server. + * + * @param shared_memory The memory to use. + * @param type The type of the operation the server has to process. + * @param k First potential argument of the request, represents the key. + * @param v Second potential argument of the request, represent the value. + * @return int The index of the request in the circular-buffer, so we can access it again for + * processing the respond. + */ int send_request( SharedMemory* shared_memory, Operations type, std::optional k, std::optional v); + + /** + * @brief Determines if the request was processed by the server. + * + * @param shared_memory The memory to use. + * @param index The index of the request in the circular-buffer. + * @return bool The request was processed by the server. + */ bool request_processed(SharedMemory* shared_memory, int index); + + /** + * @brief Processes the respond of the server. + * + * @param shared_memory The memory to use. + * @param index The index of the request in the circular-buffer. + * @return std::string The response of the server as a string. + */ std::string process_respond(SharedMemory* shared_memory, int index); }; diff --git a/src/common/shared_memory.h b/src/common/shared_memory.h index 70554e4..fe101ea 100644 --- a/src/common/shared_memory.h +++ b/src/common/shared_memory.h @@ -9,8 +9,14 @@ #define MAX_KEY_SIZE 64 #define MAX_VALUE_SIZE 128 +/** + * @brief Possible operations on the hashtable. + */ enum Operations { INSERT, DELETE, GET, PRINT }; +/** + * @brief One request constists out of the operation, the arguments and the response. + */ struct Request { Operations type; char key[MAX_KEY_SIZE]; @@ -18,6 +24,13 @@ struct Request { char response[MAX_VALUE_SIZE]; }; +/** + * @brief The shared memory between client and server. + * + * @details The shared memory consists out of: + * - A circular-buffer, to de- and enqueue multiple request at once. + * - A mutex with a conditional variable, to ensure concurrency of the buffer. + */ struct SharedMemory { Request request[QUEUE_SIZE]; pthread_mutex_t mutex; @@ -28,6 +41,9 @@ struct SharedMemory { bool full; }; +/** + * @brief Converts a generic type to a stringstream, so that it can be saved in the shared memory. + */ template std::string serialize(const T& data) { @@ -36,6 +52,9 @@ std::string serialize(const T& data) return oss.str(); } +/** + * @brief Converts a stringstream to a generic type, so that value in the shared memory can be read. + */ template T deserialize(const std::string& str) { diff --git a/src/server/hashtable.h b/src/server/hashtable.h index 2538e76..e09fcd4 100644 --- a/src/server/hashtable.h +++ b/src/server/hashtable.h @@ -9,9 +9,18 @@ #include #include +/** + * @class HashTable + * @brief Represents a generic hashtable with simple operations. + */ template class HashTable { public: + /** + * @brief Constructs a new Hashtable. + * + * @param size The number of buckets of the table. + */ HashTable(size_t size) : size { size } , table(size) @@ -19,6 +28,13 @@ public: { } + /** + * @brief Insert a kv-pair into the hashtable. + * + * @param key The key to determine the bucket. + * @param value The value to insert. + * @return bool Successful insert of the pair. + */ bool insert(K key, V value) { size_t index = get_bucket_index(key); @@ -35,6 +51,12 @@ public: return true; } + /** + * @brief Gets the value which corresponds to the key. + * + * @param key The key to look for. + * @return std::optional The value, if the key could be found. + */ std::optional get(K key) { size_t index = get_bucket_index(key); @@ -50,6 +72,12 @@ public: return std::optional(); } + /** + * @brief Removes the kv-pair which corresponds to the key. + * + * @param key The key to look for. + * @return bool The pair could be removed successfully. + */ bool remove(K key) { size_t index = get_bucket_index(key); @@ -66,6 +94,11 @@ public: return false; } + /** + * @brief Constructs a string representation of the hashtable. + * + * @return std::string The string of the hashtable. + */ std::string string() { std::ostringstream output; @@ -85,14 +118,33 @@ public: } private: + /** + * @brief The number of buckets. + */ size_t size; + /** + * @brief The hashtable. + */ std::vector>> table; + /** + * @brief A mutex for every button. + */ std::vector bucket_mutexes; + /** + * @brief The hashfunction to use for the bucket determination. + */ std::hash hash_function; + /** + * @brief Finds the kv-pair inside a bucket. + * + * @param list The bucket. + * @param key The key to look for. + * @return auto The iterator element, which points to the kv-pair or list.end(). + */ auto bucket_find_key(std::list>& list, K key) { return std::find_if(list.begin(), list.end(), [&key](const std::pair& pair) { @@ -100,10 +152,23 @@ private: }); } + /** + * @brief Checks if the bucket contains the key. + * + * @param list The bucket. + * @param key The key to look for. + * @return bool The bucket contains the key. + */ bool bucket_contains_key(std::list>& list, K key) { return list.begin() != list.end() && bucket_find_key(list, key) != list.end(); } + /** + * @brief Uses the hashfunction and the key to determine, which bucket to use. + * + * @param key The key. + * @return size_t The index of the bucket. + */ size_t get_bucket_index(K key) { return hash_function(key) % size; } }; diff --git a/src/server/shared_memory_server.h b/src/server/shared_memory_server.h index aa0e261..4580d1a 100644 --- a/src/server/shared_memory_server.h +++ b/src/server/shared_memory_server.h @@ -8,9 +8,19 @@ #include #include +/** + * @class Server + * @brief Represents the server, which performs operations on the hashtable based on the requests of + * the client. + */ template class Server { public: + /** + * @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) { @@ -34,6 +44,9 @@ public: pthread_cond_init(&shared_memory->cond_var, &cond_attr); } + /** + * @brief Unmaps and unlinks the shared memory. + */ ~Server() { munmap(shared_memory, sizeof(SharedMemory)); @@ -41,6 +54,11 @@ public: 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) { @@ -121,8 +139,18 @@ public: } private: + /** + * @brief The hashtable. + */ HashTable 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; }; -- cgit 1.4.1