NRP Core  1.4.1
nrp_mqtt_proxy.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_PROXY_H
23 #define MQTT_PROXY_H
24 
26 
27 class NRPMQTTProxy {
28 
29 public:
30 
31  NRPMQTTProxy() = delete;
32 
33  // Delete move and copy operators. This ensures this class is a singleton
34  NRPMQTTProxy(const NRPMQTTProxy &) = delete;
35  NRPMQTTProxy(NRPMQTTProxy &&) = delete;
36 
37  NRPMQTTProxy &operator=(const NRPMQTTProxy &) = delete;
38  NRPMQTTProxy &operator=(NRPMQTTProxy &&) = delete;
39 
43  static NRPMQTTProxy &getInstance();
44 
48  static NRPMQTTProxy &resetInstance(const nlohmann::json& clientParams);
49 
53  void subscribe(const std::string& address, const std::function<void (const std::string&)>& callback);
54 
58  void publish(const std::string& address, const std::string& msg, bool retained=false);
59 
63  bool isConnected();
64 
68  void disconnect();
69 
73  void clearRetained();
74 
75 protected:
76 
80  bool _doBypassBroker = false;
81 
83 
84 private:
85 
86  std::shared_ptr< NRPMQTTClient> _mqttClient;
87 
91  NRPMQTTProxy(const nlohmann::json& clientParams);
92 
93  static std::unique_ptr<NRPMQTTProxy> _instance;
94 };
95 
96 #endif //MQTT_PROXY_H
NRPMQTTProxy::resetInstance
static NRPMQTTProxy & resetInstance(const nlohmann::json &clientParams)
Reset singleton instance.
Definition: nrp_mqtt_proxy.cpp:31
NRPMQTTProxy::getInstance
static NRPMQTTProxy & getInstance()
Get singleton instance of NRPMQTTProxy.
Definition: nrp_mqtt_proxy.cpp:26
nrp_mqtt_client.h
NRPMQTTProxy::operator=
NRPMQTTProxy & operator=(const NRPMQTTProxy &)=delete
NRPMQTTProxy::_doBypassBroker
bool _doBypassBroker
If true, msgs are published directly to subscriber callbacks, without going through the MQTT broker....
Definition: nrp_mqtt_proxy.h:80
NRPMQTTProxy::isConnected
bool isConnected()
Check connection status to broker.
Definition: nrp_mqtt_proxy.cpp:55
NRPMQTTProxy
Definition: nrp_mqtt_proxy.h:27
NRPMQTTProxy::disconnect
void disconnect()
Disconnects client from MQTT Broker.
Definition: nrp_mqtt_proxy.cpp:63
NRPMQTTProxy::publish
void publish(const std::string &address, const std::string &msg, bool retained=false)
Publishes 'msg' to MQTT topic 'address'.
Definition: nrp_mqtt_proxy.cpp:37
NRPMQTTProxy::subscribe
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_proxy.cpp:45
NRPMQTTProxy::NRPMQTTProxy
NRPMQTTProxy()=delete
NRPMQTTProxy::clearRetained
void clearRetained()
Clear all topics with retain messages by sending an empty msg.
Definition: nrp_mqtt_proxy.cpp:70
NRPMQTTProxy::EventLoop_EVENT_LOOP_ENGINE_Test
friend class EventLoop_EVENT_LOOP_ENGINE_Test
Definition: nrp_mqtt_proxy.h:82
json
nlohmann::json json
Definition: engine_json_server.cpp:31