Participant test succeeds

This commit is contained in:
Pascal Serrarens 2025-01-02 12:48:14 +01:00
parent f630b0c7cc
commit c4a778e563
5 changed files with 84 additions and 26 deletions

View File

@ -41,7 +41,7 @@ unsigned char ClientMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
return ix;
return ClientMsg::length;
}
// bool ClientMsg::Send(Participant *participant, unsigned char networkId) {

View File

@ -27,6 +27,7 @@ public:
class ClientMsg : public IMessage {
public:
static const unsigned char id = 0xA0;
static const unsigned char length = 2;
unsigned char networkId;
ClientMsg(char networkId);

View File

@ -4,6 +4,7 @@
#if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#elif defined(__unix__) || defined(__APPLE__)
#include <arpa/inet.h>
@ -13,7 +14,6 @@
#endif
Passer::Control::Participant::Participant(const char *ipAddress, int port) {
sockaddr_in server_addr;
// Create a UDP socket
#if defined(_WIN32) || defined(_WIN64)
@ -28,40 +28,39 @@ Passer::Control::Participant::Participant(const char *ipAddress, int port) {
#if defined(_WIN32) || defined(_WIN64)
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#elif defined(__unix__) || defined(__APPLE__)
sock = socket(AF_INET, SOCK_DGRAM, 0);
this->sock = socket(AF_INET, SOCK_DGRAM, 0);
#endif
if (sock < 0) {
if (this->sock < 0) {
std::cerr << "Error creating socket" << std::endl;
return;
}
// Set up the server address
// memset(&server_addr, 0, sizeof(server_addr));
// server_addr.sin_family = AF_INET;
// server_addr.sin_port = htons(PORT);
// server_addr.sin_addr.s_addr = INADDR_ANY;
// Set up the server address structure
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); // Port to send the packet to
server_addr.sin_addr.s_addr =
inet_addr(ipAddress); // Destination IP address (localhost)
// Bind the socket
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error binding socket" << std::endl;
server_addr.sin_port = htons((u_short)port); // Port to send the packet to
if (inet_pton(AF_INET, "255.255.255.255", &server_addr.sin_addr) <= 0) {
std::cerr << "Invalid address" << std::endl;
closesocket(sock);
WSACleanup();
return;
}
// std::cout << "Server is listening on port " << PORT << "..." << std::endl;
BOOL broadcast = TRUE;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast,
sizeof(broadcast)) == SOCKET_ERROR) {
std::cerr << "Setting socket option for broadcast failed" << std::endl;
}
}
void Participant::Update(unsigned long currentTimeMs) {
// std::cout << "update\n";
if (currentTimeMs > this->nextPublishMe) {
std::cout << "publish\n";
this->Publish(ClientMsg(this->networkId));
ClientMsg msg = ClientMsg(this->networkId);
this->Publish(msg);
// Console.WriteLine($"{this.name} Sent ClientMsg {this.networkId}");
std::cout << this->name << " sent ClientMsg\n";
this->nextPublishMe = currentTimeMs + this->publishInterval;
}
}
@ -70,17 +69,26 @@ bool Participant::SendBuffer(unsigned char bufferSize) { return false; }
bool Participant::PublishBuffer(unsigned char bufferSize) { return false; }
bool Participant::Publish(IMessage msg) {
#include <ws2tcpip.h>
bool Participant::Publish(
ClientMsg msg) { // I want to use IMessage here, but then the serialize
// calls the IMessage.Serialize...
// Send the message to the specified address and port
int bufferSize = msg.Serialize(this->buffer);
std::cout << "buffer size " << bufferSize << "\n";
if (bufferSize <= 0)
return true;
std::cout << "Publish to " << "\n";
// char ip_str[INET_ADDRSTRLEN];
// inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
// std::cout << "Publish to " << ip_str << ":" << ntohs(server_addr.sin_port)
// << "\n";
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes == SOCKET_ERROR) {
std::cerr << "Error sending message" << std::endl;
int error_code = WSAGetLastError(); // Get the error code on Windows
std::cerr << "sendto failed with error: " << error_code << std::endl;
closesocket(sock);
WSACleanup();
return false;

View File

@ -15,6 +15,7 @@ public:
char buffer[1024];
long publishInterval = 3000; // 3 seconds
unsigned char networkId = 0;
const char *name = "Participant";
SOCKET sock;
sockaddr_in server_addr;
@ -26,12 +27,12 @@ public:
virtual bool SendBuffer(unsigned char bufferSize);
virtual bool PublishBuffer(unsigned char bufferSize);
bool Publish(IMessage msg);
bool Publish(ClientMsg msg);
void ReceiveData(unsigned char bufferSize);
protected:
long nextPublishMe = 0;
unsigned long nextPublishMe = 0;
virtual void ProcessNetworkIdMsg(NetworkIdMsg msg);
virtual void ProcessInvestigateMsg(InvestigateMsg msg);

View File

@ -5,6 +5,7 @@
#include <gtest/gtest.h>
#include <chrono>
#include <ws2tcpip.h>
#include "Participant.h"
#include "Thing.h"
@ -32,8 +33,55 @@ protected:
}
};
void send_udp_message(const char *message, const char *ip, int port) {
WSADATA wsaData;
SOCKET sock;
struct sockaddr_in server_addr;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed" << std::endl;
return;
}
// Create a UDP socket
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
std::cerr << "Socket creation failed" << std::endl;
WSACleanup();
return;
}
// Set up the sockaddr_in structure for the destination
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons((u_short)port);
// Convert the IP address
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
std::cerr << "Invalid address" << std::endl;
closesocket(sock);
WSACleanup();
return;
}
// Send the UDP message
int sent_bytes = sendto(sock, message, strlen(message), 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes == SOCKET_ERROR) {
std::cerr << "sendto failed with error: " << WSAGetLastError() << std::endl;
} else {
std::cout << "Message sent successfully!" << std::endl;
}
// Close the socket and clean up Winsock
closesocket(sock);
WSACleanup();
}
TEST_F(ControlCoreSuite, Dummytest) {
// Participant participant = Participant("127.0.0.1", 7681);
send_udp_message("Hello, UDP!", "127.0.0.1", 8080); // Send to localhost
ASSERT_EQ(1, 1);
}
@ -42,7 +90,7 @@ TEST_F(ControlCoreSuite, Participant) {
unsigned long milliseconds = get_time_ms();
unsigned long startTime = milliseconds;
while (milliseconds < startTime + 7000) {
while (milliseconds < startTime + 1000) {
participant.Update(milliseconds);
milliseconds = get_time_ms();