diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/hashtable.h | 78 | ||||
| -rw-r--r-- | src/server/main.cpp | 2 | ||||
| -rw-r--r-- | src/server/shared_memory_server.h | 84 |
3 files changed, 147 insertions, 17 deletions
diff --git a/src/server/hashtable.h b/src/server/hashtable.h index 786d8f5..e09fcd4 100644 --- a/src/server/hashtable.h +++ b/src/server/hashtable.h @@ -6,11 +6,21 @@ #include <mutex> #include <optional> #include <shared_mutex> +#include <sstream> #include <vector> +/** + * @class HashTable + * @brief Represents a generic hashtable with simple operations. + */ template <typename K, typename V> 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) @@ -18,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); @@ -34,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<V> get(K key) { size_t index = get_bucket_index(key); @@ -49,6 +72,12 @@ public: return std::optional<V>(); } + /** + * @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); @@ -65,29 +94,57 @@ public: return false; } - void print() + /** + * @brief Constructs a string representation of the hashtable. + * + * @return std::string The string of the hashtable. + */ + 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: + /** + * @brief The number of buckets. + */ size_t size; + /** + * @brief The hashtable. + */ std::vector<std::list<std::pair<K, V>>> table; + /** + * @brief A mutex for every button. + */ std::vector<std::shared_mutex> bucket_mutexes; + /** + * @brief The hashfunction to use for the bucket determination. + */ std::hash<K> 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<std::pair<K, V>>& list, K key) { return std::find_if(list.begin(), list.end(), [&key](const std::pair<K, V>& pair) { @@ -95,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<std::pair<K, V>>& 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/main.cpp b/src/server/main.cpp index 424326f..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, std::string> 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 74abedf..4580d1a 100644 --- a/src/server/shared_memory_server.h +++ b/src/server/shared_memory_server.h @@ -8,10 +8,20 @@ #include <sys/mman.h> #include <unistd.h> +/** + * @class Server + * @brief Represents the server, which performs operations on the hashtable based on the requests of + * the client. + */ template <typename K, typename V> -class SharedMemoryServer { +class Server { public: - SharedMemoryServer(size_t size) + /** + * @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) { shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666); @@ -34,13 +44,21 @@ public: pthread_cond_init(&shared_memory->cond_var, &cond_attr); } - ~SharedMemoryServer() + /** + * @brief Unmaps and unlinks the shared memory. + */ + ~Server() { munmap(shared_memory, sizeof(SharedMemory)); close(shm_fd); 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) { @@ -57,40 +75,82 @@ 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 the 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); } } private: + /** + * @brief The hashtable. + */ HashTable<K, V> 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; }; |