Example #1
0
static void app(const char *address, const char *name)
{
   SOCKET sock = init_connection(address);
   char buffer[BUF_SIZE];

   fd_set rdfs;

   /* send our name */
   write_server(sock, name);

   while(1)
   {
      FD_ZERO(&rdfs);

      /* add STDIN_FILENO */
      FD_SET(STDIN_FILENO, &rdfs);

      /* add the socket */
      FD_SET(sock, &rdfs);

      if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1)
      {
         perror("select()");
         exit(errno);
      }

      /* something from standard input : i.e keyboard */
      if(FD_ISSET(STDIN_FILENO, &rdfs))
      {
         fgets(buffer, BUF_SIZE - 1, stdin);
         {
            char *p = NULL;
            p = strstr(buffer, "\n");
            if(p != NULL)
            {
               *p = 0;
            }
            else
            {
               /* fclean */
               buffer[BUF_SIZE - 1] = 0;
            }
         }
         write_server(sock, buffer);
      }
      else if(FD_ISSET(sock, &rdfs))
      {
         int n = read_server(sock, buffer);
         /* server down */
         if(n == 0)
         {
            printf("Server disconnected !\n");
            break;
         }
         puts(buffer);
      }
   }

   end_connection(sock);
}
Example #2
0
static void app(const char *address, const char *name)
{
    SOCKET sock = init_connection(address);
    char buffer[BUF_SIZE];

    fd_set rdfs;

    /* le client envoie son nom */
    write_server(sock, name);

    while(1)
    {
	FD_ZERO(&rdfs);

	/* ajouter un STDIN_FILENO */
	FD_SET(STDIN_FILENO, &rdfs);

	/* ajouter le socket */
	FD_SET(sock, &rdfs);

	if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1)
	{
	    perror("select()");
	    exit(errno);
	}

	/* quelque chose qui vient du clavier */
	if(FD_ISSET(STDIN_FILENO, &rdfs))
	{
	    fgets(buffer, BUF_SIZE - 1, stdin);
	    {
		char *p = NULL;
		p = strstr(buffer, "\n");
		if(p != NULL)
		{
		    *p = 0;
		}
		else
		{
		    /* fclean */
		    buffer[BUF_SIZE - 1] = 0;
		}
	    }
	    write_server(sock, buffer);
	}
	else if(FD_ISSET(sock, &rdfs))
	{
	    int n = read_server(sock, buffer);
	    /* Si le serveur est down */
	    if(n == 0)
	    {
		/* On arrête */
		break;
	    }
	    puts(buffer);
	}
    }

    end_connection(sock);
}
Example #3
0
void	app(const char *address, const char *name)
{
  int sock;
  char buffer[BUF_SIZE];
  fd_set rdfs;

  strcpy(buffer, "hello");

  sock = init_connection(address);
  write_server(sock, name);
  while (1)
  {
    init_socket(sock, &rdfs);
    if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1)
    {
      perror("select()");
      exit(errno);
    }
    if(FD_ISSET(STDIN_FILENO, &rdfs))
    {
      if (read_keyboard(buffer) < 0)
	continue;
      transmit_cmd(buffer,sock);
    }
    else if(FD_ISSET(sock, &rdfs))
    {
      int n = read_server(sock, &buffer);
	printf("receive : %s|\n", buffer);
      if (fgets(buffer, BUF_SIZE - 1, stdin) == NULL)
	{
	  perror("fgets()");
	  continue;
	}
      printf("receive:%s\n",buffer);
      /* server down */
      if(n == 0)
      {
	printf("Server disconnected !\n");
	break;
      }
      puts(buffer);
    }
  }

  end_connection(sock);
}
Example #4
0
void read_message(int sock)
{
	int rcv;
	while(1)
	{

		rcv=receive_message(sock);
		if (rcv==1)
			break;
			
		fprintf(stdin," ");
		
	}	
	end_connection(sock);
	printf("[ok] End connection Server\n");

}
Example #5
0
int	main(void)
{
  t_listc	*listc;
  t_listh	*listh;
  t_listm	*listm;
  int		sock;

  sock = init_connection();
  listc = init_listc();
  listh = init_listh();
  listm = init_listm();

  app(listc, listh, listm, sock);

  remove_all_client(listc);
  end_connection(sock);

  return EXIT_SUCCESS;
}
Example #6
0
void write_message(int sock)
{
	char message[4096];
	while(1)							// ENVOIE MESSAGE FG
	{
		printf("Entrer message: ");
		
		fgets(message,4095,stdin);
		message[strlen(message)-1]='\0';
		
		send_message(sock, message);

		if(strcmp(message, "quit")==0)
			break;

	}	

	end_connection(sock);
	printf("[ok] End Client\n");
}
Example #7
0
static void app(void)
{
    SOCKET sock = init_connection();
    char buffer[BUF_SIZE];
    int max = sock;
    /* tableau des clients */
    Client clients[MAX_CLIENTS];

    fd_set rdfs;

    /* Topologie */
    struct graph * graph = initialiser_Graph();

    while(1)
    {
	int i = 0;
	FD_ZERO(&rdfs);

	/* ajouter STDIN_FILENO */
	FD_SET(STDIN_FILENO, &rdfs);

	/* ajouter le socket de connexion */
	FD_SET(sock, &rdfs);

	/* ajouter un socket pour chaque client */
	for(i = 0; i < actual; i++)
	{
	    FD_SET(clients[i].sock, &rdfs);
	}

	if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
	{
	    perror("select()");
	    exit(errno);
	}

	/* quelque chose qui rentre depuis le clavier */
	if(FD_ISSET(STDIN_FILENO, &rdfs))
	{
	    /* char message[BUF_SIZE]; */
	    fgets(buffer, BUF_SIZE - 1, stdin);
	    {
		char *p = NULL;
		p = strstr(buffer, "\n");
		if(p != NULL)
		{
		    *p = 0;
		}
		else
		{
		    /* fclean */
		    buffer[BUF_SIZE - 1] = 0;
		}
	    }
	    /* ===Pour écrire à tous les clients===
	       message[0] = 0;
	       strncpy(message, "serveur : ", 0);
	       strncat(message, buffer, 10);
	       for(i = 0; i < actual; i++)
	       {
	       write_client(clients[i].sock, message);
	       } */

	    /* On analyse ce que l'utilisateur tape et on agit en conséquence */
	    if(analyse_cmd(graph, buffer) == 0)
		break; /* L'utilisateur veut quitter */
	}
	else if(FD_ISSET(sock, &rdfs))
	{
	    /* nouveau client */
	    SOCKADDR_IN csin = { 0 };
	    socklen_t sinsize = sizeof(csin);
	    int csock = accept(sock, (SOCKADDR *)&csin, &sinsize);
	    if(csock == SOCKET_ERROR)
	    {
		perror("accept()");
		continue;
	    }

	    /* après s'être connecté le client envoie son nom */
	    if(read_client(csock, buffer) == -1)
	    {
		/* déconnecté */
		continue;
	    }

	    /* quel est le nouveau fd maximum ? */
	    max = csock > max ? csock : max;

	    FD_SET(csock, &rdfs); /* On ajoute le socket à la liste d'écoute */

	    int nbTokens;
	    char **tokens = malloc(MAX_NB_TOKENS*sizeof(char*));
	    nbTokens = strsplit(buffer, tokens, MAX_LEN_LINE, " ");
	    printf("ENVOYE : %s, NB TOKENS : %d\n", buffer, nbTokens);
	    if (nbTokens == 6 && strcmp(tokens[0], "log") == 0 && strcmp(tokens[1], "in") == 0 && strcmp(tokens[2], "as") == 0 && strcmp(tokens[4], "port") == 0)
	    {
		printf("nbTokens == 6\n");
		int port;
		tokens[5][strlen(tokens[5])-1] = '\0';
		port = secure_atoi(tokens[5]);
		/* On cherche si un client utilise déjà le nom demandé
		 * si le nom existe
		 * et si le port est bon*/
		if (port <= 65535 && port >= 0) {
		    printf("port OK\n");
		    int indice_noeud, indice_noeud_gen;
		    Client c;
		    c.sock = csock;
		    c.port_number = port;
		    c.address = inet_ntoa(csin.sin_addr);
		    indice_noeud = exist_Noeud(graph, tokens[3]);
		    indice_noeud_gen = obtenir_Indice_Noeud_Non_Actif(graph);
		    if (indice_noeud < actual  && indice_noeud >= 0 && est_actif_noeud(graph, indice_noeud)== 0) {
			printf("indice noeud OK, %s\n", tokens[3]);
			strcpy(c.name, tokens[3]);
			activer_Noeud(graph, indice_noeud);
			clients[actual] = c;
			actual = actual + 1;
			char bufG[BUF_SIZE] = "";
			sprintf(bufG, "greeting %s*\n", c.name);
			write_client(c.sock, bufG, clients);
		    }
		    else if(indice_noeud_gen != -1) {
			printf("indice noeud gen OK\n");
			strcpy(c.name, obtenir_Nom_Noeud(graph, indice_noeud_gen));
			activer_Noeud(graph, indice_noeud_gen);
			clients[actual] = c;
			actual = actual + 1;
			char bufG[BUF_SIZE] = "";
			sprintf(bufG, "greeting %s*\n", c.name);
			printf("bufG : %s\n", bufG);
			write_client(c.sock, bufG, clients);
			printf("écrit\n");
		    }
		    else {
			printf("Tous les noeuds sont déjà occupés !\n");
		    }

		}
		else
		    printf("Erreur de port : %s\n", tokens[5]);
	    }
	    else if (nbTokens == 4 && strcmp(tokens[0], "log") == 0 && strcmp(tokens[1], "in") == 0 && strcmp(tokens[2], "port") == 0)
	    {
		int port;
		tokens[5][strlen(tokens[5])-1] = '\0';
		port = secure_atoi(tokens[5]);
		/* On cherche si un client utilise déjà le nom demandé
		 * si le nom existe
		 * et si le port est bon*/
		if (port <= 65535 && port >= 0) {
		    int indice_noeud_gen;
		    Client c;
		    c.sock = csock;
		    c.port_number = port;
		    c.address = inet_ntoa(csin.sin_addr);
		    indice_noeud_gen = obtenir_Indice_Noeud_Non_Actif(graph);
		    if(indice_noeud_gen != -1) {
			strcpy(c.name, obtenir_Nom_Noeud(graph, indice_noeud_gen));
			activer_Noeud(graph, indice_noeud_gen);
			clients[actual] = c;
			actual = actual + 1;
			char bufG[BUF_SIZE] = "";
			sprintf(bufG, "greeting %s*\n", c.name);
			/* printf("bufG : %s\n", bufG); */
			write_client(c.sock, bufG, clients);
			/* printf("écrit\n"); */
		    }
		    else {
			printf("Tous les noeuds sont déjà occupés !\n");
		    }
		}
		else
		    printf("Erreur de port : %s\n", tokens[5]);
	    }
	   
	    /*
	    for(i=0; i < nbTokens; i++) {
		free(tokens[i]);
	    }
	    free(tokens);
	    */
	}
	else
	{
	    int i;
	    for(i = 0; i < actual; i++)
	    {
		/* un client parle */
		if(FD_ISSET(clients[i].sock, &rdfs))
		{
		    Client client = clients[i];
		    int c = read_client(clients[i].sock, buffer);
		    /* client déconnecté */
		    if(c == 0)
		    {
			closesocket(clients[i].sock);
			remove_client(clients, i, &actual);
			strncpy(buffer, client.name, BUF_SIZE - 1);
			strncat(buffer, " déconnecté !", BUF_SIZE - strlen(buffer) - 1);
			send_message_to_all_clients(clients, client, actual, buffer, 1);
		    }
		    else
		    {
			/* On analyse la requête du client stockée dans buffer et on lui répond */
			printf("router_poll : %s\n", buffer);
			if(router_poll(graph, &clients[i], buffer, clients) == 0) {
			    /* Si le routeur veut être déconnecté */
			    closesocket(clients[i].sock);
			    remove_client(clients, i, &actual);
			}
			/* ====Pour envoyer un message à tous les routeurs====
			   send_message_to_all_clients(clients, client, actual, buffer, 0); */
		    }
		    break;
		}
	    }
	}
    }

    /* libération des données */
    free_graph(graph);
    clear_clients(clients, actual);
    end_connection(sock);
}
Example #8
0
int	main(int argc , char ** argv)
{
	if(argc < 2)
	{
		printf("Usage : %s [address] [pseudo]\n", argv[0]);
		return 1;
	}

	char buffer[BUF_SIZE_SOCKET];

	fd_set rdfs;
	//SOCKET define sur intd
	SOCKET sock = init_connection(argv[1]);

	/* send our name*/
	write_server(sock, argv[2]);


	while (1)
	{
		FD_ZERO(&rdfs); // Vide l'ensemble
		/*add STDION_FILENO */ //descripteur 0
		FD_SET(STDIN_FILENO, &rdfs);
		/* add socket*/
		FD_SET(sock, &rdfs);

		if (select(sock + 1, &rdfs, NULL, NULL, NULL) == -1)
		{
			perror("error select");
			exit(errno);
		}
		if (FD_ISSET(STDIN_FILENO, &rdfs))
		{
			fgets(buffer, BUF_SIZE_SOCKET - 1, stdin);
			{
				char *p = NULL;
				p = strstr(buffer, "\n");
				if (p != NULL)
				{
					*p = 0;
				}
				else
				{
					/* fclean */
					buffer[BUF_SIZE_SOCKET -1] = 0;
				}
			}
			write_server(sock, buffer);
		}
		else if(FD_ISSET(sock, &rdfs))
		{
			int n = read_server(sock, buffer);

			/*server down*/
			if (n == 0)
			{
				printf("Server disconnected !\n");
				break;
			}
			puts(buffer);
			write_server(sock, "ok");
		}
	}
	end_connection(sock);
	return (0);
}
Example #9
0
/**
* Methode principale
**/
static void app(void)
{

   /* Initialisation du socket */
   SOCKET sock = init_connection();
   char buffer[BUF_SIZE];
   char message[BUF_SIZE];
   /* Index de l'array */
   int actual = 0;
   int max = sock;
   int sizeread;
   /* Le tableau qui va stocker les clients */
   Client clients[MAX_CLIENTS];

   /* Lot de descripteurs */
   fd_set rdfs;

   char *commande;
   char *nomFichier;
   char *contenuFichier;

   while(1)
   {
      int i = 0;
      /* Remise à zéro des descripteurs */
      FD_ZERO(&rdfs);

      /* ajout STDIN_FILENO (console)*/
      FD_SET(STDIN_FILENO, &rdfs);

      /* ajout du socket comme descripteur */
      FD_SET(sock, &rdfs);

      /* Ajout d'un socket pour chaque clients */
      for(i = 0; i < actual; i++)
      {
         FD_SET(clients[i].sock, &rdfs);
      }

      /* Son sélectionne tous les descripteurs prêts */
      if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
      {
         perror("select()");
         exit(errno);
      }

      /* SI c'est une entrée clavier */
      if(FD_ISSET(STDIN_FILENO, &rdfs))
      {
         /* Si on a tapé quelque chose au clavier on arrête l'application pour permettre à l'utilisateur d'écrire*/
         break;
      } /* Sinon si c'est une entrée socket  ( nouveau )*/
      else if(FD_ISSET(sock, &rdfs))
      {
         /* Nouveau client */
         SOCKADDR_IN csin = { 0 };
         size_t sinsize = sizeof csin;
         int csock = accept(sock, (SOCKADDR *)&csin, &sinsize);

         if(csock == SOCKET_ERROR)
         {
            perror("accept()");
            continue;
         }

         /* Après s'être connecté le client envoi son pseudo */
        if(read_client(csock, buffer) == -1)
         {
            /* déconnecté */
            continue;
         }

         /* Quel est le nouveau numéro de descripteur max (pour le select) ? */
         max = csock > max ? csock : max;

         /* On ajoute le client à la liste des descripteurs */
         FD_SET(csock, &rdfs);

         /* On ajoute le client à la liste des clients avec son numéro de socket */
         Client c = { csock };
         strncpy(c.name, buffer, BUF_SIZE - 1);
         clients[actual] = c;
         actual++;
      }
      else /* Sinon on vérifie si un client a parlé */
      {
         int i = 0;
         for(i = 0; i < actual; i++) /* Pour chaque client */
         {
            /* On vérifie que le socket du client contient quelque chose */
            if(FD_ISSET(clients[i].sock, &rdfs))
            {
               /* Si c'est le cas on traite le message */
               Client client = clients[i];
               /* On lie le message envoyé par le client */
               int c = read_client(clients[i].sock, buffer);
               /* Client deconnecté */
               if(c == 0)
               {
                  /* Dans ce cas on ferme le socket */
                  closesocket(clients[i].sock);
                  remove_client(clients, i, &actual);
                  strncpy(buffer, client.name, BUF_SIZE - 1);
                  strncat(buffer, " disconnected !", BUF_SIZE - strlen(buffer) - 1);
                  send_message_to_all_clients(clients, client, actual, buffer, 1); /* On notifie tout les clients que ce dernier s'est déconnecté */
               }
               else
               {
                   strcpy(message, buffer);
                   commande = strtok(buffer, " ");
                    if( commande != NULL ) {
                       if(!strncasecmp(commande,"/getfile", strlen(commande))){
                            printf("%s\n", "Début envoi fichier");
                            nomFichier = strtok(NULL, " ");
                            FILE* fp = fopen(nomFichier, "r");
                            sizeread = fread(buffer,sizeof(char),1000,fp);
                            buffer[sizeread] = 0;
                            write(clients[i].sock,buffer,strlen(buffer));
                            printf("%s\n", "Fin envoi fichier");
                       } else if(!strncasecmp(commande,"/sendfile", strlen(commande))){
                            printf("%s\n", "Début réception fichier");
                            nomFichier = strtok(NULL, " ");
                            FILE* fp = fopen(nomFichier, "w");
                            contenuFichier = strtok(NULL, "\0");
                            fprintf(fp,"%s\n",contenuFichier);
                            fclose(fp);
                            printf("%s\n", "Fin réception fichier");
                        } else {

                          /* Sinon on se chargfe juste de relayer le message */
                          send_message_to_all_clients(clients, client, actual, message, 0);
                        }
                     }

               }
               break;
            }
         }
      }
   }

    /* On supprime les clients */
   clear_clients(clients, actual);

   /* On ferme les connexions */
   end_connection(sock);
}
Example #10
0
void server(void)
{
   SOCKET sock = init_connection();
   char buffer[BUF_SIZE];
   int max = sock;
   fd_set rdfs;
   while(1)
   {
      FD_ZERO(&rdfs);
      FD_SET(STDIN_FILENO, &rdfs);
      FD_SET(sock, &rdfs);
      if(connected)
      {
         FD_SET(client.sock, &rdfs);
      }
      if(select(max + 1, &rdfs,NULL, NULL, NULL) == -1)
      {
         perror("select()");
         exit(errno);
      }
    if(FD_ISSET(STDIN_FILENO, &rdfs))
      {
         fgets(buffer, BUF_SIZE - 1, stdin);
         {
            char *p = NULL;
            p = strstr(buffer, "\n");
            if(p != NULL)
           {
               *p = 0;
            }
            else
            {
               buffer[BUF_SIZE - 1] = 0;
            }
         }
         if(connected && send(client.sock, buffer, strlen(buffer), 0) < 0)
	{
	  perror("send()");
	  exit(errno);
	  }
      }
      else if(FD_ISSET(sock, &rdfs))
      {
         SOCKADDR_IN csin = { 0 };
         socklen_t sinsize = sizeof csin;
         int csock = accept(sock, (SOCKADDR *)&csin, &sinsize);
         if(csock == SOCKET_ERROR)
         {
            perror("accept()");
            continue;
         }
         if(read_client(csock, buffer) == -1)
         {
              continue;
         }
         max = csock > max ? csock : max;
         FD_SET(csock, &rdfs);
         Client c = { csock };
         strncpy(c.name, buffer, BUF_SIZE - 1);
	 client=c;
         connected=1;
	}
      else
      {
         if(connected)
         {
           if(FD_ISSET(client.sock, &rdfs))
            {
               int c = read_client(client.sock, buffer);
               if(c == 0)
               {
                  closesocket(client.sock);
                  remove_client(&client);
		  puts("Client déconnecté");
               }
               else
               {
		  printf("%s : ",client.name);
                  puts(buffer);
               }
            }
         }
      }
   }

   clear_client(&client);
   end_connection(sock);
}
Example #11
0
static void app(void)
{
    SOCKET sock = init_connection();
    int actual = 0;
    int max = sock;
    Client clients[MAX_CLIENTS];
    srand(time(NULL));
    /* Initialiser la plateau de jeu de la vie
     * et son plateau d'etats
     */

    plateau* p_vie = jv_newPlat(GAME_SIZE, GAME_SIZE);
    jv_randPlat(p_vie);
    plateau* p_vie_next = jv_newPlat(GAME_SIZE, GAME_SIZE);
    PlateauStatut* p_statuts = jvs_newPlat(GAME_SIZE, GAME_SIZE);
    int fintache = 0;

    aff_init();

    fd_set rdfs;
    while(1)
    {
	int i = 0;
	FD_ZERO(&rdfs);
	FD_SET(STDIN_FILENO, &rdfs);
	FD_SET(sock, &rdfs);
	for(i=0; i<actual; i++)
	{
	    FD_SET(clients[i].sock, &rdfs);
	}

	if(select(max+1, &rdfs, NULL, NULL, NULL) == -1)
	{
	    perror("select()");
	    exit(errno);
	}

	if(FD_ISSET(STDIN_FILENO, &rdfs))
	{
	    /* stop when type on keyboard */
	    break;
	}
	else if(FD_ISSET(sock, &rdfs))
	{
	    /* new client */
	    SOCKADDR_IN csin = { 0 };
	    size_t sinsize = sizeof csin;
	    int csock = accept(sock, (SOCKADDR *)&csin, &sinsize);
	    if(csock == SOCKET_ERROR)
	    {
		perror("accept()");
		continue;
	    }

	    /* Lire la commande startCom */
	    Command cmd;
	    if(readCmd(csock, &cmd) == -1)
	    {
		continue;
	    }
	    if(cmd.type != CMD_START_COMMUNICATION)
	    {
		continue;
	    }
	    
	    if(writeCmd(csock, &cmd) > 0)
	    {
		max = csock > max ? csock : max;

		FD_SET(csock, &rdfs);
		Client c;
		c.sock = csock;
		c.type = 0;
		c.generation = -1;
		c.x = -1;
		c.y = -1;
		c.width = -1;
		c.height = -1;
		clients[actual] = c;
		actual ++;
	    }
	}
	else
	{
	    int i = 0;
	    for(i=0; i<actual; i++)
	    {
		/* Commande d'un client */
		if(FD_ISSET(clients[i].sock, &rdfs))
		{
		    Client client = clients[i];
		    Command cmd;
		    int c = readCmd(clients[i].sock, &cmd);
		    if(c==0)
		    {
			close(clients[i].sock);
			remove_client(clients, i, &actual);
		    }
		    else
		    {
			switch(cmd.type)
			{
			    Command com;
			    /* CLient demande une tache */
			    case CMD_REQUEST_TASK:
			    {	
			    	/* TODO verifier si le process n'est pas déja sur un block non traité ou en traitement */
			    	
				/* sélection aleatoire d'une commande */
				int ctp;
				if (!fintache)
				    ctp = rand() % 100;
				else
				    ctp = 1;

				/* Retirer une tache du sac */
				if (ctp < 80)
				{
				    Block b = jvs_getBlock(p_statuts, BLOCK_SIZE, BLOCK_SIZE);
				    if (b.x == -1)
				    {
					/* erreur, aucun block libre */
					Command att;
					att.type = CMD_NO_TASK;
					att.noTask.waitingTime = 0.5;
					writeCmd(clients[i].sock, &att);
					break;
				    }

				    /* verifier le type de bloc */
				    if (b.t == NORMAL)
				    {
					/* Construction et envoi de la commande */
					char* pack = jv_pack_s(p_vie, b.x, b.y, b.width+2, b.height+2);
					com.type = CMD_TASK;
					com.task.width = b.width+2;
					com.task.height = b.height+2;
					com.task.cells = pack;
					if (writeCmd(clients[i].sock, &com)>0)
					{
					    /* Assigne le block au client */
					    if (jvs_assigne(p_statuts, b.x, b.y, b.width, b.height, clients[i].sock) == -1)
						printf("erreur");
					    clients[i].generation = p_statuts->generation;
					    clients[i].x = b.x; clients[i].y = b.y; clients[i].width = b.width; clients[i].height = b.height;		    	 
					    clients[i].type = CMD_TASK;
					}
					else
					{
					    /* enlever le client */
					    close(clients[i].sock);
					    remove_client(clients, i, &actual);
					}
					free(pack);
				    }
				    else
				    {
					fintache = 1;
					/* construire la commande */
					com.type = CMD_LIST_CELL;
					if (b.t == HEAL) com.listCell.type = CMD_HEAL;
					else com.listCell.type = CMD_VIRUS;
					com.listCell.nb =1;
					com.listCell.list = (coord*)malloc(sizeof(coord));
					com.listCell.list[0].cell = p_vie->grille[b.x][b.y];
					com.listCell.list[0].x = b.x; com.listCell.list[0].y = b.y;
					/* envoyer la cellule */
					if (writeCmd(clients[i].sock, &com)>0)
					{
					    /* Assigner le traitement heal */
					    jvs_assigne_vh(p_statuts, b.x, b.y, clients[i].sock);
					    clients[i].generation = p_statuts->generation;
					    clients[i].x = b.x; clients[i].y = b.y; clients[i].width = b.width; clients[i].height = b.height;
					    if (b.t == HEAL) clients[i].type = CMD_HEAL;
					    else clients[i].type = CMD_VIRUS;
					}
					else
					{
					    /* Enelever le client */
					    close(clients[i].sock);
					    remove_client(clients, i, &actual);
					}
					free(com.listCell.list);
				    }
				}
				/* Générer vie */
				else if (ctp<98)
				{
				    /* Construction commande */
				    com.type = CMD_HEAL;
				    com.heal.width = p_vie->width;
				    com.heal.height = p_vie->height;
				    /* Envoi de la commande */
				    if (writeCmd(clients[i].sock, &com)<=0)
				    {
					/* Enlever le client */
					close(clients[i].sock);
					remove_client(clients, i, &actual);
				    }
				}
				/* Infection virale */
				else
				{
				    /* Construction commande */
				    com.type = CMD_VIRUS;
				    com.virus.width = p_vie->width;
				    com.virus.height = p_vie->height;
				    /* Envoi de la commande */
				    if (writeCmd(clients[i].sock, &com) <=0)
				    {
					/* Enlever client */
					close(clients[i].sock);
					remove_client(clients, i, &actual);
				    }
				}
				break;
			    }

			    /* Client envoi resultat generation */
			    case CMD_TASK:
			    if (p_statuts->generation == clients[i].generation)
			    {
				jv_unpack_s(p_vie_next, cmd.task.cells, clients[i].x, clients[i].y, cmd.task.width, cmd.task.height);
				jvs_termine(p_statuts, clients[i].x, clients[i].y, clients[i].width, clients[i].height);
				//jvs_termine2(p_statuts, clients[i].sock);
			    }	
				break;

			    /* autres types commandes */
			    case CMD_LIST_CELL:
			    {
				type t = VIRUS;
				switch (cmd.listCell.type)
				{
				case CMD_HEAL:
				    t=HEAL;
				case CMD_VIRUS:
				{
				    int k; 
				    for (k = 0; k <cmd.listCell.nb; k++)
				    {
					jvs_set_vh(p_statuts, t, cmd.listCell.list[k].x, cmd.listCell.list[k].y);
				    }
				    break;
				}
				case CMD_TASK:
				{
				    /* Récupérer l'etat de la cellule */
				    int k;
				    for (k=0; k<cmd.listCell.nb; k++)
				    {
					p_vie_next->grille[cmd.listCell.list[k].x][cmd.listCell.list[k].y] = cmd.listCell.list[k].cell;
					p_statuts->grille[cmd.listCell.list[k].x][cmd.listCell.list[k].y].statut = TRAITEE; 
				    }
				    break;
				}
				default:
				    break;
				}
				free(cmd.listCell.list);
			    }
			    default:
				break;
			    
			}
		    }
		    break;
		}
	    }
	}
	
	aff_plateau_s(p_vie, p_statuts);

	/* verifier changement generations */
	if (jvs_nextGen(p_statuts) != -1)
	{
	    p_vie = p_vie_next;
	    fintache = 0;
	}

    }
    clear_clients(clients, actual);
    end_connection(sock);
    aff_end();
}
Example #12
0
static void cleanup(int ret)
{
	size_t i;

	while(receivers) {
		struct receiver *rcvr = receivers;
		/* clear our command queue */
		while(rcvr->queue) {
			struct cmdqueue *ptr = rcvr->queue;
			free(ptr->cmd);
			rcvr->queue = ptr->next;
			free(ptr);
		}
		/* reset/close our receiver device */
		if(rcvr->fd > -1) {
			xclose(rcvr->fd);
		}
		receivers = receivers->next;
		free(rcvr);
	}

	/* close the log file descriptor */
	if(logfd > -1) {
		xclose(logfd);
		logfd = -1;
	}

	/* loop through listener descriptors and close them */
	for(i = 0; i < listener_count; i++) {
		if(listeners[i] > -1) {
			struct sockaddr saddr;
			socklen_t sl = (socklen_t)sizeof(struct sockaddr);
			if(getsockname(listeners[i], &saddr, &sl)) {
				perror("getsockname()");
			} else {
				/* for unix sockets, we want to unlink the path */
				if(saddr.sa_family == AF_UNIX) {
					unlink(((struct sockaddr_un *)&saddr)->sun_path);
				}
			}
			xclose(listeners[i]);
		}
	}
	free(listeners);
	listeners = NULL;

	/* loop through connection descriptors and close them */
	while(connections) {
		struct conn *ptr = connections;
		end_connection(connections, 1);
		connections = connections->next;
		free(ptr);
	}

	/* close our signal listener */
	if(signalpipe[WRITE] > -1) {
		xclose(signalpipe[WRITE]);
		signalpipe[WRITE] = -1;
	}
	if(signalpipe[READ] > -1) {
		xclose(signalpipe[READ]);
		signalpipe[READ] = -1;
	}

	exit(ret);
}