<commit_before>#include "pse.h"
#include <pthread.h> 
#include "InfoThread.h"
#include "LockableFile.h"
#include <sys/time.h>
#include "constantes.h"

//the thread function
void	*connexionHandler(void *);
void	connexionHandlerClient(void *);
void	connexionHandlerServer(void *);
int	rAzLog(int fd); 
void	iniCohorte(InfoThread *cohorte, int size);

InfoThread cohorteServer[NBSERVERS];
InfoThread cohorteClient[NBCLIENTS];

int	main(int argc , char *argv[])
{
	//InfoThread *cohorteClient = malloc(sizeof(InfoThread)*NBCLIENTS);
	int	isServer = -1,
		readSize,
		identifier,
		j;
	int 	ecoute, 
		client_sock, 
		c;
	struct	sockaddr_in annuaire, 
			   client;
	FileL	log, 
		datasServers;
	
	if (argc < 2)
		erreur("usage: %s port\n", argv[0]);
	
	//Create socket
	ecoute = socket(AF_INET , SOCK_STREAM , 0);
	if (ecoute == -1)
	{
		erreur_IO("Could not create socket");
	}
	//puts("Socket created");
	
	
	//Prepare the sockaddr_in structure
	annuaire.sin_family = AF_INET;
	annuaire.sin_addr.s_addr = INADDR_ANY;
	annuaire.sin_port = htons( atoi(argv[1]) );
	
	//Bind
	if( bind(ecoute,(struct sockaddr *)&annuaire , sizeof(annuaire)) < 0){
		perror("bind failed. Error");
		return 1;
	}
	//puts("bind done");
	
	log.fd = open("journal.log", O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0660);
	if (log.fd == -1) {
		erreur_IO("open log");
	}
	log.mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;

	datasServers.fd = open("servers.datas", O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0660);
	if (datasServers.fd == -1) {
		erreur_IO("open datas Servers");
	}
	datasServers.mutex =(pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
	
	//Listen
	listen(ecoute , NBCLIENTS + NBSERVERS);
	
	for (j = 0; j<NBSERVERS;j++){
		cohorteServer[j].mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
		cohorteServer[j].isServer = 1;
		cohorteServer[j].isFree = 1;
		cohorteServer[j].ip = malloc(sizeof(char)*16);
	}

	//Accept an incoming connection
	//puts("Waiting for incoming connections...");
	InfoThread *th;
	c = sizeof(struct sockaddr_in);
	while( (client_sock = accept(ecoute, (struct sockaddr *)&client, (socklen_t*)&c)) )
	{
		
		puts("Connection accepted");
	 	
		fd_set 	rfds;
		struct 	timeval tv = {5,0};

		FD_ZERO(&rfds);
		FD_SET(client_sock, &rfds);

		readSize = select(client_sock+1, &rfds, NULL, NULL, &tv);
		if (readSize == -1)
		       perror("select()");
		else if (readSize){
			readSize = recv(client_sock, &identifier, sizeof(int),0);
			printf("Identifier received : %d\n", identifier);
			if (identifier == ID_SERVER){//0X00F0
				isServer = 1;

				for(j=0;j<NBSERVERS;j++){
					if(cohorteServer[j].isFree==1)
						break;
				}

				pthread_mutex_lock(&(cohorteServer[j].mutex));
					printf("Affectation Server Thread %p = %d\n",&(cohorteServer[j]),cohorteServer[j].isFree);
					cohorteServer[j].logFile.fd = log.fd;
					cohorteServer[j].datasFile.fd = datasServers.fd;
					cohorteServer[j].sock = client_sock;
					printf("Adresse du serveur %s\n",stringIP(ntohl(annuaire.sin_addr.s_addr)));
					strcpy(cohorteServer[j].ip,stringIP(ntohl(annuaire.sin_addr.s_addr)));
				pthread_mutex_unlock(&(cohorteServer[j].mutex));
				if (pthread_create(&(cohorteServer[j].thread_id), NULL, connexionHandlerServer, &j) < 0){
					perror("could not create thread server");
				}
				printf("Server joined\n");

			}
			else if (identifier == ID_CLIENT){//0X0F00
				isServer = 0;
				for(j = 0; j < NBCLIENTS; j++){
					if (cohorteClient[j].isFree==1)
						break;
				}
				cohorteClient[j].isFree = 0;
				printf("Client joined\n");
			}
		}
		//Now join the thread , so that we dont terminate before the thread
		//pthread_join( thread_id , NULL);
		puts("Handler assigned");
	}
	
	if (client_sock < 0)
	{
		perror("accept failed");
		return 1;
	}
	
	return 0;
}
/*
void	*connexionHandler(void *tDatas){
	
	int 	retval;
	int 	readSize;

	//int 	identifier = *(int *)tDatas;
	int j = 
	InfoThread *threadData = (InfoThread *) tDatas;//&(cohorteServer[identifier]);
	int sock = threadData->sock;
	//printf("Thread Identifier <%d>\n", identifier);
	//printf("ConnexionHandler <%p>\n", &(threadData));

	pthread_mutex_lock(&(threadData->mutex));
	if (threadData->isServer == 0)
		connexionHandlerClient(tDatas);
	else if (threadData->isServer == 1)
		connexionHandlerServer(tDatas);
	else{
		erreur("Type de thread non reconnu\n");
	}
	pthread_mutex_unlock(&(threadData->mutex));
}*/

void	connexionHandlerServer(void *tDatas){
	printf("Je suis un server!!!\n");
	fd_set 	rfds;
	struct	timeval tv = {5,0};
	int	retval;

	int j = *(int *) tDatas;
	InfoThread threadData = cohorteServer[j];
	int	log = threadData.logFile.fd;
	int	datasFile = threadData.datasFile.fd;
	int	sock = threadData.sock;

	int	readSize;
	short	*port = malloc(sizeof(short));
	char	pingValue = 1;

	FD_ZERO(&rfds);
	FD_SET(sock, &rfds);

	retval = select(sock+1, &rfds, NULL, NULL, &tv);
	if (retval == -1)
               perror("select()");
	else if (retval){
		readSize = recv(sock, port, sizeof(short),0);
	}
	else
		printf("No data within five seconds.\n");

	pthread_mutex_lock(&(threadData.datasFile.mutex));

	//write adresse et port to datasFile
	write(threadData.datasFile.fd, threadData.ip, sizeof(threadData.ip));
	write(threadData.datasFile.fd, " ", sizeof(char));
	write(threadData.datasFile.fd, port, sizeof(short));
	write(threadData.datasFile.fd, '\n', sizeof(char));
	pthread_mutex_unlock(&(threadData.datasFile.mutex));

	while(1){
		retval = select(sock+1, &rfds, NULL, NULL, &tv);
		if (retval == -1)
		       perror("select()");
		else if (retval){
			tv.tv_usec = 0;
			tv.tv_sec = 5;
			readSize = recv(sock, &pingValue, sizeof(char),0);
			if (readSize <=0 || readSize == LIGNE_MAX)
				erreur_IO("lireLigne");
			else if (readSize==0)
				continue;
			else{
				//Traitement des données recues si necessaire mais ici on ne recoit que des pings normalement
			}
		}
		else{
			printf("No data within five seconds : Tiemout.\n");<commit_msg>enregistrement donnees serveur et non prise en compte de connexions supérieures à la limite fixée pour les serveurs. Reste à faire même respect de limite pour les clients et refus de connexion plutôt que non prise en compte.<commit_after>#include "pse.h"
#include <pthread.h> 
#include "InfoThread.h"
#include "LockableFile.h"
#include <sys/time.h>
#include "constantes.h"

//the thread function
void	*connexionHandler(void *);
void	connexionHandlerClient(void *);
void	connexionHandlerServer(void *);
int	rAzLog(int fd); 
void	iniCohorte(InfoThread *cohorte, int size);

InfoThread cohorteServer[NBSERVERS];
InfoThread cohorteClient[NBCLIENTS];

int	main(int argc , char *argv[])
{
	int	isServer = -1,
		readSize,
		identifier,
		j, k;
	int 	ecoute, 
		client_sock, 
		c;
	struct	sockaddr_in annuaire, 
			   client;
	FileL	log, 
		datasServers;
	
	if (argc < 2)
		erreur("usage: %s port\n", argv[0]);
	
	//Create socket
	ecoute = socket(AF_INET , SOCK_STREAM , 0);
	if (ecoute == -1)
	{
		erreur_IO("Could not create socket");
	}
	//puts("Socket created");
	
	
	//Prepare the sockaddr_in structure
	annuaire.sin_family = AF_INET;
	annuaire.sin_addr.s_addr = INADDR_ANY;
	annuaire.sin_port = htons( atoi(argv[1]) );
	
	//Bind
	if( bind(ecoute,(struct sockaddr *)&annuaire , sizeof(annuaire)) < 0){
		perror("bind failed. Error");
		return 1;
	}
	//puts("bind done");
	
	log.fd = open("journal.log", O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0660);
	if (log.fd == -1) {
		erreur_IO("open log");
	}
	log.mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;

	datasServers.fd = open("servers.datas", O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0660);
	if (datasServers.fd == -1) {
		erreur_IO("open datas Servers");
	}
	datasServers.mutex =(pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
	
	//Listen
	listen(ecoute , NBCLIENTS + NBSERVERS);
	
	for (j = 0; j<NBSERVERS;j++){
		cohorteServer[j].mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
		cohorteServer[j].isServer = 1;
		cohorteServer[j].isFree = 1;
		cohorteServer[j].ip = malloc(sizeof(char)*17);
	}

	//Accept an incoming connection
	//puts("Waiting for incoming connections...");
	InfoThread *th;
	c = sizeof(struct sockaddr_in);
	while( (client_sock = accept(ecoute, (struct sockaddr *)&client, (socklen_t*)&c)) )
	{
		
		puts("Connection accepted");
	 	
		fd_set 	rfds;
		struct 	timeval tv = {5,0};

		FD_ZERO(&rfds);
		FD_SET(client_sock, &rfds);

		readSize = select(client_sock+1, &rfds, NULL, NULL, &tv);
		if (readSize == -1)
		       perror("select()");
		else if (readSize){
			readSize = recv(client_sock, &identifier, sizeof(int),0);
			printf("Identifier received : %d\n", identifier);
			if (identifier == ID_SERVER){//0X00F0
				isServer = 1;

				for(j=0;j<NBSERVERS;j++){
					if(cohorteServer[j].isFree==1)
						break;
				}
				
				if (j == NBSERVERS){
					shutdown(ecoute, 2);
					close(ecoute);
					continue;
				}

				pthread_mutex_lock(&(cohorteServer[j].mutex));
					printf("Affectation Server Thread %p = %d\n",&(cohorteServer[j]),cohorteServer[j].isFree);
					cohorteServer[j].logFile.fd = log.fd;
					cohorteServer[j].datasFile.fd = datasServers.fd;
					cohorteServer[j].sock = client_sock;
					printf("Adresse du serveur %s\n",stringIP(ntohl(client.sin_addr.s_addr)));
					strcpy(cohorteServer[j].ip,stringIP(ntohl(client.sin_addr.s_addr)));
				pthread_mutex_unlock(&(cohorteServer[j].mutex));
				
				if (pthread_create(&(cohorteServer[j].thread_id), NULL, connexionHandlerServer, &j) < 0){
					perror("could not create thread server");
				}
				printf("Server joined\n");
			}
			else if (identifier == ID_CLIENT){//0X0F00
				isServer = 0;
				for(j = 0; j < NBCLIENTS; j++){
					if (cohorteClient[j].isFree==1)
						break;
				}
				cohorteClient[j].isFree = 0;
				printf("Client joined\n");
			}
		}
		//Now join the thread , so that we dont terminate before the thread
		//pthread_join( thread_id , NULL);
		puts("Handler assigned");
	}
	
	if (client_sock < 0)
	{
		perror("accept failed");
		return 1;
	}
	
	return 0;
}
/*
void	*connexionHandler(void *tDatas){
	
	int 	retval;
	int 	readSize;

	//int 	identifier = *(int *)tDatas;
	int j = 
	InfoThread *threadData = (InfoThread *) tDatas;//&(cohorteServer[identifier]);
	int sock = threadData->sock;
	//printf("Thread Identifier <%d>\n", identifier);
	//printf("ConnexionHandler <%p>\n", &(threadData));

	pthread_mutex_lock(&(threadData->mutex));
	if (threadData->isServer == 0)
		connexionHandlerClient(tDatas);
	else if (threadData->isServer == 1)
		connexionHandlerServer(tDatas);
	else{
		erreur("Type de thread non reconnu\n");
	}
	pthread_mutex_unlock(&(threadData->mutex));
}*/

void	connexionHandlerServer(void *tDatas){
	printf("Je suis un server!!!\n");
	fd_set 	rfds;
	struct	timeval tv = {5,0};
	int	retval;

	int j = *(int *) tDatas;
	InfoThread threadData = cohorteServer[j];
	int	log = threadData.logFile.fd;
	int	datasFile = threadData.datasFile.fd;
	int	sock = threadData.sock;

	int	readSize;
	short	*port = malloc(sizeof(short));
	char	pingValue = 1;

	FD_ZERO(&rfds);
	FD_SET(sock, &rfds);

	retval = select(sock+1, &rfds, NULL, NULL, &tv);
	if (retval == -1)
               perror("select()");
	else if (retval){
		readSize = recv(sock, port, sizeof(short),0);
	}
	else
		printf("No data within five seconds.\n");

	pthread_mutex_lock(&(threadData.datasFile.mutex));
	//write adresse et port to datasFile
		char str[8];
		sprintf(str, "%d", *port);
		write(threadData.datasFile.fd, threadData.ip, sizeof(char)*16);
		write(threadData.datasFile.fd, " ", sizeof(char));
		write(threadData.datasFile.fd, str, 4*sizeof(char));
		write(threadData.datasFile.fd, "\n", sizeof(char));
	pthread_mutex_unlock(&(threadData.datasFile.mutex));

	while(1){
		retval = select(sock+1, &rfds, NULL, NULL, &tv);
		if (retval == -1)
		       perror("select()");
		else if (retval){
			tv.tv_usec = 0;
			tv.tv_sec = 5;
			readSize = recv(sock, &pingValue, sizeof(char),0);
			if (readSize <=0 || readSize == LIGNE_MAX)
				erreur_IO("lireLigne");
			else if (readSize==0)
				continue;
			else{
				//Traitement des données recues si necessaire mais ici on ne recoit que des pings normalement
			}
		}
		else{
			printf("No data within five seconds : Tiemout.\n");
			cohorteServer[j].isFree=1;
{'fraction_non_alphanumeric': 0.1565195957604141, 'fraction_numerical': 0.010845452304658615, 'mean_word_length': 4.164191769198133, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 21, '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': '2293961', 'n_tokens_mistral': 5244, 'n_tokens_neox': 4641, 'n_words': 971}