<commit_before>#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "main_routine.h"
#include "thread_routine.h"
#include "../libs/list.h"
#include "../libs/protocol.h"

#define DEBUG 1
#define BUFFER_LEN 1024

void *thread_routine(void* arg) {
  client_l  client = (client_l) arg;
  client_l  *speaker = &client->speaker;
  int       ret;
  int       query_size;
  int       *id = &client->id;
  int       *status = &client->status;
  int       descriptor = client->descriptor;
  char      data[BUFFER_LEN];
  char      *request_name;
  char      *name = client->name;
  char      *data_pointer;
  sem_t     *sem = &client->sem;

  memset(data, 0, BUFFER_LEN);

  // Ricevo il nome dal client
  ret = recv_message(descriptor, data, BUFFER_LEN, N_FLAG);
  if (ret == -1) pthread_exit(NULL);

  data[ret-1] = '\0';

  // Verifico se esiste gà un client con tale nome
  if (valid_name(data) == -1) {
    query_size = strlen(NAME_ALREADY_USED);
    memset(data, 0, BUFFER_LEN);
    memcpy(data, NAME_ALREADY_USED, query_size);
    ret = send_message(descriptor, data, query_size, N_FLAG);
    pthread_exit(NULL);
  }

  memset(name, 0, LIST_LEN_NAME);
  memcpy(name, data, LIST_LEN_NAME);

  // Aggiungo il client alla lista dei client connessi
  ret = add_cl(client);
  if (ret == -1) pthread_exit(NULL);

  if (DEBUG) fprintf(stderr, "New client %s connected\n", name);

  // Inizio del ciclo sentinella
  while(1) {

    memset(data, 0, BUFFER_LEN);

    ret = recv_message(descriptor, data, BUFFER_LEN, N_FLAG);
    if (ret == -1) {
      if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
      remove_cl(*id);
      if (DEBUG) fprintf(stderr, " OK\n");
      pthread_exit(NULL);
    }

    if (strncmp(data, QUIT, strlen(QUIT)) == 0 && *status == BUSY) {
      fprintf(stderr, "User %s close the chat session:", name);
      if (sem_wait(&(*speaker)->sem) == -1) {
        remove_cl((*speaker)->id);
        pthread_exit(NULL);
      }
      (*speaker)->speaker = NULL;
      (*speaker)->status = ONLINE;
      if (sem_post(&(*speaker)->sem) == -1) {
        remove_cl((*speaker)->id);
        pthread_exit(NULL);
      }
      ret = send_message((*speaker)->descriptor, data, BUFFER_LEN, N_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
        remove_cl((*speaker)->id);
        if (DEBUG) fprintf(stderr, " OK\n");
        pthread_exit(NULL);
      }
      if (sem_wait(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      *speaker = NULL;
      *status = ONLINE;
      if (sem_post(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      fprintf(stderr, " OK\n");
    }

    else if (*status == BUSY && strncmp(data, NO, strlen(NO)) == 0) {
      if (sem_wait(&(*speaker)->sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      (*speaker)->speaker = NULL;
      (*speaker)->status = ONLINE;
      if (sem_post(&(*speaker)->sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      ret = send_message((*speaker)->descriptor, data, strlen(NO), N_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", (*speaker)->name);
        remove_cl((*speaker)->id);
        if (DEBUG) fprintf(stderr, " OK\n");
        pthread_exit(NULL);
      }
      if (sem_wait(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      *speaker = NULL;
      *status = ONLINE;
      if (sem_post(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      fprintf(stderr, "NO\n");
    }

    else if (*status == BUSY) {
      fprintf(stderr, "YES\n");
      ret = send_message((*speaker)->descriptor, data, BUFFER_LEN, N_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", (*speaker)->name);
        remove_cl((*speaker)->id);
        if (DEBUG) fprintf(stderr, " OK\n");
      }
    }

    else if (*status == ONLINE && strncmp(data, QUIT, strlen(QUIT)) == 0) {
      if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
      remove_cl(*id);
      if (DEBUG) fprintf(stderr, " OK\n");
      pthread_exit(NULL);
    }

    else if (*status == ONLINE && strncmp(data, LIST, strlen(LIST)) == 0) {
      if (DEBUG) fprintf(stderr, "Client %s requests client list:", name);
      ret = get_list(&data_pointer);
      ret = send_message(descriptor, data_pointer, ret, Z_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
        remove_cl(*id);
        if (DEBUG) fprintf(stderr, " OK\n");
        pthread_exit(NULL);
      }
      if (DEBUG) {
        fprintf(stderr, " OK\n");
      }<commit_msg>Riorganizzazione del codice<commit_after>#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "main_routine.h"
#include "thread_routine.h"
#include "../libs/list.h"
#include "../libs/protocol.h"

#define DEBUG 1
#define BUFFER_LEN 1024

void *thread_routine(void* arg) {
  client_l  client = (client_l) arg;
  client_l  *speaker = &client->speaker;
  int       ret;
  int       query_size;
  int       *id = &client->id;
  int       *status = &client->status;
  int       descriptor = client->descriptor;
  char      data[BUFFER_LEN];
  char      *request_name;
  char      *name = client->name;
  char      *data_pointer;
  sem_t     *sem = &client->sem;

  memset(data, 0, BUFFER_LEN);

  // Ricevo il nome dal client
  ret = recv_message(descriptor, data, BUFFER_LEN, N_FLAG);
  if (ret == -1) pthread_exit(NULL);

  data[ret-1] = '\0';

  // Verifico se esiste gà un client con tale nome
  if (valid_name(data) == -1) {
    query_size = strlen(NAME_ALREADY_USED);
    memset(data, 0, BUFFER_LEN);
    memcpy(data, NAME_ALREADY_USED, query_size);
    ret = send_message(descriptor, data, query_size, N_FLAG);
    pthread_exit(NULL);
  }

  memset(name, 0, LIST_LEN_NAME);
  memcpy(name, data, LIST_LEN_NAME);

  // Aggiungo il client alla lista dei client connessi
  ret = add_cl(client);
  if (ret == -1) pthread_exit(NULL);

  if (DEBUG) fprintf(stderr, "New client %s connected\n", name);

  // Inizio del ciclo sentinella
  while(1) {

    memset(data, 0, BUFFER_LEN);

    ret = recv_message(descriptor, data, BUFFER_LEN, N_FLAG);
    if (ret == -1) {
      if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
      remove_cl(*id);
      if (DEBUG) fprintf(stderr, " OK\n");
      pthread_exit(NULL);
    }

    if (strncmp(data, QUIT, strlen(QUIT)) == 0 && *status == BUSY) {
      if (DEBUG) fprintf(stderr, "User %s close the chat session:", name);
      if (sem_wait(&(*speaker)->sem) == -1) {
        remove_cl((*speaker)->id);
        pthread_exit(NULL);
      }
      (*speaker)->speaker = NULL;
      (*speaker)->status = ONLINE;
      if (sem_post(&(*speaker)->sem) == -1) {
        remove_cl((*speaker)->id);
        pthread_exit(NULL);
      }
      ret = send_message((*speaker)->descriptor, data, BUFFER_LEN, N_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
        remove_cl((*speaker)->id);
        if (DEBUG) fprintf(stderr, " OK\n");
        pthread_exit(NULL);
      }
      if (sem_wait(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      *speaker = NULL;
      *status = ONLINE;
      if (sem_post(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      if (DEBUG) fprintf(stderr, " OK\n");
    }

    else if (*status == BUSY && strncmp(data, NO, strlen(NO)) == 0) {
      if (sem_wait(&(*speaker)->sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      (*speaker)->speaker = NULL;
      (*speaker)->status = ONLINE;
      if (sem_post(&(*speaker)->sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      ret = send_message((*speaker)->descriptor, data, strlen(NO), N_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", (*speaker)->name);
        remove_cl((*speaker)->id);
        if (DEBUG) fprintf(stderr, " OK\n");
        pthread_exit(NULL);
      }
      if (sem_wait(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      *speaker = NULL;
      *status = ONLINE;
      if (sem_post(sem) == -1) {
        remove_cl(*id);
        pthread_exit(NULL);
      }
      if (DEBUG) fprintf(stderr, " NO\n");
    }

    else if (*status == BUSY) {
      if (DEBUG) fprintf(stderr, " YES\n");
      ret = send_message((*speaker)->descriptor, data, BUFFER_LEN, N_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", (*speaker)->name);
        remove_cl((*speaker)->id);
        if (DEBUG) fprintf(stderr, " OK\n");
      }
    }

    else if (*status == ONLINE && strncmp(data, QUIT, strlen(QUIT)) == 0) {
      if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
      remove_cl(*id);
      if (DEBUG) fprintf(stderr, " OK\n");
      pthread_exit(NULL);
    }

    else if (*status == ONLINE && strncmp(data, LIST, strlen(LIST)) == 0) {
      if (DEBUG) fprintf(stderr, "Client %s requests client list:", name);
      ret = get_list(&data_pointer);
      ret = send_message(descriptor, data_pointer, ret, Z_FLAG);
      if (ret == -1) {
        if (DEBUG) fprintf(stderr, "Client %s disconnected:", name);
        remove_cl(*id);
        if (DEBUG) fprintf(stderr, " OK\n");
        pthread_exit(NULL);
      }
      if (DEBUG) fprintf(stderr, " OK\n");
{'fraction_non_alphanumeric': 0.17119838872104734, 'fraction_numerical': 0.006445115810674723, 'mean_word_length': 2.652445752114748, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 77, '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': '2937511', 'n_tokens_mistral': 3604, 'n_tokens_neox': 3521, 'n_words': 796}