NRP Core  1.4.1
nrp_mqtt_client.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef MQTT_CLIENT_H
23 #define MQTT_CLIENT_H
24 
25 
26 #include <functional>
27 
28 #include "nlohmann/json.hpp"
29 
31 #include "mqtt/async_client.h"
32 
33 #define MQTT_BASE "nrp_simulation"
34 
35 // TODO: QOS and non-clean session are hardcoded, they could be parameters
36 const int QOS = 1;
37 const auto TIMEOUT = std::chrono::seconds(10);
38 
39 class NRPMQTTClient;
40 
41 // TODO: mqtt callbacks can also take care of handling disconnections with server, failed sending or receiving, etc.
42 // As a future extension these possibilities can be explored further and added here.
43 class MQTTCallback : public virtual mqtt::callback {
44  void message_arrived(mqtt::const_message_ptr msg) override;
45 
46 public:
47 
50 };
51 
53 
54 public:
58  NRPMQTTClient(nlohmann::json clientParams);
59 
60  NRPMQTTClient();
61 
62  virtual ~NRPMQTTClient() = default;
63 
67  virtual void subscribe(const std::string& address, const std::function<void (const std::string&)>& callback);
68 
72  virtual void publish(const std::string& address, const std::string& msg, bool retained=false);
73 
77  void publishDirect(const std::string& address, const std::string& msg);
78 
82  virtual void disconnect();
83 
87  virtual bool isConnected();
88 
92  virtual void clearRetained();
93 
94 private:
95 
99  std::shared_ptr< mqtt::async_client> _mqttClient;
100 
104  std::map<std::string, mqtt::topic> _topics;
105 
109  std::map<std::string, std::function<void (const std::string&)>> _subscribers;
110 
114  MQTTCallback _callback;
115 
116  friend MQTTCallback;
117 };
118 
119 #endif //MQTT_CLIENT_H
NRPMQTTClient::isConnected
virtual bool isConnected()
Check connection status to broker.
Definition: nrp_mqtt_client.cpp:26
nrp_logger.h
MQTTCallback::_proxy
NRPMQTTClient * _proxy
Definition: nrp_mqtt_client.h:49
MQTTCallback
Definition: nrp_mqtt_client.h:43
TIMEOUT
const auto TIMEOUT
Definition: nrp_mqtt_client.h:37
NRPMQTTClient
Definition: nrp_mqtt_client.h:52
MQTTCallback::MQTTCallback
MQTTCallback(NRPMQTTClient *proxy)
Definition: nrp_mqtt_client.cpp:130
NRPMQTTClient::publishDirect
void publishDirect(const std::string &address, const std::string &msg)
Publishes 'msg' directly to subscriber callbacks, without going through the MQTT broker....
Definition: nrp_mqtt_client.cpp:55
NRPMQTTClient::disconnect
virtual void disconnect()
Disconnects client from MQTT Broker.
Definition: nrp_mqtt_client.cpp:104
QOS
const int QOS
Definition: nrp_mqtt_client.h:36
NRPMQTTClient::~NRPMQTTClient
virtual ~NRPMQTTClient()=default
NRPMQTTClient::subscribe
virtual void subscribe(const std::string &address, const std::function< void(const std::string &)> &callback)
Subscribe to MQTT topic 'address' with callback function 'callback'.
Definition: nrp_mqtt_client.cpp:61
NRPMQTTClient::publish
virtual void publish(const std::string &address, const std::string &msg, bool retained=false)
Publishes 'msg' to MQTT topic 'address'.
Definition: nrp_mqtt_client.cpp:38
NRPMQTTClient::clearRetained
virtual void clearRetained()
Clear all topics with retain messages by sending an empty msg.
Definition: nrp_mqtt_client.cpp:117
NRPMQTTClient::NRPMQTTClient
NRPMQTTClient()
Definition: nrp_mqtt_client.cpp:100
json
nlohmann::json json
Definition: engine_json_server.cpp:31