<commit_before>#include "server_header.h"

// Routine principale del server
int server_routine(int argc, char const *argv[]) {

  int server_desc ,
      client_desc,
      client_addr_len,
      ret;

  struct sockaddr_in  server_addr ,
                      client_addr;

  // Configurazione del Server
  if (atexit(server_exit) != 0) exit(EXIT_FAILURE);
  if (signal(SIGINT, exit) == SIG_ERR) exit(EXIT_FAILURE);

  ret = server_init(&server_desc, &server_addr);
  if (ret < 1) exit(EXIT_FAILURE);
  fprintf(stderr, "Server Started\n");

  client_addr_len = sizeof(client_addr);

  // Metto il server in attesa di richieste di connessione
  while(1) {
    memset(&client_addr, 0, sizeof(client_addr));
    client_desc = accept(server_desc,
                         (struct sockaddr *)&client_addr,
                         (socklen_t*)&client_addr_len);
    if (client_desc < 0) continue;

    // Se arriva un client rimando la gestione all'apposito thread
    client_l thread_arg = malloc(sizeof(client_t));
    thread_arg->client_desc = client_desc;
    pthread_t* init_client_thread = malloc(sizeof(pthread_t));
    ret = pthread_create(init_client_thread,
                         NULL,
                         client_routine,
                         (void*) thread_arg);
    if (ret != 0) continue;
    ret = pthread_detach(*init_client_thread);
  }
  exit(EXIT_SUCCESS);
}

/* Routine di gestione dei client */
void*	client_routine(void *arg) {

  client_l  client = (client_l) arg;

  int       ret;
  int       bytes_read;
  int       bytes_send;
  int       client_desc = client->client_desc;
  char*     client_name = client->client_name;
  char      data[MAX_DATA_LEN];

  memset(data, 0, MAX_LEN_NAME);

  bytes_read = 0;

  // Recupero il nome da attribuire al client
  while (bytes_read < MAX_LEN_NAME) {
    ret = recv(client_desc, data + bytes_read, 1, 0);
    if (ret == -1 && errno == EINTR) continue;
    if (ret == -1) pthread_exit(NULL);
    if (ret == 0) pthread_exit(NULL);
    bytes_read++;
    if (data[bytes_read-1] == '\n' ||
	      data[bytes_read-1] == '\r' ||
	      data[bytes_read-1] == '\0')
      break;
  }
  data[bytes_read-1] = '\0';

  memcpy(client_name, data, bytes_read);

  bytes_send = 0;

  // Verifico se esiste gà un client con tale nome
  if (valid_name(client_name) <= 0) {
    int query_size = strlen(NAME_ALREADY_USED);
    char* query = malloc(sizeof(char)*query_size);
    fprintf(stderr, "Connection error: NAME_ALREADY_USED\n");
    memcpy(query, NAME_ALREADY_USED, query_size);
    while (bytes_send < query_size) {
      ret = send(client_desc, query + bytes_send, 1, 0);
      if (ret == -1 && errno == EINTR) continue;
      if (ret == -1) pthread_exit(NULL);
      bytes_send++;
    }
    pthread_exit(NULL);
  }

  add_cl(client);

  fprintf(stderr, "New client %s connected\n", client_name);

  bytes_read = 0;
  memset(data, 0, MAX_DATA_LEN);

  while (1) {
    while (1) {
      ret = recv(client_desc, data + bytes_read, 1, 0);
      if (ret == -1 && errno == EINTR) continue;
      if (ret == -1) pthread_exit(NULL);
      if (ret == 0) pthread_exit(NULL);
      bytes_read++;
      if (data[bytes_read-1] == '\n' ||
	        data[bytes_read-1] == '\r' ||
	        data[bytes_read-1] == '\0')
        break;
    }

    data[bytes_read-1] = '\n';

    if (strcmp(data, QUIT) == 0) {
      fprintf(stderr, "QUIT");
    }

    if (strcmp(data, LIST) == 0) {
      fprintf(stderr, "LIST");
    }
  }
  pthread_exit(NULL);
}

/* Routine di inizializzazione del server */
int server_init(int* sock_desc, struct sockaddr_in* sock_addr) {
  nclients = 0;

  if (sem_init(&client_list_semaphore, 0, 1)) return 0;

  *sock_desc = socket(AF_INET , SOCK_STREAM , 0);<commit_msg>	modified:   server/server_header.c<commit_after>#include "server_header.h"

// Routine principale del server
int server_routine(int argc, char const *argv[]) {

  int server_desc ,
      client_desc,
      client_addr_len,
      ret;

  struct sockaddr_in  server_addr ,
                      client_addr;

  // Configurazione del Server
  if (atexit(server_exit) != 0) exit(EXIT_FAILURE);
  if (signal(SIGINT, exit) == SIG_ERR) exit(EXIT_FAILURE);

  ret = server_init(&server_desc, &server_addr);
  if (ret < 1) exit(EXIT_FAILURE);
  fprintf(stderr, "Server Started\n");

  client_addr_len = sizeof(client_addr);

  // Metto il server in attesa di richieste di connessione
  while(1) {
    memset(&client_addr, 0, sizeof(client_addr));
    client_desc = accept(server_desc,
                         (struct sockaddr *)&client_addr,
                         (socklen_t*)&client_addr_len);
    if (client_desc < 0) continue;

    // Se arriva un client rimando la gestione all'apposito thread
    client_l thread_arg = malloc(sizeof(client_t));
    thread_arg->client_desc = client_desc;
    pthread_t* init_client_thread = malloc(sizeof(pthread_t));
    ret = pthread_create(init_client_thread,
                         NULL,
                         client_routine,
                         (void*) thread_arg);
    if (ret != 0) continue;
    ret = pthread_detach(*init_client_thread);
  }
  exit(EXIT_SUCCESS);
}

/* Routine di gestione dei client */
void*	client_routine(void *arg) {

  client_l  client = (client_l) arg;

  int       ret;
  int       bytes_read;
  int       bytes_send;
  int       client_desc = client->client_desc;
  char*     client_name = client->client_name;
  char      data[MAX_DATA_LEN];

  memset(data, 0, MAX_LEN_NAME);

  bytes_read = 0;

  // Recupero il nome da attribuire al client
  while (bytes_read < MAX_LEN_NAME) {
    ret = recv(client_desc, data + bytes_read, 1, 0);
    if (ret == -1 && errno == EINTR) continue;
    if (ret == -1) pthread_exit(NULL);
    if (ret == 0) pthread_exit(NULL);
    bytes_read++;
    if (data[bytes_read-1] == '\n' ||
	      data[bytes_read-1] == '\r' ||
	      data[bytes_read-1] == '\0')
      break;
  }
  data[bytes_read-1] = '\0';

  memcpy(client_name, data, bytes_read);

  bytes_send = 0;

  // Verifico se esiste gà un client con tale nome
  if (valid_name(client_name) <= 0) {
    int query_size = strlen(NAME_ALREADY_USED);
    char* query = malloc(sizeof(char)*query_size);
    fprintf(stderr, "Connection error: NAME_ALREADY_USED\n");
    memcpy(query, NAME_ALREADY_USED, query_size);
    while (bytes_send < query_size) {
      ret = send(client_desc, query + bytes_send, 1, 0);
      if (ret == -1 && errno == EINTR) continue;
      if (ret == -1) pthread_exit(NULL);
      bytes_send++;
    }
    pthread_exit(NULL);
  }

  add_cl(client);

  fprintf(stderr, "New client %s connected\n", client_name);

  bytes_read = 0;

  while (1) {

    memset(data, 0, MAX_DATA_LEN);

    while (1) {
      ret = recv(client_desc, data + bytes_read, 1, 0);
      if (ret == -1 && errno == EINTR) continue;
      if (ret == -1) pthread_exit(NULL);
      if (ret == 0) pthread_exit(NULL);
      bytes_read++;
      if (data[bytes_read-1] == '\n' ||
	        data[bytes_read-1] == '\r' ||
	        data[bytes_read-1] == '\0')
        break;
    }

    data[bytes_read-1] = '\n';

    if (strcmp(data, QUIT) == 0) {
      fprintf(stderr, "QUIT\n");
    }

    if (strcmp(data, LIST) == 0) {
      fprintf(stderr, "LIST\n");
    }
  }
  pthread_exit(NULL);
}

/* Routine di inizializzazione del server */
int server_init(int* sock_desc, struct sockaddr_in* sock_addr) {
  nclients = 0;

  if (sem_init(&client_list_semaphore, 0, 1)) return 0;

  *sock_desc = socket(AF_INET , SOCK_STREAM , 0);
{'fraction_non_alphanumeric': 0.13449746734204213, 'fraction_numerical': 0.012263396427619302, 'mean_word_length': 2.764676367285499, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '1269757', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2650, 'n_words': 609}