author | František Kučera <franta-hg@frantovo.cz> |
Sun, 05 Jun 2022 00:08:52 +0200 | |
branch | v_0 |
changeset 3 | 62ced7c41c67 |
parent 2 | 1a0fbd17db13 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <iostream> |
|
23 |
#include <sstream> |
|
24 |
#include <locale> |
|
25 |
#include <codecvt> |
|
2 | 26 |
#include <random> |
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
27 |
#include <regex> |
0 | 28 |
|
1
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
#include <mosquittopp.h> |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
|
0 | 31 |
#include <relpipe/common/type/typedefs.h> |
32 |
#include <relpipe/reader/TypeId.h> |
|
33 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
34 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
35 |
||
36 |
#include "Configuration.h" |
|
37 |
#include "Hex.h" |
|
38 |
||
39 |
namespace relpipe { |
|
40 |
namespace out { |
|
41 |
namespace mqtt { |
|
42 |
||
43 |
class MQTTHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
44 |
private: |
|
45 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
46 |
Configuration configuration; |
|
2 | 47 |
std::shared_ptr<mosqpp::mosquittopp> mq = std::make_shared<mosqpp::mosquittopp>(generateClientID().c_str()); |
48 |
||
49 |
/** |
|
50 |
* @return unique (random) client ID for MQTT to allow multiple simultaneous connections |
|
51 |
*/ |
|
52 |
static std::string generateClientID() { |
|
53 |
std::stringstream result; |
|
54 |
std::string symbols("0123456789abcdef"); |
|
55 |
||
56 |
std::random_device dev; |
|
57 |
std::mt19937 rng(dev()); |
|
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
std::uniform_int_distribution<std::mt19937::result_type> dist(0, symbols.size()); |
2 | 59 |
|
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
for (int i = 0; i < 8; i++) result << symbols[dist(rng)]; |
2 | 61 |
|
62 |
return result.str(); |
|
63 |
} |
|
0 | 64 |
|
65 |
struct CurrentRelation { |
|
66 |
relpipe::common::type::StringX name; |
|
67 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
68 |
relpipe::common::type::Integer attributeIndex = 0; |
|
69 |
std::string currentValue; |
|
70 |
} currentRelation; |
|
71 |
||
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
static void parseConnectionString(const std::string& connectionString, std::string& hostname, int& port) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
std::regex pattern("mqtt:(//)?([^:]+)(:([0-9]+))?"); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
74 |
std::smatch match; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
if (std::regex_match(connectionString, match, pattern)) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
hostname = match[2]; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
77 |
port = stoi(match[4]); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
78 |
} else { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
79 |
throw std::invalid_argument("Invalid connection string format. Expecting something like: mqtt://localhost:1883"); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
80 |
} |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
81 |
} |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
82 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
static void check(std::string operation, int result) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
84 |
if (result) throw std::logic_error("mosquitto operation failed: " + operation + " = " + std::to_string(result)); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
85 |
} |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
86 |
|
0 | 87 |
public: |
88 |
||
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
89 |
static MQTTHandler* create(Configuration configuration) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
90 |
MQTTHandler* h = new MQTTHandler(); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
91 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
92 |
std::string connectionString = h->convertor.to_bytes(configuration.connectionString); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
93 |
std::string username; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
94 |
std::string password; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
std::string hostname; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
96 |
int port; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
97 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
98 |
parseConnectionString(connectionString, hostname, port); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
99 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
for (auto o : configuration.connectionOptions) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
101 |
if (o.name == L"username") username = h->convertor.to_bytes(o.value); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
else if (o.name == L"password") password = h->convertor.to_bytes(o.value); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
103 |
else throw std::invalid_argument("Unsupported connection option: " + h->convertor.to_bytes(o.name)); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
104 |
} |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
if (username.size()) check("set credentials", h->mq->username_pw_set(username.c_str(), password.c_str())); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
check("connect", h->mq->connect(hostname.c_str(), port)); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
109 |
|
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
return h; |
0 | 111 |
} |
112 |
||
113 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
2 | 114 |
// TODO: check relation name according to the configuration |
0 | 115 |
currentRelation = CurrentRelation{name, attributes}; |
116 |
} |
|
117 |
||
118 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
119 |
||
120 |
auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName(); |
|
121 |
if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value); |
|
122 |
else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str(); |
|
123 |
else if (attributeName == L"text"); // keep empty or value from 'data' |
|
124 |
else if (attributeName == L"data"); // keep empty or value from 'text' |
|
125 |
||
126 |
currentRelation.attributeIndex++; |
|
127 |
if (currentRelation.attributeIndex == currentRelation.attributes.size()) { |
|
128 |
currentRelation.attributeIndex = 0; |
|
2 | 129 |
int mid = -1; |
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
130 |
check("publish", mq->publish(&mid, convertor.to_bytes(configuration.stream).c_str(), currentRelation.currentValue.size(), currentRelation.currentValue.c_str())); |
2 | 131 |
// std::cerr << "MQTT message enqueued: " << mid << std::endl; |
0 | 132 |
} |
133 |
||
134 |
} |
|
135 |
||
136 |
void endOfPipe() { |
|
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
137 |
check("disconnect", mq->disconnect()); |
0 | 138 |
} |
139 |
||
140 |
}; |
|
141 |
||
142 |
} |
|
143 |
} |
|
144 |
} |