MQTTSuite
Loading...
Searching...
No Matches
SubProtocolFactory.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 <net/config/ConfigInstance.h>
20
21#ifndef DOXYGEN_SHOULD_SKIP_THIS
22
23#include <exception>
24#include <log/Logger.h>
25#include <utility>
26
27#endif
28#include <web/websocket/SubProtocolContext.h>
29
30namespace {
31
33 std::string rawTable = "mqtt_messages";
34 bool autoCreateRawTable = true;
35 std::string projectionFile;
36 };
37
38 StorageOptions getStorageOptions(const mqtt::mqttstore::lib::ConfigStorage* configStorage) {
39 if (configStorage == nullptr) {
40 return {};
41 }
42
43 return {.rawTable = configStorage->getRawTable(),
46 }
47
48} // namespace
49
50namespace mqtt::mqttstore::websocket {
51
52#define NAME "mqtt"
53
55 : web::websocket::SubProtocolFactory<web::websocket::client::SubProtocol>::SubProtocolFactory(NAME) {
56 }
57
58 iot::mqtt::client::SubProtocol* SubProtocolFactory::create(web::websocket::SubProtocolContext* subProtocolContext) {
59 const net::config::ConfigInstance* configInstance = subProtocolContext->getSocketConnection()->getConfigInstance();
60
61 const lib::ConfigSession* configSession = configInstance->getSubCommand<lib::ConfigSession>();
62 const lib::ConfigSubscribe* configSubscribe = configInstance->getSubCommand<lib::ConfigSubscribe>();
63 const lib::ConfigDatabase* configDatabase = configInstance->getSubCommand<lib::ConfigDatabase>();
64 const lib::ConfigStorage* configStorage = configDatabase->getSubCommand<lib::ConfigStorage>();
65 const StorageOptions storageOptions = getStorageOptions(configStorage);
66 lib::StoragePlan storagePlan;
67 try {
68 storagePlan = lib::StoragePlan::fromFile(storageOptions.projectionFile);
69 } catch (const std::exception& exception) {
70 VLOG(0) << subProtocolContext->getSocketConnection()->getConnectionName() << " MQTTStore startup failed: " << exception.what();
71 throw;
72 }
73
74 return new iot::mqtt::client::SubProtocol(
75 subProtocolContext,
76 getName(),
77 new mqtt::mqttstore::lib::Mqtt(subProtocolContext->getSocketConnection()->getConnectionName(),
78 configSession->getClientId(),
79 configSession->getQoS(),
80 configSession->getKeepAlive(),
81 !configSession->getRetainSession(),
82 configSession->getWillTopic(),
83 configSession->getWillMessage(),
84 configSession->getWillQoS(),
85 configSession->getWillRetain(),
86 configSession->getUsername(),
87 configSession->getPassword(),
88 configSubscribe->getTopic(),
89 {.database = configDatabase->getDatabase(),
90 .username = configDatabase->getUsername(),
91 .password = configDatabase->getPassword(),
92 .host = configDatabase->getHost(),
93 .port = configDatabase->getPort(),
94 .socket = configDatabase->getSocket(),
95 .flags = configDatabase->getFlags()},
96 storageOptions.rawTable,
97 storageOptions.autoCreateRawTable,
98 std::move(storagePlan),
99 configSession->getSessionStore()));
100 }
101
102} // namespace mqtt::mqttstore::websocket
103
104extern "C" web::websocket::SubProtocolFactory<web::websocket::client::SubProtocol>* mqttClientSubProtocolFactory() {
105 return new mqtt::mqttstore::websocket::SubProtocolFactory();
106}
static StoragePlan fromFile(const std::string &fileName)
iot::mqtt::client::SubProtocol * create(web::websocket::SubProtocolContext *subProtocolContext) override
#define NAME
web::websocket::SubProtocolFactory< web::websocket::client::SubProtocol > * mqttClientSubProtocolFactory()
StorageOptions getStorageOptions(const mqtt::mqttstore::lib::ConfigStorage *configStorage)