Skip to content

Commit

Permalink
Make Configuration as a part of interface type
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Sep 4, 2024
1 parent 9402fd5 commit 84ebf49
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 151 deletions.
10 changes: 5 additions & 5 deletions AmApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace am
using namespace common::types;
using namespace analyze;

AmApi::AmApi(const configuration::Configuration &conf) : loggerPtr(std::make_shared<am::common::Logger>("log.log")),
AmApi::AmApi(const Configuration &conf) : loggerPtr(std::make_shared<am::common::Logger>("log.log")),
extractor(loggerPtr)
{
am::configuration::ConfigurationReader reader;
setConfiguration(conf);
configuration::ConfigurationReader reader;
setConfiguration(conf);
}

algorithm::DescObjects AmApi::compare(const std::string &base_img, const std::string &cmp_img)
Expand Down Expand Up @@ -59,12 +59,12 @@ namespace am
#endif
}

const configuration::Configuration &AmApi::getConfiguration()
const Configuration &AmApi::getConfiguration()
{
return configuration;
}

void AmApi::setConfiguration(const configuration::Configuration &newConf)
void AmApi::setConfiguration(const Configuration &newConf)
{
configuration = newConf;
const size_t opt_threads = am::common::getOptimalThreadsCount(configuration.ThreadsMultiplier);
Expand Down
8 changes: 4 additions & 4 deletions AmApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace am
class AmApi
{
public:
AmApi(const configuration::Configuration& conf);
AmApi(const Configuration& conf);
analyze::algorithm::DescObjects compare(const std::string &base_img, const std::string &cmp_img);
void compare_and_save_diff_img(const std::string &base_img, const std::string &cmp_img, std::string &&out_diff_img);
void enable_database_reports(const char *db_name);
const configuration::Configuration& getConfiguration();
void setConfiguration(const configuration::Configuration& newConf);
const Configuration& getConfiguration();
void setConfiguration(const Configuration& newConf);

private:
std::shared_ptr<am::common::Logger> loggerPtr;
Expand All @@ -32,6 +32,6 @@ namespace am
#if defined __unix__ || defined __APPLE__
std::unique_ptr<database::DataBaseCommunicator> dbcPtr;
#endif
configuration::Configuration configuration;
Configuration configuration;
};
}
92 changes: 92 additions & 0 deletions ConnectionsInfo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once
#include <memory>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#include "sh_mem/Message.hpp"

enum State : size_t
{
UNKNOWN = 0,
READY,
CONFIGURED
};
struct ClientInfo
{
size_t id;
State state;
Configuration configuration;
};
struct ConnectionsInfo
{
bool isRequestValid(const Message *message)
{
std::cerr << "isRequestValid" << message->id << message->type << std::endl;
auto client_iterator = std::find_if(clients.begin(), clients.end(), [&](const ClientInfo &c)
{ return c.id == message->id; });
if (client_iterator == clients.end())
{
std::cerr << "isRequestValid 1" << std::endl;
// add clinet with default configuration
clients.push_back({message->id, State::UNKNOWN, {75, 10, 1, 50, 5, 10.0}});
client_iterator = std::find_if(clients.begin(), clients.end(), [&](const ClientInfo &c)
{ return c.id == message->id; });
}
if (message->type == MessageType::HANDSHAKE || message->type == MessageType::DISCONNECT)
{
std::cerr << "isRequestValid HANDSHAKE OK" << std::endl;
return true;
}
else if (message->type == MessageType::SET_CONFIG)
{
std::cerr << "isRequestValid SET_CONFIG " << std::endl;
return client_iterator->state >= State::READY ? true : false;
}
else if (message->type == MessageType::COMPARE_REQUEST)
{
std::cerr << "isRequestValid COMPARE_REQUEST " << std::endl;
if (client_iterator->state == State::UNKNOWN)
return false;
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::vector<ClientInfo>::const_iterator processActionUpdate(const Message *msg)
{
auto client_iterator = std::find_if(clients.begin(), clients.end(), [&](const ClientInfo &c)
{ return c.id == msg->id; });
if (client_iterator == clients.end())
{
std::cerr << "Unknown client" << std::endl;
return client_iterator;
}

if (msg->type == MessageType::HANDSHAKE)
client_iterator->state = State::READY;
else if (msg->type == MessageType::SET_CONFIG)
{
const MessageSetConfig *messageSetConfig = static_cast<const MessageSetConfig *>(msg);
client_iterator->state = State::CONFIGURED;
std::memcpy(&client_iterator->configuration, &messageSetConfig->configuration, sizeof(Configuration));
//printf("set_conf id:%zu {%d %f %d %d %zd %f}\n", msg->id, client_iterator->configuration.AffinityThreshold,
//client_iterator->configuration.CalculationTimeLimit, client_iterator->configuration.IdleTimeout,
//client_iterator->configuration.MinPixelsForObject, client_iterator->configuration.PixelStep, client_iterator->configuration.ThreadsMultiplier);
}
else if (msg->type == MessageType::DISCONNECT)
{
client_iterator->state = State::UNKNOWN;
}
else
{
std::cout << "No updates" << std::endl;
}
return client_iterator;
}

std::unique_ptr<ServerProcCommunicator> commuicator;
std::unique_ptr<am::AmApi> amApi;
std::vector<ClientInfo> clients;
};
2 changes: 1 addition & 1 deletion analyze/algorithm/BfsObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace am::analyze::algorithm

BfsObjectDetector::BfsObjectDetector(
const size_t threads,
const am::configuration::Configuration &conf,
const Configuration &conf,
std::shared_ptr<am::common::Logger> &logger)
: mThreadsCount(threads), mConfiguration(conf), mLogger(logger) {}

Expand Down
6 changes: 3 additions & 3 deletions analyze/algorithm/BfsObjectDetector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "IObjectDetector.h"
#include "configuration/Configuration.hpp"
#include "sh_mem/Message.hpp"
#include "common/Logger.hpp"
#include "Object.h"

Expand Down Expand Up @@ -33,11 +33,11 @@ namespace am::analyze::algorithm

protected:
// abstract class construction limited, aggregate common functions and interface requirement
BfsObjectDetector(const size_t threads, const am::configuration::Configuration &conf,
BfsObjectDetector(const size_t threads, const Configuration &conf,
std::shared_ptr<am::common::Logger> &logger);

size_t mThreadsCount;
am::configuration::Configuration mConfiguration;
Configuration mConfiguration;
std::shared_ptr<am::common::Logger> mLogger;
};
} // namespace am::analyze::algorithm
4 changes: 2 additions & 2 deletions analyze/algorithm/DiffObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace am::analyze::algorithm
return object;
}

static std::vector<ObjectRectangle> startObjectsSearch(MatrixU16 &changes, const am::configuration::Configuration& conf, const ImageRowSegment &row)
static std::vector<ObjectRectangle> startObjectsSearch(MatrixU16 &changes, const Configuration& conf, const ImageRowSegment &row)
{
std::vector<ObjectRectangle> result;
const size_t step = conf.PixelStep;
Expand All @@ -63,7 +63,7 @@ namespace am::analyze::algorithm
}
}

DiffObjectDetector::DiffObjectDetector(const size_t threads, const am::configuration::Configuration &conf, std::shared_ptr<am::common::Logger> &logger) : BfsObjectDetector(threads, conf, logger)
DiffObjectDetector::DiffObjectDetector(const size_t threads, const Configuration &conf, std::shared_ptr<am::common::Logger> &logger) : BfsObjectDetector(threads, conf, logger)
{
}

Expand Down
2 changes: 1 addition & 1 deletion analyze/algorithm/DiffObjectDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace am::analyze::algorithm
{
public:
DiffObjectDetector(const size_t threads,
const am::configuration::Configuration &conf,
const Configuration &conf,
std::shared_ptr<am::common::Logger> &logger);
~DiffObjectDetector() = default;

Expand Down
6 changes: 3 additions & 3 deletions analyze/algorithm/ObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace am::analyze::algorithm

ObjectDetector::ObjectDetector(
const size_t threads,
const am::configuration::Configuration &conf,
const Configuration &conf,
std::shared_ptr<am::common::Logger> &logger)
: BfsObjectDetector(threads, conf, logger) {}

Expand All @@ -21,7 +21,7 @@ namespace am::analyze::algorithm
ObjectRectangle bfs(const ImagePair &pair, MatrixU16 &visited, Pixels &toCheck,
ObjectRectangle &object, ImageRowSegment row,
std::chrono::steady_clock::time_point &startTime,
const configuration::Configuration &conf)
const Configuration &conf)
{
auto timeNow = std::chrono::steady_clock::now();
std::chrono::duration<double> calcDuration = timeNow - startTime;
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace am::analyze::algorithm

static std::vector<ObjectRectangle>
startObjectsSearchInPair(const ImagePair &pair, const ImageRowSegment &row,
const configuration::Configuration &conf)
const Configuration &conf)
{
auto startTime = std::chrono::steady_clock::now();
MatrixU16 changes(pair.getWidth(), pair.getHeight());
Expand Down
5 changes: 3 additions & 2 deletions analyze/algorithm/ObjectDetector.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once

#include "BfsObjectDetector.h"
#include "sh_mem/Message.hpp"

namespace am::analyze::algorithm
{

ObjectRectangle bfs(const ImagePair &pair, common::types::MatrixU16 &visited,
std::vector<Pixel> &toCheck, ObjectRectangle &object,
ImageRowSegment row, std::chrono::steady_clock::time_point &startTime,
const configuration::Configuration &conf);
const Configuration &conf);

class ObjectDetector : public BfsObjectDetector
{
public:
ObjectDetector(const size_t threads, const am::configuration::Configuration &conf,
ObjectDetector(const size_t threads, const Configuration &conf,
std::shared_ptr<am::common::Logger> &logger);
~ObjectDetector() = default;

Expand Down
2 changes: 1 addition & 1 deletion analyze/algorithm/movement/MovementDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace am::analyze::algorithm::movement
}

MovementDetector::MovementDetector(const size_t threads,
const am::configuration::Configuration &conf,
const Configuration &conf,
std::shared_ptr<am::common::Logger> &logger)
: ObjectDetector(threads, conf, logger)
{
Expand Down
2 changes: 1 addition & 1 deletion analyze/algorithm/movement/MovementDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace am::analyze::algorithm::movement
class MovementDetector : public ObjectDetector, IMovementDetector
{
public:
MovementDetector(const size_t threads, const am::configuration::Configuration &conf, std::shared_ptr<am::common::Logger> &logger);
MovementDetector(const size_t threads, const Configuration &conf, std::shared_ptr<am::common::Logger> &logger);
~MovementDetector() = default;

// set objects which will be used for BFS search, like stencil on whole Image
Expand Down
15 changes: 0 additions & 15 deletions configuration/Configuration.hpp

This file was deleted.

10 changes: 5 additions & 5 deletions configuration/ConfigurationReader.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#pragma once

#include "Configuration.hpp"
#include <fstream>
#include <sstream>
#include <vector>
#include <memory>
#include "common/exceptions/FileAccessException.hpp"
#include "common/exceptions/AmException.hpp"
#include "analyze/algorithm/ObjectDetector.h"
#include <sh_mem/Message.hpp>

namespace
{
const char PARAM_LIMITER = ':';
size_t PARAM_COUNT = 6;
}

namespace am::configuration
namespace configuration
{
class ConfigurationReader
{
Expand All @@ -30,7 +30,7 @@ namespace am::configuration
if (!file.is_open())
{
std::string errorMsg("Configuration file access failed!");
throw common::exceptions::FileAccessException(errorMsg);
throw am::common::exceptions::FileAccessException(errorMsg);
}

while (file.good())
Expand All @@ -41,7 +41,7 @@ namespace am::configuration
}

std::string errorMsg("Configuration parse failed, check file parameters!");
throw common::exceptions::AmException(errorMsg);
throw am::common::exceptions::AmException(errorMsg);
}

private:
Expand Down Expand Up @@ -77,4 +77,4 @@ namespace am::configuration
}
}
};
} // namespace am::configuration
} // namespace Configuration
Loading

0 comments on commit 84ebf49

Please sign in to comment.