Skip to content

Commit

Permalink
Add Service with typical message flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Sep 4, 2024
1 parent 091c77e commit 7cfff36
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 197 deletions.
90 changes: 0 additions & 90 deletions .vscode/settings.json

This file was deleted.

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(AM_SOURCE_FILES
common/Timers.hpp
AmApi.cpp
service/ConnectionsInfo.cpp
service/SilberService.cpp
)

IF (UNIX OR APPLE)
Expand Down
113 changes: 10 additions & 103 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
#include "sh_mem/ServerProcCommunicator.h"
#include "sh_mem/ClientProcCommunicator.h"
#include "service/ConnectionsInfo.h"
#include "service/SilberService.h"

const std::string shared_memory_name{"/_shmem1107"};
std::unique_ptr<ServerProcCommunicator> slave;
std::unique_ptr<am::service::SilberService> server;

void handleSignal(int signal)
{
std::cout << "Received SIGTERM signal (" << signal << "). Cleaning up and exiting...\n";
slave.reset();

if (server)
{
server->stop();
server.reset();
}
exit(0); // Exit the program with status code 0
}

Expand All @@ -42,105 +45,9 @@ int main(int argc, char *argv[])
}
#endif

slave = std::make_unique<ServerProcCommunicator>(shared_memory_name);
Configuration default_conf{75, 10, 1, 50, 5, 10.0};
std::unique_ptr<am::AmApi> amApi = std::make_unique<am::AmApi>(default_conf);

// confirm master connection by Handshake message, and then set configuration
// such order defines expected message flow
ConnectionsInfo connections;

bool isRunning = true;
while (isRunning)
{
Message *message = slave->receive();
printf("received %d message\n", message->type);
if (!connections.isRequestValid(message))
{
std::cout << "received UNEXPECTED_REQUEST req\n"
<< message->type << std::endl;
Message response{message->id, MessageType::UNEXPECTED_REQUEST};
slave->send(&response);
continue;
}
if (message->type == MessageType::HANDSHAKE)
{
std::cout << "received HANDSHAKE req\n";
Message msg{message->id, MessageType::HANDSHAKE_OK};
auto iter = connections.processActionUpdate(message);
slave->send(&msg);
}
else if (message->type == MessageType::SET_CONFIG)
{
MessageSetConfig *messageConf = static_cast<MessageSetConfig *>(message);
std::cout << "received SET_CONFIG req px:" << messageConf->configuration.MinPixelsForObject << std::endl;
Message msg{message->id, MessageType::SET_CONFIG_OK};
auto iter = connections.processActionUpdate(message);
Configuration newConf{messageConf->configuration.AffinityThreshold, messageConf->configuration.MinPixelsForObject, messageConf->configuration.PixelStep, messageConf->configuration.CalculationTimeLimit, messageConf->configuration.IdleTimeout, messageConf->configuration.ThreadsMultiplier};

amApi->setConfiguration(newConf);
std::cout << "received SET_CONFIG OK \n";
slave->send(&msg);
std::cout << "received SET_CONFIG OK sent\n";
}
else if (message->type == MessageType::COMPARE_REQUEST)
{
std::cout << "received COMPARE_REQUEST req\n";
auto iter = connections.processActionUpdate(message);
amApi->setConfiguration(iter->configuration);

MessageCompareRequest *messageCompare = static_cast<MessageCompareRequest *>(message);
if (messageCompare)
{
printf("compare_ %s %s _\n", messageCompare->base, messageCompare->to_compare);
auto iter = connections.processActionUpdate(message);
amApi->setConfiguration(iter->configuration); // set configuration for this client
printf("conf pixs %d\n", iter->configuration.MinPixelsForObject);
am::analyze::algorithm::DescObjects result;
try
{
result = amApi->compare(messageCompare->base, messageCompare->to_compare);
}
catch (const am::common::exceptions::AmException exc)
{
std::cout << "Exception has been caught: " << exc.what() << ::std::endl;
Message failed{messageCompare->id, MessageType::COMPARE_FAIL};
slave->send(&failed);
continue;
}
std::cout << "received after compare\n";
MessageCompareResult compare_result;
compare_result.id = messageCompare->id;

compare_result.payload_bytes = result.size() * sizeof(Rect);
Rect *rects = static_cast<Rect *>(compare_result.payload);
size_t id = 0;
for (auto &rectangle : result)
{
if (id == 100) // limit by 100 objects
{
compare_result.payload_bytes = 100 * sizeof(Rect);
break;
}
printf("_ %zd %zd %zd %zd _\n", rectangle.getLeft(), rectangle.getRight(), rectangle.getMaxHeight(), rectangle.getMinHeight());
rects[id++] = {rectangle.getLeft(), rectangle.getRight(), rectangle.getMaxHeight(), rectangle.getMinHeight()};
printf("_send %zd %zd %zd %zd _\n", rects[id - 1].l, rects[id - 1].r, rects[id - 1].t, rects[id - 1].b);
}
compare_result.type = MessageType::COMPARE_RESULT;
slave->send(&compare_result);
}
}
else if (message->type == MessageType::DISCONNECT)
{
std::cout << "received DISCONNECT req\n";
isRunning = false;
Message msg{message->id, MessageType::DISCONNECT};
auto iter = connections.processActionUpdate(message);

slave->send(&msg);
}
}

const std::string shared_memory_name{"/_shmem1107"};
server = std::make_unique<am::service::SilberService>(shared_memory_name);
server->start();
std::cout << "Aquamarine service performed all actions. Disconnect requested, exiting process..." << std::endl;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ scp -rp inputs/ build/inputs &&
echo "Image samples copied." &&
cd build/ &&
./aquamarine &&
echo "Execution of tests finished."
echo "Execution of 'aquamarine' program is finished."
4 changes: 2 additions & 2 deletions service/ConnectionsInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ bool ConnectionsInfo::isRequestValid(const Message *message)
{
return false;
}
else if (client_iterator->state == State::READY)
else if (client_iterator->state >= State::READY)
{
std::cerr << "Client was not properly configured. Use default configuration." << std::endl;
return client_iterator->state == State::CONFIGURED ? true : false;
}
}
std::cerr << "isRequestValid unknown state. If new Message type has been added - extend validation for it." << std::endl;
std::cerr << "isRequestValid unknown state. If new Message type has been added - extend validation for it." <<client_iterator->state<< std::endl;
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion service/IService.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace am
class IService
{
public:
virtual ~IService() = default;
~IService() = default;

virtual void start() = 0;
virtual void stop() = 0;
Expand Down
112 changes: 112 additions & 0 deletions service/SilberService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "SilberService.h"
#include "sh_mem/Message.hpp"

static Configuration default_conf{75, 10, 1, 50, 5, 10.0};
namespace am::service
{
SilberService::SilberService(const std::string &shMemName) : m_server(std::make_unique<ServerProcCommunicator>(shMemName)),
m_amApi(std::make_unique<am::AmApi>(default_conf)),
m_isRunning(false)

{
}

void SilberService::start()
{
m_isRunning = true;
while (m_isRunning)
{
Message *message = m_server->receive();
printf("received %d message\n", message->type);
if (!m_connections.isRequestValid(message))
{
std::cout << "received UNEXPECTED_REQUEST req\n"
<< message->type << std::endl;
Message response{message->id, MessageType::UNEXPECTED_REQUEST};
m_server->send(&response);
continue;
}
if (message->type == MessageType::HANDSHAKE)
{
std::cout << "received HANDSHAKE req\n";
Message msg{message->id, MessageType::HANDSHAKE_OK};
auto iter = m_connections.processActionUpdate(message);
m_server->send(&msg);
}
else if (message->type == MessageType::SET_CONFIG)
{
MessageSetConfig *messageConf = static_cast<MessageSetConfig *>(message);
std::cout << "received SET_CONFIG req px:" << messageConf->configuration.MinPixelsForObject << std::endl;
Message msg{message->id, MessageType::SET_CONFIG_OK};
auto iter = m_connections.processActionUpdate(message);
// Configuration newConf{messageConf->configuration.AffinityThreshold, messageConf->configuration.MinPixelsForObject, messageConf->configuration.PixelStep, messageConf->configuration.CalculationTimeLimit, messageConf->configuration.IdleTimeout, messageConf->configuration.ThreadsMultiplier};

m_amApi->setConfiguration(messageConf->configuration);
std::cout << "received SET_CONFIG OK \n";
m_server->send(&msg);
std::cout << "received SET_CONFIG OK sent\n";
}
else if (message->type == MessageType::COMPARE_REQUEST)
{
std::cout << "received COMPARE_REQUEST req\n";
auto iter = m_connections.processActionUpdate(message);
m_amApi->setConfiguration(iter->configuration);

MessageCompareRequest *messageCompare = static_cast<MessageCompareRequest *>(message);
if (messageCompare)
{
printf("compare_ %s %s _\n", messageCompare->base, messageCompare->to_compare);
auto iter = m_connections.processActionUpdate(message);
m_amApi->setConfiguration(iter->configuration); // set configuration for this client
printf("conf pixs %d\n", iter->configuration.MinPixelsForObject);
am::analyze::algorithm::DescObjects result;
try
{
result = m_amApi->compare(messageCompare->base, messageCompare->to_compare);
}
catch (const am::common::exceptions::AmException exc)
{
std::cout << "Exception has been caught: " << exc.what() << ::std::endl;
Message failed{messageCompare->id, MessageType::COMPARE_FAIL};
m_server->send(&failed);
continue;
}
std::cout << "received after compare\n";
MessageCompareResult compare_result;
compare_result.id = messageCompare->id;

compare_result.payload_bytes = result.size() * sizeof(Rect);
Rect *rects = static_cast<Rect *>(compare_result.payload);
size_t id = 0;
for (auto &rectangle : result)
{
if (id == 100) // limit by 100 objects
{
compare_result.payload_bytes = 100 * sizeof(Rect);
break;
}
printf("_ %zd %zd %zd %zd _\n", rectangle.getLeft(), rectangle.getRight(), rectangle.getMaxHeight(), rectangle.getMinHeight());
rects[id++] = {rectangle.getLeft(), rectangle.getRight(), rectangle.getMaxHeight(), rectangle.getMinHeight()};
printf("_send %zd %zd %zd %zd _\n", rects[id - 1].l, rects[id - 1].r, rects[id - 1].t, rects[id - 1].b);
}
compare_result.type = MessageType::COMPARE_RESULT;
m_server->send(&compare_result);
}
}
else if (message->type == MessageType::DISCONNECT)
{
std::cout << "received DISCONNECT req\n";
m_isRunning = false;
Message msg{message->id, MessageType::DISCONNECT};
auto iter = m_connections.processActionUpdate(message);

m_server->send(&msg);
}
}
};

void SilberService::stop()
{
m_isRunning = false;
}
}
Loading

0 comments on commit 7cfff36

Please sign in to comment.