Some more thoughts

This commit is contained in:
Blue 2025-03-03 21:36:02 +02:00
parent b997972ec1
commit bc2dc9bf07
Signed by: blue
GPG key ID: 9B203B252A63EE38
18 changed files with 146 additions and 42 deletions

View file

@ -2,12 +2,14 @@ set(SOURCES
config.cpp
logger.cpp
actor.cpp
router.cpp
)
set(HEADERS
config.h
logger.h
actor.h
router.h
)
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})

View file

@ -2,3 +2,11 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "actor.h"
Actor::Actor(const std::string& group) :
group(group)
{}
void Actor::setGroup(const std::string& newGroup) {
group = newGroup;
}

View file

@ -3,5 +3,14 @@
#pragma once
#include <string>
class Actor {
public:
Actor(const std::string& group);
void setGroup(const std::string& newGroup);
private:
std::string group;
};

View file

@ -46,14 +46,14 @@ gloox::TLSPolicy Config::getTLSPolicy() const {
}
std::set<std::string> Config::getOwners() const {
std::set<std::string> result;
YAML::Node owners = root["resource"];
if (!owners.IsSequence())
std::map<std::string, std::string> Config::getOwners() const {
std::map<std::string, std::string> result;
YAML::Node owners = root["actors"];
if (!owners.IsMap())
return result;
for (const YAML::Node& node : owners)
result.insert(node.as<std::string>());
for (const auto& node : owners)
result.emplace(node.first.as<std::string>(), node.second.as<std::string>());
return result;
}

View file

@ -4,7 +4,7 @@
#pragma once
#include <string>
#include <set>
#include <map>
#include "gloox/gloox.h"
@ -20,7 +20,7 @@ public:
std::string getFullJID() const;
std::string getPassword() const;
std::string getResource() const;
std::set<std::string> getOwners() const;
std::map<std::string, std::string> getOwners() const;
gloox::LogLevel getLogLevel() const;
gloox::TLSPolicy getTLSPolicy() const;

30
component/router.cpp Normal file
View file

@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "router.h"
Router::Router():
actors(),
defaultGroup("Stranger")
{}
void Router::registerModule(const std::string& key, const std::shared_ptr<Module::Module>& module) {
modules[key] = module;
}
void Router::registerActor(const std::string& key, const std::string& group) {
if (key == "default") {
defaultGroup = group;
return;
}
Actors::iterator act = actors.find(key);
if (act == actors.end())
actors.emplace(key, group);
else
act->second.setGroup(group);
}
void Router::routeMessage(const std::string& sender, const std::string& body) {
}

28
component/router.h Normal file
View file

@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <string>
#include <map>
#include <memory>
#include "actor.h"
#include "module/module.h"
class Router {
public:
Router();
void registerModule(const std::string& key, const std::shared_ptr<Module::Module>& module);
void registerActor(const std::string& key, const std::string& group);
void routeMessage(const std::string& sender, const std::string& body);
private:
typedef std::map<std::string, std::weak_ptr<Module::Module>> Modules;
typedef std::map<std::string, Actor> Actors;
Modules modules;
Actors actors;
std::string defaultGroup;
};