site participant test succeeds on MacOS
This commit is contained in:
parent
f4727326bf
commit
4c9018d451
@ -44,11 +44,11 @@ else()
|
|||||||
LinearAlgebra
|
LinearAlgebra
|
||||||
)
|
)
|
||||||
|
|
||||||
if(MSVC)
|
# if(MSVC)
|
||||||
target_compile_options(ControlCoreTest PRIVATE /W4 /WX)
|
# target_compile_options(ControlCoreTest PRIVATE /W4 /WX)
|
||||||
# else()
|
# # else()
|
||||||
# target_compile_options(ControlCoreTest PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
# # target_compile_options(ControlCoreTest PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||||
endif()
|
# endif()
|
||||||
|
|
||||||
|
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
|
105
Participant.cpp
105
Participant.cpp
@ -13,6 +13,7 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h> // For fcntl
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Participant::Participant() {}
|
Participant::Participant() {}
|
||||||
@ -67,8 +68,12 @@ void Passer::Control::Participant::SetupUDP(int localPort,
|
|||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,8 +84,12 @@ void Passer::Control::Participant::SetupUDP(int localPort,
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,31 +97,61 @@ void Passer::Control::Participant::SetupUDP(int localPort,
|
|||||||
if (bind(this->sock, (const struct sockaddr *)&server_addr,
|
if (bind(this->sock, (const struct sockaddr *)&server_addr,
|
||||||
sizeof(server_addr)) < 0) {
|
sizeof(server_addr)) < 0) {
|
||||||
std::cerr << "Bind failed" << std::endl;
|
std::cerr << "Bind failed" << std::endl;
|
||||||
closesocket(this->sock);
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
closesocket(sock);
|
||||||
|
WSACleanup();
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
close(sock);
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bind the socket to the specified port
|
||||||
|
// if (bind(this->sock, (const struct sockaddr *)&server_addr,
|
||||||
|
// sizeof(server_addr)) < 0) {
|
||||||
|
// std::cerr << "Bind failed" << std::endl;
|
||||||
|
// #if defined(_WIN32) || defined(_WIN64)
|
||||||
|
// closesocket(sock);
|
||||||
|
// WSACleanup();
|
||||||
|
// #elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
// close(sock);
|
||||||
|
// #endif
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
// Set up the broadcast address
|
// Set up the broadcast address
|
||||||
memset(&broadcast_addr, 0, sizeof(broadcast_addr));
|
memset(&broadcast_addr, 0, sizeof(broadcast_addr));
|
||||||
broadcast_addr.sin_family = AF_INET;
|
broadcast_addr.sin_family = AF_INET;
|
||||||
broadcast_addr.sin_port = htons((u_short)remotePort);
|
broadcast_addr.sin_port = htons((u_short)remotePort);
|
||||||
if (inet_pton(AF_INET, "255.255.255.255", &broadcast_addr.sin_addr) <= 0) {
|
if (inet_pton(AF_INET, "255.255.255.255", &broadcast_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;
|
||||||
}
|
}
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
BOOL broadcast = TRUE;
|
BOOL broadcast = TRUE;
|
||||||
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast,
|
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast,
|
||||||
sizeof(broadcast)) == SOCKET_ERROR) {
|
sizeof(broadcast)) == SOCKET_ERROR) {
|
||||||
std::cerr << "Setting socket option for broadcast failed" << std::endl;
|
std::cerr << "Setting socket option for broadcast failed" << std::endl;
|
||||||
}
|
}
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
int broadcastEnable = 1;
|
||||||
|
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)) < 0) {
|
||||||
|
std::cerr << "Failed to set socket options" << std::endl;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Participant::Update(unsigned long currentTimeMs) {
|
void Participant::Update(unsigned long currentTimeMs) {
|
||||||
if (currentTimeMs > this->nextPublishMe) {
|
if (currentTimeMs > this->nextPublishMe) {
|
||||||
this->Publish(&ClientMsg(this->networkId));
|
ClientMsg* msg = new ClientMsg(this->networkId);
|
||||||
|
this->Publish(msg);
|
||||||
|
delete msg;
|
||||||
std::cout << this->name << " published ClientMsg\n";
|
std::cout << this->name << " published ClientMsg\n";
|
||||||
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
||||||
}
|
}
|
||||||
@ -128,15 +167,22 @@ void Participant::ReceiveUDP() {
|
|||||||
// << 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,
|
int packetSize = recvfrom(this->sock, buffer, sizeof(buffer), 0,
|
||||||
(struct sockaddr *)&client_addr, &len);
|
(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_ntop(AF_INET, &(client_addr.sin_addr), sender_ipAddress,
|
||||||
@ -147,9 +193,11 @@ void Participant::ReceiveUDP() {
|
|||||||
this->GetParticipant(sender_ipAddress, sender_port);
|
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 remote participant " << sender_ipAddress << ":"
|
// std::cout << "New sender " << sender_ipAddress << ":"
|
||||||
<< sender_port << " "
|
// << sender_port << "\n";
|
||||||
<< (unsigned char)remoteParticipant->networkId << "|\n";
|
// std::cout << "New remote participant " << remoteParticipant->ipAddress << ":"
|
||||||
|
// << remoteParticipant->port << " "
|
||||||
|
// << (int)remoteParticipant->networkId << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
ReceiveData(packetSize, remoteParticipant);
|
ReceiveData(packetSize, remoteParticipant);
|
||||||
@ -167,7 +215,6 @@ Participant *Participant::GetParticipant(const char *ipAddress, int port) {
|
|||||||
Participant *Participant::AddParticipant(const char *ipAddress, int port) {
|
Participant *Participant::AddParticipant(const char *ipAddress, int port) {
|
||||||
Participant *participant = new Participant(ipAddress, port);
|
Participant *participant = new Participant(ipAddress, port);
|
||||||
participant->networkId = (unsigned char)this->participants.size();
|
participant->networkId = (unsigned char)this->participants.size();
|
||||||
std::cout << "nw id " << (unsigned char)participant->networkId << "\n";
|
|
||||||
this->participants.push_back(participant);
|
this->participants.push_back(participant);
|
||||||
return participant;
|
return participant;
|
||||||
}
|
}
|
||||||
@ -182,10 +229,10 @@ void Participant::SendThingInfo(Thing *thing) {
|
|||||||
msg = new NameMsg(this->networkId, thing);
|
msg = new NameMsg(this->networkId, thing);
|
||||||
this->Send(msg);
|
this->Send(msg);
|
||||||
delete msg;
|
delete msg;
|
||||||
// msg = new ModelUrlMsg(this->networkId, thing);
|
msg = new ModelUrlMsg(this->networkId, thing);
|
||||||
// this->Send(msg);
|
this->Send(msg);
|
||||||
// delete msg;
|
delete msg;
|
||||||
this->Send(&ModelUrlMsg(this->networkId, thing));
|
// this->Send(&ModelUrlMsg(this->networkId, thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Participant::Send(IMessage *msg) {
|
bool Participant::Send(IMessage *msg) {
|
||||||
@ -196,18 +243,26 @@ bool Participant::Send(IMessage *msg) {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
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, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
|
||||||
std::cout << "Publish to " << ip_str << ":" << ntohs(server_addr.sin_port)
|
std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port)
|
||||||
<< "\n";
|
<< "\n";
|
||||||
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0,
|
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0,
|
||||||
(struct sockaddr *)&remote_addr, sizeof(remote_addr));
|
(struct sockaddr *)&remote_addr, sizeof(remote_addr));
|
||||||
if (sent_bytes == SOCKET_ERROR) {
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
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;
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
if (sent_bytes < 0) {
|
||||||
|
std::cerr << "sendto failed with error: " << sent_bytes << std::endl;
|
||||||
|
close(sock);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,13 +278,21 @@ bool Participant::Publish(IMessage *msg) {
|
|||||||
int sent_bytes =
|
int sent_bytes =
|
||||||
sendto(sock, this->buffer, bufferSize, 0,
|
sendto(sock, this->buffer, bufferSize, 0,
|
||||||
(struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr));
|
(struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr));
|
||||||
if (sent_bytes == SOCKET_ERROR) {
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
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;
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
if (sent_bytes < 0) {
|
||||||
|
std::cerr << "sendto failed with error: " << sent_bytes << std::endl;
|
||||||
|
close(sock);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,10 +306,14 @@ void Participant::ReceiveData(unsigned char bufferSize,
|
|||||||
unsigned char msgId = this->buffer[0];
|
unsigned char msgId = this->buffer[0];
|
||||||
switch (msgId) {
|
switch (msgId) {
|
||||||
case ClientMsg::id: {
|
case ClientMsg::id: {
|
||||||
Process(remoteParticipant, &ClientMsg(this->buffer));
|
ClientMsg* msg = new ClientMsg(this->buffer);
|
||||||
|
Process(remoteParticipant, msg);
|
||||||
|
delete msg;
|
||||||
}
|
}
|
||||||
case NetworkIdMsg::id: {
|
case NetworkIdMsg::id: {
|
||||||
Process(remoteParticipant, &NetworkIdMsg(this->buffer));
|
NetworkIdMsg* msg = new NetworkIdMsg(this->buffer);
|
||||||
|
Process(remoteParticipant, msg);
|
||||||
|
delete msg;
|
||||||
} break;
|
} break;
|
||||||
case InvestigateMsg::id: {
|
case InvestigateMsg::id: {
|
||||||
InvestigateMsg msg = InvestigateMsg(this->buffer);
|
InvestigateMsg msg = InvestigateMsg(this->buffer);
|
||||||
@ -271,7 +338,7 @@ void Participant::Process(Participant *sender, ClientMsg *msg) {}
|
|||||||
|
|
||||||
void Participant::Process(Participant *sender, NetworkIdMsg *msg) {
|
void Participant::Process(Participant *sender, NetworkIdMsg *msg) {
|
||||||
std::cout << this->name << " receive network id " << (int)this->networkId
|
std::cout << this->name << " receive network id " << (int)this->networkId
|
||||||
<< " " << msg->networkId << "\n";
|
<< " " << (int)msg->networkId << "\n";
|
||||||
if (this->networkId != msg->networkId) {
|
if (this->networkId != msg->networkId) {
|
||||||
this->networkId = msg->networkId;
|
this->networkId = msg->networkId;
|
||||||
Thing **allThings = Thing::GetAllThings();
|
Thing **allThings = Thing::GetAllThings();
|
||||||
|
@ -11,6 +11,11 @@
|
|||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Passer {
|
namespace Passer {
|
||||||
@ -28,7 +33,11 @@ public:
|
|||||||
const char *ipAddress = "0.0.0.0";
|
const char *ipAddress = "0.0.0.0";
|
||||||
int port = 0;
|
int port = 0;
|
||||||
|
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
SOCKET sock;
|
SOCKET sock;
|
||||||
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
|
int sock;
|
||||||
|
#endif
|
||||||
sockaddr_in remote_addr;
|
sockaddr_in remote_addr;
|
||||||
sockaddr_in server_addr;
|
sockaddr_in server_addr;
|
||||||
sockaddr_in broadcast_addr;
|
sockaddr_in broadcast_addr;
|
||||||
|
@ -19,8 +19,11 @@ void Passer::Control::SiteServer::Update(unsigned long currentTimeMs) {
|
|||||||
void Passer::Control::SiteServer::Process(Participant *sender, ClientMsg *msg) {
|
void Passer::Control::SiteServer::Process(Participant *sender, ClientMsg *msg) {
|
||||||
if (msg->networkId == 0) {
|
if (msg->networkId == 0) {
|
||||||
std::cout << this->name << " received New Client -> "
|
std::cout << this->name << " received New Client -> "
|
||||||
<< (unsigned char)sender->networkId << "\n";
|
<< sender->ipAddress << " "
|
||||||
sender->Send(&NetworkIdMsg(sender->networkId));
|
<< (int)sender->networkId << "\n";
|
||||||
|
NetworkIdMsg* msg = new NetworkIdMsg(sender->networkId);
|
||||||
|
sender->Send(msg);
|
||||||
|
delete msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <ws2tcpip.h>
|
// #include <ws2tcpip.h>
|
||||||
|
|
||||||
#include "Participant.h"
|
#include "Participant.h"
|
||||||
#include "SiteServer.h"
|
#include "SiteServer.h"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user