MQTTSuite
Loading...
Searching...
No Matches
mqttcli.cpp File Reference
#include "SocketContextFactory.h"
#include "config.h"
#include "lib/ConfigSections.h"
#include <core/SNodeC.h>
#include <net/config/ConfigInstance.h>
#include <net/in/stream/legacy/SocketClient.h>
#include <net/in/stream/tls/SocketClient.h>
#include <net/in6/stream/legacy/SocketClient.h>
#include <net/in6/stream/tls/SocketClient.h>
#include <net/un/stream/legacy/SocketClient.h>
#include <net/un/stream/tls/SocketClient.h>
#include <web/http/client/ConfigHTTP.h>
#include <web/http/http_utils.h>
#include <web/http/legacy/in/Client.h>
#include <web/http/legacy/in6/Client.h>
#include <web/http/legacy/un/Client.h>
#include <web/http/tls/in/Client.h>
#include <web/http/tls/in6/Client.h>
#include <web/http/tls/un/Client.h>
Include dependency graph for mqttcli.cpp:

Go to the source code of this file.

Functions

static void reportState (const std::string &instanceName, const core::socket::SocketAddress &socketAddress, const core::socket::State &state)
static void logResponse (const std::shared_ptr< web::http::client::Request > &req, const std::shared_ptr< web::http::client::Response > &res)
template<template< typename SocketContextFactoryT, typename... ArgsT > typename SocketClient>
static SocketClient< mqtt::mqttcli::SocketContextFactorystartClient (const std::string &instanceName, const std::function< void(typename SocketClient< mqtt::mqttcli::SocketContextFactory >::Config *)> &configurator)
template<typename HttpClient>
static HttpClient startClient (const std::string &name, const std::function< void(typename HttpClient::Config *)> &configurator)
static void createConfig (net::config::ConfigInstance *config)
static void createWSConfig (net::config::ConfigInstance *config)
int main (int argc, char *argv[])

Function Documentation

◆ createConfig()

void createConfig ( net::config::ConfigInstance * config)
static

Definition at line 204 of file mqttcli.cpp.

204 {
205 config->newSubCommand<mqtt::mqttcli::lib::ConfigSession>();
206 config->newSubCommand<mqtt::mqttcli::lib::ConfigSubscribe>();
207 config->newSubCommand<mqtt::mqttcli::lib::ConfigPublish>();
208
209 config->setRequireCallback([config]() {
210 if (!config->getDisabled() && config->getShowConfigTriggerApp() == nullptr &&
211 config->getParent()->getOption("--write-config")->count() == 0) {
212 const mqtt::mqttcli::lib::ConfigPublish* pubApp = config->getSubCommand<mqtt::mqttcli::lib::ConfigPublish>();
213 const mqtt::mqttcli::lib::ConfigSubscribe* subApp = config->getSubCommand<mqtt::mqttcli::lib::ConfigSubscribe>();
214
215 if (pubApp->getTopic().empty() && subApp->getTopic().empty()) {
216 throw CLI::RequiresError(config->getParent()->getName() + ":" + config->getInstanceName() +
217 " requires at least one of {sub | pub}",
218 CLI::ExitCodes::RequiresError);
219 }
220
221 if (!pubApp->getTopic().empty()) {
222 VLOG(0) << "[" << Color::Code::FG_LIGHT_GREEN << "Success" << Color::Code::FG_DEFAULT << "] " << "Bootstrap of "
223 << config->getInstanceName() << ":pub";
224 }
225
226 if (!subApp->getTopic().empty()) {
227 VLOG(0) << "[" << Color::Code::FG_LIGHT_GREEN << "Success" << Color::Code::FG_DEFAULT << "] " << "Bootstrap of "
228 << config->getInstanceName() << ":sub";
229 }
230 }
231 });
232}

References mqtt::mqttcli::lib::ConfigPublish::getTopic().

Referenced by createWSConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createWSConfig()

void createWSConfig ( net::config::ConfigInstance * config)
static

Definition at line 234 of file mqttcli.cpp.

234 {
235 createConfig(config);
236
237 config->getSubCommand<web::http::client::ConfigHTTP>()
238 ->addOption("--target", "Websocket endpoint", "string", "/ws", CLI::TypeValidator<std::string>())
239 ->configurable();
240}
static void createConfig(net::config::ConfigInstance *config)
Definition mqttcli.cpp:204

References createConfig().

Here is the call graph for this function:

◆ logResponse()

void logResponse ( const std::shared_ptr< web::http::client::Request > & req,
const std::shared_ptr< web::http::client::Response > & res )
static

Definition at line 104 of file mqttcli.cpp.

104 {
105 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << " HTTP response for: " << req->method << " "
106 << req->url << " HTTP/" << req->httpMajor << "." << req->httpMinor << "\n"
107 << httputils::toString(req->method,
108 req->url,
109 "HTTP/" + std::to_string(req->httpMajor) + "." + std::to_string(req->httpMinor),
110 req->getQueries(),
111 req->getHeaders(),
112 req->getTrailer(),
113 req->getCookies(),
114 {})
115 << "\n"
116 << httputils::toString(res->httpVersion, res->statusCode, res->reason, res->headers, res->cookies, res->body);
117}

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 242 of file mqttcli.cpp.

242 {
243 core::SNodeC::init(argc, argv);
244
245#if defined(CONFIG_MQTTSUITE_CLI_TCP_IPV4)
247 "in-mqtt",
248 [](net::in::stream::legacy::config::ConfigSocketClient* config) {
249 config->Remote::setPort(1883);
250 config->setDisableNagleAlgorithm();
251
252 createConfig(config); // cppcheck-suppress throwInEntryPoint
253 });
254#endif
255
256#if defined(CONFIG_MQTTSUITE_CLI_TLS_IPV4)
258 "in-mqtts",
259 [](net::in::stream::tls::config::ConfigSocketClient* config) {
260 config->Remote::setPort(1883);
261 config->setDisableNagleAlgorithm();
262
263 createConfig(config); // cppcheck-suppress throwInEntryPoint
264 });
265#endif
266
267#if defined(CONFIG_MQTTSUITE_CLI_TCP_IPV6)
269 "in6-mqtt",
270 [](net::in6::stream::legacy::config::ConfigSocketClient* config) {
271 config->Remote::setPort(1883);
272 config->setDisableNagleAlgorithm();
273
274 createConfig(config); // cppcheck-suppress throwInEntryPoint
275 });
276#endif
277
278#if defined(CONFIG_MQTTSUITE_CLI_TLS_IPV6)
280 "in6-mqtts",
281 [](net::in6::stream::tls::config::ConfigSocketClient* config) {
282 config->Remote::setPort(1883);
283 config->setDisableNagleAlgorithm();
284
285 createConfig(config); // cppcheck-suppress throwInEntryPoint
286 });
287#endif
288
289#if defined(CONFIG_MQTTSUITE_CLI_UNIX)
291 "un-mqtt",
292 [](net::un::stream::legacy::config::ConfigSocketClient* config) {
293 createConfig(config); // cppcheck-suppress throwInEntryPoint
294 });
295#endif
296
297#if defined(CONFIG_MQTTSUITE_CLI_UNIX_TLS)
299 "un-mqtts",
300 [](net::un::stream::tls::config::ConfigSocketClient* config) {
301 createConfig(config); // cppcheck-suppress throwInEntryPoint
302 });
303#endif
304
305#if defined(CONFIG_MQTTSUITE_CLI_TCP_IPV4) && defined(CONFIG_MQTTSUITE_CLI_WS)
307 "in-wsmqtt",
308 [](net::in::stream::legacy::config::ConfigSocketClient* config) {
309 config->Remote::setPort(8080);
310 config->setDisableNagleAlgorithm();
311
312 createWSConfig(config);
313 });
314#endif
315
316#if defined(CONFIG_MQTTSUITE_CLI_TLS_IPV4) && defined(CONFIG_MQTTSUITE_CLI_WSS)
318 "in-wsmqtts",
319 [](net::in::stream::tls::config::ConfigSocketClient* config) {
320 config->Remote::setPort(8088);
321 config->setDisableNagleAlgorithm();
322
323 createWSConfig(config);
324 });
325#endif
326
327#if defined(CONFIG_MQTTSUITE_CLI_TCP_IPV6) && defined(CONFIG_MQTTSUITE_CLI_WS)
329 "in6-wsmqtt",
330 [](net::in6::stream::legacy::config::ConfigSocketClient* config) {
331 config->Remote::setPort(8080);
332 config->setDisableNagleAlgorithm();
333
334 createWSConfig(config);
335 });
336#endif
337
338#if defined(CONFIG_MQTTSUITE_CLI_TLS_IPV6) && defined(CONFIG_MQTTSUITE_CLI_WSS)
340 "in6-wsmqtts",
341 [](net::in6::stream::tls::config::ConfigSocketClient* config) {
342 config->Remote::setPort(8088);
343 config->setDisableNagleAlgorithm();
344
345 createWSConfig(config);
346 });
347#endif
348
349#if defined(CONFIG_MQTTSUITE_CLI_UNIX) && defined(CONFIG_MQTTSUITE_CLI_WS)
351 "un-wsmqtt",
352 [](net::un::stream::legacy::config::ConfigSocketClient* config) {
353 createWSConfig(config);
354 });
355#endif
356
357#if defined(CONFIG_MQTTSUITE_CLI_UNIX_TLS) && defined(CONFIG_MQTTSUITE_CLI_WSS)
359 "un-wsmqtts",
360 [](net::un::stream::tls::config::ConfigSocketClient* config) {
361 createWSConfig(config);
362 });
363#endif
364
365 return core::SNodeC::start();
366}
static SocketClient< mqtt::mqttcli::SocketContextFactory > startClient(const std::string &instanceName, const std::function< void(typename SocketClient< mqtt::mqttcli::SocketContextFactory >::Config *)> &configurator)
Definition mqttcli.cpp:121
static void createWSConfig(net::config::ConfigInstance *config)
Definition mqttcli.cpp:234

◆ reportState()

void reportState ( const std::string & instanceName,
const core::socket::SocketAddress & socketAddress,
const core::socket::State & state )
static

Definition at line 87 of file mqttcli.cpp.

87 {
88 switch (state) {
89 case core::socket::State::OK:
90 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
91 break;
92 case core::socket::State::DISABLED:
93 VLOG(1) << instanceName << ": disabled";
94 break;
95 case core::socket::State::ERROR:
96 VLOG(1) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
97 break;
98 case core::socket::State::FATAL:
99 VLOG(1) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
100 break;
101 }
102}

◆ startClient() [1/2]

template<template< typename SocketContextFactoryT, typename... ArgsT > typename SocketClient>
SocketClient< mqtt::mqttcli::SocketContextFactory > startClient ( const std::string & instanceName,
const std::function< void(typename SocketClient< mqtt::mqttcli::SocketContextFactory >::Config *)> & configurator )
static

Definition at line 121 of file mqttcli.cpp.

122 {
123 using Client = SocketClient<mqtt::mqttcli::SocketContextFactory>;
124 using SocketAddress = typename Client::SocketAddress;
125
126 Client socketClient = core::socket::stream::Client<Client>(instanceName, configurator);
127
128 socketClient.getConfig()->setRetry();
129 socketClient.getConfig()->setRetryBase(1);
130 socketClient.getConfig()->setReconnect();
131 socketClient.getConfig()->setDisabled();
132
133 socketClient.connect([instanceName](const SocketAddress& socketAddress, const core::socket::State& state) {
134 reportState(instanceName, socketAddress, state);
135 });
136
137 return socketClient;
138}
static void reportState(const std::string &instanceName, const core::socket::SocketAddress &socketAddress, const core::socket::State &state)
Definition mqttcli.cpp:87

◆ startClient() [2/2]

template<typename HttpClient>
HttpClient startClient ( const std::string & name,
const std::function< void(typename HttpClient::Config *)> & configurator )
static

Definition at line 141 of file mqttcli.cpp.

141 {
142 using SocketAddress = typename HttpClient::SocketAddress;
143
144 const HttpClient httpClient(
145 name,
146 [](const std::shared_ptr<web::http::client::MasterRequest>& req) {
147 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
148 const std::string target = req->getSocketContext()
149 ->getSocketConnection()
150 ->getConfigInstance()
151 ->getSubCommand<web::http::client::ConfigHTTP>()
152 ->getOption("--target")
153 ->as<std::string>();
154
155 req->set("Sec-WebSocket-Protocol", "mqtt");
156
157 req->upgrade(
158 target,
159 "websocket",
160 [connectionName](bool success) {
161 VLOG(1) << connectionName << ": HTTP Upgrade (http -> websocket||"
162 << "mqtt" << ") start " << (success ? "success" : "failed");
163 },
164 [connectionName](const std::shared_ptr<web::http::client::Request>& req,
165 const std::shared_ptr<web::http::client::Response>& res,
166 bool success) {
167 logResponse(req, res);
168
169 VLOG(1) << connectionName << ": HTTP Upgrade " << (success ? "success" : "failed");
170 },
171 [connectionName](const std::shared_ptr<web::http::client::Request>& req, const std::string& message) {
172 VLOG(1) << connectionName << ": Response parse error: " << message;
173 VLOG(1) << " Request was: " << req->method << " " << req->url << " HTTP/" << req->httpMajor << "." << req->httpMinor
174 << "\n"
175 << httputils::toString(req->method,
176 req->url,
177 "HTTP/" + std::to_string(req->httpMajor) + "." + std::to_string(req->httpMinor),
178 req->getQueries(),
179 req->getHeaders(),
180 req->getTrailer(),
181 req->getCookies(),
182 {})
183 << "\n";
184 });
185 },
186 []([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& req) {
187 VLOG(1) << "Session ended";
188 });
189
190 configurator(httpClient.getConfig());
191
192 httpClient.getConfig()->setRetry();
193 httpClient.getConfig()->setRetryBase(1);
194 httpClient.getConfig()->setReconnect();
195 httpClient.getConfig()->setDisabled();
196
197 httpClient.connect([name](const SocketAddress& socketAddress, const core::socket::State& state) {
198 reportState(name, socketAddress, state);
199 });
200
201 return httpClient;
202}
static void logResponse(const std::shared_ptr< web::http::client::Request > &req, const std::shared_ptr< web::http::client::Response > &res)
Definition mqttcli.cpp:104