<commit_before>#include "Discovery.h"

#include <iostream>
#include <string>
#include <fstream>

#ifdef WIN32
#include <ws2tcpip.h>

#include <Windows.h>
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")

#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <fcntl.h>

#endif



void print_last_errno(int err = 0);

Discovery::Discovery()
{
	m_updateCounter = 0;
}


Discovery::~Discovery()
{
	shutdown((SOCKET)m_recvSocket, 2);
	shutdown((SOCKET)m_soc, 2);

#ifndef WIN32
	close((SOCKET)m_recvSocket);
	close((SOCKET)m_soc);
#endif
}

bool Discovery::start(int broadcastPort)
{

	m_broadcastPort = broadcastPort;
	m_updateCounter = 0;

	// bind receiving socket
	m_recvSocket = (void*)socket(AF_INET, SOCK_DGRAM, 0);
	struct sockaddr_in local;
	memset(&local, 0, sizeof(local));
	local.sin_family = AF_INET;
	local.sin_port = htons(m_broadcastPort);
	local.sin_addr.s_addr = INADDR_ANY;
	int rv = bind((SOCKET)m_recvSocket, (struct sockaddr *) &local, sizeof(local));

	if (rv != 0) {
		printf("Failed to bind UDP receiving socket: ");
		print_last_errno();
		return false;
	}

#ifndef WIN32
	if (-1 == fcntl((SOCKET)m_recvSocket, F_GETFL, 0)) {
		printf("Cannot set non-blocking I/O!\n");
		return false;
	}
#else
	u_long iMode = 1; // non-blocking
	if (ioctlsocket((SOCKET)m_recvSocket, FIONBIO, &iMode) != NO_ERROR)
	{
		printf("Cannot set non-blocking I/O!\n");
		return false;
	}

#endif



	SOCKET soc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (soc == -1) {
		printf("Error: could not create broadcast socket!\n");
		return false;
	}
#ifdef WIN32
	char yes = 1;
#else
	int yes = 1;
#endif
	// allow broadcast
	rv = setsockopt(soc, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
	if (rv == -1) {
		printf("Error: could not set broadcast socket options!\n");
		print_last_errno();
		return false;
	}

	m_soc = (void*)soc;


	return true;
}

const Discovery::NodeDevice &Discovery::findById(const std::string &id) const
{
	for (auto n : m_discovered) {
		if (n.id == id) {
			return n;
		}
	}
	return NodeDevice();
}

bool Discovery::update()
{
	while (true) {
		struct sockaddr_in remote;  // UDP Sender IPv4 Adress und Port Struktur
#ifndef WIN32
		unsigned
#endif
			char recvBuffer[1024];
		int addr_len = sizeof(remote);
		int len = recvfrom((SOCKET)m_recvSocket, (char*)recvBuffer, sizeof(recvBuffer), 0, (struct sockaddr *) &remote, &addr_len);
		
		// would block? 
		if (len == -1 || len > sizeof(recvBuffer))
			break;	

		if (len < 20)
			continue;


		recvBuffer[1000] = 0;

		NodeDevice nd;
		nd.name = std::string(&recvBuffer[strlen("ULLTRA_DEV_R") + 1]);
		nd.id = std::string(&recvBuffer[strlen("ULLTRA_DEV_R") + nd.name.length() + 2]);
		nd.addr = remote.sin_addr;

		if (strcmp("ULLTRA_DEV_R", recvBuffer) == 0) { // register
			// check if we somehow received our own broadcast
			if (nd.id ==  getHwAddress()) {
				continue;
			}

			// already there?
			if (findById(nd.id).exists()) {
				continue;
			}

			m_discovered.push_back(nd);
		}
		else if (strcmp("ULLTRA_DEV_Z", recvBuffer) == 0) { // unregister
			char client_name[32];
			strncpy_s(client_name, sizeof(client_name), &recvBuffer[18], 32);

			for (auto it = m_discovered.begin(); it != m_discovered.end(); it++) {
				if (it->id == nd.id || it->addr.s_addr == nd.addr.s_addr) {
					m_discovered.erase(it);
					break;
				}
			}
		}
		else if ((strcmp("ULLTRA_DEV_H", recvBuffer)) == 0) { // heartbeat

			//ProcessHeartbeat(remote.sin_addr, ((HeartBeatData*)&recvBuffer[10]));
		}
	}
	

	if(m_updateCounter % 4 == 0)
		sayIAmHere(true);

	m_updateCounter++;

	return true;
}

<commit_msg>fixes<commit_after>#include "Discovery.h"

#include <iostream>
#include <string>
#include <fstream>

#ifdef WIN32
#include <ws2tcpip.h>

#include <Windows.h>
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")

#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <fcntl.h>

#endif



void print_last_errno(int err = 0);

Discovery::Discovery()
{
	m_updateCounter = 0;
}


Discovery::~Discovery()
{
	shutdown((SOCKET)m_recvSocket, 2);
	shutdown((SOCKET)m_soc, 2);

#ifndef WIN32
	close((SOCKET)m_recvSocket);
	close((SOCKET)m_soc);
#endif
}

bool Discovery::start(int broadcastPort)
{

	m_broadcastPort = broadcastPort;
	m_updateCounter = 0;

	// bind receiving socket
	m_recvSocket = (void*)socket(AF_INET, SOCK_DGRAM, 0);
	struct sockaddr_in local;
	memset(&local, 0, sizeof(local));
	local.sin_family = AF_INET;
	local.sin_port = htons(m_broadcastPort);
	local.sin_addr.s_addr = INADDR_ANY;
	int rv = bind((SOCKET)m_recvSocket, (struct sockaddr *) &local, sizeof(local));

	if (rv != 0) {
		printf("Failed to bind UDP receiving socket: ");
		print_last_errno();
		return false;
	}

#ifndef WIN32
	if (-1 == fcntl((SOCKET)m_recvSocket, F_GETFL, 0)) {
		printf("Cannot set non-blocking I/O!\n");
		return false;
	}
#else
	u_long iMode = 1; // non-blocking
	if (ioctlsocket((SOCKET)m_recvSocket, FIONBIO, &iMode) != NO_ERROR)
	{
		printf("Cannot set non-blocking I/O!\n");
		return false;
	}

#endif



	SOCKET soc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (soc == -1) {
		printf("Error: could not create broadcast socket!\n");
		return false;
	}
#ifdef WIN32
	char yes = 1;
#else
	int yes = 1;
#endif
	// allow broadcast
	rv = setsockopt(soc, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
	if (rv == -1) {
		printf("Error: could not set broadcast socket options!\n");
		print_last_errno();
		return false;
	}

	m_soc = (void*)soc;


	return true;
}

const Discovery::NodeDevice &Discovery::findById(const std::string &id) const
{
	for (auto n : m_discovered) {
		if (n.id == id) {
			return n;
		}
	}
	return NodeDevice();
}

bool Discovery::update()
{
	while (true) {
		struct sockaddr_in remote;  // UDP Sender IPv4 Adress und Port Struktur
#ifndef WIN32
		unsigned
#endif
			char recvBuffer[1024];
		int addr_len = sizeof(remote);
		int len = recvfrom((SOCKET)m_recvSocket, (char*)recvBuffer, sizeof(recvBuffer), 0, (struct sockaddr *) &remote, &addr_len);
		
		// would block? 
		if (len == -1 || len > sizeof(recvBuffer))
			break;	

		if (len < 20)
			continue;


		recvBuffer[1000] = 0;

		NodeDevice nd;
		nd.name = std::string(&recvBuffer[strlen("ULLTRA_DEV_R") + 1]);
		nd.id = std::string(&recvBuffer[strlen("ULLTRA_DEV_R") + nd.name.length() + 2]);
		nd.addr = remote.sin_addr;

		if (strcmp("ULLTRA_DEV_R", recvBuffer) == 0) { // register
			// check if we somehow received our own broadcast
			if (nd.id ==  getHwAddress()) {
				continue;
			}

			// already there?
			if (findById(nd.id).exists()) {
				continue;
			}

			m_discovered.push_back(nd);
			std::cout << "Discovered node " << nd.name << " (id " << nd.id << ")" << std::endl;
		}
		else if (strcmp("ULLTRA_DEV_Z", recvBuffer) == 0) { // unregister
			char client_name[32];
			strncpy_s(client_name, sizeof(client_name), &recvBuffer[18], 32);

			for (auto it = m_discovered.begin(); it != m_discovered.end(); it++) {
				if (it->id == nd.id || it->addr.s_addr == nd.addr.s_addr) {
					m_discovered.erase(it);
					break;
				}
			}
		}
		else if ((strcmp("ULLTRA_DEV_H", recvBuffer)) == 0) { // heartbeat

			//ProcessHeartbeat(remote.sin_addr, ((HeartBeatData*)&recvBuffer[10]));
		}
	}
	

	if(m_updateCounter % 4 == 0)
		sayIAmHere(true);

	m_updateCounter++;

	return true;
}


{'fraction_non_alphanumeric': 0.16224503399546727, 'fraction_numerical': 0.014664711371817091, 'mean_word_length': 4.086101694915254, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6220964', 'n_tokens_mistral': 3224, 'n_tokens_neox': 2902, 'n_words': 645}