58reportState(
const std::string& instanceName,
const core::socket::SocketAddress& socketAddress,
const core::socket::State& state) {
60 case core::socket::State::OK:
61 VLOG(1) << instanceName <<
": connected to '" << socketAddress.toString() <<
"'";
63 case core::socket::State::DISABLED:
64 VLOG(1) << instanceName <<
": disabled";
66 case core::socket::State::ERROR:
67 VLOG(1) << instanceName <<
": " << socketAddress.toString() <<
": " << state.what();
69 case core::socket::State::FATAL:
70 VLOG(1) << instanceName <<
": " << socketAddress.toString() <<
": " << state.what();
75static void logResponse(
const std::shared_ptr<web::http::client::Request>& req,
const std::shared_ptr<web::http::client::Response>& res) {
76 VLOG(1) << req->getConnectionName() <<
" HTTP response for: " << req->method <<
" " << req->url <<
" HTTP/" << req->httpMajor <<
"."
77 << req->httpMinor <<
"\n"
78 << httputils::toString(req->method,
80 "HTTP/" + std::to_string(req->httpMajor) +
"." + std::to_string(req->httpMinor),
87 << httputils::toString(res->httpVersion, res->statusCode, res->reason, res->headers, res->cookies, res->body);
93 const std::function<
void(
typename SocketClient<mqtt::mqttstore::
SocketContextFactory>::Config*)>& configurator) {
95 using SocketAddress =
typename Client::SocketAddress;
97 Client socketClient = core::socket::stream::Client<Client>(instanceName, configurator);
99 socketClient.getConfig()->setRetry();
100 socketClient.getConfig()->setRetryBase(1);
101 socketClient.getConfig()->setReconnect();
102 socketClient.getConfig()->setDisabled();
104 socketClient.connect([instanceName](
const SocketAddress& socketAddress,
const core::socket::State& state) {
105 reportState(instanceName, socketAddress, state);
112static HttpClient
startClient(
const std::string& name,
const std::function<
void(
typename HttpClient::Config*)>& configurator) {
113 using SocketAddress =
typename HttpClient::SocketAddress;
115 const HttpClient httpClient(
117 [](
const std::shared_ptr<web::http::client::MasterRequest>& req) {
118 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
119 const std::string target = req->getSocketContext()
120 ->getSocketConnection()
121 ->getConfigInstance()
122 ->getSubCommand<web::http::client::ConfigHTTP>()
123 ->getOption(
"--target")
126 req->set(
"Sec-WebSocket-Protocol",
"mqtt");
131 [connectionName](
bool success) {
132 VLOG(1) << connectionName <<
": HTTP Upgrade (http -> websocket||mqtt) start " << (success ?
"success" :
"failed");
134 [connectionName](
const std::shared_ptr<web::http::client::Request>& req,
135 const std::shared_ptr<web::http::client::Response>& res,
137 logResponse(req, res);
138 VLOG(1) << connectionName <<
": HTTP Upgrade " << (success ?
"success" :
"failed");
140 [connectionName](
const std::shared_ptr<web::http::client::Request>& req,
const std::string& message) {
141 VLOG(1) << connectionName <<
": Response parse error: " << message;
142 VLOG(1) <<
" Request was: " << req->method <<
" " << req->url <<
" HTTP/" << req->httpMajor <<
"." << req->httpMinor;
145 []([[maybe_unused]]
const std::shared_ptr<web::http::client::Request>& req) {
146 VLOG(1) <<
"Session ended";
149 configurator(httpClient.getConfig());
151 httpClient.getConfig()->setRetry();
152 httpClient.getConfig()->setRetryBase(1);
153 httpClient.getConfig()->setReconnect();
154 httpClient.getConfig()->setDisabled();
156 httpClient.connect([name](
const SocketAddress& socketAddress,
const core::socket::State& state) {
157 reportState(name, socketAddress, state);
183static int run(
int argc,
char* argv[]) {
184 core::SNodeC::init(argc, argv);
186#ifdef CONFIG_MQTTSUITE_STORE_TCP_IPV4
187 startClient<net::in::stream::legacy::SocketClient>(
"in-mqtt", [](net::in::stream::legacy::config::ConfigSocketClient* config) {
188 config->Remote::setPort(1883);
189 config->setDisableNagleAlgorithm();
190 createConfig(config);
194#ifdef CONFIG_MQTTSUITE_STORE_TLS_IPV4
195 startClient<net::in::stream::tls::SocketClient>(
"in-mqtts", [](net::in::stream::tls::config::ConfigSocketClient* config) {
196 config->Remote::setPort(1883);
197 config->setDisableNagleAlgorithm();
198 createConfig(config);
202#ifdef CONFIG_MQTTSUITE_STORE_TCP_IPV6
203 startClient<net::in6::stream::legacy::SocketClient>(
"in6-mqtt", [](net::in6::stream::legacy::config::ConfigSocketClient* config) {
204 config->Remote::setPort(1883);
205 config->setDisableNagleAlgorithm();
206 createConfig(config);
210#ifdef CONFIG_MQTTSUITE_STORE_TLS_IPV6
211 startClient<net::in6::stream::tls::SocketClient>(
"in6-mqtts", [](net::in6::stream::tls::config::ConfigSocketClient* config) {
212 config->Remote::setPort(1883);
213 config->setDisableNagleAlgorithm();
214 createConfig(config);
218#ifdef CONFIG_MQTTSUITE_STORE_UNIX
219 startClient<net::un::stream::legacy::SocketClient>(
"un-mqtt", [](net::un::stream::legacy::config::ConfigSocketClient* config) {
220 createConfig(config);
224#ifdef CONFIG_MQTTSUITE_STORE_UNIX_TLS
225 startClient<net::un::stream::tls::SocketClient>(
"un-mqtts", [](net::un::stream::tls::config::ConfigSocketClient* config) {
226 createConfig(config);
230#if defined(CONFIG_MQTTSUITE_STORE_TCP_IPV4) && defined(CONFIG_MQTTSUITE_STORE_WS)
231 startClient<web::http::legacy::in::Client>(
"in-wsmqtt", [](net::in::stream::legacy::config::ConfigSocketClient* config) {
232 config->Remote::setPort(8080);
233 config->setDisableNagleAlgorithm();
234 createWSConfig(config);
238#if defined(CONFIG_MQTTSUITE_STORE_TLS_IPV4) && defined(CONFIG_MQTTSUITE_STORE_WSS)
239 startClient<web::http::tls::in::Client>(
"in-wsmqtts", [](net::in::stream::tls::config::ConfigSocketClient* config) {
240 config->Remote::setPort(8088);
241 config->setDisableNagleAlgorithm();
242 createWSConfig(config);
246#if defined(CONFIG_MQTTSUITE_STORE_TCP_IPV6) && defined(CONFIG_MQTTSUITE_STORE_WS)
247 startClient<web::http::legacy::in6::Client>(
"in6-wsmqtt", [](net::in6::stream::legacy::config::ConfigSocketClient* config) {
248 config->Remote::setPort(8080);
249 config->setDisableNagleAlgorithm();
250 createWSConfig(config);
254#if defined(CONFIG_MQTTSUITE_STORE_TLS_IPV6) && defined(CONFIG_MQTTSUITE_STORE_WSS)
255 startClient<web::http::tls::in6::Client>(
"in6-wsmqtts", [](net::in6::stream::tls::config::ConfigSocketClient* config) {
256 config->Remote::setPort(8088);
257 config->setDisableNagleAlgorithm();
258 createWSConfig(config);
262#if defined(CONFIG_MQTTSUITE_STORE_UNIX) && defined(CONFIG_MQTTSUITE_STORE_WS)
263 startClient<web::http::legacy::un::Client>(
"un-wsmqtt", [](net::un::stream::legacy::config::ConfigSocketClient* config) {
264 createWSConfig(config);
268#if defined(CONFIG_MQTTSUITE_STORE_UNIX_TLS) && defined(CONFIG_MQTTSUITE_STORE_WSS)
269 startClient<web::http::tls::un::Client>(
"un-wsmqtts", [](net::un::stream::tls::config::ConfigSocketClient* config) {
270 createWSConfig(config);
274 return core::SNodeC::start();