Skip to content

Commit

Permalink
Fix UT and Win build
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Sep 1, 2024
1 parent 77324e2 commit 9402fd5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
34 changes: 32 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#include "AmApi.h"

#include "sh_mem/ServerProcCommunicator.h"
#include "sh_mem/ClientProcCommunicator.h"
#include <algorithm>

#include <csignal>

enum State : size_t
{
UNKNOWN = 0,
Expand Down Expand Up @@ -95,12 +99,38 @@ struct ConnectionsInfo
std::unique_ptr<am::AmApi> amApi;
std::vector<ClientInfo> clients;
};
const std::string shared_memory_name{"/_shmem1107"};
std::unique_ptr<ServerProcCommunicator> slave;
void handleSignal(int signal)
{
std::cout << "Received SIGTERM signal (" << signal << "). Cleaning up and exiting...\n";
slave.reset();

exit(0); // Exit the program with status code 0
}

int main(int argc, char *argv[])
{
const std::string shared_memory_name{"/_shmem1103"};
#ifndef _WIN32
struct sigaction sa;
sa.sa_handler = handleSignal; // Set the handler function
sa.sa_flags = 0; // No special flags
sigemptyset(&sa.sa_mask); // No additional signals blocked during handler execution

// Set up handlers for SIGTERM and SIGINT
if (sigaction(SIGTERM, &sa, nullptr) == -1)
{
std::cerr << "Error setting up SIGTERM handler\n";
return 1;
}
if (sigaction(SIGINT, &sa, nullptr) == -1)
{
std::cerr << "Error setting up SIGINT handler\n";
return 1;
}
#endif
bool isStopRequested{false}, connectionConfirmed{false};
std::unique_ptr<ServerProcCommunicator> slave = std::make_unique<ServerProcCommunicator>(shared_memory_name);
slave = std::make_unique<ServerProcCommunicator>(shared_memory_name);
am::configuration::Configuration default_conf{75, 10, 1, 50, 5, 10.0};
std::unique_ptr<am::AmApi> amApi = std::make_unique<am::AmApi>(default_conf);

Expand Down
2 changes: 0 additions & 2 deletions sh_mem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ ADD_LIBRARY(shared_mem STATIC
ClientProcCommunicator.cpp
ServerProcCommunicator.cpp)

add_executable(sh_mem_test main.cpp)
target_link_libraries(sh_mem_test shared_mem)
15 changes: 9 additions & 6 deletions sh_mem/ClientProcCommunicator.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include "ClientProcCommunicator.h"


ClientProcCommunicator::ClientProcCommunicator(
const std::string &shMemName) : ProcCommunicator(shMemName)
{
printf("1 \n");
m_sender = std::make_unique<SharedMemorySender>( m_master_mem_name.c_str());
printf("2 \n");
m_receiver = std::make_unique<SharedMemoryReceiver>(m_slave_mem_name.c_str());

printf("3 \n");
#ifndef _WIN32
m_master_received = sem_open(m_master_received_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_received = sem_open(m_slave_received_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_master_sent = sem_open(m_master_sent_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_sent = sem_open(m_slave_sent_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_ready = sem_open(m_slave_ready_s.c_str(), O_CREAT, 0666, SEMAPHORE_ENABLED);

printf("4 \n");
if (m_master_received == SEM_FAILED || m_slave_received == SEM_FAILED ||
m_master_sent == SEM_FAILED || m_slave_sent == SEM_FAILED || m_slave_ready == SEM_FAILED || m_slave_ready == SEM_FAILED)
{
Expand All @@ -22,11 +25,11 @@ ClientProcCommunicator::ClientProcCommunicator(
#else
std::wstring wshMemName(shMemName.begin(), shMemName.end());

m_master_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_m_rsem").c_str()));
m_slave_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_rsem").c_str()));
m_master_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_m_rsem").c_str());
m_slave_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_rsem").c_str());
m_master_sent = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_m_sent").c_str());
m_slave_sent = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_sent").c_str()));
m_slave_ready = CreateSemaphoreW(NULL, SEMAPHORE_ENABLED, MAXLONG, (wshMemName + L"_s_ready").c_str()));
m_slave_sent = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_sent").c_str());
m_slave_ready = CreateSemaphoreW(NULL, SEMAPHORE_ENABLED, MAXLONG, (wshMemName + L"_s_ready").c_str());

if (m_master_received == NULL || m_slave_received == NULL ||
m_master_sent == NULL || m_slave_sent == NULL || m_slave_ready == NULL)
Expand Down
3 changes: 3 additions & 0 deletions sh_mem/ProcCommunicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "Message.hpp"
#ifndef _WIN32
#include <semaphore.h>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#else
#include <windows.h>
#endif
Expand Down
11 changes: 7 additions & 4 deletions sh_mem/ServerProcCommunicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ ServerProcCommunicator::ServerProcCommunicator(

std::wstring wshMemName(shMemName.begin(), shMemName.end());

m_master_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_m_rsem").c_str()));
m_slave_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_rsem").c_str()));
m_master_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_m_rsem").c_str());
m_slave_received = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_rsem").c_str());
m_master_sent = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_m_sent").c_str());
m_slave_sent = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_sent").c_str()));
m_slave_ready = CreateSemaphoreW(NULL, SEMAPHORE_ENABLED, MAXLONG, (wshMemName + L"_s_ready").c_str()));
m_slave_sent = CreateSemaphoreW(NULL, SEMAPHORE_DISABLED, MAXLONG, (wshMemName + L"_s_sent").c_str());
m_slave_ready = CreateSemaphoreW(NULL, SEMAPHORE_ENABLED, MAXLONG, (wshMemName + L"_s_ready").c_str());

if (m_master_received == NULL || m_slave_received == NULL ||
m_master_sent == NULL || m_slave_sent == NULL || m_slave_ready == NULL)
Expand All @@ -38,9 +38,11 @@ ServerProcCommunicator::ServerProcCommunicator(
}

#endif

}
ServerProcCommunicator::~ServerProcCommunicator()
{
#ifndef _WIN32
if (sem_unlink(m_master_received_s.c_str()) == -1)
{
perror("Failed to unlink m_master_received semaphore");
Expand All @@ -65,6 +67,7 @@ ServerProcCommunicator::~ServerProcCommunicator()
{
perror("Failed to unlink m_slave_ready semaphore");
}
#endif
}
#ifndef _WIN32

Expand Down
31 changes: 16 additions & 15 deletions unit_tests/MessagingTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ProcCommunicator.h"
#include "ServerProcCommunicator.h"
#include "ClientProcCommunicator.h"
#include <iostream>
#include <thread>
#include <chrono>
Expand All @@ -10,17 +11,17 @@ static const std::string shared_mem_name{"/shmem_test"};
void backgroundTask()
{
std::cout << "Background task starts.\n";
Message msg_hand{2, MessageType::HANDSHAKE};
ProcCommunicator master(true, true, shared_mem_name);
Message request{1, MessageType::HANDSHAKE};
Message response{666, MessageType::HANDSHAKE};
ClientProcCommunicator master(shared_mem_name);
int counter = 0;

while (counter < 10)
{
//each client must receive its id
master.send(&msg_hand);
auto msg_resp = master.receive();
EXPECT_EQ(msg_resp->id, 2);
master.ackNotify();
master.sendRequestGetResponse(&request, response);
EXPECT_EQ(response.id, 1);
EXPECT_EQ(response.type, MessageType::HANDSHAKE_OK);
counter++;
}

Expand All @@ -30,17 +31,17 @@ void backgroundTask()
void backgroundTaskMasterMaster()
{
std::cout << "Background Master task started...\n";
ProcCommunicator master(true, true, shared_mem_name);
Message msg_hand{1, MessageType::HANDSHAKE};

ClientProcCommunicator master( shared_mem_name);
Message request{2, MessageType::HANDSHAKE};
Message response{666, MessageType::HANDSHAKE};
int counter = 0;

while (counter < 10)
{
master.send(&msg_hand);
auto msg_resp = master.receive();
EXPECT_EQ(msg_resp->id, 1);
master.ackNotify();
//each client must receive its id
master.sendRequestGetResponse(&request, response);
EXPECT_EQ(response.id, 2);
EXPECT_EQ(response.type, MessageType::HANDSHAKE_OK);
counter++;
}

Expand All @@ -49,7 +50,7 @@ void backgroundTaskMasterMaster()
TEST(MessagingTest, TestMultiMasterMode)
{
std::cout << "Server starts...\n";
ProcCommunicator *slave = new ProcCommunicator(false, true, shared_mem_name);
ServerProcCommunicator *slave = new ServerProcCommunicator(shared_mem_name);
Message *res = nullptr;

int counter = 0;
Expand Down

0 comments on commit 9402fd5

Please sign in to comment.