Compare commits

...

2 Commits

Author SHA1 Message Date
12d91378e5 Fix refactoring issues 2025-04-08 10:43:20 +02:00
b594bd59f4 Refactoring 2025-04-08 09:49:52 +02:00
23 changed files with 210 additions and 238 deletions

View File

@ -19,7 +19,7 @@
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
void LocalParticipant::Setup(int localPort, void ParticipantUDP::Setup(int localPort,
const char* remoteIpAddress, const char* remoteIpAddress,
int remotePort) { int remotePort) {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
@ -44,7 +44,7 @@ void LocalParticipant::Setup(int localPort,
#endif #endif
} }
void LocalParticipant::GetBroadcastAddress() { void ParticipantUDP::GetBroadcastAddress() {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
IPAddress broadcastAddress = WiFi.localIP(); IPAddress broadcastAddress = WiFi.localIP();
broadcastAddress[3] = 255; broadcastAddress[3] = 255;
@ -56,7 +56,7 @@ void LocalParticipant::GetBroadcastAddress() {
#endif #endif
} }
void LocalParticipant::Receive() { void ParticipantUDP::Receive() {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
int packetSize = udp.parsePacket(); int packetSize = udp.parsePacket();
while (packetSize > 0) { while (packetSize > 0) {
@ -86,7 +86,7 @@ void LocalParticipant::Receive() {
#endif #endif
} }
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
// std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":" // std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":"
// << remoteParticipant->port << "\n"; // << remoteParticipant->port << "\n";
@ -106,7 +106,7 @@ bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
return true; return true;
} }
bool LocalParticipant::Publish(IMessage* msg) { bool ParticipantUDP::Publish(IMessage* msg) {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
int bufferSize = msg->Serialize((char*)this->buffer); int bufferSize = msg->Serialize((char*)this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "../LocalParticipant.h" #include "Participants/ParticipantUDP.h"
#if defined(HAS_WIFI) #if defined(HAS_WIFI)
#include <WiFiUdp.h> #include <WiFiUdp.h>
@ -9,7 +9,7 @@
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
class LocalParticipant : public RoboidControl::LocalParticipant { class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();

View File

@ -9,6 +9,7 @@ file(GLOB srcs
Windows/*.cpp Windows/*.cpp
EspIdf/*.cpp EspIdf/*.cpp
LinearAlgebra/*.cpp LinearAlgebra/*.cpp
Participants/*.cpp
) )
if(ESP_PLATFORM) if(ESP_PLATFORM)

View File

@ -1,11 +1,13 @@
#include "EspIdfParticipant.h" #include "EspIdfParticipant.h"
#if defined(IDF_VER)
#include "esp_wifi.h" #include "esp_wifi.h"
#endif
namespace RoboidControl { namespace RoboidControl {
namespace EspIdf { namespace EspIdf {
void LocalParticipant::Setup(int localPort, void ParticipantUDP::Setup(int localPort,
const char* remoteIpAddress, const char* remoteIpAddress,
int remotePort) { int remotePort) {
#if defined(IDF_VER) #if defined(IDF_VER)
@ -43,41 +45,41 @@ void LocalParticipant::Setup(int localPort,
return; return;
} }
// struct sockaddr_in dest_addr; // Initialize the dest_addr structure
memset(dest_addr.sin_zero, 0, sizeof(dest_addr.sin_zero)); memset(&this->dest_addr, 0, sizeof(this->dest_addr)); // Clear the entire structure
dest_addr.sin_family = AF_INET; this->dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(this->remoteSite->port); this->dest_addr.sin_port = htons(this->remoteSite->port);
inet_pton(AF_INET, this->remoteSite->ipAddress, &dest_addr.sin_addr.s_addr); inet_pton(AF_INET, this->remoteSite->ipAddress, &this->dest_addr.sin_addr.s_addr);
std::cout << "Wifi sync started local " << this->port << ", remote " std::cout << "Wifi sync started local " << this->port << ", remote "
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port << this->remoteSite->ipAddress << ":" << this->remoteSite->port
<< "\n"; << "\n";
#endif #endif // IDF_VER
} }
void LocalParticipant::GetBroadcastAddress() { void ParticipantUDP::GetBroadcastAddress() {
esp_netif_ip_info_t ip_info; // SOMEHOW, THIS FUNCTION RESULTS IN MEMORY CORRUPION...
esp_netif_t* esp_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
// Get IP information (IP address, netmask, gateway) // esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK) { // esp_netif_t* esp_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
std::cout << "Failed to get IP info\n";
return;
}
ip_addr_t broadcast_addr = {}; // // Get IP information (IP address, netmask, gateway)
broadcast_addr.u_addr.ip4.addr = // if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK) {
(ip_info.ip.addr & ip_info.netmask.addr) | ~ip_info.netmask.addr; // std::cout << "Failed to get IP info\n";
// return;
// }
this->broadcastIpAddress = new char[16]; // IPv4 address can have a max of 15 // ip_addr_t broadcast_addr = {};
// characters + null terminator // broadcast_addr.u_addr.ip4.addr =
snprintf(this->broadcastIpAddress, 16, IPSTR, // (ip_info.ip.addr & ip_info.netmask.addr) | ~ip_info.netmask.addr;
IP2STR(&broadcast_addr.u_addr.ip4));
std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n"; // snprintf(this->broadcastIpAddress, INET_ADDRSTRLEN, IPSTR,
// IP2STR(&broadcast_addr.u_addr.ip4));
// std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n";
} }
void LocalParticipant::Receive() { void ParticipantUDP::Receive() {
#if defined(IDF_VER) #if defined(IDF_VER)
struct pollfd fds; struct pollfd fds;
fds.fd = sockfd; fds.fd = sockfd;
@ -90,20 +92,21 @@ void LocalParticipant::Receive() {
return; return;
} }
socklen_t addr_len = sizeof(this->src_addr); socklen_t addr_len = sizeof(this->src_addr);
char sender_ipAddress[INET_ADDRSTRLEN];
while (ret > 0 && fds.revents & POLLIN) { while (ret > 0 && fds.revents & POLLIN) {
int packetSize = recvfrom(sockfd, buffer, sizeof(buffer) - 1, 0, int packetSize = recvfrom(this->sockfd, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr*)&this->src_addr, &addr_len); (struct sockaddr*)&this->src_addr, &addr_len);
if (packetSize < 0) { if (packetSize < 0) {
std::cout << "recvfrom() error\n"; std::cout << "recvfrom() error\n";
return; return;
} }
char sender_ipAddress[16]; std::cout << "receiving " << packetSize << " bytes\n";
inet_ntoa_r(this->src_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN); // inet_ntoa_r(this->src_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN);
unsigned int sender_port = ntohs(this->src_addr.sin_port); // unsigned int sender_port = ntohs(this->src_addr.sin_port);
ReceiveData(packetSize, sender_ipAddress, sender_port); // ReceiveData(packetSize, sender_ipAddress, sender_port);
int ret = poll(&fds, 1, 0); int ret = poll(&fds, 1, 0);
if (ret == -1) { if (ret == -1) {
@ -112,15 +115,15 @@ void LocalParticipant::Receive() {
} }
} }
#endif #endif // IDF_VER
} }
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
#if defined(IDF_VER) #if defined(IDF_VER)
std::cout << "Sending to " << remoteParticipant->ipAddress << ":" std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
<< remoteParticipant->port << "\n"; << remoteParticipant->port << "\n";
int err = sendto(sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr, int err = sendto(this->sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
sizeof(dest_addr)); sizeof(dest_addr));
if (errno != 0) if (errno != 0)
std::cout << "Send error " << err << " or " << errno << "\n"; std::cout << "Send error " << err << " or " << errno << "\n";
@ -129,7 +132,7 @@ bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
return true; return true;
} }
bool LocalParticipant::Publish(IMessage* msg) { bool ParticipantUDP::Publish(IMessage* msg) {
#if defined(IDF_VER) #if defined(IDF_VER)
int bufferSize = msg->Serialize((char*)this->buffer); int bufferSize = msg->Serialize((char*)this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)

View File

@ -1,13 +1,15 @@
#pragma once #pragma once
#include "../LocalParticipant.h" #include "Participants/ParticipantUDP.h"
#if defined(IDF_VER)
#include "lwip/sockets.h" #include "lwip/sockets.h"
#endif
namespace RoboidControl { namespace RoboidControl {
namespace EspIdf { namespace EspIdf {
class LocalParticipant : public RoboidControl::LocalParticipant { class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();
@ -15,13 +17,13 @@ class LocalParticipant : public RoboidControl::LocalParticipant {
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected:
// const char* remoteIpAddress = nullptr; #if defined(IDF_VER)
// unsigned short remotePort = 0; char broadcastIpAddress[INET_ADDRSTRLEN];
char* broadcastIpAddress = nullptr;
int sockfd; int sockfd;
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
struct sockaddr_in src_addr; struct sockaddr_in src_addr;
#endif
void GetBroadcastAddress(); void GetBroadcastAddress();
}; };

View File

@ -18,15 +18,15 @@ unsigned char IMessage::Serialize(char* buffer) {
return 0; return 0;
} }
// bool IMessage::SendMsg(LocalParticipant *client, IMessage msg) { // bool IMessage::SendMsg(ParticipantUDP *client, IMessage msg) {
// // return SendMsg(client, client.buffer, );nameLength // // return SendMsg(client, client.buffer, );nameLength
// return client->SendBuffer(msg.Serialize(client->buffer)); // return client->SendBuffer(msg.Serialize(client->buffer));
// } // }
// bool IMessage::Publish(LocalParticipant *participant) { // bool IMessage::Publish(ParticipantUDP *participant) {
// return participant->PublishBuffer(Serialize(participant->buffer)); // return participant->PublishBuffer(Serialize(participant->buffer));
// } // }
// bool IMessage::SendTo(LocalParticipant *participant) { // bool IMessage::SendTo(ParticipantUDP *participant) {
// return participant->SendBuffer(Serialize(participant->buffer)); // return participant->SendBuffer(Serialize(participant->buffer));
// } // }

View File

@ -6,7 +6,7 @@
namespace RoboidControl { namespace RoboidControl {
class LocalParticipant; class ParticipantUDP;
class IMessage { class IMessage {
public: public:
@ -15,8 +15,8 @@ class IMessage {
static unsigned char* ReceiveMsg(unsigned char packetSize); static unsigned char* ReceiveMsg(unsigned char packetSize);
// bool Publish(LocalParticipant *participant); // bool Publish(ParticipantUDP *participant);
// bool SendTo(LocalParticipant *participant); // bool SendTo(ParticipantUDP *participant);
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -19,7 +19,7 @@ unsigned char ParticipantMsg::Serialize(char* buffer) {
return ParticipantMsg::length; return ParticipantMsg::length;
} }
// bool ParticipantMsg::Send(LocalParticipant *participant, unsigned char networkId) { // bool ParticipantMsg::Send(ParticipantUDP *participant, unsigned char networkId) {
// ParticipantMsg msg = ParticipantMsg() // ParticipantMsg msg = ParticipantMsg()
// } // }
// Client Msg // Client Msg

View File

@ -27,6 +27,8 @@ Participant::~Participant() {
delete[] this->ipAddress; delete[] this->ipAddress;
} }
void Participant::Update(unsigned long currentTimeMs) {}
Thing* Participant::Get(unsigned char networkId, unsigned char thingId) { Thing* Participant::Get(unsigned char networkId, unsigned char thingId) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
// if (thing->networkId == networkId && thing->id == thingId) // if (thing->networkId == networkId && thing->id == thingId)

View File

@ -33,6 +33,8 @@ class Participant {
/// @brief Destructor for the participant /// @brief Destructor for the participant
~Participant(); ~Participant();
virtual void Update(unsigned long currentTimeMs = 0);
protected: protected:
#if defined(NO_STD) #if defined(NO_STD)
unsigned char thingCount = 0; unsigned char thingCount = 0;

View File

@ -0,0 +1,14 @@
#include "IsolatedParticipant.h"
#include "ParticipantUDP.h"
namespace RoboidControl {
static ParticipantUDP* isolatedParticipant = nullptr;
Participant* IsolatedParticipant::Isolated() {
if (isolatedParticipant == nullptr)
isolatedParticipant = new ParticipantUDP(0);
return isolatedParticipant;
}
} // namespace RoboidControl

View File

@ -0,0 +1,13 @@
#include "Participant.h"
namespace RoboidControl {
class IsolatedParticipant {
public:
/// @brief Isolated participant is used when the application is run without
/// networking
/// @return A participant without networking support
static Participant* Isolated();
};
}

View File

@ -1,4 +1,4 @@
#include "LocalParticipant.h" #include "ParticipantUDP.h"
#include "Thing.h" #include "Thing.h"
@ -25,14 +25,14 @@
namespace RoboidControl { namespace RoboidControl {
LocalParticipant::LocalParticipant(int port) { ParticipantUDP::ParticipantUDP(int port) {
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
this->port = port; this->port = port;
if (this->port == 0) if (this->port == 0)
this->isIsolated = true; this->isIsolated = true;
} }
LocalParticipant::LocalParticipant(const char* ipAddress, ParticipantUDP::ParticipantUDP(const char* ipAddress,
int port, int port,
int localPort) int localPort)
: Participant("127.0.0.1", localPort) { : Participant("127.0.0.1", localPort) {
@ -42,45 +42,45 @@ LocalParticipant::LocalParticipant(const char* ipAddress,
this->remoteSite = new Participant(ipAddress, port); this->remoteSite = new Participant(ipAddress, port);
} }
static LocalParticipant* isolatedParticipant = nullptr; static ParticipantUDP* isolatedParticipant = nullptr;
LocalParticipant* LocalParticipant::Isolated() { ParticipantUDP* ParticipantUDP::Isolated() {
if (isolatedParticipant == nullptr) if (isolatedParticipant == nullptr)
isolatedParticipant = new LocalParticipant(0); isolatedParticipant = new ParticipantUDP(0);
return isolatedParticipant; return isolatedParticipant;
} }
void LocalParticipant::begin() { void ParticipantUDP::begin() {
if (this->isIsolated) if (this->isIsolated)
return; return;
SetupUDP(this->port, this->remoteSite->ipAddress, this->remoteSite->port); SetupUDP(this->port, this->remoteSite->ipAddress, this->remoteSite->port);
} }
void LocalParticipant::SetupUDP(int localPort, void ParticipantUDP::SetupUDP(int localPort,
const char* remoteIpAddress, const char* remoteIpAddress,
int remotePort) { int remotePort) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::LocalParticipant* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::LocalParticipant*>(this); static_cast<Windows::ParticipantUDP*>(this);
thisWindows->Setup(localPort, remoteIpAddress, remotePort); thisWindows->Setup(localPort, remoteIpAddress, remotePort);
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
Posix::LocalParticipant* thisPosix = Posix::ParticipantUDP* thisPosix =
static_cast<Posix::LocalParticipant*>(this); static_cast<Posix::ParticipantUDP*>(this);
thisPosix->Setup(localPort, remoteIpAddress, remotePort); thisPosix->Setup(localPort, remoteIpAddress, remotePort);
#elif defined(ARDUINO) #elif defined(ARDUINO)
Arduino::LocalParticipant* thisArduino = Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::LocalParticipant*>(this); static_cast<Arduino::ParticipantUDP*>(this);
thisArduino->Setup(localPort, remoteIpAddress, remotePort); thisArduino->Setup(localPort, remoteIpAddress, remotePort);
#elif defined(IDF_VER) #elif defined(IDF_VER)
EspIdf::LocalParticipant* thisEspIdf = EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::LocalParticipant*>(this); static_cast<EspIdf::ParticipantUDP*>(this);
thisEspIdf->Setup(localPort, remoteIpAddress, remotePort); thisEspIdf->Setup(localPort, remoteIpAddress, remotePort);
#endif #endif
this->connected = true; this->connected = true;
} }
void LocalParticipant::Update(unsigned long currentTimeMs) { void ParticipantUDP::Update(unsigned long currentTimeMs) {
if (currentTimeMs == 0) if (currentTimeMs == 0)
currentTimeMs = Thing::GetTimeMs(); currentTimeMs = Thing::GetTimeMs();
@ -114,27 +114,27 @@ void LocalParticipant::Update(unsigned long currentTimeMs) {
} }
} }
void LocalParticipant::ReceiveUDP() { void ParticipantUDP::ReceiveUDP() {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::LocalParticipant* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::LocalParticipant*>(this); static_cast<Windows::ParticipantUDP*>(this);
thisWindows->Receive(); thisWindows->Receive();
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
Posix::LocalParticipant* thisPosix = Posix::ParticipantUDP* thisPosix =
static_cast<Posix::LocalParticipant*>(this); static_cast<Posix::ParticipantUDP*>(this);
thisPosix->Receive(); thisPosix->Receive();
#elif defined(ARDUINO) #elif defined(ARDUINO)
Arduino::LocalParticipant* thisArduino = Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::LocalParticipant*>(this); static_cast<Arduino::ParticipantUDP*>(this);
thisArduino->Receive(); thisArduino->Receive();
#elif defined(IDF_VER) #elif defined(IDF_VER)
EspIdf::LocalParticipant* thisEspIdf = EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::LocalParticipant*>(this); static_cast<EspIdf::ParticipantUDP*>(this);
thisEspIdf->Receive(); thisEspIdf->Receive();
#endif #endif
} }
Participant* LocalParticipant::GetParticipant(const char* ipAddress, int port) { Participant* ParticipantUDP::GetParticipant(const char* ipAddress, int port) {
for (Participant* sender : this->senders) { for (Participant* sender : this->senders) {
if (strcmp(sender->ipAddress, ipAddress) == 0 && sender->port == port) if (strcmp(sender->ipAddress, ipAddress) == 0 && sender->port == port)
return sender; return sender;
@ -142,7 +142,7 @@ Participant* LocalParticipant::GetParticipant(const char* ipAddress, int port) {
return nullptr; return nullptr;
} }
Participant* LocalParticipant::AddParticipant(const char* ipAddress, int port) { Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) {
// std::cout << "New Participant " << ipAddress << ":" << port << "\n"; // std::cout << "New Participant " << ipAddress << ":" << port << "\n";
Participant* participant = new Participant(ipAddress, port); Participant* participant = new Participant(ipAddress, port);
#if defined(NO_STD) #if defined(NO_STD)
@ -157,7 +157,7 @@ Participant* LocalParticipant::AddParticipant(const char* ipAddress, int port) {
#pragma region Send #pragma region Send
void LocalParticipant::SendThingInfo(Participant* remoteParticipant, void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
Thing* thing) { Thing* thing) {
// std::cout << "Send thing info " << (int)thing->id << " \n"; // std::cout << "Send thing info " << (int)thing->id << " \n";
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
@ -177,33 +177,33 @@ void LocalParticipant::SendThingInfo(Participant* remoteParticipant,
delete customMsg; delete customMsg;
} }
bool LocalParticipant::Send(Participant* remoteParticipant, IMessage* msg) { bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) {
int bufferSize = msg->Serialize(this->buffer); int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
return true; return true;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::LocalParticipant* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::LocalParticipant*>(this); static_cast<Windows::ParticipantUDP*>(this);
return thisWindows->Send(remoteParticipant, bufferSize); return thisWindows->Send(remoteParticipant, bufferSize);
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
Posix::LocalParticipant* thisPosix = Posix::ParticipantUDP* thisPosix =
static_cast<Posix::LocalParticipant*>(this); static_cast<Posix::ParticipantUDP*>(this);
return thisPosix->Send(remoteParticipant, bufferSize); return thisPosix->Send(remoteParticipant, bufferSize);
#elif defined(ARDUINO) #elif defined(ARDUINO)
Arduino::LocalParticipant* thisArduino = Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::LocalParticipant*>(this); static_cast<Arduino::ParticipantUDP*>(this);
return thisArduino->Send(remoteParticipant, bufferSize); return thisArduino->Send(remoteParticipant, bufferSize);
#elif defined(IDF_VER) #elif defined(IDF_VER)
EspIdf::LocalParticipant* thisEspIdf = EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::LocalParticipant*>(this); static_cast<EspIdf::ParticipantUDP*>(this);
return thisEspIdf->Send(remoteParticipant, bufferSize); return thisEspIdf->Send(remoteParticipant, bufferSize);
#else #else
return false; return false;
#endif #endif
} }
void LocalParticipant::PublishThingInfo(Thing* thing) { void ParticipantUDP::PublishThingInfo(Thing* thing) {
// std::cout << "Publish thing info" << thing->networkId << "\n"; // std::cout << "Publish thing info" << thing->networkId << "\n";
// Strange, when publishing, the network id is irrelevant, because it is // Strange, when publishing, the network id is irrelevant, because it is
// connected to a specific site... // connected to a specific site...
@ -224,22 +224,22 @@ void LocalParticipant::PublishThingInfo(Thing* thing) {
delete customMsg; delete customMsg;
} }
bool LocalParticipant::Publish(IMessage* msg) { bool ParticipantUDP::Publish(IMessage* msg) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::LocalParticipant* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::LocalParticipant*>(this); static_cast<Windows::ParticipantUDP*>(this);
return thisWindows->Publish(msg); return thisWindows->Publish(msg);
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
Posix::LocalParticipant* thisPosix = Posix::ParticipantUDP* thisPosix =
static_cast<Posix::LocalParticipant*>(this); static_cast<Posix::ParticipantUDP*>(this);
return thisPosix->Publish(msg); return thisPosix->Publish(msg);
#elif defined(ARDUINO) #elif defined(ARDUINO)
Arduino::LocalParticipant* thisArduino = Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::LocalParticipant*>(this); static_cast<Arduino::ParticipantUDP*>(this);
return thisArduino->Publish(msg); return thisArduino->Publish(msg);
#elif defined(IDF_VER) #elif defined(IDF_VER)
EspIdf::LocalParticipant* thisEspIdf = EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::LocalParticipant*>(this); static_cast<EspIdf::ParticipantUDP*>(this);
return thisEspIdf->Publish(msg); return thisEspIdf->Publish(msg);
#else #else
return false; return false;
@ -251,7 +251,7 @@ bool LocalParticipant::Publish(IMessage* msg) {
#pragma region Receive #pragma region Receive
void LocalParticipant::ReceiveData(unsigned char packetSize, void ParticipantUDP::ReceiveData(unsigned char packetSize,
char* senderIpAddress, char* senderIpAddress,
unsigned int senderPort) { unsigned int senderPort) {
Participant* remoteParticipant = Participant* remoteParticipant =
@ -268,7 +268,7 @@ void LocalParticipant::ReceiveData(unsigned char packetSize,
ReceiveData(packetSize, remoteParticipant); ReceiveData(packetSize, remoteParticipant);
} }
void LocalParticipant::ReceiveData(unsigned char bufferSize, void ParticipantUDP::ReceiveData(unsigned char bufferSize,
Participant* remoteParticipant) { Participant* remoteParticipant) {
unsigned char msgId = this->buffer[0]; unsigned char msgId = this->buffer[0];
// std::cout << "receive msg " << (int)msgId << "\n"; // std::cout << "receive msg " << (int)msgId << "\n";
@ -311,9 +311,9 @@ void LocalParticipant::ReceiveData(unsigned char bufferSize,
}; };
} }
void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {} void ParticipantUDP::Process(Participant* sender, ParticipantMsg* msg) {}
void LocalParticipant::Process(Participant* sender, SiteMsg* msg) { void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
std::cout << this->name << ": process Site Id " << (int)this->networkId std::cout << this->name << ": process Site Id " << (int)this->networkId
<< "->" << (int)msg->networkId << "\n"; << "->" << (int)msg->networkId << "\n";
if (this->networkId != msg->networkId) { if (this->networkId != msg->networkId) {
@ -324,25 +324,14 @@ void LocalParticipant::Process(Participant* sender, SiteMsg* msg) {
} }
} }
void LocalParticipant::Process(Participant* sender, InvestigateMsg* msg) {} void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {}
void LocalParticipant::Process(Participant* sender, ThingMsg* msg) { void ParticipantUDP::Process(Participant* sender, ThingMsg* msg) {
std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/" std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/"
<< (int)msg->thingId << "]\n"; << (int)msg->thingId << "]\n";
#if !defined(NO_STD)
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
//Thing* newThing;
if (thingMsgProcessor != thingMsgProcessors.end()) { // found item
//newThing =
thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
} else
//newThing =
new Thing(sender, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
#endif
} }
void LocalParticipant::Process(Participant* sender, NameMsg* msg) { void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
std::cout << this->name << ": process Name [" << (int)msg->networkId << "/" std::cout << this->name << ": process Name [" << (int)msg->networkId << "/"
<< (int)msg->thingId << "]\n"; << (int)msg->thingId << "]\n";
@ -369,14 +358,14 @@ void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
} }
} }
void LocalParticipant::Process(Participant* sender, PoseMsg* msg) { void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
std::cout << this->name << ": process Pose [" << (int)this->networkId << "/" std::cout << this->name << ": process Pose [" << (int)this->networkId << "/"
<< (int)msg->networkId << "]\n"; << (int)msg->networkId << "]\n";
} }
void LocalParticipant::Process(Participant* sender, BinaryMsg* msg) { void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
std::cout << this->name << ": process Binary [" << (int)this->networkId << "/" std::cout << this->name << ": process Binary [" << (int)msg->networkId << "/"
<< (int)msg->networkId << "]\n"; << (int)msg->thingId << "]\n";
Thing* thing = sender->Get(msg->networkId, msg->thingId); Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBinary(msg->bytes); thing->ProcessBinary(msg->bytes);

View File

@ -23,8 +23,6 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#elif defined(ARDUINO)
// #include <WiFiUdp.h>
#endif #endif
namespace RoboidControl { namespace RoboidControl {
@ -40,20 +38,20 @@ constexpr int MAX_SENDER_COUNT = 256;
/// It is possible to work with an hidden participant by creating things without /// 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 /// specifying a participant in the constructor. In that case an hidden isolated
/// participant is created which can be obtained using /// participant is created which can be obtained using
/// RoboidControl::LocalParticipant::Isolated(). /// RoboidControl::IsolatedParticipant::Isolated().
/// @sa RoboidControl::Thing::Thing() /// @sa RoboidControl::Thing::Thing()
class LocalParticipant : public Participant { class ParticipantUDP : public Participant {
public: public:
/// @brief Create a participant without connecting to a site /// @brief Create a participant without connecting to a site
/// @param port The port on which the participant communicates /// @param port The port on which the participant communicates
/// These participant typically broadcast Participant messages to let site /// These participant typically broadcast Participant messages to let site
/// servers on the local network know their presence. Alternatively they can /// servers on the local network know their presence. Alternatively they can
/// broadcast information which can be used directly by other participants. /// broadcast information which can be used directly by other participants.
LocalParticipant(int port = 7681); ParticipantUDP(int port = 7681);
/// @brief Create a participant which will try to connect to a site. /// @brief Create a participant which will try to connect to a site.
/// @param ipAddress The IP address of the site /// @param ipAddress The IP address of the site
/// @param port The port used by the site /// @param port The port used by the site
LocalParticipant(const char* ipAddress, ParticipantUDP(const char* ipAddress,
int port = 7681, int port = 7681,
int localPort = 7681); int localPort = 7681);
// Note to self: one cannot specify the port used by the local participant // Note to self: one cannot specify the port used by the local participant
@ -62,7 +60,7 @@ class LocalParticipant : public Participant {
/// @brief Isolated participant is used when the application is run without /// @brief Isolated participant is used when the application is run without
/// networking /// networking
/// @return A participant without networking support /// @return A participant without networking support
static LocalParticipant* Isolated(); static ParticipantUDP* Isolated();
/// @brief True if the participant is running isolated. /// @brief True if the participant is running isolated.
/// Isolated participants do not communicate with other participants /// Isolated participants do not communicate with other participants
@ -73,7 +71,7 @@ class LocalParticipant : public Participant {
long publishInterval = 3000; // 3 seconds long publishInterval = 3000; // 3 seconds
/// @brief The name of the participant /// @brief The name of the participant
const char* name = "LocalParticipant"; const char* name = "ParticipantUDP";
// int localPort = 0; // int localPort = 0;
@ -101,7 +99,7 @@ class LocalParticipant : public Participant {
void begin(); void begin();
bool connected = false; bool connected = false;
virtual void Update(unsigned long currentTimeMs = 0); virtual void Update(unsigned long currentTimeMs = 0) override;
void SendThingInfo(Participant* remoteParticipant, Thing* thing); void SendThingInfo(Participant* remoteParticipant, Thing* thing);
void PublishThingInfo(Thing* thing); void PublishThingInfo(Thing* thing);
@ -142,31 +140,31 @@ class LocalParticipant : public Participant {
virtual void Process(Participant* sender, BinaryMsg* msg); virtual void Process(Participant* sender, BinaryMsg* msg);
#if !defined(NO_STD) #if !defined(NO_STD)
public: // public:
using ThingConstructor = std::function<Thing*(Participant* participant, // using ThingConstructor = std::function<Thing*(Participant* participant,
unsigned char networkId, // unsigned char networkId,
unsigned char thingId)>; // unsigned char thingId)>;
template <typename ThingClass> // template <typename ThingClass>
void Register(unsigned char thingType) { // void Register(unsigned char thingType) {
thingMsgProcessors[thingType] = [](Participant* participant, // thingMsgProcessors[thingType] = [](Participant* participant,
unsigned char networkId, // unsigned char networkId,
unsigned char thingId) { // unsigned char thingId) {
return new ThingClass(participant, networkId, thingId); // return new ThingClass(participant, networkId, thingId);
}; // };
}; // };
template <typename ThingClass> // template <typename ThingClass>
void Register2(unsigned char thingType, ThingConstructor f) { // void Register2(unsigned char thingType, ThingConstructor f) {
thingMsgProcessors[thingType] = [f](Participant* participant, // thingMsgProcessors[thingType] = [f](Participant* participant,
unsigned char networkId, // unsigned char networkId,
unsigned char thingId) { // unsigned char thingId) {
return f(participant, networkId, thingId); // return f(participant, networkId, thingId);
}; // };
}; // };
protected: // protected:
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors; // std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
#endif #endif
}; };

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "LocalParticipant.h" #include "ParticipantUDP.h"
#if !defined(NO_STD) #if !defined(NO_STD)
#include <functional> #include <functional>
@ -11,7 +11,7 @@
namespace RoboidControl { namespace RoboidControl {
/// @brief A participant is device which can communicate with other participants /// @brief A participant is device which can communicate with other participants
class SiteServer : public LocalParticipant { class SiteServer : public ParticipantUDP {
public: public:
SiteServer(int port = 7681); SiteServer(int port = 7681);

View File

@ -11,7 +11,7 @@
namespace RoboidControl { namespace RoboidControl {
namespace Posix { namespace Posix {
void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int remotePort) { void Setup(int localPort, const char* remoteIpAddress, int remotePort) {
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
// Create a UDP socket // Create a UDP socket
@ -63,7 +63,7 @@ void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int rem
#endif #endif
} }
void LocalParticipant::Receive() { void Receive() {
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
sockaddr_in client_addr; sockaddr_in client_addr;
socklen_t len = sizeof(client_addr); socklen_t len = sizeof(client_addr);
@ -90,7 +90,7 @@ void LocalParticipant::Receive() {
#endif #endif
} }
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) { bool Send(Participant* remoteParticipant, int bufferSize) {
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
// std::cout << "Send to " << remoteParticipant->ipAddress << ":" << ntohs(remoteParticipant->port) // std::cout << "Send to " << remoteParticipant->ipAddress << ":" << ntohs(remoteParticipant->port)
// << "\n"; // << "\n";
@ -113,7 +113,7 @@ bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
return true; return true;
} }
bool LocalParticipant::Publish(IMessage* msg) { bool Publish(IMessage* msg) {
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
int bufferSize = msg->Serialize(this->buffer); int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include "../LocalParticipant.h" #include "Participants/ParticipantUDP.h"
namespace RoboidControl { namespace RoboidControl {
namespace Posix { namespace Posix {
class LocalParticipant : public RoboidControl::LocalParticipant { class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();

View File

@ -1,6 +1,7 @@
#include "Thing.h" #include "Thing.h"
#include "LocalParticipant.h" #include "Participant.h"
#include "Participants/IsolatedParticipant.h"
#include <string.h> #include <string.h>
@ -21,7 +22,7 @@ namespace RoboidControl {
// return isolatedParticipant; // return isolatedParticipant;
// } // }
Thing::Thing(int thingType) : Thing(LocalParticipant::Isolated(), thingType) {} Thing::Thing(int thingType) : Thing(IsolatedParticipant::Isolated(), thingType) {}
Thing::Thing(Participant* owner, Type thingType) Thing::Thing(Participant* owner, Type thingType)
: Thing(owner, (unsigned char)thingType) {} : Thing(owner, (unsigned char)thingType) {}
@ -201,7 +202,7 @@ void Thing::Update(unsigned long currentTimeMs, bool recursive) {
} }
void Thing::UpdateThings(unsigned long currentTimeMs) { void Thing::UpdateThings(unsigned long currentTimeMs) {
LocalParticipant::Isolated()->Update(currentTimeMs); IsolatedParticipant::Isolated()->Update(currentTimeMs);
} }
void Thing::GenerateBinary(char* buffer, unsigned char* ix) { void Thing::GenerateBinary(char* buffer, unsigned char* ix) {

View File

@ -10,7 +10,7 @@
namespace RoboidControl { namespace RoboidControl {
class Participant; class Participant;
class LocalParticipant; class ParticipantUDP;
#define THING_STORE_SIZE 256 #define THING_STORE_SIZE 256
// IMPORTANT: values higher than 256 will need to change the Thing::id type // IMPORTANT: values higher than 256 will need to change the Thing::id type

View File

@ -4,36 +4,22 @@
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#elif defined(__unix__) || defined(__APPLE__)
#include <arpa/inet.h>
#include <fcntl.h> // For fcntl
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#endif #endif
namespace RoboidControl { namespace RoboidControl {
namespace Windows { namespace Windows {
void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int remotePort) { void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
// Create a UDP socket // Create a UDP socket
#if defined(_WIN32) || defined(_WIN64)
// Windows-specific Winsock initialization // Windows-specific Winsock initialization
WSADATA wsaData; WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed" << std::endl; std::cerr << "WSAStartup failed" << std::endl;
return; return;
} }
#endif
#if defined(_WIN32) || defined(_WIN64)
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#elif defined(__unix__) || defined(__APPLE__)
this->sock = socket(AF_INET, SOCK_DGRAM, 0);
#endif
if (this->sock < 0) { if (this->sock < 0) {
std::cerr << "Error creating socket" << std::endl; std::cerr << "Error creating socket" << std::endl;
@ -41,13 +27,8 @@ void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int rem
} }
// Set the socket to non-blocking mode // Set the socket to non-blocking mode
#if defined(_WIN32) || defined(_WIN64)
u_long mode = 1; // 1 to enable non-blocking socket u_long mode = 1; // 1 to enable non-blocking socket
ioctlsocket(this->sock, FIONBIO, &mode); ioctlsocket(this->sock, FIONBIO, &mode);
#elif defined(__unix__) || defined(__APPLE__)
int flags = fcntl(this->sock, F_GETFL, 0);
fcntl(this->sock, F_SETFL, flags | O_NONBLOCK);
#endif
if (remotePort != 0) { if (remotePort != 0) {
// Set up the address to send to // Set up the address to send to
@ -56,12 +37,8 @@ void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int rem
remote_addr.sin_port = htons((u_short)remotePort); remote_addr.sin_port = htons((u_short)remotePort);
if (inet_pton(AF_INET, remoteIpAddress, &remote_addr.sin_addr) <= 0) { if (inet_pton(AF_INET, remoteIpAddress, &remote_addr.sin_addr) <= 0) {
std::cerr << "Invalid address" << std::endl; std::cerr << "Invalid address" << std::endl;
#if defined(_WIN32) || defined(_WIN64)
closesocket(sock); closesocket(sock);
WSACleanup(); WSACleanup();
#elif defined(__unix__) || defined(__APPLE__)
close(sock);
#endif
return; return;
} }
} }
@ -72,30 +49,22 @@ void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int rem
server_addr.sin_port = htons((u_short)localPort); server_addr.sin_port = htons((u_short)localPort);
if (inet_pton(AF_INET, "0.0.0.0", &server_addr.sin_addr) <= 0) { if (inet_pton(AF_INET, "0.0.0.0", &server_addr.sin_addr) <= 0) {
std::cerr << "Invalid address" << std::endl; std::cerr << "Invalid address" << std::endl;
#if defined(_WIN32) || defined(_WIN64)
closesocket(sock); closesocket(sock);
WSACleanup(); WSACleanup();
#elif defined(__unix__) || defined(__APPLE__)
close(sock);
#endif
return; return;
} }
// Bind the socket to the specified port // Bind the socket to the specified port
if (bind(this->sock, (const struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { if (bind(this->sock, (const struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Bind failed" << std::endl; std::cerr << "Bind failed" << std::endl;
#if defined(_WIN32) || defined(_WIN64)
closesocket(sock); closesocket(sock);
WSACleanup(); WSACleanup();
#elif defined(__unix__) || defined(__APPLE__)
close(sock);
#endif
} }
#endif #endif // _WIN32 || _WIN64
} }
void LocalParticipant::Receive() { void ParticipantUDP::Receive() {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
// char ip_str[INET_ADDRSTRLEN]; // char ip_str[INET_ADDRSTRLEN];
// inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN); // inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
@ -103,28 +72,20 @@ void LocalParticipant::Receive() {
// << ntohs(server_addr.sin_port) << "\n"; // << ntohs(server_addr.sin_port) << "\n";
sockaddr_in client_addr; sockaddr_in client_addr;
#if defined(_WIN32) || defined(_WIN64)
int len = sizeof(client_addr); int len = sizeof(client_addr);
#elif defined(__unix__) || defined(__APPLE__)
socklen_t len = sizeof(client_addr);
#endif
int packetSize = recvfrom(this->sock, buffer, sizeof(buffer), 0, (struct sockaddr*)&client_addr, &len); int packetSize = recvfrom(this->sock, buffer, sizeof(buffer), 0, (struct sockaddr*)&client_addr, &len);
// std::cout << "received data " << packetSize << "\n"; // std::cout << "received data " << packetSize << "\n";
if (packetSize < 0) { if (packetSize < 0) {
#if defined(_WIN32) || defined(_WIN64)
int error_code = WSAGetLastError(); // Get the error code on Windows int error_code = WSAGetLastError(); // Get the error code on Windows
if (error_code != WSAEWOULDBLOCK) if (error_code != WSAEWOULDBLOCK)
std::cerr << "recvfrom failed with error: " << error_code << std::endl; std::cerr << "recvfrom failed with error: " << error_code << std::endl;
#else
// std::cerr << "recvfrom failed with error: " << packetSize << std::endl;
#endif
} else if (packetSize > 0) { } else if (packetSize > 0) {
char sender_ipAddress[INET_ADDRSTRLEN]; char sender_ipAddress[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(client_addr.sin_addr), sender_ipAddress, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(client_addr.sin_addr), sender_ipAddress, INET_ADDRSTRLEN);
unsigned int sender_port = ntohs(client_addr.sin_port); unsigned int sender_port = ntohs(client_addr.sin_port);
ReceiveData(packetSize, sender_ipAddress, sender_port); ReceiveData(packetSize, sender_ipAddress, sender_port);
// RoboidControl::LocalParticipant* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port); // RoboidControl::ParticipantUDP* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port);
// if (remoteParticipant == nullptr) { // if (remoteParticipant == nullptr) {
// remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); // remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port);
// // std::cout << "New sender " << sender_ipAddress << ":" // // std::cout << "New sender " << sender_ipAddress << ":"
@ -138,16 +99,16 @@ void LocalParticipant::Receive() {
// ReceiveData(packetSize, remoteParticipant); // ReceiveData(packetSize, remoteParticipant);
} }
#endif #endif // _WIN32 || _WIN64
} }
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
char ip_str[INET_ADDRSTRLEN]; char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port) << "\n"; std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port) << "\n";
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr*)&remote_addr, sizeof(remote_addr)); int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr*)&remote_addr, sizeof(remote_addr));
#if defined(_WIN32) || defined(_WIN64)
if (sent_bytes <= SOCKET_ERROR) { if (sent_bytes <= SOCKET_ERROR) {
int error_code = WSAGetLastError(); // Get the error code on Windows int error_code = WSAGetLastError(); // Get the error code on Windows
std::cerr << "sendto failed with error: " << error_code << std::endl; std::cerr << "sendto failed with error: " << error_code << std::endl;
@ -155,18 +116,11 @@ bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
WSACleanup(); WSACleanup();
return false; return false;
} }
#elif defined(__unix__) || defined(__APPLE__) #endif // _WIN32 || _WIN64
if (sent_bytes < 0) {
std::cerr << "sendto failed with error: " << sent_bytes << std::endl;
close(sock);
return false;
}
#endif
#endif
return true; return true;
} }
bool LocalParticipant::Publish(IMessage* msg) { bool ParticipantUDP::Publish(IMessage* msg) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int bufferSize = msg->Serialize(this->buffer); int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
@ -176,7 +130,7 @@ bool LocalParticipant::Publish(IMessage* msg) {
inet_ntop(AF_INET, &(broadcast_addr.sin_addr), ip_str, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(broadcast_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
std::cout << "Publish to " << ip_str << ":" << ntohs(broadcast_addr.sin_port) << "\n"; std::cout << "Publish to " << ip_str << ":" << ntohs(broadcast_addr.sin_port) << "\n";
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr*)&broadcast_addr, sizeof(broadcast_addr)); int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr*)&broadcast_addr, sizeof(broadcast_addr));
#if defined(_WIN32) || defined(_WIN64)
if (sent_bytes <= SOCKET_ERROR) { if (sent_bytes <= SOCKET_ERROR) {
int error_code = WSAGetLastError(); // Get the error code on Windows int error_code = WSAGetLastError(); // Get the error code on Windows
std::cerr << "sendto failed with error: " << error_code << std::endl; std::cerr << "sendto failed with error: " << error_code << std::endl;
@ -184,14 +138,7 @@ bool LocalParticipant::Publish(IMessage* msg) {
WSACleanup(); WSACleanup();
return false; return false;
} }
#elif defined(__unix__) || defined(__APPLE__) #endif // _WIN32 || _WIN64
if (sent_bytes < 0) {
std::cerr << "sendto failed with error: " << sent_bytes << std::endl;
close(sock);
return false;
}
#endif
#endif
return true; return true;
} }

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include "../LocalParticipant.h" #include "Participants/ParticipantUDP.h"
namespace RoboidControl { namespace RoboidControl {
namespace Windows { namespace Windows {
class LocalParticipant : public RoboidControl::LocalParticipant { class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();

View File

@ -9,7 +9,7 @@
// #include <ws2tcpip.h> // #include <ws2tcpip.h>
#include "Participant.h" #include "Participant.h"
#include "SiteServer.h" #include "Participants/SiteServer.h"
#include "Thing.h" #include "Thing.h"
using namespace RoboidControl; using namespace RoboidControl;
@ -52,8 +52,8 @@ protected:
// TEST_F(ParticipantSuite, LocalParticipant) { // TEST_F(ParticipantSuite, ParticipantUDP) {
// LocalParticipant* participant = new LocalParticipant("127.0.0.1", 7681); // ParticipantUDP* participant = new ParticipantUDP("127.0.0.1", 7681);
// unsigned long milliseconds = get_time_ms(); // unsigned long milliseconds = get_time_ms();
// unsigned long startTime = milliseconds; // unsigned long startTime = milliseconds;
@ -80,7 +80,7 @@ protected:
// TEST_F(ParticipantSuite, SiteParticipant) { // TEST_F(ParticipantSuite, SiteParticipant) {
// SiteServer site = SiteServer(7681); // SiteServer site = SiteServer(7681);
// LocalParticipant participant = LocalParticipant("127.0.0.1", 7681); // ParticipantUDP participant = ParticipantUDP("127.0.0.1", 7681);
// unsigned long milliseconds = get_time_ms(); // unsigned long milliseconds = get_time_ms();
// unsigned long startTime = milliseconds; // unsigned long startTime = milliseconds;
@ -96,7 +96,7 @@ protected:
// TEST_F(ParticipantSuite, Thing) { // TEST_F(ParticipantSuite, Thing) {
// SiteServer site = SiteServer(7681); // SiteServer site = SiteServer(7681);
// LocalParticipant participant = LocalParticipant("127.0.0.1", 7681); // ParticipantUDP participant = ParticipantUDP("127.0.0.1", 7681);
// Thing thing = Thing(&participant); // Thing thing = Thing(&participant);
// unsigned long milliseconds = get_time_ms(); // unsigned long milliseconds = get_time_ms();

View File

@ -4,7 +4,7 @@
// not supported using Visual Studio 2022 compiler... // not supported using Visual Studio 2022 compiler...
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "LocalParticipant.h" #include "Participants/ParticipantUDP.h"
#include "Thing.h" #include "Thing.h"
using namespace RoboidControl; using namespace RoboidControl;
@ -23,7 +23,7 @@ TEST(RoboidControlSuite, HiddenParticipant) {
} }
TEST(RoboidControlSuite, IsolatedParticipant) { TEST(RoboidControlSuite, IsolatedParticipant) {
LocalParticipant* participant = LocalParticipant::Isolated(); ParticipantUDP* participant = ParticipantUDP::Isolated();
Thing* thing = new Thing(participant); Thing* thing = new Thing(participant);
unsigned long milliseconds = Thing::GetTimeMs(); unsigned long milliseconds = Thing::GetTimeMs();