about summary refs log tree commit diff stats
path: root/src/client/client.h
diff options
context:
space:
mode:
authorckrinitsin <101062646+ckrinitsin@users.noreply.github.com>2025-03-21 22:15:01 +0100
committerGitHub <noreply@github.com>2025-03-21 22:15:01 +0100
commit5fca08baa98058508c743101dcf10cd1b178ec7a (patch)
tree9346b38f25ab5880af3f40e49ba1e64c4f7e0ca2 /src/client/client.h
parent2a22b4123dce661b0500dc07012d61215bdce161 (diff)
parent7212db13b9013aa15673ae65da65eeaf97ee0d12 (diff)
downloadBT-Programming-Assignment-5fca08baa98058508c743101dcf10cd1b178ec7a.tar.gz
BT-Programming-Assignment-5fca08baa98058508c743101dcf10cd1b178ec7a.zip
Merge pull request #4 from ckrinitsin/client-input
Client input
Diffstat (limited to 'src/client/client.h')
-rw-r--r--src/client/client.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/client/client.h b/src/client/client.h
new file mode 100644
index 0000000..bc2d702
--- /dev/null
+++ b/src/client/client.h
@@ -0,0 +1,83 @@
+#pragma once
+
+#include <cstring>
+#include <fcntl.h>
+#include <iostream>
+#include <optional>
+#include <pthread.h>
+#include <string>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <utility>
+
+#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<const std::string> k,
+        std::optional<const std::string> 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);
+};