Some further achitecture thoguhts

This commit is contained in:
Blue 2025-03-02 21:33:22 +02:00
parent ac8db32552
commit b997972ec1
Signed by: blue
GPG key ID: 9B203B252A63EE38
21 changed files with 103 additions and 15 deletions

11
handler/CMakeLists.txt Normal file
View file

@ -0,0 +1,11 @@
set(SOURCES
message.cpp
connection.cpp
)
set(HEADERS
message.h
connection.h
)
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})

27
handler/connection.cpp Normal file
View file

@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "connection.h"
Connection::Connection(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client):
config(config),
client(client)
{
client->registerConnectionListener(this);
}
Connection::~Connection() {
if (std::shared_ptr<gloox::Client> cl = client.lock())
cl->removeConnectionListener(this);
}
void Connection::onConnect() {}
void Connection::onDisconnect(gloox::ConnectionError e) {}
bool Connection::onTLSConnect(const gloox::CertInfo& info) {
return true;
}

25
handler/connection.h Normal file
View file

@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "gloox/client.h"
#include "gloox/connectionlistener.h"
#include "component/config.h"
class Connection : public gloox::ConnectionListener {
public:
Connection(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client);
~Connection();
void onConnect() override;
void onDisconnect(gloox::ConnectionError e) override;
bool onTLSConnect(const gloox::CertInfo&) override;
private:
std::weak_ptr<Config> config;
std::weak_ptr<gloox::Client> client;
};

30
handler/message.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 "message.h"
#include <iostream>
Message::Message(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client):
config(config),
client(client)
{
client->registerMessageHandler(this);
}
Message::~Message() {
if (std::shared_ptr<gloox::Client> cl = client.lock())
cl->removeMessageHandler(this);
}
void Message::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
std::shared_ptr<Config> cfg = config.lock();
if (!cfg)
return;
if (cfg->getOwners().count(message.from().bare())) {
std::cout << "Received message from owner: " << message.body() << std::endl;
} else {
std::cout << "Received message: " << message.body() << std::endl;
}
}

24
handler/message.h Normal file
View file

@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include <gloox/client.h>
#include <gloox/messagehandler.h>
#include <gloox/message.h>
#include "component/config.h"
class Message : public gloox::MessageHandler {
public:
Message(const std::shared_ptr<Config>& config, const std::shared_ptr<gloox::Client>& client);
~Message();
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
private:
std::weak_ptr<Config> config;
std::weak_ptr<gloox::Client> client;
};