about summary refs log tree commit diff stats
path: root/src/client/main.cpp
blob: 2e4a606d53b5aba13f0624ebb6840deb43d923cc (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
#include "shared_memory.h"
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
#include <utility>

void sendRequest(
    SharedMemory* shared_memory,
    Operations type,
    std::pair<const std::string&, const std::string&> arguments)
{
    shared_memory->request.type = type;
    strncpy(shared_memory->request.key, serialize(arguments.first).c_str(), MAX_KEY_SIZE);
    strncpy(shared_memory->request.value, serialize(arguments.second).c_str(), MAX_VALUE_SIZE);
    shared_memory->processed = false;

    std::cout << "Command sent" << '\n';

    sleep(2);

    if (type == Operations::GET) {
        std::string result(shared_memory->response);
        std::cout << "Result: " << result << std::endl;
    }
}

int main()
{
    int shm_fd = shm_open(SHM_NAME, O_RDWR, 0666);
    if (shm_fd == -1) {
        std::cout << "Server not running" << '\n';
        return -1;
    }

    SharedMemory* shared_memory =
        (SharedMemory*)mmap(0, sizeof(SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

    std::cout << "Start inserting.." << '\n';
    sendRequest(shared_memory, INSERT, std::pair(serialize(10), serialize(5)));

    std::cout << "Start printing.." << '\n';
    sendRequest(shared_memory, PRINT, std::pair(serialize(10), serialize(5)));

    munmap(shared_memory, sizeof(SharedMemory));
    close(shm_fd);
    return 0;
}