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;
};

View File

@ -3,6 +3,7 @@ password: "supersecret"
logLevel: debug
tls: required
resource: bot
owners:
- user1@xmpp.server
- user2@xmpp.server
actors:
user1@xmpp.server: Owner
user2@xmpp.server: User
default: Stranger

View File

@ -5,7 +5,8 @@
#include <iostream>
Message::Message(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client):
Message::Message(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client, const Callback& callback):
callback(callback),
config(config),
client(client)
{

View File

@ -3,7 +3,9 @@
#pragma once
#include <string>
#include <memory>
#include <functional>
#include <gloox/client.h>
#include <gloox/messagehandler.h>
@ -12,13 +14,17 @@
#include "component/config.h"
class Message : public gloox::MessageHandler {
private:
typedef std::function<void (const std::string&, const std::string&)> Callback;
public:
Message(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client);
Message(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client, const Callback& callback);
~Message();
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
private:
Callback callback;
std::weak_ptr<Config> config;
std::weak_ptr<gloox::Client> client;
};

19
jay.cpp
View File

@ -6,11 +6,11 @@
Jay::Jay(const std::string& configPath):
runMutex(),
config(std::make_shared<Config>(configPath)),
router(std::make_unique<Router>()),
client(),
messageHandler(),
connectionHandler(),
loggers(),
actors()
loggers()
{}
Jay::~Jay() {}
@ -31,10 +31,16 @@ void Jay::initialize() {
createClient();
if (!messageHandler)
messageHandler = std::make_unique<Message>(config, client);
messageHandler = std::make_unique<Message>(
config,
client,
std::bind(&Router::routeMessage, router.get(), std::placeholders::_1, std::placeholders::_2)
);
if (!connectionHandler)
connectionHandler = std::make_unique<Connection>(config, client);
createActors();
}
void Jay::createClient() {
@ -55,11 +61,8 @@ void Jay::createClient() {
}
void Jay::createActors() {
for (const std::string& jid : config->getOwners()) {
Actors::const_iterator act = actors.find(jid);
if (act == actors.end())
actors.emplace(jid, std::make_unique<Actor>());
}
for (const std::pair<const std::string, std::string>& pair : config->getOwners())
router->registerActor(pair.first, pair.second);
}

6
jay.h
View File

@ -15,7 +15,7 @@
#include "component/logger.h"
#include "component/config.h"
#include "component/actor.h"
#include "component/router.h"
#include "handler/message.h"
#include "handler/connection.h"
@ -35,13 +35,11 @@ private:
void createActors();
private:
typedef std::map<std::string, std::unique_ptr<Actor>> Actors;
std::mutex runMutex;
std::shared_ptr<Config> config;
std::unique_ptr<Router> router;
std::shared_ptr<gloox::Client> client;
std::unique_ptr<Message> messageHandler;
std::unique_ptr<Connection> connectionHandler;
std::vector<std::unique_ptr<Logger>> loggers;
Actors actors;
};

View File

@ -1,11 +1,11 @@
set(SOURCES
module.cpp
owner.cpp
actor.cpp
)
set(HEADERS
module.h
owner.h
actor.h
)
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})

14
module/actor.cpp Normal file
View File

@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "actor.h"
Module::Actor::Actor():
Module()
{}
Module::Actor::~Actor() noexcept {}
void Module::Actor::message(const std::string& text) {
}

View File

@ -5,8 +5,14 @@
#include "module.h"
class Owner : public Module {
namespace Module {
class Actor : public Module {
public:
Owner();
~Owner() noexcept;
Actor();
~Actor() noexcept;
virtual void message(const std::string & text) override;
};
}

View File

@ -3,7 +3,7 @@
#include "module.h"
Module::Module()
Module::Module::Module()
{}
Module::~Module() noexcept {}
Module::Module::~Module() noexcept {}

View File

@ -3,10 +3,18 @@
#pragma once
#include <string>
namespace Module {
class Module {
protected:
Module();
virtual void message(const std::string& text) = 0;
public:
virtual ~Module() noexcept;
};
}

View File

@ -1,10 +0,0 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "owner.h"
Owner::Owner():
Module()
{}
Owner::~Owner() noexcept {}