diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-03-21 10:42:09 +0100 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-03-21 10:42:09 +0100 |
| commit | 44f772a7442e617d4a93c0b3ed318ffb42991d8b (patch) | |
| tree | b8cb1009b3a089a75fe15c600386cd96cac5c9d9 /src/client/main.cpp | |
| parent | 20ae154d21b8b9111e550b936ea23325a6de8e84 (diff) | |
| download | BT-Programming-Assignment-44f772a7442e617d4a93c0b3ed318ffb42991d8b.tar.gz BT-Programming-Assignment-44f772a7442e617d4a93c0b3ed318ffb42991d8b.zip | |
implement communication between server and client (without mutexes and with queue_length of 1)
Diffstat (limited to 'src/client/main.cpp')
| -rw-r--r-- | src/client/main.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/client/main.cpp b/src/client/main.cpp new file mode 100644 index 0000000..2e4a606 --- /dev/null +++ b/src/client/main.cpp @@ -0,0 +1,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; +} |