From 44f772a7442e617d4a93c0b3ed318ffb42991d8b Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Fri, 21 Mar 2025 10:42:09 +0100 Subject: implement communication between server and client (without mutexes and with queue_length of 1) --- src/client/main.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/client/main.cpp (limited to 'src/client/main.cpp') 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 +#include +#include +#include +#include +#include + +void sendRequest( + SharedMemory* shared_memory, + Operations type, + std::pair 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; +} -- cgit 1.4.1