author | František Kučera <franta-hg@frantovo.cz> |
Sun, 05 Jun 2022 22:51:45 +0200 | |
branch | v_0 |
changeset 3 | 610783d70ae9 |
parent 2 | 0799eaf338b9 |
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 |
#include <cstdlib> |
|
18 |
#include <vector> |
|
19 |
#include <memory> |
|
20 |
#include <locale> |
|
21 |
#include <regex> |
|
22 |
#include <algorithm> |
|
23 |
#include <unistd.h> |
|
24 |
#include <sstream> |
|
25 |
#include <iomanip> |
|
2 | 26 |
#include <random> |
3
610783d70ae9
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
4993a084b8ba
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
#include <mosquittopp.h> |
4993a084b8ba
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
|
0 | 31 |
#include <relpipe/writer/RelationalWriter.h> |
32 |
#include <relpipe/writer/RelpipeWriterException.h> |
|
33 |
#include <relpipe/writer/AttributeMetadata.h> |
|
34 |
#include <relpipe/writer/Factory.h> |
|
35 |
#include <relpipe/writer/TypeId.h> |
|
36 |
||
37 |
#include <relpipe/cli/CLI.h> |
|
38 |
||
39 |
#include "MQTTCommand.h" |
|
40 |
#include "Hex.h" |
|
41 |
||
42 |
using namespace relpipe::cli; |
|
43 |
using namespace relpipe::writer; |
|
44 |
||
45 |
namespace relpipe { |
|
46 |
namespace in { |
|
47 |
namespace mqtt { |
|
48 |
||
2 | 49 |
class MQTTClient : public mosqpp::mosquittopp { |
50 |
private: |
|
51 |
std::shared_ptr<writer::RelationalWriter> writer; |
|
52 |
Configuration& configuration; |
|
53 |
int messageCount = 0; |
|
54 |
std::string clientId; |
|
0 | 55 |
|
2 | 56 |
/** |
57 |
* @return unique (random) client ID for MQTT to allow multiple simultaneous connections |
|
58 |
*/ |
|
59 |
static std::string generateClientID() { |
|
60 |
std::stringstream result; |
|
61 |
std::string symbols("0123456789abcdef"); |
|
62 |
||
63 |
std::random_device dev; |
|
64 |
std::mt19937 rng(dev()); |
|
3
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
std::uniform_int_distribution<std::mt19937::result_type> dist(0, symbols.size()); |
2 | 66 |
|
3
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
for (int i = 0; i < 8; i++) result << symbols[dist(rng)]; |
2 | 68 |
|
69 |
return result.str(); |
|
70 |
} |
|
71 |
public: |
|
72 |
||
73 |
MQTTClient(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : mosqpp::mosquittopp((generateClientID()).c_str()), writer(writer), configuration(configuration) { |
|
1
4993a084b8ba
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
} |
0 | 75 |
|
2 | 76 |
void on_message(const mosquitto_message* message) override { |
77 |
// std::cerr << "got MQTT message: length=" << message->payloadlen << std::endl; |
|
78 |
std::string payload = std::string((const char*) message->payload, message->payloadlen); |
|
79 |
writer->writeAttribute(configuration.stream); |
|
80 |
writer->writeAttribute(Hex::toTxt(payload)); |
|
81 |
writer->writeAttribute(Hex::toHex(payload)); |
|
82 |
messageCount++; |
|
83 |
} |
|
84 |
||
85 |
int popMessageCount() { |
|
86 |
int count = messageCount; |
|
87 |
messageCount = 0; |
|
88 |
return count; |
|
89 |
} |
|
90 |
||
91 |
}; |
|
92 |
||
3
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
93 |
void MQTTCommand::parseConnectionString(const std::string& connectionString, std::string& hostname, int& port) { |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
94 |
std::regex pattern("mqtt:(//)?([^:]+)(:([0-9]+))?"); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
std::smatch match; |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
96 |
if (std::regex_match(connectionString, match, pattern)) { |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
97 |
hostname = match[2]; |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
98 |
port = stoi(match[4]); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
99 |
} else { |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
throw std::invalid_argument("Invalid connection string format. Expecting something like: mqtt://localhost:1883"); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
101 |
} |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
} |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
103 |
|
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
104 |
void MQTTCommand::check(std::string operation, int result) { |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
if (result) throw std::logic_error("mosquitto operation failed: " + operation + " = " + std::to_string(result)); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
} |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
|
2 | 108 |
void MQTTCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) { |
109 |
std::shared_ptr<MQTTClient> mq = std::make_shared<MQTTClient>(writer, configuration); |
|
110 |
||
0 | 111 |
writer->startRelation(configuration.relation,{ |
2 | 112 |
{L"stream", TypeId::STRING}, |
0 | 113 |
{L"text", TypeId::STRING}, |
114 |
{L"data", TypeId::STRING} |
|
115 |
}, true); |
|
116 |
||
3
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
std::string connectionString = convertor.to_bytes(configuration.connectionString); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
std::string username; |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
std::string password; |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
120 |
std::string hostname; |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
int port; |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
122 |
|
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
parseConnectionString(connectionString, hostname, port); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
124 |
|
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
125 |
for (auto o : configuration.connectionOptions) { |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
126 |
if (o.name == L"username") username = convertor.to_bytes(o.value); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
else if (o.name == L"password") password = convertor.to_bytes(o.value); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
128 |
else throw std::invalid_argument("Unsupported connection option: " + convertor.to_bytes(o.name)); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
129 |
} |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
130 |
|
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
131 |
if (username.size()) check("set credentials", mq->username_pw_set(username.c_str(), password.c_str())); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
132 |
|
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
133 |
check("set maximum inflight messages", mq->max_inflight_messages_set(1)); |
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
134 |
check("connect", mq->connect(hostname.c_str(), port)); |
2 | 135 |
int mid; |
3
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
136 |
check("substcribe", mq->subscribe(&mid, convertor.to_bytes(configuration.stream).c_str())); |
0 | 137 |
|
2 | 138 |
//for (int i = configuration.messageCount; continueProcessing && i > 0; i--) { |
139 |
for (int i = configuration.messageCount; continueProcessing && i > 0; i = i - mq->popMessageCount()) { |
|
140 |
// std::cerr << "loop(): i=" << i << std::endl; |
|
141 |
||
142 |
//mq->loop(); |
|
3
610783d70ae9
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
check("loop", mq->loop(1000, 1)); |
2 | 144 |
//mq->loop(1000, -1); |
145 |
//mq->loop_forever(); |
|
146 |
//mq->loop_write(); |
|
0 | 147 |
} |
148 |
||
2 | 149 |
// FIXME: move do destructor |
150 |
mq->disconnect(); |
|
0 | 151 |
} |
152 |
||
153 |
MQTTCommand::~MQTTCommand() { |
|
154 |
} |
|
155 |
||
156 |
} |
|
157 |
} |
|
158 |
} |