diff --git a/dep/server.c b/dep/server.c index 3ae9979..4247e57 100644 --- a/dep/server.c +++ b/dep/server.c @@ -132,9 +132,9 @@ void MANAGE_REQUEST(char* pRequest, int* USER_SOCKET, int* FTP_SOCKET, int* DUSE char response[maxBuffLen]; // contiendra la réponse (2*taille buffer pour strcat(BUFLEN, BUFLEN)) - int nbSend; // contiendra le nombre de données envoyées - char rCommand[5]; // contiendra les commandes (1ère partie) - char rContent[maxBuffLen]; // contiendra le contenu associé à la commande (2ème partie) + int nbSend; // contiendra le nombre de données envoyées + char rCommand[5]; // contiendra les commandes (1ère partie) + char rContent[maxBuffLen]; // contiendra le contenu associé à la commande (2ème partie) // on vide les buffers memset(&rCommand, '\0', sizeof(rCommand)); diff --git a/proxy_ftp.c b/proxy_ftp.c index 66be055..8b0008c 100644 --- a/proxy_ftp.c +++ b/proxy_ftp.c @@ -40,15 +40,33 @@ int main(int argc, char* argv[]){ /* [2] Attente d'une demande de connection, puis création d'un THREAD ============================================================================*/ - int index = 0; - while( index <= maxListLen ){ - THREADABLE_SOCKET = -1; // on initialise la SOCKET en attendant la connexion + int index = -1; + while( 1 ){ + /* 1. On initialise la SOCKET en attendant la connexion et le rang du "manager" inactif */ + THREADABLE_SOCKET = -1; + index = -1; + /* 2. On attends une connection */ THREADABLE_SOCKET = accept(LISTENSOCK, (struct sockaddr *) &clientInfo, &len); - // on lance un thread pour le traitement - pthread_create(&managers[index], NULL, manageConnection, (void*)(intptr_t) THREADABLE_SOCKET); - index++; + /* 3. On cherche un "manager" libre */ + int i; + + // si on trouve un "manager" inactif, on l'enregistre dans index + for( i = 0 ; i < maxListLen ; i++ ) + if( activeManagers[i] == 0 ){ index = i; break; } + + // si on a trouvé un "manager" libre + if( index != -1 ){ + + /* 4. On lance un thread pour le traitement de ce client */ + pthread_create(&managers[index], NULL, manageConnection, (void*)(intptr_t) THREADABLE_SOCKET); + if( DEBUGMOD&THR ) printf("THREAD[%d] démarré\n", index); + + /* 5. On signale que ce "manager" est maintenant actif */ + activeManagers[index] = 1; + }else + if( DEBUGMOD&THR ) printf("Demande de thread refusée!\n"); } @@ -154,6 +172,17 @@ void* manageConnection(void* THREADABLE_SOCKET){ close(USER_SOCKET); close(FTP_SOCKET); - // on arrête le THREAD + /* [10] Arrêt du THREAD + ============================================================================*/ + /* 1. On récupère le rang dans les "managers" */ + int i, index = -1; + for( i = 0 ; i < maxListLen ; i++ ) + if( managers[i] == pthread_self() ){ index = i; break; } + + /* 2. On met à jour "activeManagers" */ + if( index != -1 ) + activeManagers[index] = 0; + + if( DEBUGMOD&THR ) printf("THREAD[%d] libéré\n", index); pthread_exit(NULL); } \ No newline at end of file diff --git a/proxy_ftp.h b/proxy_ftp.h index 6366e4b..57e5dc6 100644 --- a/proxy_ftp.h +++ b/proxy_ftp.h @@ -22,23 +22,25 @@ #define REVEALS 0x08 // debug EXPLICITATION des strings #define BUFFERS 0x10 // debug des BUFFERS #define HEADERS 0x20 // debug des HEADERS de fonctions +#define THREADS 0x40 // debug des THREADS -#define SCK 0x01 // FILTRE pour ONLY_SOCKET -#define CMD 0x02 // FILTRE pour ONLY_COMMAND -#define DAT 0x04 // FILTRE pour ONLY_DATA -#define RVL 0x08 // FILTRE pour ONLY_REVEALS -#define BUF 0x10 // FILTRE pour ONLY_BUFFERS -#define HDR 0x20 // FILTRE pour ONLY_HEADERS +#define SCK 0x01 // FILTRE pour SOCKET +#define CMD 0x02 // FILTRE pour COMMAND +#define DAT 0x04 // FILTRE pour DATA +#define RVL 0x08 // FILTRE pour REVEALS +#define BUF 0x10 // FILTRE pour BUFFERS +#define HDR 0x20 // FILTRE pour HEADERS +#define THR 0x40 // FILTRE pour THREADS // possibilité de cumuler les DEBUGMODES -#define DEBUGMOD COMMANDS // REVEALS + HEADER +#define DEBUGMOD 0 // REVEALS + HEADER + THREADS /* vars */ #define remoteHost "localhost" #define remotePort "80" #define maxBuffLen 4096 -#define maxListLen 2 +#define maxListLen 10 #define maxHostLen 64 #define maxPortLen 6 @@ -111,5 +113,5 @@ void* manageConnection(void* THREADABLE_SOCKET); // VARIABLES -static pthread_t managers[maxListLen]; // contiendra les THREADS -static int activeManagers = 0x00; // sert à détecter les THREADS utilisés \ No newline at end of file +static pthread_t managers[maxListLen]; // contiendra les THREADS +static int activeManagers[maxListLen] = {0}; // contiendra les THREADS actifs diff --git a/test b/test index f5cb986..020360e 100755 Binary files a/test and b/test differ