Skip to content

Commit

Permalink
Hide declaration of SpikeMessageHandler: #23
Browse files Browse the repository at this point in the history
  • Loading branch information
a-vartenkov committed Dec 4, 2024
1 parent 1b9635f commit 636fd86
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 53 deletions.
76 changes: 60 additions & 16 deletions knp/base-framework/impl/model_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,62 @@
namespace knp::framework
{


class ModelExecutor::SpikeMessageHandler
{
public:
using MessageIn = knp::core::messaging::SpikeMessage;
using MessageData = knp::core::messaging::SpikeData;
using FunctionType = std::function<MessageData(std::vector<MessageIn> &)>;

SpikeMessageHandler(FunctionType &&function, knp::core::MessageEndpoint &&endpoint, const knp::core::UID &uid = {})
: message_handler_function_(std::move(function)), endpoint_(std::move(endpoint)), base_{uid}
{
}

SpikeMessageHandler(SpikeMessageHandler &&other) noexcept = default;

SpikeMessageHandler(const SpikeMessageHandler &) = delete;

void subscribe(const std::vector<core::UID> &entities) { endpoint_.subscribe<MessageIn>(base_.uid_, entities); }

void update(size_t step);

[[nodiscard]] knp::core::UID get_uid() const { return base_.uid_; };

~SpikeMessageHandler() = default;

private:
FunctionType message_handler_function_;
knp::core::MessageEndpoint endpoint_;
knp::core::BaseData base_;
};


void ModelExecutor::SpikeMessageHandler::update(size_t step)
{
endpoint_.receive_all_messages();
auto incoming_messages = endpoint_.unload_messages<MessageIn>(base_.uid_);
knp::core::messaging::SpikeMessage outgoing_message = {
{base_.uid_, step}, message_handler_function_(incoming_messages)};
if (!(outgoing_message.neuron_indexes_.empty()))
{
endpoint_.send_message(outgoing_message);
}
}


ModelExecutor::ModelExecutor(
knp::framework::Model &model, std::shared_ptr<core::Backend> backend, ModelLoader::InputChannelMap i_map)
: loader_(backend, i_map)
{
loader_.load(model);
}


ModelExecutor::~ModelExecutor() = default;


void ModelExecutor::start()
{
start([](knp::core::Step) { return true; });
Expand Down Expand Up @@ -58,7 +114,7 @@ void ModelExecutor::start(core::Backend::RunPredicate run_predicate)
// Running handlers
for (auto &handler : message_handlers_)
{
handler.update(get_backend()->get_step());
handler->update(get_backend()->get_step());
}
// Run monitoring observers.
for (auto &observer : observers_)
Expand All @@ -78,26 +134,14 @@ void ModelExecutor::stop()
}


void ModelExecutor::SpikeMessageHandler::update(size_t step)
{
endpoint_.receive_all_messages();
auto incoming_messages = endpoint_.unload_messages<MessageIn>(base_.uid_);
knp::core::messaging::SpikeMessage outgoing_message = {
{base_.uid_, step}, message_handler_function_(incoming_messages)};
if (!(outgoing_message.neuron_indexes_.empty()))
{
endpoint_.send_message(outgoing_message);
}
}


void ModelExecutor::add_spike_message_handler(
typename SpikeMessageHandler::FunctionType &&message_handler_function, const std::vector<core::UID> &senders,
const std::vector<core::UID> &receivers, const knp::core::UID &uid)
{
knp::core::MessageEndpoint endpoint = get_backend()->get_message_bus().create_endpoint();
message_handlers_.emplace_back(SpikeMessageHandler{std::move(message_handler_function), std::move(endpoint), uid});
message_handlers_.back().subscribe(senders);
message_handlers_.emplace_back(
std::make_unique<SpikeMessageHandler>(std::move(message_handler_function), std::move(endpoint), uid));
message_handlers_.back()->subscribe(senders);
for (const knp::core::UID &rec_uid : receivers)
{
get_backend()->subscribe<knp::core::messaging::SpikeMessage>(rec_uid, {uid});
Expand Down
45 changes: 8 additions & 37 deletions knp/base-framework/include/knp/framework/model_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ class KNP_DECLSPEC ModelExecutor
* @param i_map input channel map.
*/
ModelExecutor(
knp::framework::Model &model, std::shared_ptr<core::Backend> backend, ModelLoader::InputChannelMap i_map)
: loader_(backend, i_map)
{
loader_.load(model);
}
knp::framework::Model &model, std::shared_ptr<core::Backend> backend, ModelLoader::InputChannelMap i_map);

/**
* @brief ModelExecutor destructor.
*/
~ModelExecutor();

public:
/**
Expand Down Expand Up @@ -134,42 +135,12 @@ class KNP_DECLSPEC ModelExecutor
auto &get_loader() { return loader_; }

private:
/**
* @brief An object that receives and processes messages.
*/
class SpikeMessageHandler
{
public:
using MessageIn = knp::core::messaging::SpikeMessage;
using MessageData = knp::core::messaging::SpikeData;
using FunctionType = std::function<MessageData(std::vector<MessageIn> &)>;

SpikeMessageHandler(
FunctionType &&function, knp::core::MessageEndpoint &&endpoint, const knp::core::UID &uid = {})
: message_handler_function_(std::move(function)), endpoint_(std::move(endpoint)), base_{uid}
{
}

SpikeMessageHandler(SpikeMessageHandler &&other) noexcept = default;

SpikeMessageHandler(const SpikeMessageHandler &) = delete;

void subscribe(const std::vector<core::UID> &entities) { endpoint_.subscribe<MessageIn>(base_.uid_, entities); }

void update(size_t step);

[[nodiscard]] knp::core::UID get_uid() const { return base_.uid_; };

private:
FunctionType message_handler_function_;
core::MessageEndpoint endpoint_;
knp::core::BaseData base_;
};
class SpikeMessageHandler;

knp::core::BaseData base_;
ModelLoader loader_;

std::vector<monitoring::AnyObserverVariant> observers_;
std::vector<SpikeMessageHandler> message_handlers_;
std::vector<std::unique_ptr<SpikeMessageHandler>> message_handlers_;
};
} // namespace knp::framework

0 comments on commit 636fd86

Please sign in to comment.