about summary refs log tree commit diff stats
path: root/src/client/client.h
blob: 556e9bfb6263a631d4ece9fe7eb86bbadaf95051 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <optional>
#include <pthread.h>
#include <string>
#include <sys/mman.h>
#include <unistd.h>

#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);
};