95reportState(
const std::string& instanceName,
const core::socket::SocketAddress& socketAddress,
const core::socket::State& state) {
97 case core::socket::State::OK:
98 VLOG(1) << instanceName <<
": connected to '" << socketAddress.toString() <<
"'";
100 case core::socket::State::DISABLED:
101 VLOG(1) << instanceName <<
": disabled";
103 case core::socket::State::ERROR:
104 VLOG(1) << instanceName <<
": " << socketAddress.toString() <<
": " << state.what();
106 case core::socket::State::FATAL:
107 VLOG(1) << instanceName <<
": " << socketAddress.toString() <<
": " << state.what();
115 const std::function<
void(
typename SocketClientT<mqtt::mqttintegrator::
SocketContextFactory>::Config*)>& configurator,
118 using SocketAddress =
typename Client::SocketAddress;
120 Client socketClient = core::socket::stream::Client<Client>(instanceName, configurator, std::forward<Args>(args)...);
122 socketClient.getConfig()->setRetry();
123 socketClient.getConfig()->setRetryBase(1);
124 socketClient.getConfig()->setReconnect();
126 socketClient.connect([instanceName](
const SocketAddress& socketAddress,
const core::socket::State& state) {
127 reportState(instanceName, socketAddress, state);
134HttpClient
startClient(
const std::string& name,
const std::function<
void(
typename HttpClient::Config*)>& configurator =
nullptr) {
135 using SocketAddress =
typename HttpClient::SocketAddress;
137 const HttpClient httpClient(
139 [](
const std::shared_ptr<web::http::client::MasterRequest>& req) {
140 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
142 req->set(
"Sec-WebSocket-Protocol",
"mqtt");
147 [connectionName](
bool success) {
148 VLOG(1) << connectionName <<
": HTTP Upgrade (http -> websocket||"
149 <<
"mqtt" <<
") start " << (success ?
"success" :
"failed");
151 []([[maybe_unused]]
const std::shared_ptr<web::http::client::Request>& req,
152 [[maybe_unused]]
const std::shared_ptr<web::http::client::Response>& res,
153 [[maybe_unused]]
bool success) {
155 [connectionName](
const std::shared_ptr<web::http::client::Request>&,
const std::string& message) {
156 VLOG(1) << connectionName <<
": Request parse error: " << message;
159 []([[maybe_unused]]
const std::shared_ptr<web::http::client::Request>& req) {
160 VLOG(1) <<
"Session ended";
163 if (configurator !=
nullptr) {
164 configurator(httpClient.getConfig());
167 httpClient.getConfig()->setRetry();
168 httpClient.getConfig()->setRetryBase(1);
169 httpClient.getConfig()->setReconnect();
171 httpClient.connect([name](
const SocketAddress& socketAddress,
const core::socket::State& state) {
172 reportState(name, socketAddress, state);
178int main(
int argc,
char* argv[]) {
181 configMqttIntegrator->setMappingFile(
"mapping.json");
183 configMqttIntegrator->setMapping(
185 {
186 "discover_prefix": "",
187 "connection": {
188 "keep_alive": 60,
189 "client_id": "",
190 "clean_session": true,
191 "will_topic": "",
192 "will_message": "",
193 "will_qos": 0,
194 "will_retain": false
195 },
196 "mapping": {
197 "plugins": [],
198 "topic_level": [{
199 "name": "value",
200 "subscription": {
201 "value": {
202 "mapped_topic": "mapping/json",
203 "mapping_template": "{\"state\":\"{{message}}\"}"
204 }
205 }
206 }]
207 }
208 }
209 )");
211
212
213
214
215
216
218 core::SNodeC::init(argc, argv);
222 mqtt::lib::admin::makeMappingAdminRouter(configMqttIntegrator, mqtt::lib::admin::AdminOptions{}, [](
bool mustReconnect) {
226 express::legacy::in::Server(
"in-http", router,
reportState, [](net::in::stream::legacy::config::ConfigSocketServer* config) {
227 config->setPort(8085);
231 express::tls::in::Server(
"in-https", router,
reportState, [](net::in::stream::tls::config::ConfigSocketServer* config) {
232 config->setPort(8086);
236#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TCP_IPV4)
237 startClient<net::in::stream::legacy::SocketClient>(
239 [](net::in::stream::legacy::config::ConfigSocketClient* config) {
240 config->Remote::setPort(1883);
242 config->setDisableNagleAlgorithm();
246#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TLS_IPV4)
247 startClient<net::in::stream::tls::SocketClient>(
249 [](net::in::stream::tls::config::ConfigSocketClient* config) {
250 config->Remote::setPort(1883);
251 config->setDisableNagleAlgorithm();
255#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TCP_IPV6)
256 startClient<net::in6::stream::legacy::SocketClient>(
258 [](net::in6::stream::legacy::config::ConfigSocketClient* config) {
259 config->Remote::setPort(1883);
260 config->setDisableNagleAlgorithm();
264#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TLS_IPV6)
265 startClient<net::in6::stream::tls::SocketClient>(
267 [](net::in6::stream::tls::config::ConfigSocketClient* config) {
268 config->Remote::setPort(1883);
269 config->setDisableNagleAlgorithm();
273#if defined(CONFIG_MQTTSUITE_INTEGRATOR_UNIX)
274 startClient<net::un::stream::legacy::SocketClient>(
276 []([[maybe_unused]]
const net::un::stream::legacy::config::ConfigSocketClient* config) {
280#if defined(CONFIG_MQTTSUITE_INTEGRATOR_UNIX_TLS)
281 startClient<net::un::stream::tls::SocketClient>(
283 []([[maybe_unused]]
const net::un::stream::tls::config::ConfigSocketClient* config) {
287#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TCP_IPV4) && defined(CONFIG_MQTTSUITE_INTEGRATOR_WS)
288 startClient<web::http::legacy::in::Client>(
290 [](net::in::stream::legacy::config::ConfigSocketClient* config) {
291 config->Remote::setPort(8080);
292 config->setDisableNagleAlgorithm();
296#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TLS_IPV4) && defined(CONFIG_MQTTSUITE_INTEGRATOR_WSS)
297 startClient<web::http::tls::in::Client>(
299 [](net::in::stream::tls::config::ConfigSocketClient* config) {
300 config->Remote::setPort(8088);
301 config->setDisableNagleAlgorithm();
305#if defined(CONFIG_MQTTSUITE_INTEGRATOR_TCP_IPV6) && defined(CONFIG_MQTTSUITE_INTEGRATOR_WS)
306 startClient<web::http::legacy::in6::Client>(
308 [](net::in6::stream::legacy::config::ConfigSocketClient* config) {
309 config->Remote::setPort(8080);
310 config->setDisableNagleAlgorithm();