RoboidControl-cpp/Participants/ParticipantMQTT.h
Pascal Serrarens 26a80cd94a Improvements
2025-06-28 09:15:16 +02:00

129 lines
3.7 KiB
C++

#pragma once
#include "Messages/BinaryMsg.h"
#include "Messages/DestroyMsg.h"
#include "Messages/InvestigateMsg.h"
#include "Messages/ModelUrlMsg.h"
#include "Messages/NameMsg.h"
#include "Messages/NetworkIdMsg.h"
#include "Messages/ParticipantMsg.h"
#include "Messages/PoseMsg.h"
#include "Messages/TextMsg.h"
#include "Messages/ThingMsg.h"
#include "Participant.h"
#if !defined(NO_STD)
#include <functional>
#include <list>
// #include <unordered_map>
#endif
#if defined(_WIN32) || defined(_WIN64)
#include "Windows/WindowsParticipant.h"
#elif defined(__unix__) || defined(__APPLE__)
#include "Posix/PosixParticipant.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
namespace RoboidControl {
/// @brief A participant using UDP communication
/// A local participant is the local device which can communicate with
/// other participants It manages all local things and communcation with other
/// participants. Each application has a local participant which is usually
/// explicit in the code. An participant can be isolated. In that case it is
/// standalong and does not communicate with other participants.
///
/// It is possible to work with an hidden participant by creating things without
/// specifying a participant in the constructor. In that case an hidden isolated
/// participant is created which can be obtained using
/// RoboidControl::IsolatedParticipant::Isolated().
/// @sa RoboidControl::Thing::Thing()
class ParticipantMQTT : public Participant {
#pragma region Init
public:
/// @brief Create a participant which will try to connect to a MQTT server.
/// @param ipAddress The IP address of the MQTT server
/// @param port The port used by the MQTT server
ParticipantMQTT(const char* ipAddress, int port = 1883);
#pragma endregion Init
#pragma region Properties
public:
/// @brief The remote site when this participant is connected to a site
Participant* remoteSite = nullptr;
protected:
#if defined(__unix__) || defined(__APPLE__)
sockaddr_in server_addr;
int sock;
#endif
public:
// void Begin();
bool connected = false;
#pragma endregion Properties
#pragma region Update
public:
virtual void Update() override;
#pragma endregion Update
#pragma region Send
protected:
// MQTT Connect Packet Structure
struct MQTTConnectPacket {
uint8_t fixed_header;
uint8_t remaining_length;
uint8_t protocol_name_length;
const char* protocol_name;
uint8_t protocol_level;
uint8_t connect_flags;
uint16_t keep_alive;
const char* client_id;
};
void send_mqtt_connect(const char* client_id);
void sendSubscribe(const char* topic);
#pragma endregion Send
#pragma region Receive
protected:
int ReceiveTCP();
void ReceiveData(unsigned char bufferSize);
// void ReceiveData(unsigned char bufferSize, Participant*
// remoteParticipant);
// void SetupUDP(int localPort, const char* remoteIpAddress, int
// remotePort);
// void ReceiveUDP();
// virtual void Process(Participant* sender, ParticipantMsg* msg);
// virtual void Process(Participant* sender, NetworkIdMsg* msg);
// virtual void Process(Participant* sender, InvestigateMsg* msg);
// virtual void Process(Participant* sender, ThingMsg* msg);
// virtual void Process(Participant* sender, NameMsg* msg);
// virtual void Process(Participant* sender, ModelUrlMsg* msg);
// virtual void Process(Participant* sender, PoseMsg* msg);
// virtual void Process(Participant* sender, BinaryMsg* msg);
// virtual void Process(Participant* sender, TextMsg* msg);
// virtual void Process(Participant* sender, DestroyMsg* msg);
#pragma endregion Receive
};
} // namespace RoboidControl