Compare commits
8 Commits
230a41f143
...
19a5c5d4d5
Author | SHA1 | Date | |
---|---|---|---|
19a5c5d4d5 | |||
f20ebd7ad2 | |||
8901e6933a | |||
b5f07f77c2 | |||
9a26e98b0f | |||
1add0647e1 | |||
0ad9d3e9ec | |||
df19ecf953 |
@ -1,4 +1,5 @@
|
||||
#include "ArduinoParticipant.h"
|
||||
#if defined(ARDUINO)
|
||||
|
||||
#if !defined(NO_STD)
|
||||
#include <iostream>
|
||||
@ -21,7 +22,6 @@
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
WiFiUDP* udp;
|
||||
@ -88,7 +88,7 @@ void ParticipantUDP::Receive() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
// std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":"
|
||||
// << remoteParticipant->port << "\n";
|
||||
@ -131,5 +131,5 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
return true;
|
||||
};
|
||||
|
||||
} // namespace Arduino
|
||||
} // namespace RoboidControl
|
||||
#endif
|
@ -1,15 +1,15 @@
|
||||
#pragma once
|
||||
#if defined(ARDUINO)
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class ParticipantUDP : public ParticipantUDPGeneric {
|
||||
public:
|
||||
void Setup();
|
||||
void Receive();
|
||||
bool Send(Participant* remoteParticipant, int bufferSize);
|
||||
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
protected:
|
||||
@ -18,5 +18,5 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
void GetBroadcastAddress();
|
||||
};
|
||||
|
||||
} // namespace Arduino
|
||||
} // namespace RoboidControl
|
||||
#endif
|
@ -19,7 +19,7 @@
|
||||
using namespace RoboidControl;
|
||||
using namespace RoboidControl::Arduino;
|
||||
|
||||
ParticipantUDP* localParticipant;
|
||||
ParticipantUDPGeneric* localParticipant;
|
||||
|
||||
DifferentialDrive* bb2b;
|
||||
TouchSensor* touchLeft;
|
||||
@ -34,7 +34,7 @@ void setup() {
|
||||
Serial.println("started");
|
||||
|
||||
StartWifi("serrarens", "192.168.76.44");
|
||||
localParticipant = new ParticipantUDP("192.168.77.76");
|
||||
localParticipant = new ParticipantUDPGeneric("192.168.77.76");
|
||||
|
||||
bb2b = new DifferentialDrive();
|
||||
touchLeft = new TouchSensor(bb2b);
|
||||
|
@ -4,13 +4,12 @@
|
||||
#include "esp_wifi.h"
|
||||
#endif
|
||||
|
||||
#include <arpa/inet.h>
|
||||
namespace RoboidControl {
|
||||
namespace EspIdf {
|
||||
|
||||
void ParticipantUDP::Setup(int localPort,
|
||||
void ParticipantUDP::SetupUDP(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
#if defined(IDF_VER)
|
||||
std::cout << "Set up UDP\n";
|
||||
GetBroadcastAddress();
|
||||
|
||||
@ -22,42 +21,57 @@ void ParticipantUDP::Setup(int localPort,
|
||||
}
|
||||
|
||||
// Create a UDP socket
|
||||
this->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (this->sockfd < 0) {
|
||||
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (this->sock < 0) {
|
||||
std::cout << "Unable to create UDP socket: errno " << errno << "\n";
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set up the server address structure
|
||||
/*
|
||||
// Set up the receiving(local) address
|
||||
struct sockaddr_in local_addr;
|
||||
memset(&local_addr, 0, sizeof(local_addr));
|
||||
local_addr.sin_family = AF_INET;
|
||||
local_addr.sin_port = htons(this->port);
|
||||
local_addr.sin_port = htons(localPort);
|
||||
local_addr.sin_addr.s_addr =
|
||||
htonl(INADDR_ANY); // Listen on all available network interfaces
|
||||
|
||||
// Bind the socket to the address and port
|
||||
if (bind(this->sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) <
|
||||
0) {
|
||||
// Bind the socket to the receiving address
|
||||
if (bind(this->sock, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) {
|
||||
std::cout << "Unable to bind UDP socket: errno " << errno << "\n";
|
||||
close(sockfd);
|
||||
close(sock);
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
// Initialize the dest_addr structure
|
||||
// Initialize the destination(remote) address
|
||||
memset(&this->dest_addr, 0,
|
||||
sizeof(this->dest_addr)); // Clear the entire structure
|
||||
this->dest_addr.sin_family = AF_INET;
|
||||
this->dest_addr.sin_port = htons(this->remoteSite->port);
|
||||
inet_pton(AF_INET, this->remoteSite->ipAddress,
|
||||
&this->dest_addr.sin_addr.s_addr);
|
||||
this->dest_addr.sin_addr.s_addr = inet_addr(this->remoteSite->ipAddress);
|
||||
// inet_pton(AF_INET, this->remoteSite->ipAddress,
|
||||
// &this->dest_addr.sin_addr.s_addr);
|
||||
|
||||
std::cout << "Wifi sync started local " << this->port << ", remote "
|
||||
this->connected = true;
|
||||
|
||||
std::cout << "Wifi sync started local " << localPort << ", remote "
|
||||
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port
|
||||
<< "\n";
|
||||
#endif // IDF_VER
|
||||
|
||||
// std::cout << "socket: " << (int)this->sock << std::endl;
|
||||
// ParticipantMsg* msg = new ParticipantMsg(this->networkId);
|
||||
// int bufferSize = msg->Serialize(this->buffer);
|
||||
// int err = sendto(this->sock, buffer, bufferSize, 0,
|
||||
// (struct sockaddr*)&dest_addr, sizeof(dest_addr));
|
||||
// if (errno != 0)
|
||||
// std::cout << "AASend error " << err << " or " << errno << "\n";
|
||||
// else
|
||||
// std::cout << "AASend SUCCESS\n";
|
||||
|
||||
//SendTest();
|
||||
}
|
||||
|
||||
void ParticipantUDP::GetBroadcastAddress() {
|
||||
@ -83,10 +97,11 @@ void ParticipantUDP::GetBroadcastAddress() {
|
||||
#endif // IDF_VER
|
||||
}
|
||||
|
||||
void ParticipantUDP::Receive() {
|
||||
void ParticipantUDP::ReceiveUDP() {
|
||||
#if defined(IDF_VER)
|
||||
/*
|
||||
struct pollfd fds[1];
|
||||
fds[0].fd = sockfd;
|
||||
fds[0].fd = sock;
|
||||
fds[0].events = POLLIN; // We're looking for data available to read
|
||||
|
||||
// Use poll() with a timeout of 0 to return immediately
|
||||
@ -103,7 +118,7 @@ void ParticipantUDP::Receive() {
|
||||
char sender_ipAddress[INET_ADDRSTRLEN];
|
||||
|
||||
while (ret > 0 && fds[0].revents & POLLIN) {
|
||||
int packetSize = recvfrom(this->sockfd, buffer, sizeof(buffer) - 1, 0,
|
||||
int packetSize = recvfrom(this->sock, buffer, sizeof(buffer) - 1, 0,
|
||||
(struct sockaddr*)&source_addr, &addr_len);
|
||||
if (packetSize < 0) {
|
||||
std::cout << "recvfrom() error\n";
|
||||
@ -126,18 +141,66 @@ void ParticipantUDP::Receive() {
|
||||
}
|
||||
}
|
||||
// std::cout << "no more messages\n";
|
||||
|
||||
*/
|
||||
#endif // IDF_VER
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
#if defined(IDF_VER)
|
||||
// std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
|
||||
// << remoteParticipant->port << "\n";
|
||||
ParticipantUDP::ParticipantUDP(int port) : ParticipantUDPGeneric(port) {}
|
||||
|
||||
int err = sendto(this->sockfd, buffer, bufferSize, 0,
|
||||
(struct sockaddr*)&dest_addr, sizeof(dest_addr));
|
||||
if (errno != 0)
|
||||
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
|
||||
: ParticipantUDPGeneric(ipAddress, port, localPort) {}
|
||||
|
||||
// bool ParticipantUDP::SendTest() {
|
||||
// #if defined(IDF_VER)
|
||||
// // std::cout << "socket: " << (int)this->sock << std::endl;
|
||||
// // UBaseType_t stack_size = uxTaskGetStackHighWaterMark(NULL); // NULL to check the main task
|
||||
// // size_t free_heap = xPortGetFreeHeapSize();
|
||||
// // std::cout << "Stack High Water Mark: " << stack_size << " heap " << free_heap << std::endl;
|
||||
|
||||
// ParticipantMsg* msg = new ParticipantMsg(this->networkId);
|
||||
// int bSize = msg->Serialize(this->buffer);
|
||||
// // std::cout << "buffer size " << bSize << std::endl;
|
||||
// int err = sendto(this->sock, buffer, bSize, 0, (struct sockaddr*)&dest_addr,
|
||||
// sizeof(dest_addr));
|
||||
// if (errno != 0)
|
||||
// std::cout << "BBSend error " << err << " or " << errno << "\n";
|
||||
// else
|
||||
// std::cout << "BBSend SUCCESS\n";
|
||||
|
||||
// #endif
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool ParticipantUDP::Send(IMessage* msg) {
|
||||
int bufferSize = msg->Serialize(this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
return true;
|
||||
|
||||
std::cout << "send msg " << (static_cast<int>(this->buffer[0]) & 0xff)
|
||||
<< " to " << this->remoteSite->ipAddress << std::endl;
|
||||
|
||||
return this->SendTo(this->remoteSite, bufferSize);
|
||||
}
|
||||
|
||||
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant,
|
||||
int bufferSize) {
|
||||
#if defined(IDF_VER)
|
||||
uint16_t port = ntohs(dest_addr.sin_port);
|
||||
|
||||
char ip_str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &dest_addr.sin_addr, ip_str, sizeof(ip_str));
|
||||
std::cout << "Sending " << bufferSize << " bytes to " << ip_str << ":" << port << "\n";
|
||||
|
||||
// Print the IP address and port
|
||||
// printf("IP Address: %s\n", ip_str);
|
||||
// printf("Port: %d\n", port);
|
||||
|
||||
this->dest_addr.sin_port = htons(remoteParticipant->port);
|
||||
this->dest_addr.sin_addr.s_addr = inet_addr(remoteParticipant->ipAddress);
|
||||
|
||||
int err = sendto(this->sock, buffer, bufferSize, 0,
|
||||
(struct sockaddr*)&this->dest_addr, sizeof(this->dest_addr));
|
||||
if (errno < 0)
|
||||
std::cout << "Send error " << err << " or " << errno << "\n";
|
||||
|
||||
#endif
|
||||
@ -154,7 +217,7 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
dest_addr.sin_family = AF_INET;
|
||||
dest_addr.sin_port = htons(this->port);
|
||||
inet_pton(AF_INET, this->broadcastIpAddress, &dest_addr.sin_addr.s_addr);
|
||||
int err = sendto(sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
|
||||
int err = sendto(sock, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
|
||||
sizeof(dest_addr));
|
||||
if (err != 0)
|
||||
std::cout << "Publish error\n";
|
||||
@ -162,5 +225,4 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
return true;
|
||||
};
|
||||
|
||||
} // namespace EspIdf
|
||||
} // namespace RoboidControl
|
||||
|
@ -1,26 +1,47 @@
|
||||
#pragma once
|
||||
#if defined(IDF_VER)
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
|
||||
#if defined(IDF_VER)
|
||||
#include "lwip/sockets.h"
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace EspIdf {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class ParticipantUDP : public ParticipantUDPGeneric {
|
||||
public:
|
||||
/// @brief Create a participant without connecting to a site
|
||||
/// @param port The port on which the participant communicates
|
||||
/// These participant typically broadcast Participant messages to let site
|
||||
/// servers on the local network know their presence. Alternatively they can
|
||||
/// broadcast information which can be used directly by other participants.
|
||||
ParticipantUDP(int port = 7681);
|
||||
/// @brief Create a participant which will try to connect to a site.
|
||||
/// @param ipAddress The IP address of the site
|
||||
/// @param port The port used by the site
|
||||
/// @param localPort The port used by the local participant
|
||||
ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681);
|
||||
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
bool Send(Participant* remoteParticipant, int bufferSize);
|
||||
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
// bool SendTest();
|
||||
|
||||
/// @brief Sens a message to the remote site (if set)
|
||||
/// @param msg The message to send
|
||||
/// @return True if a message could be sent.
|
||||
bool Send(IMessage* msg) override;
|
||||
void SetupUDP(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) override;
|
||||
void ReceiveUDP() override;
|
||||
|
||||
protected:
|
||||
#if defined(IDF_VER)
|
||||
char broadcastIpAddress[INET_ADDRSTRLEN];
|
||||
|
||||
int sockfd;
|
||||
int sock;
|
||||
struct sockaddr_in dest_addr;
|
||||
// struct sockaddr_in src_addr;
|
||||
#endif
|
||||
@ -28,5 +49,6 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
void GetBroadcastAddress();
|
||||
};
|
||||
|
||||
} // namespace EspIdf
|
||||
} // namespace RoboidControl
|
||||
|
||||
#endif
|
||||
|
204
Participant.cpp
204
Participant.cpp
@ -10,8 +10,6 @@ namespace RoboidControl {
|
||||
|
||||
#pragma region Participant
|
||||
|
||||
ParticipantRegistry Participant::registry;
|
||||
|
||||
Participant* Participant::LocalParticipant = new Participant();
|
||||
|
||||
void Participant::ReplaceLocalParticipant(Participant& newParticipant) {
|
||||
@ -24,6 +22,7 @@ Participant::Participant() {
|
||||
//this->Add(this->root);
|
||||
}
|
||||
|
||||
/*
|
||||
Participant::Participant(const char* ipAddress, int port) {
|
||||
Thing::CreateRoot(this);
|
||||
//this->Add(this->root);
|
||||
@ -43,19 +42,26 @@ Participant::Participant(const char* ipAddress, int port) {
|
||||
this->ipAddress = addressString;
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
*/
|
||||
Participant::~Participant() {
|
||||
// registry.Remove(this);
|
||||
delete[] this->ipAddress;
|
||||
// delete[] this->ipAddress;
|
||||
}
|
||||
|
||||
void Participant::Update() {
|
||||
void Participant::Update(bool recurse) {
|
||||
for (Thing* thing : this->things) {
|
||||
if (thing != nullptr)
|
||||
thing->Update(true);
|
||||
thing->Update();
|
||||
}
|
||||
}
|
||||
|
||||
bool Participant::Send(IMessage* msg) {
|
||||
std::cout << "sending message " << (static_cast<int>(this->buffer[0]) & 0xff)
|
||||
<< " to base Participant without communcation support " << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
bool Participant::Send(IMessage* msg) {
|
||||
int bufferSize = msg->Serialize(this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
@ -67,31 +73,31 @@ bool Participant::Send(IMessage* msg) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
return thisWindows->Send(this, bufferSize);
|
||||
return thisWindows->SendTo(this, bufferSize);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
|
||||
return thisPosix->Send(this, bufferSize);
|
||||
return thisPosix->SendTo(this, bufferSize);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
return thisArduino->Send(this, bufferSize);
|
||||
return thisArduino->SendTo(this, bufferSize);
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
return thisEspIdf->Send(remoteParticipant, bufferSize);
|
||||
return thisEspIdf->SendTo(this, bufferSize);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
*/
|
||||
|
||||
Thing* Participant::Get(unsigned char thingId) {
|
||||
Thing* Participant::Get(unsigned char networkId, unsigned char thingId) {
|
||||
for (Thing* thing : this->things) {
|
||||
if (thing->id == thingId)
|
||||
if (thing->owner->networkId == networkId && thing->id == thingId)
|
||||
return thing;
|
||||
}
|
||||
// std::cout << "Could not find thing " << this->ipAddress << ":" <<
|
||||
// this->port
|
||||
// << "[" << (int)thingId << "]\n";
|
||||
std::cout << "Could not find thing " //<< this->ipAddress << ":" << this->port
|
||||
<< "[" << (int)thingId << "]\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -113,22 +119,24 @@ void Participant::Add(Thing* thing, bool checkId) {
|
||||
thing->id = highestIx + 1;
|
||||
this->things.push_back(thing);
|
||||
#endif
|
||||
// std::cout << "Add thing with generated ID " << this->ipAddress << ":"
|
||||
// << this->port << "[" << (int)thing->id << "]\n";
|
||||
std::cout << "Add thing with generated ID "
|
||||
//<< this->ipAddress << ":" << this->port
|
||||
<< "[" << (int)thing->id << "]\n";
|
||||
} else {
|
||||
Thing* foundThing = Get(thing->id);
|
||||
Thing* foundThing = Get(thing->owner->networkId, thing->id);
|
||||
if (foundThing == nullptr) {
|
||||
#if defined(NO_STD)
|
||||
this->things[this->thingCount++] = thing;
|
||||
#else
|
||||
this->things.push_back(thing);
|
||||
#endif
|
||||
// std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
|
||||
// "["
|
||||
// << (int)thing->id << "]\n";
|
||||
std::cout << "Add thing " << //this->ipAddress << ":" << this->port <<
|
||||
"["
|
||||
<< (int)thing->id << "]\n";
|
||||
} else {
|
||||
// std::cout << "Did not add, existing thing " << this->ipAddress << ":"
|
||||
// << this->port << "[" << (int)thing->id << "]\n";
|
||||
std::cout << "Did not add, existing thing "
|
||||
//<< this->ipAddress << ":" << this->port
|
||||
<< "[" << (int)thing->id << "]\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -156,86 +164,88 @@ void Participant::Remove(Thing* thing) {
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region ParticipantRegistry
|
||||
// #pragma region ParticipantRegistry
|
||||
|
||||
Participant* ParticipantRegistry::Get(const char* ipAddress,
|
||||
unsigned int port) {
|
||||
#if !defined(NO_STD)
|
||||
for (Participant* participant : ParticipantRegistry::participants) {
|
||||
if (participant == nullptr)
|
||||
continue;
|
||||
if (strcmp(participant->ipAddress, ipAddress) == 0 &&
|
||||
participant->port == port) {
|
||||
// std::cout << "found participant " << participant->ipAddress << ":"
|
||||
// << (int)participant->port << std::endl;
|
||||
return participant;
|
||||
}
|
||||
}
|
||||
std::cout << "Could not find participant " << ipAddress << ":" << (int)port
|
||||
<< std::endl;
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
// /*
|
||||
// Participant* ParticipantRegistry::Get(const char* ipAddress,
|
||||
// unsigned int port) {
|
||||
// #if !defined(NO_STD)
|
||||
// for (Participant* participant : ParticipantRegistry::participants) {
|
||||
// if (participant == nullptr)
|
||||
// continue;
|
||||
// if (strcmp(participant->ipAddress, ipAddress) == 0 &&
|
||||
// participant->port == port) {
|
||||
// // std::cout << "found participant " << participant->ipAddress << ":"
|
||||
// // << (int)participant->port << std::endl;
|
||||
// return participant;
|
||||
// }
|
||||
// }
|
||||
// std::cout << "Could not find participant " << ipAddress << ":" << (int)port
|
||||
// << std::endl;
|
||||
// #endif
|
||||
// return nullptr;
|
||||
// }
|
||||
// */
|
||||
// Participant* ParticipantRegistry::Get(unsigned char participantId) {
|
||||
// #if !defined(NO_STD)
|
||||
// for (Participant* participant : ParticipantRegistry::participants) {
|
||||
// if (participant == nullptr)
|
||||
// continue;
|
||||
// if (participant->networkId == participantId)
|
||||
// return participant;
|
||||
// }
|
||||
// std::cout << "Could not find participant " << (int)participantId << std::endl;
|
||||
// #endif
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
Participant* ParticipantRegistry::Get(unsigned char participantId) {
|
||||
#if !defined(NO_STD)
|
||||
for (Participant* participant : ParticipantRegistry::participants) {
|
||||
if (participant == nullptr)
|
||||
continue;
|
||||
if (participant->networkId == participantId)
|
||||
return participant;
|
||||
}
|
||||
std::cout << "Could not find participant " << (int)participantId << std::endl;
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
// // Participant* ParticipantRegistry::Add(const char* ipAddress,
|
||||
// // unsigned int port) {
|
||||
// // Participant* participant = new Participant(ipAddress, port);
|
||||
// // Add(participant);
|
||||
// // return participant;
|
||||
// // }
|
||||
|
||||
Participant* ParticipantRegistry::Add(const char* ipAddress,
|
||||
unsigned int port) {
|
||||
Participant* participant = new Participant(ipAddress, port);
|
||||
Add(participant);
|
||||
return participant;
|
||||
}
|
||||
// void ParticipantRegistry::Add(Participant* participant) {
|
||||
// Participant* foundParticipant =
|
||||
// Get(participant->networkId);
|
||||
// //Get(participant->ipAddress, participant->port);
|
||||
|
||||
void ParticipantRegistry::Add(Participant* participant) {
|
||||
Participant* foundParticipant =
|
||||
Get(participant->ipAddress, participant->port);
|
||||
// if (foundParticipant == nullptr) {
|
||||
// #if defined(NO_STD)
|
||||
// // this->things[this->thingCount++] = thing;
|
||||
// #else
|
||||
// ParticipantRegistry::participants.push_back(participant);
|
||||
// #endif
|
||||
// // std::cout << "Add participant " << participant->ipAddress << ":"
|
||||
// // << participant->port << "[" << (int)participant->networkId
|
||||
// // << "]\n";
|
||||
// // std::cout << "participants " <<
|
||||
// // ParticipantRegistry::participants.size()
|
||||
// // << "\n";
|
||||
// // } else {
|
||||
// // std::cout << "Did not add, existing participant " <<
|
||||
// // participant->ipAddress
|
||||
// // << ":" << participant->port << "[" <<
|
||||
// // (int)participant->networkId
|
||||
// // << "]\n";
|
||||
// }
|
||||
// }
|
||||
|
||||
if (foundParticipant == nullptr) {
|
||||
#if defined(NO_STD)
|
||||
// this->things[this->thingCount++] = thing;
|
||||
#else
|
||||
ParticipantRegistry::participants.push_back(participant);
|
||||
#endif
|
||||
// std::cout << "Add participant " << participant->ipAddress << ":"
|
||||
// << participant->port << "[" << (int)participant->networkId
|
||||
// << "]\n";
|
||||
// std::cout << "participants " <<
|
||||
// ParticipantRegistry::participants.size()
|
||||
// << "\n";
|
||||
// } else {
|
||||
// std::cout << "Did not add, existing participant " <<
|
||||
// participant->ipAddress
|
||||
// << ":" << participant->port << "[" <<
|
||||
// (int)participant->networkId
|
||||
// << "]\n";
|
||||
}
|
||||
}
|
||||
// void ParticipantRegistry::Remove(Participant* participant) {
|
||||
// // participants.remove(participant);
|
||||
// }
|
||||
|
||||
void ParticipantRegistry::Remove(Participant* participant) {
|
||||
// participants.remove(participant);
|
||||
}
|
||||
// #if defined(NO_STD)
|
||||
// Participant** ParticipantRegistry::GetAll() const {
|
||||
// return ParticipantRegistry::participants;
|
||||
// }
|
||||
// #else
|
||||
// const std::list<Participant*>& ParticipantRegistry::GetAll() const {
|
||||
// return ParticipantRegistry::participants;
|
||||
// }
|
||||
// #endif
|
||||
|
||||
#if defined(NO_STD)
|
||||
Participant** ParticipantRegistry::GetAll() const {
|
||||
return ParticipantRegistry::participants;
|
||||
}
|
||||
#else
|
||||
const std::list<Participant*>& ParticipantRegistry::GetAll() const {
|
||||
return ParticipantRegistry::participants;
|
||||
}
|
||||
#endif
|
||||
|
||||
#pragma endregion ParticipantRegistry
|
||||
// #pragma endregion ParticipantRegistry
|
||||
|
||||
} // namespace RoboidControl
|
||||
|
@ -7,6 +7,7 @@ namespace RoboidControl {
|
||||
|
||||
constexpr int MAX_THING_COUNT = 256;
|
||||
|
||||
/*
|
||||
/// @brief class which manages all known participants
|
||||
class ParticipantRegistry {
|
||||
public:
|
||||
@ -14,7 +15,7 @@ class ParticipantRegistry {
|
||||
/// @param ipAddress The IP address of the participant
|
||||
/// @param port The port number of the participant
|
||||
/// @return The participant or a nullptr when it could not be found
|
||||
Participant* Get(const char* ipAddress, unsigned int port);
|
||||
//Participant* Get(const char* ipAddress, unsigned int port);
|
||||
/// @brief Retrieve a participant by its network ID
|
||||
/// @param networkID The network ID of the participant
|
||||
/// @return The participant or a nullptr when it could not be found
|
||||
@ -24,7 +25,8 @@ class ParticipantRegistry {
|
||||
/// @param ipAddress The IP address of the participant
|
||||
/// @param port The port number of the participant
|
||||
/// @return The added participant
|
||||
Participant* Add(const char* ipAddress, unsigned int port);
|
||||
//Participant* Add(const char* ipAddress, unsigned int port);
|
||||
|
||||
/// @brief Add a participant
|
||||
/// @param participant The participant to add
|
||||
void Add(Participant* participant);
|
||||
@ -52,6 +54,7 @@ class ParticipantRegistry {
|
||||
std::list<Participant*> participants;
|
||||
#endif
|
||||
};
|
||||
*/
|
||||
|
||||
/// @brief A participant is a device which manages things.
|
||||
/// It can communicate with other participant to synchronise the state of
|
||||
@ -88,15 +91,6 @@ class Participant {
|
||||
/// @brief The name of the participant
|
||||
const char* name = "Participant";
|
||||
|
||||
/// @brief The Ip Address of a participant.
|
||||
/// @remarks This does not belong here, it should move to ParticipantUDP or
|
||||
/// something like that in the future
|
||||
const char* ipAddress = "0.0.0.0";
|
||||
/// @brief The port number for UDP communication with the participant.
|
||||
/// @remarks This does not belong here, it should move to ParticipantUDP or
|
||||
/// something like that in the future
|
||||
unsigned int port = 0;
|
||||
|
||||
/// @brief The network Id to identify the participant
|
||||
unsigned char networkId = 0;
|
||||
|
||||
@ -112,9 +106,10 @@ class Participant {
|
||||
std::list<Thing*> things;
|
||||
#endif
|
||||
/// @brief Find a thing managed by this participant
|
||||
/// @param networkId The network ID of the thing
|
||||
/// @param thingId The ID of the thing
|
||||
/// @return The thing if found, nullptr when no thing has been found
|
||||
Thing* Get(unsigned char thingId);
|
||||
Thing* Get(unsigned char networkId, unsigned char thingId);
|
||||
/// @brief Add a new thing for this participant.
|
||||
/// @param thing The thing to add
|
||||
/// @param checkId If true, the thing.id is regenerated if it is zero
|
||||
@ -129,7 +124,7 @@ class Participant {
|
||||
|
||||
public:
|
||||
/// @brief Update all things for this participant
|
||||
virtual void Update();
|
||||
virtual void Update(bool recurse = true);
|
||||
|
||||
#pragma endregion Update
|
||||
|
||||
@ -142,12 +137,12 @@ class Participant {
|
||||
|
||||
#pragma endregion Send
|
||||
|
||||
#pragma region Participant Registry
|
||||
// #pragma region Participant Registry
|
||||
|
||||
public:
|
||||
static ParticipantRegistry registry;
|
||||
// public:
|
||||
// static ParticipantRegistry registry;
|
||||
|
||||
#pragma endregion Participant Registry
|
||||
// #pragma endregion Participant Registry
|
||||
};
|
||||
|
||||
} // namespace RoboidControl
|
||||
|
@ -1,14 +1,16 @@
|
||||
/*
|
||||
#include "IsolatedParticipant.h"
|
||||
#include "ParticipantUDP.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
static ParticipantUDP* isolatedParticipant = nullptr;
|
||||
static ParticipantUDPGeneric* isolatedParticipant = nullptr;
|
||||
|
||||
Participant* IsolatedParticipant::Isolated() {
|
||||
if (isolatedParticipant == nullptr)
|
||||
isolatedParticipant = new ParticipantUDP(0);
|
||||
isolatedParticipant = new ParticipantUDPGeneric(0);
|
||||
return isolatedParticipant;
|
||||
}
|
||||
|
||||
} // namespace RoboidControl
|
||||
*/
|
@ -1,3 +1,4 @@
|
||||
/*
|
||||
#include "Participant.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
@ -11,3 +12,4 @@ class IsolatedParticipant {
|
||||
};
|
||||
|
||||
}
|
||||
*/
|
@ -8,73 +8,167 @@
|
||||
#include "Posix/PosixParticipant.h"
|
||||
#include "Windows/WindowsParticipant.h"
|
||||
|
||||
#include "Things/DifferentialDrive.h"
|
||||
#include "Things/DistanceSensor.h"
|
||||
#include "Things/TouchSensor.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
#pragma region ParticipantRegistry
|
||||
|
||||
ParticipantRegistry ParticipantUDPGeneric::registry;
|
||||
|
||||
RemoteParticipantUDP* ParticipantRegistry::Get(const char* ipAddress,
|
||||
unsigned int port) {
|
||||
#if !defined(NO_STD)
|
||||
for (RemoteParticipantUDP* participant : ParticipantRegistry::participants) {
|
||||
if (participant == nullptr)
|
||||
continue;
|
||||
if (strcmp(participant->ipAddress, ipAddress) == 0 &&
|
||||
participant->port == port) {
|
||||
// std::cout << "found participant " << participant->ipAddress << ":"
|
||||
// << (int)participant->port << std::endl;
|
||||
return participant;
|
||||
}
|
||||
}
|
||||
std::cout << "Could not find participant " << ipAddress << ":" << (int)port
|
||||
<< std::endl;
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RemoteParticipantUDP* ParticipantRegistry::Get(unsigned char participantId) {
|
||||
#if !defined(NO_STD)
|
||||
for (RemoteParticipantUDP* participant : ParticipantRegistry::participants) {
|
||||
if (participant == nullptr)
|
||||
continue;
|
||||
if (participant->networkId == participantId)
|
||||
return participant;
|
||||
}
|
||||
std::cout << "Could not find participant " << (int)participantId << std::endl;
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RemoteParticipantUDP* ParticipantRegistry::Add(const char* ipAddress,
|
||||
unsigned int port) {
|
||||
RemoteParticipantUDP* participant = new RemoteParticipantUDP(ipAddress, port);
|
||||
Add(participant);
|
||||
return participant;
|
||||
}
|
||||
|
||||
void ParticipantRegistry::Add(RemoteParticipantUDP* participant) {
|
||||
Participant* foundParticipant = Get(participant->networkId);
|
||||
// Get(participant->ipAddress, participant->port);
|
||||
|
||||
if (foundParticipant == nullptr) {
|
||||
#if defined(NO_STD)
|
||||
// this->things[this->thingCount++] = thing;
|
||||
#else
|
||||
ParticipantRegistry::participants.push_back(participant);
|
||||
#endif
|
||||
// std::cout << "Add participant " << participant->ipAddress << ":"
|
||||
// << participant->port << "[" << (int)participant->networkId
|
||||
// << "]\n";
|
||||
// std::cout << "participants " <<
|
||||
// ParticipantRegistry::participants.size()
|
||||
// << "\n";
|
||||
// } else {
|
||||
// std::cout << "Did not add, existing participant " <<
|
||||
// participant->ipAddress
|
||||
// << ":" << participant->port << "[" <<
|
||||
// (int)participant->networkId
|
||||
// << "]\n";
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantRegistry::Remove(RemoteParticipantUDP* participant) {
|
||||
// participants.remove(participant);
|
||||
}
|
||||
|
||||
#if defined(NO_STD)
|
||||
RemoteParticipantUDP** ParticipantRegistry::GetAll() const {
|
||||
return ParticipantRegistry::participants;
|
||||
}
|
||||
#else
|
||||
const std::list<RemoteParticipantUDP*>& ParticipantRegistry::GetAll() const {
|
||||
return ParticipantRegistry::participants;
|
||||
}
|
||||
#endif
|
||||
|
||||
#pragma endregion ParticipantRegistry
|
||||
|
||||
RemoteParticipantUDP::RemoteParticipantUDP(const char* ipAddress, int port) {
|
||||
// make a copy of the ip address string
|
||||
int addressLength = (int)strlen(ipAddress);
|
||||
int stringLength = addressLength + 1;
|
||||
char* addressString = new char[stringLength];
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
strncpy_s(addressString, stringLength, ipAddress,
|
||||
addressLength); // Leave space for null terminator
|
||||
#else
|
||||
strncpy(addressString, ipAddress, addressLength);
|
||||
#endif
|
||||
addressString[addressLength] = '\0';
|
||||
|
||||
this->ipAddress = addressString;
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
bool RemoteParticipantUDP::Send(IMessage* msg) {
|
||||
// No message is actually sent, because this class has no networking
|
||||
// implementation
|
||||
return false;
|
||||
}
|
||||
|
||||
#pragma region Init
|
||||
|
||||
ParticipantUDP::ParticipantUDP(int port) : Participant("127.0.0.1", port) {
|
||||
ParticipantUDPGeneric::ParticipantUDPGeneric(int port)
|
||||
: RemoteParticipantUDP("127.0.0.1", port) {
|
||||
this->name = "ParticipantUDP";
|
||||
this->remoteSite = nullptr;
|
||||
if (this->port == 0)
|
||||
this->isIsolated = true;
|
||||
Participant::registry.Add(this);
|
||||
registry.Add(this);
|
||||
|
||||
this->root = Thing::LocalRoot(); //::LocalParticipant->root;
|
||||
this->root->owner = this;
|
||||
this->root->name = "UDP Root";
|
||||
std::cout << "P2 " << (int)this->root << std::endl;
|
||||
this->Add(this->root);
|
||||
|
||||
Participant::ReplaceLocalParticipant(*this);
|
||||
}
|
||||
|
||||
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
|
||||
: Participant("127.0.0.1", localPort) {
|
||||
ParticipantUDPGeneric::ParticipantUDPGeneric(const char* ipAddress,
|
||||
int port,
|
||||
int localPort)
|
||||
: RemoteParticipantUDP("127.0.0.1", localPort) {
|
||||
this->name = "ParticipantUDP";
|
||||
if (this->port == 0)
|
||||
this->isIsolated = true;
|
||||
else
|
||||
this->remoteSite = new Participant(ipAddress, port);
|
||||
Participant::registry.Add(this);
|
||||
this->remoteSite = new RemoteParticipantUDP(ipAddress, port);
|
||||
registry.Add(this);
|
||||
|
||||
this->root = Thing::LocalRoot(); // Participant::LocalParticipant->root;
|
||||
this->root->owner = this;
|
||||
this->root->name = "UDP Root";
|
||||
std::cout << "P1 " << (int)this->root << std::endl;
|
||||
this->Add(this->root);
|
||||
|
||||
Participant::ReplaceLocalParticipant(*this);
|
||||
}
|
||||
|
||||
void ParticipantUDP::begin() {
|
||||
void ParticipantUDPGeneric::begin() {
|
||||
if (this->isIsolated || this->remoteSite == nullptr)
|
||||
return;
|
||||
|
||||
SetupUDP(this->port, this->remoteSite->ipAddress, this->remoteSite->port);
|
||||
}
|
||||
|
||||
void ParticipantUDP::SetupUDP(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
thisWindows->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
|
||||
thisPosix->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
thisArduino->Setup();
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
thisEspIdf->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#endif
|
||||
this->connected = true;
|
||||
}
|
||||
|
||||
#pragma endregion Init
|
||||
|
||||
#pragma region Update
|
||||
@ -83,7 +177,7 @@ void ParticipantUDP::SetupUDP(int localPort,
|
||||
// 1. receive external messages
|
||||
// 2. update the state
|
||||
// 3. send out the updated messages
|
||||
void ParticipantUDP::Update() {
|
||||
void ParticipantUDPGeneric::Update(bool recurse) {
|
||||
unsigned long currentTimeMs = Thing::GetTimeMs();
|
||||
|
||||
if (this->isIsolated == false) {
|
||||
@ -92,24 +186,24 @@ void ParticipantUDP::Update() {
|
||||
|
||||
if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) {
|
||||
ParticipantMsg* msg = new ParticipantMsg(this->networkId);
|
||||
|
||||
if (this->remoteSite == nullptr)
|
||||
this->Publish(msg);
|
||||
else
|
||||
this->Send(msg);
|
||||
|
||||
delete msg;
|
||||
|
||||
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
||||
}
|
||||
|
||||
this->ReceiveUDP();
|
||||
//this->ReceiveUDP();
|
||||
}
|
||||
|
||||
UpdateMyThings();
|
||||
UpdateOtherThings();
|
||||
}
|
||||
|
||||
void ParticipantUDP::UpdateMyThings() {
|
||||
void ParticipantUDPGeneric::UpdateMyThings() {
|
||||
for (Thing* thing : this->things) {
|
||||
if (thing == nullptr) // || thing->GetParent() != nullptr)
|
||||
continue;
|
||||
@ -125,13 +219,13 @@ void ParticipantUDP::UpdateMyThings() {
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::UpdateOtherThings() {
|
||||
void ParticipantUDPGeneric::UpdateOtherThings() {
|
||||
#if defined(NO_STD)
|
||||
Participant** participants = Participant::registry.GetAll();
|
||||
for (int ix = 0; ix < Participant::registry.count; ix++) {
|
||||
Participant* participant = participants[ix];
|
||||
#else
|
||||
for (Participant* participant : Participant::registry.GetAll()) {
|
||||
for (Participant* participant : registry.GetAll()) {
|
||||
#endif
|
||||
if (participant == nullptr || participant == this)
|
||||
continue;
|
||||
@ -159,7 +253,7 @@ void ParticipantUDP::UpdateOtherThings() {
|
||||
|
||||
#pragma region Send
|
||||
|
||||
void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
|
||||
void ParticipantUDPGeneric::SendThingInfo(Participant* remoteParticipant,
|
||||
Thing* thing) {
|
||||
// std::cout << "Send thing info [" << (int)thing->id << "] \n";
|
||||
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
|
||||
@ -179,14 +273,14 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
|
||||
delete binaryMsg;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(IMessage* msg) {
|
||||
bool ParticipantUDPGeneric::Send(IMessage* msg) {
|
||||
if (this->remoteSite != nullptr)
|
||||
return this->remoteSite->Send(msg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ParticipantUDP::PublishThingInfo(Thing* thing) {
|
||||
void ParticipantUDPGeneric::PublishThingInfo(Thing* thing) {
|
||||
// std::cout << "Publish thing info" << thing->networkId << "\n";
|
||||
// Strange, when publishing, the network id is irrelevant, because it is
|
||||
// connected to a specific site...
|
||||
@ -207,71 +301,31 @@ void ParticipantUDP::PublishThingInfo(Thing* thing) {
|
||||
delete customMsg;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
// std::cout << "publish msg\n";
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
return thisWindows->Publish(msg);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
|
||||
return thisPosix->Publish(msg);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
return thisArduino->Publish(msg);
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
return thisEspIdf->Publish(msg);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Send
|
||||
#pragma endregion
|
||||
|
||||
#pragma region Receive
|
||||
|
||||
void ParticipantUDP::ReceiveUDP() {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
thisWindows->Receive();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
|
||||
thisPosix->Receive();
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
thisArduino->Receive();
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
thisEspIdf->Receive();
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::ReceiveData(unsigned char packetSize,
|
||||
void ParticipantUDPGeneric::ReceiveData(unsigned char packetSize,
|
||||
char* senderIpAddress,
|
||||
unsigned int senderPort) {
|
||||
// std::cout << "Receive data from " << senderIpAddress << ":" << senderPort
|
||||
// << std::endl;
|
||||
Participant* sender = this->registry.Get(senderIpAddress, senderPort);
|
||||
RemoteParticipantUDP* sender =
|
||||
this->registry.Get(senderIpAddress, senderPort);
|
||||
if (sender == nullptr) {
|
||||
sender = this->registry.Add(senderIpAddress, senderPort);
|
||||
#if !defined(NO_STD)
|
||||
std::cout << "New remote participant " << sender->ipAddress << ":"
|
||||
<< sender->port << std::endl;
|
||||
// std::cout << "New remote participant " << sender->ipAddress << ":"
|
||||
// << sender->port << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
ReceiveData(packetSize, sender);
|
||||
}
|
||||
|
||||
void ParticipantUDP::ReceiveData(unsigned char bufferSize,
|
||||
Participant* sender) {
|
||||
void ParticipantUDPGeneric::ReceiveData(unsigned char bufferSize,
|
||||
RemoteParticipantUDP* sender) {
|
||||
unsigned char msgId = this->buffer[0];
|
||||
// std::cout << "receive msg " << (int)msgId << "\n";
|
||||
// std::cout << " buffer size = " <<(int) bufferSize << "\n";
|
||||
@ -344,14 +398,16 @@ void ParticipantUDP::ReceiveData(unsigned char bufferSize,
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, ParticipantMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
ParticipantMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": Process ParticipantMsg " << (int)msg->networkId
|
||||
<< "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, NetworkIdMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
NetworkIdMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process NetworkIdMsg " << (int)this->networkId
|
||||
<< " -> " << (int)msg->networkId << "\n";
|
||||
@ -366,28 +422,68 @@ void ParticipantUDP::Process(Participant* sender, NetworkIdMsg* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
InvestigateMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": Process InvestigateMsg [" << (int)msg->networkId
|
||||
<< "/" << (int)msg->thingId << "]\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, ThingMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
ThingMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process ThingMsg [" << (int)msg->networkId
|
||||
<< "/" << (int)msg->thingId << "] " << (int)msg->thingType << " "
|
||||
<< (int)msg->parentId << "\n";
|
||||
#endif
|
||||
RemoteParticipantUDP* owner = registry.Get(msg->networkId);
|
||||
if (owner == nullptr) {
|
||||
owner = new RemoteParticipantUDP(sender->ipAddress, sender->port);
|
||||
owner->networkId = msg->networkId;
|
||||
registry.Add(owner);
|
||||
}
|
||||
|
||||
Thing* thing = owner->Get(msg->networkId, msg->thingId);
|
||||
if (thing == nullptr) {
|
||||
bool isRemote = (sender->networkId != owner->networkId);
|
||||
thing = ProcessNewThing(owner, msg, isRemote);
|
||||
thing->id = msg->thingId;
|
||||
thing->type = msg->thingType;
|
||||
thing->isRemote = isRemote;
|
||||
}
|
||||
|
||||
if (msg->parentId != 0) {
|
||||
thing->SetParent(owner->Get(msg->networkId, msg->parentId));
|
||||
if (thing->GetParent() == nullptr)
|
||||
std::cout << "Could not find parent" << std::endl;
|
||||
} else
|
||||
thing->SetParent(nullptr);
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
|
||||
Thing* ParticipantUDPGeneric::ProcessNewThing(RemoteParticipantUDP* owner,
|
||||
ThingMsg* msg,
|
||||
bool isRemote) {
|
||||
switch (msg->thingType) {
|
||||
case Thing::Type::DistanceSensor:
|
||||
return new DistanceSensor(owner->root);
|
||||
case Thing::Type::TouchSensor:
|
||||
return new TouchSensor(owner->root);
|
||||
case Thing::Type::DifferentialDrive:
|
||||
return new DifferentialDrive(owner->root);
|
||||
default:
|
||||
return new Thing(owner->root);
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
NameMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process NameMsg [" << (int)msg->networkId << "/"
|
||||
<< (int)msg->thingId << "] ";
|
||||
#endif
|
||||
|
||||
Thing* thing = sender->Get(msg->thingId);
|
||||
Thing* thing = sender->Get(msg->networkId, msg->thingId);
|
||||
if (thing != nullptr) {
|
||||
int nameLength = msg->nameLength;
|
||||
int stringLen = nameLength + 1;
|
||||
@ -413,23 +509,25 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, ModelUrlMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
ModelUrlMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process ModelUrlMsg [" << (int)msg->networkId
|
||||
<< "/" << (int)msg->thingId << "]\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
PoseMsg* msg) {
|
||||
#if !defined(DEBUG) && !defined(NO_STD)
|
||||
std::cout << this->name << ": process PoseMsg [" << (int)this->networkId
|
||||
<< "/" << (int)msg->networkId << "] " << (int)msg->poseType << "\n";
|
||||
#endif
|
||||
Participant* owner = Participant::registry.Get(msg->networkId);
|
||||
Participant* owner = registry.Get(msg->networkId);
|
||||
if (owner == nullptr)
|
||||
return;
|
||||
|
||||
Thing* thing = owner->Get(msg->thingId);
|
||||
Thing* thing = owner->Get(msg->networkId, msg->thingId);
|
||||
if (thing == nullptr)
|
||||
return;
|
||||
|
||||
@ -443,15 +541,16 @@ void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
|
||||
thing->SetAngularVelocity(msg->angularVelocity);
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
BinaryMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process BinaryMsg [" << (int)msg->networkId
|
||||
<< "/" << (int)msg->thingId << "]\n";
|
||||
#endif
|
||||
|
||||
Participant* owner = Participant::registry.Get(msg->networkId);
|
||||
Participant* owner = registry.Get(msg->networkId);
|
||||
if (owner != nullptr) {
|
||||
Thing* thing = owner->Get(msg->thingId);
|
||||
Thing* thing = owner->Get(msg->networkId, msg->thingId);
|
||||
if (thing != nullptr)
|
||||
thing->ProcessBinary(msg->data);
|
||||
#if !defined(NO_STD)
|
||||
@ -465,20 +564,22 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, TextMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
TextMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process TextMsg " << (int)msg->textLength << " "
|
||||
<< (int)msg->text << "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, DestroyMsg* msg) {
|
||||
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
|
||||
DestroyMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
std::cout << this->name << ": process Destroy [" << (int)msg->networkId << "/"
|
||||
<< (int)msg->thingId << "]\n";
|
||||
#endif
|
||||
|
||||
Thing* thing = sender->Get(msg->thingId);
|
||||
Thing* thing = sender->Get(msg->networkId, msg->thingId);
|
||||
if (thing != nullptr)
|
||||
this->Remove(thing);
|
||||
}
|
||||
|
@ -5,11 +5,11 @@
|
||||
#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/NetworkIdMsg.h"
|
||||
#include "Messages/ThingMsg.h"
|
||||
#include "Messages/TextMsg.h"
|
||||
#include "Messages/ThingMsg.h"
|
||||
#include "Participant.h"
|
||||
|
||||
#if !defined(NO_STD)
|
||||
@ -31,6 +31,74 @@ namespace RoboidControl {
|
||||
|
||||
constexpr int MAX_SENDER_COUNT = 256;
|
||||
|
||||
class RemoteParticipantUDP : public Participant {
|
||||
public:
|
||||
/// @brief Create a new participant with the given communcation info
|
||||
/// @param ipAddress The IP address of the participant
|
||||
/// @param port The UDP port of the participant
|
||||
/// @remarks This does not belong here, it should move to ParticipantUDP or
|
||||
/// something like that in the future
|
||||
RemoteParticipantUDP(const char* ipAddress, int port);
|
||||
|
||||
/// @brief The Ip Address of a participant.
|
||||
/// @remarks This does not belong here, it should move to ParticipantUDP or
|
||||
/// something like that in the future
|
||||
const char* ipAddress = "0.0.0.0";
|
||||
/// @brief The port number for UDP communication with the participant.
|
||||
/// @remarks This does not belong here, it should move to ParticipantUDP or
|
||||
/// something like that in the future
|
||||
unsigned int port = 0;
|
||||
|
||||
bool Send(IMessage* msg) override;
|
||||
};
|
||||
|
||||
/// @brief class which manages all known participants
|
||||
class ParticipantRegistry {
|
||||
public:
|
||||
/// @brief Retrieve a participant by its address
|
||||
/// @param ipAddress The IP address of the participant
|
||||
/// @param port The port number of the participant
|
||||
/// @return The participant or a nullptr when it could not be found
|
||||
RemoteParticipantUDP* Get(const char* ipAddress, unsigned int port);
|
||||
/// @brief Retrieve a participant by its network ID
|
||||
/// @param networkID The network ID of the participant
|
||||
/// @return The participant or a nullptr when it could not be found
|
||||
RemoteParticipantUDP* Get(unsigned char networkID);
|
||||
|
||||
/// @brief Add a participant with the given details
|
||||
/// @param ipAddress The IP address of the participant
|
||||
/// @param port The port number of the participant
|
||||
/// @return The added participant
|
||||
RemoteParticipantUDP* Add(const char* ipAddress, unsigned int port);
|
||||
|
||||
/// @brief Add a participant
|
||||
/// @param participant The participant to add
|
||||
void Add(RemoteParticipantUDP* participant);
|
||||
|
||||
/// @brief Remove a participant
|
||||
/// @param participant The participant to remove
|
||||
void Remove(RemoteParticipantUDP* participant);
|
||||
|
||||
private:
|
||||
#if defined(NO_STD)
|
||||
public:
|
||||
RemoteParticipantUDP** GetAll() const;
|
||||
int count = 0;
|
||||
|
||||
private:
|
||||
RemoteParticipantUDP** participants;
|
||||
#else
|
||||
public:
|
||||
/// @brief Get all participants
|
||||
/// @return All participants
|
||||
const std::list<RemoteParticipantUDP*>& GetAll() const;
|
||||
|
||||
private:
|
||||
/// @brief The list of known participants
|
||||
std::list<RemoteParticipantUDP*> participants;
|
||||
#endif
|
||||
};
|
||||
|
||||
/// @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
|
||||
@ -43,8 +111,7 @@ constexpr int MAX_SENDER_COUNT = 256;
|
||||
/// participant is created which can be obtained using
|
||||
/// RoboidControl::IsolatedParticipant::Isolated().
|
||||
/// @sa RoboidControl::Thing::Thing()
|
||||
class ParticipantUDP : public Participant {
|
||||
|
||||
class ParticipantUDPGeneric : public RemoteParticipantUDP {
|
||||
#pragma region Init
|
||||
|
||||
public:
|
||||
@ -53,31 +120,32 @@ class ParticipantUDP : public Participant {
|
||||
/// These participant typically broadcast Participant messages to let site
|
||||
/// servers on the local network know their presence. Alternatively they can
|
||||
/// broadcast information which can be used directly by other participants.
|
||||
ParticipantUDP(int port = 7681);
|
||||
ParticipantUDPGeneric(int port = 7681);
|
||||
/// @brief Create a participant which will try to connect to a site.
|
||||
/// @param ipAddress The IP address of the site
|
||||
/// @param port The port used by the site
|
||||
/// @param localPort The port used by the local participant
|
||||
ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681);
|
||||
ParticipantUDPGeneric(const char* ipAddress,
|
||||
int port = 7681,
|
||||
int localPort = 7681);
|
||||
|
||||
#pragma endregion Init
|
||||
|
||||
#pragma region Properties
|
||||
|
||||
public:
|
||||
public:
|
||||
/// @brief True if the participant is running isolated.
|
||||
/// Isolated participants do not communicate with other participants
|
||||
bool isIsolated = false;
|
||||
|
||||
/// @brief The remote site when this participant is connected to a site
|
||||
Participant* remoteSite = nullptr;
|
||||
RemoteParticipantUDP* remoteSite = nullptr;
|
||||
|
||||
/// The interval in milliseconds for publishing (broadcasting) data on the
|
||||
/// local network
|
||||
long publishInterval = 3000; // 3 seconds
|
||||
|
||||
protected:
|
||||
|
||||
#if !defined(ARDUINO)
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
int sock;
|
||||
@ -96,13 +164,13 @@ public:
|
||||
#pragma region Update
|
||||
|
||||
public:
|
||||
virtual void Update() override;
|
||||
virtual void Update(bool recurse = true) override;
|
||||
|
||||
protected:
|
||||
unsigned long nextPublishMe = 0;
|
||||
|
||||
/// @brief Prepare the local things for the next update
|
||||
//virtual void PrepMyThings();
|
||||
// virtual void PrepMyThings();
|
||||
virtual void UpdateMyThings();
|
||||
virtual void UpdateOtherThings();
|
||||
|
||||
@ -114,35 +182,45 @@ public:
|
||||
void PublishThingInfo(Thing* thing);
|
||||
|
||||
virtual bool Send(IMessage* msg) override;
|
||||
bool Publish(IMessage* msg);
|
||||
virtual bool Publish(IMessage* msg) = 0;
|
||||
|
||||
#pragma endregion Send
|
||||
|
||||
#pragma region Receive
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void ReceiveData(unsigned char bufferSize,
|
||||
char* senderIpAddress,
|
||||
unsigned int senderPort);
|
||||
void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
|
||||
void ReceiveData(unsigned char bufferSize, RemoteParticipantUDP* remoteParticipant);
|
||||
|
||||
void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
virtual void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) = 0;
|
||||
|
||||
void ReceiveUDP();
|
||||
virtual void ReceiveUDP() = 0;
|
||||
|
||||
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);
|
||||
virtual void Process(RemoteParticipantUDP* sender, ParticipantMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, NetworkIdMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, InvestigateMsg* msg);
|
||||
|
||||
virtual void Process(RemoteParticipantUDP* sender, ThingMsg* msg);
|
||||
virtual Thing* ProcessNewThing(RemoteParticipantUDP* sender,
|
||||
ThingMsg* msg,
|
||||
bool isRemote);
|
||||
|
||||
virtual void Process(RemoteParticipantUDP* sender, NameMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, ModelUrlMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, PoseMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, BinaryMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, TextMsg* msg);
|
||||
virtual void Process(RemoteParticipantUDP* sender, DestroyMsg* msg);
|
||||
|
||||
#pragma endregion Receive
|
||||
|
||||
public:
|
||||
static ParticipantRegistry registry;
|
||||
|
||||
};
|
||||
|
||||
} // namespace RoboidControl
|
||||
|
||||
#include "EspIdf/EspIdfParticipant.h"
|
||||
|
@ -11,11 +11,11 @@ namespace RoboidControl {
|
||||
|
||||
#pragma region Init
|
||||
|
||||
SiteServer::SiteServer(int port) : ParticipantUDP(port) {
|
||||
SiteServer::SiteServer(int port) : ParticipantUDPGeneric(port) {
|
||||
this->name = "Site Server";
|
||||
this->publishInterval = 0;
|
||||
|
||||
SetupUDP(port, ipAddress, 0);
|
||||
//SetupUDP(port, ipAddress, 0);
|
||||
}
|
||||
|
||||
#pragma endregion Init
|
||||
@ -36,7 +36,7 @@ void SiteServer::UpdateMyThings() {
|
||||
for (int ix = 0; ix < Participant::registry.count; ix++) {
|
||||
Participant* participant = participants[ix];
|
||||
#else
|
||||
for (Participant* participant : Participant::registry.GetAll()) {
|
||||
for (Participant* participant : registry.GetAll()) {
|
||||
#endif
|
||||
if (participant == nullptr || participant == this)
|
||||
continue;
|
||||
@ -56,7 +56,7 @@ void SiteServer::UpdateMyThings() {
|
||||
|
||||
#pragma region Receive
|
||||
|
||||
void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
|
||||
void SiteServer::Process(RemoteParticipantUDP* sender, ParticipantMsg* msg) {
|
||||
if (msg->networkId != sender->networkId) {
|
||||
// std::cout << this->name << " received New Client -> " <<
|
||||
// sender->ipAddress
|
||||
@ -67,19 +67,19 @@ void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
void SiteServer::Process(Participant* sender, NetworkIdMsg* msg) {}
|
||||
void SiteServer::Process(RemoteParticipantUDP* sender, NetworkIdMsg* msg) {}
|
||||
|
||||
void SiteServer::Process(Participant* sender, ThingMsg* msg) {
|
||||
Thing* thing = sender->Get(msg->thingId);
|
||||
if (thing == nullptr)
|
||||
void SiteServer::Process(RemoteParticipantUDP* sender, ThingMsg* msg) {
|
||||
Thing* thing = sender->Get(msg->networkId, msg->thingId);
|
||||
if (thing == nullptr) {
|
||||
// new Thing(sender, (Thing::Type)msg->thingType, msg->thingId);
|
||||
// Thing::Reconstruct(sender, msg->thingType, msg->thingId);
|
||||
//thing = new Thing(msg->thingType, sender->root);
|
||||
;
|
||||
}
|
||||
thing->id = msg->thingId;
|
||||
|
||||
if (msg->parentId != 0) {
|
||||
thing->SetParent(Get(msg->parentId));
|
||||
thing->SetParent(Get(msg->networkId, msg->parentId));
|
||||
if (thing->IsRoot())
|
||||
// if (thing->GetParent() != nullptr)
|
||||
#if defined(NO_STD)
|
||||
|
@ -11,7 +11,7 @@
|
||||
namespace RoboidControl {
|
||||
|
||||
/// @brief A participant is device which can communicate with other participants
|
||||
class SiteServer : public ParticipantUDP {
|
||||
class SiteServer : public ParticipantUDPGeneric {
|
||||
|
||||
#pragma region Init
|
||||
|
||||
@ -33,9 +33,9 @@ class SiteServer : public ParticipantUDP {
|
||||
protected:
|
||||
unsigned long nextPublishMe = 0;
|
||||
|
||||
virtual void Process(Participant* sender, ParticipantMsg* msg) override;
|
||||
virtual void Process(Participant* sender, NetworkIdMsg* msg) override;
|
||||
virtual void Process(Participant* sender, ThingMsg* msg) override;
|
||||
virtual void Process(RemoteParticipantUDP* sender, ParticipantMsg* msg) override;
|
||||
virtual void Process(RemoteParticipantUDP* sender, NetworkIdMsg* msg) override;
|
||||
virtual void Process(RemoteParticipantUDP* sender, ThingMsg* msg) override;
|
||||
|
||||
#pragma endregion Receive
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "PosixParticipant.h"
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
#include <arpa/inet.h>
|
||||
@ -9,7 +10,6 @@
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Posix {
|
||||
|
||||
void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
@ -90,7 +90,7 @@ void ParticipantUDP::Receive() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize) {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
// std::cout << "Send to " << remoteParticipant->ipAddress << ":" << ntohs(remoteParticipant->port)
|
||||
// << "\n";
|
||||
@ -132,5 +132,6 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Posix
|
||||
} // namespace RoboidControl
|
||||
|
||||
#endif
|
@ -1,15 +1,15 @@
|
||||
#pragma once
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Posix {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class ParticipantUDP : public ParticipantUDPGeneric {
|
||||
public:
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
bool Send(Participant* remoteParticipant, int bufferSize);
|
||||
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
protected:
|
||||
@ -20,5 +20,5 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace Posix
|
||||
} // namespace RoboidControl
|
||||
#endif
|
||||
|
@ -42,7 +42,7 @@ Thing::Thing(Participant* owner) {
|
||||
|
||||
this->owner = owner;
|
||||
this->owner->Add(this);
|
||||
// std::cout << this->owner->name << ": New root thing " << std::endl;
|
||||
std::cout << this->owner->name << ": New root thing " << std::endl;
|
||||
}
|
||||
|
||||
void Thing::CreateRoot(Participant* owner) {
|
||||
@ -262,7 +262,7 @@ void Thing::Update(bool recursive) {
|
||||
this->nameChanged = false;
|
||||
|
||||
if (recursive) {
|
||||
// std::cout << "# children: " << (int)this->childCount << std::endl;
|
||||
std::cout << "# children: " << (int)this->childCount << std::endl;
|
||||
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
|
||||
Thing* child = this->children[childIx];
|
||||
if (child == nullptr)
|
||||
|
7
Thing.h
7
Thing.h
@ -10,7 +10,7 @@
|
||||
namespace RoboidControl {
|
||||
|
||||
class Participant;
|
||||
class ParticipantUDP;
|
||||
class ParticipantUDPGeneric;
|
||||
|
||||
#define THING_STORE_SIZE 256
|
||||
// IMPORTANT: values higher than 256 will need to change the Thing::id type
|
||||
@ -88,6 +88,11 @@ class Thing {
|
||||
/// This can be either a Thing::Type of a byte value for custom types
|
||||
unsigned char type = Type::Undetermined;
|
||||
|
||||
/// @brief Is this a remote thing?
|
||||
/// A remote thing is owned by other participant
|
||||
/// and is not simulated by the local participant
|
||||
bool isRemote = false;
|
||||
|
||||
/// @brief The participant owning this thing
|
||||
Participant* owner = nullptr;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "WindowsParticipant.h"
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <winsock2.h>
|
||||
@ -7,7 +8,6 @@
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Windows {
|
||||
|
||||
void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
@ -102,7 +102,7 @@ void ParticipantUDP::Receive() {
|
||||
#endif // _WIN32 || _WIN64
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
char ip_str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
|
||||
@ -142,5 +142,6 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Windows
|
||||
} // namespace RoboidControl
|
||||
|
||||
#endif
|
@ -1,15 +1,15 @@
|
||||
#pragma once
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Windows {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class ParticipantUDP : public ParticipantUDPGeneric {
|
||||
public:
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
bool Send(Participant* remoteParticipant, int bufferSize);
|
||||
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
protected:
|
||||
@ -18,5 +18,5 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace Windows
|
||||
} // namespace RoboidControl
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user