MQTTSuite
Loading...
Searching...
No Matches
SocketContextFactory.cpp
Go to the documentation of this file.
1/*
2 * MQTTSuite - A lightweight MQTT Integration System
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 */
11
13
14#include "lib/ConfigSections.h"
15#include "lib/Mqtt.h"
16#include "lib/StoragePlan.h"
17
18#include <core/socket/stream/SocketConnection.h>
19#include <iot/mqtt/SocketContext.h>
20#include <net/config/ConfigInstance.h>
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <exception>
25#include <log/Logger.h>
26#include <string>
27#include <utility>
28
29#endif
30
31namespace {
32
34 std::string rawTable = "mqtt_messages";
35 bool autoCreateRawTable = true;
36 std::string projectionFile;
37 };
38
39 StorageOptions getStorageOptions(const mqtt::mqttstore::lib::ConfigStorage* configStorage) {
40 if (configStorage == nullptr) {
41 return {};
42 }
43
44 return {.rawTable = configStorage->getRawTable(),
47 }
48
49} // namespace
50
51namespace mqtt::mqttstore {
52
53 core::socket::stream::SocketContext* SocketContextFactory::create(core::socket::stream::SocketConnection* socketConnection) {
54 const net::config::ConfigInstance* configInstance = socketConnection->getConfigInstance();
55
56 const lib::ConfigSession* configSession = configInstance->getSubCommand<lib::ConfigSession>();
57 const lib::ConfigSubscribe* configSubscribe = configInstance->getSubCommand<lib::ConfigSubscribe>();
58 const lib::ConfigDatabase* configDatabase = configInstance->getSubCommand<lib::ConfigDatabase>();
59 const lib::ConfigStorage* configStorage = configDatabase->getSubCommand<lib::ConfigStorage>();
60 const StorageOptions storageOptions = getStorageOptions(configStorage);
61 lib::StoragePlan storagePlan;
62 try {
63 storagePlan = lib::StoragePlan::fromFile(storageOptions.projectionFile);
64 } catch (const std::exception& exception) {
65 VLOG(0) << socketConnection->getConnectionName() << " MQTTStore startup failed: " << exception.what();
66 throw;
67 }
68
69 return new iot::mqtt::SocketContext(socketConnection,
70 new mqtt::mqttstore::lib::Mqtt(socketConnection->getConnectionName(),
71 configSession->getClientId(),
72 configSession->getQoS(),
73 configSession->getKeepAlive(),
74 !configSession->getRetainSession(),
75 configSession->getWillTopic(),
76 configSession->getWillMessage(),
77 configSession->getWillQoS(),
78 configSession->getWillRetain(),
79 configSession->getUsername(),
80 configSession->getPassword(),
81 configSubscribe->getTopic(),
82 {.database = configDatabase->getDatabase(),
83 .username = configDatabase->getUsername(),
84 .password = configDatabase->getPassword(),
85 .host = configDatabase->getHost(),
86 .port = configDatabase->getPort(),
87 .socket = configDatabase->getSocket(),
88 .flags = configDatabase->getFlags()},
89 storageOptions.rawTable,
90 storageOptions.autoCreateRawTable,
91 std::move(storagePlan),
92 configSession->getSessionStore()));
93 }
94
95} // namespace mqtt::mqttstore
core::socket::stream::SocketContext * create(core::socket::stream::SocketConnection *socketConnection) final
static StoragePlan fromFile(const std::string &fileName)
StorageOptions getStorageOptions(const mqtt::mqttstore::lib::ConfigStorage *configStorage)