Some more thoughts
This commit is contained in:
parent
b997972ec1
commit
bc2dc9bf07
@ -2,12 +2,14 @@ set(SOURCES
|
|||||||
config.cpp
|
config.cpp
|
||||||
logger.cpp
|
logger.cpp
|
||||||
actor.cpp
|
actor.cpp
|
||||||
|
router.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
config.h
|
config.h
|
||||||
logger.h
|
logger.h
|
||||||
actor.h
|
actor.h
|
||||||
|
router.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})
|
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})
|
||||||
|
@ -2,3 +2,11 @@
|
|||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
#include "actor.h"
|
#include "actor.h"
|
||||||
|
|
||||||
|
Actor::Actor(const std::string& group) :
|
||||||
|
group(group)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void Actor::setGroup(const std::string& newGroup) {
|
||||||
|
group = newGroup;
|
||||||
|
}
|
||||||
|
@ -3,5 +3,14 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class Actor {
|
class Actor {
|
||||||
|
public:
|
||||||
|
Actor(const std::string& group);
|
||||||
|
|
||||||
|
void setGroup(const std::string& newGroup);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string group;
|
||||||
};
|
};
|
||||||
|
@ -46,14 +46,14 @@ gloox::TLSPolicy Config::getTLSPolicy() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::set<std::string> Config::getOwners() const {
|
std::map<std::string, std::string> Config::getOwners() const {
|
||||||
std::set<std::string> result;
|
std::map<std::string, std::string> result;
|
||||||
YAML::Node owners = root["resource"];
|
YAML::Node owners = root["actors"];
|
||||||
if (!owners.IsSequence())
|
if (!owners.IsMap())
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
for (const YAML::Node& node : owners)
|
for (const auto& node : owners)
|
||||||
result.insert(node.as<std::string>());
|
result.emplace(node.first.as<std::string>(), node.second.as<std::string>());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <map>
|
||||||
|
|
||||||
#include "gloox/gloox.h"
|
#include "gloox/gloox.h"
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ public:
|
|||||||
std::string getFullJID() const;
|
std::string getFullJID() const;
|
||||||
std::string getPassword() const;
|
std::string getPassword() const;
|
||||||
std::string getResource() const;
|
std::string getResource() const;
|
||||||
std::set<std::string> getOwners() const;
|
std::map<std::string, std::string> getOwners() const;
|
||||||
gloox::LogLevel getLogLevel() const;
|
gloox::LogLevel getLogLevel() const;
|
||||||
gloox::TLSPolicy getTLSPolicy() const;
|
gloox::TLSPolicy getTLSPolicy() const;
|
||||||
|
|
||||||
|
30
component/router.cpp
Normal file
30
component/router.cpp
Normal 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
28
component/router.h
Normal 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;
|
||||||
|
};
|
@ -3,6 +3,7 @@ password: "supersecret"
|
|||||||
logLevel: debug
|
logLevel: debug
|
||||||
tls: required
|
tls: required
|
||||||
resource: bot
|
resource: bot
|
||||||
owners:
|
actors:
|
||||||
- user1@xmpp.server
|
user1@xmpp.server: Owner
|
||||||
- user2@xmpp.server
|
user2@xmpp.server: User
|
||||||
|
default: Stranger
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#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),
|
config(config),
|
||||||
client(client)
|
client(client)
|
||||||
{
|
{
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <gloox/client.h>
|
#include <gloox/client.h>
|
||||||
#include <gloox/messagehandler.h>
|
#include <gloox/messagehandler.h>
|
||||||
@ -12,13 +14,17 @@
|
|||||||
#include "component/config.h"
|
#include "component/config.h"
|
||||||
|
|
||||||
class Message : public gloox::MessageHandler {
|
class Message : public gloox::MessageHandler {
|
||||||
|
private:
|
||||||
|
typedef std::function<void (const std::string&, const std::string&)> Callback;
|
||||||
|
|
||||||
public:
|
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();
|
~Message();
|
||||||
|
|
||||||
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
|
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Callback callback;
|
||||||
std::weak_ptr<Config> config;
|
std::weak_ptr<Config> config;
|
||||||
std::weak_ptr<gloox::Client> client;
|
std::weak_ptr<gloox::Client> client;
|
||||||
};
|
};
|
||||||
|
19
jay.cpp
19
jay.cpp
@ -6,11 +6,11 @@
|
|||||||
Jay::Jay(const std::string& configPath):
|
Jay::Jay(const std::string& configPath):
|
||||||
runMutex(),
|
runMutex(),
|
||||||
config(std::make_shared<Config>(configPath)),
|
config(std::make_shared<Config>(configPath)),
|
||||||
|
router(std::make_unique<Router>()),
|
||||||
client(),
|
client(),
|
||||||
messageHandler(),
|
messageHandler(),
|
||||||
connectionHandler(),
|
connectionHandler(),
|
||||||
loggers(),
|
loggers()
|
||||||
actors()
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Jay::~Jay() {}
|
Jay::~Jay() {}
|
||||||
@ -31,10 +31,16 @@ void Jay::initialize() {
|
|||||||
createClient();
|
createClient();
|
||||||
|
|
||||||
if (!messageHandler)
|
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)
|
if (!connectionHandler)
|
||||||
connectionHandler = std::make_unique<Connection>(config, client);
|
connectionHandler = std::make_unique<Connection>(config, client);
|
||||||
|
|
||||||
|
createActors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Jay::createClient() {
|
void Jay::createClient() {
|
||||||
@ -55,11 +61,8 @@ void Jay::createClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Jay::createActors() {
|
void Jay::createActors() {
|
||||||
for (const std::string& jid : config->getOwners()) {
|
for (const std::pair<const std::string, std::string>& pair : config->getOwners())
|
||||||
Actors::const_iterator act = actors.find(jid);
|
router->registerActor(pair.first, pair.second);
|
||||||
if (act == actors.end())
|
|
||||||
actors.emplace(jid, std::make_unique<Actor>());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
6
jay.h
6
jay.h
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
#include "component/logger.h"
|
#include "component/logger.h"
|
||||||
#include "component/config.h"
|
#include "component/config.h"
|
||||||
#include "component/actor.h"
|
#include "component/router.h"
|
||||||
#include "handler/message.h"
|
#include "handler/message.h"
|
||||||
#include "handler/connection.h"
|
#include "handler/connection.h"
|
||||||
|
|
||||||
@ -35,13 +35,11 @@ private:
|
|||||||
void createActors();
|
void createActors();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::map<std::string, std::unique_ptr<Actor>> Actors;
|
|
||||||
|
|
||||||
std::mutex runMutex;
|
std::mutex runMutex;
|
||||||
std::shared_ptr<Config> config;
|
std::shared_ptr<Config> config;
|
||||||
|
std::unique_ptr<Router> router;
|
||||||
std::shared_ptr<gloox::Client> client;
|
std::shared_ptr<gloox::Client> client;
|
||||||
std::unique_ptr<Message> messageHandler;
|
std::unique_ptr<Message> messageHandler;
|
||||||
std::unique_ptr<Connection> connectionHandler;
|
std::unique_ptr<Connection> connectionHandler;
|
||||||
std::vector<std::unique_ptr<Logger>> loggers;
|
std::vector<std::unique_ptr<Logger>> loggers;
|
||||||
Actors actors;
|
|
||||||
};
|
};
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
set(SOURCES
|
set(SOURCES
|
||||||
module.cpp
|
module.cpp
|
||||||
owner.cpp
|
actor.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
module.h
|
module.h
|
||||||
owner.h
|
actor.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})
|
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})
|
||||||
|
14
module/actor.cpp
Normal file
14
module/actor.cpp
Normal 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) {
|
||||||
|
|
||||||
|
}
|
@ -5,8 +5,14 @@
|
|||||||
|
|
||||||
#include "module.h"
|
#include "module.h"
|
||||||
|
|
||||||
class Owner : public Module {
|
namespace Module {
|
||||||
|
|
||||||
|
class Actor : public Module {
|
||||||
public:
|
public:
|
||||||
Owner();
|
Actor();
|
||||||
~Owner() noexcept;
|
~Actor() noexcept;
|
||||||
|
|
||||||
|
virtual void message(const std::string & text) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "module.h"
|
#include "module.h"
|
||||||
|
|
||||||
Module::Module()
|
Module::Module::Module()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Module::~Module() noexcept {}
|
Module::Module::~Module() noexcept {}
|
||||||
|
@ -3,10 +3,18 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace Module {
|
||||||
|
|
||||||
class Module {
|
class Module {
|
||||||
protected:
|
protected:
|
||||||
Module();
|
Module();
|
||||||
|
|
||||||
|
virtual void message(const std::string& text) = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~Module() noexcept;
|
virtual ~Module() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -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 {}
|
|
Loading…
x
Reference in New Issue
Block a user