jay/jay.cpp

68 lines
1.8 KiB
C++

// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "jay.h"
#include "module/actor.h"
static const std::map<
std::string,
std::function<
std::shared_ptr<Module::Module>(
const std::shared_ptr<Core>&,
const std::shared_ptr<Connection>&,
const Module::Module::Permissions& permissions
)
>
> moduleNames = {
{"actor", [](
const std::shared_ptr<Core>& core,
const std::shared_ptr<Connection>& connection,
const Module::Module::Permissions& permissions
) { return std::make_shared<Module::Actor>(core, connection, permissions); }}
};
Jay::Jay(const std::string& configPath):
core(std::make_shared<Core>(configPath)),
connection(std::make_shared<Connection>(core)),
modules()
{}
Jay::~Jay() {
connection->deinitialize();
}
bool Jay::isConfigValid() const {
return core->config.isValid();
}
void Jay::run() {
initialize();
connection->connect();
}
void Jay::initialize() {
connection->initiialize();
createModules();
createActors();
}
void Jay::createActors() {
for (const std::pair<const std::string, std::string>& pair : core->config.getActors()) {
core->logger.log(Logger::info, "registering actor " + pair.first + " as " + pair.second, {"Jay"});
core->router.registerActor(pair.first, pair.second);
}
}
void Jay::createModules() {
for (const auto& pair : moduleNames) {
Config::Module conf = core->config.getModuleConfig(pair.first);
if (!conf.enabled)
continue;
core->logger.log(Logger::info, "enabling module " + pair.first, {"Jay"});
modules.emplace_back(pair.second(core, connection, conf.permissions));
core->router.registerModule(pair.first, modules.back());
}
}