unsigned int Keywords::printBucket (const unsigned int i) { hashEntry **const table = getHashTable (); hashEntry *entry = table [i]; unsigned int measure = 1; boolean first = true; printf ("%2d:", i); if (entry == NULL) printf ("\n"); else while (entry != NULL) { if (! first) printf (" "); else { printf (" "); first = false; } printEntry (entry); entry = entry->next; measure = 2 * measure; } return measure - 1; }
int main(int argc, char* argv[]){ struct hashtable *a; struct abc **tb; struct abc *abc; int i; a = hashTableCreate(10); tb = (struct abc**)getHashTable(a); for(i=0;i<10;i++){ if(tb[i] == NULL) printf("%d:null\n",i); } for(i=0;i<5;i++){ abc=malloc(sizeof(struct abc)); abc->a=i+1; abc->b=i+2; tb[i]=abc; } for(i=0;i<10;i++){ if(tb[i] == NULL) printf("%d:null\n",i); else printf("%d:a:%d,b:%d\n",i, tb[i]->a, tb[i]->b); } return 1; }
/** *A chaque connection d'un client, un thread est créé et talk_to_client est appelée. */ void *talk_to_client(void *idSocket) { cle_t K; donnee_t D; requete_t type_requete; socket_t sockClient = (socket_t) idSocket; tabClient_t curr, prev; uint64_t h; /* Pour STATUS */ idConnexion_t ident; while (1) { pthread_mutex_lock(&MUTEX_NB_JOBS); NB_JOBS++; pthread_mutex_unlock(&MUTEX_NB_JOBS); recevoirTypeMessage(&type_requete, sockClient); #ifdef DEBUG_SERVEUR_IMPL printf("%d:reçu %d\n", sockClient, type_requete); #endif switch (type_requete) { case PUT: pthread_cond_signal(&condition_cond); recevoirCle(&K, sockClient); h = hash(K); #ifdef DEBUG_SERVEUR_IMPL printf("talk_to_client:PUT:cle(%s), hash(%llu)\n", K, h); #endif if (SERVEUR.h <= h && h < (SERVEUR.h + SERVEUR.tabl.taille)) { envoyerOctet(1, sockClient); recevoirDonnee(&D, sockClient); pthread_mutex_lock(&(SERVEUR.tabl.mutexTab[h - SERVEUR.h])); putHashTable(D, SERVEUR.tabl); pthread_mutex_unlock(&(SERVEUR.tabl.mutexTab[h - SERVEUR.h])); } else { envoyerOctet(0, sockClient); envoyerSockAddr(SERVEUR.suivServeur, sockClient); } //pour les tests #ifdef DEBUG_SERVEUR_IMPL printf("put terminé\n"); #endif break; case GET: pthread_cond_signal(&condition_cond); recevoirCle(&K, sockClient); h = hash(K); #ifdef DEBUG_SERVEUR_IMPL printf("talk_to_client:GET:cle(%s), hash(%llu)\n", K, h); #endif if (SERVEUR.h <= h && h < (SERVEUR.h + SERVEUR.tabl.taille)) { D = getHashTable(K, SERVEUR.tabl); envoyerOctet(1, sockClient); if (D == NULL) { envoyerOctet(0, sockClient); } else { envoyerOctet(1, sockClient); envoyerDonnee(D, sockClient); } } else { envoyerOctet(0, sockClient); envoyerSockAddr(SERVEUR.suivServeur, sockClient); } break; case REMOVEKEY: pthread_cond_signal(&condition_cond); recevoirCle(&K, sockClient); h = hash(K); #ifdef DEBUG_SERVEUR_IMPL printf("talk_to_client:cle(%s), hash(%llu)\n", K, h); #endif if (SERVEUR.h <= h && h < (SERVEUR.h + SERVEUR.tabl.taille)) { D = getHashTable(K, SERVEUR.tabl); envoyerOctet(1, sockClient); if (D == NULL) { envoyerOctet(0, sockClient); } else { pthread_mutex_lock(&(SERVEUR.tabl.mutexTab[h - SERVEUR.h])); valeur_t V = removeHashTable(K, SERVEUR.tabl); pthread_mutex_unlock(&(SERVEUR.tabl.mutexTab[h - SERVEUR.h])); envoyerOctet(1, sockClient); envoyerValeur(V, sockClient); free(V); } } else { envoyerOctet(0, sockClient); envoyerSockAddr(SERVEUR.suivServeur,sockClient); } break; case CONNECT: pthread_cond_signal(&condition_cond); break; case QUIT: /*pthread_cond_signal(&condition_cond);!!! */ #ifdef DEBUG_SERVEUR_IMPL printf("QUIT\n"); #endif pthread_mutex_lock(&SERVER_IS_DYING); if (SERVER_IS_DYING_VAR == 0) { SERVER_IS_DYING_VAR = 1; //envoyer octet 1; } else { printf("serveur is already dead"); //envoyerOctet 0 } pthread_mutex_unlock(&SERVER_IS_DYING); free(SERVEUR.tabl.mutexTab); if (NB_JOBS != 1) { printf ("en attente que les requetes soient satisfaites\n"); pthread_cond_wait(&COND_NB_JOBS, &MUTEX_NB_JOBS); } #ifdef DEBUG_SERVEUR_IMPL printf("le serveur est libre\n"); #endif message_quit(); exit(0); break; case DISCONNECT: pthread_cond_signal(&condition_cond); #ifdef DEBUG_SERVEUR_IMPL printf("disconnect\n"); #endif curr = SERVEUR.tableauClient; prev = SERVEUR.tableauClient; if (curr->client.idSocket == sockClient) { SERVEUR.tableauClient = curr->suiv; free(curr); } else { while(curr->client.idSocket != sockClient && curr) { prev = curr; curr = curr->suiv; } if (curr) { prev->suiv = curr->suiv; free(curr); } else { #ifdef DEBUG_SERVEUR_IMPL printf("talk_to_client:DISCONNECT:client de socket %d inconnu.\n" , sockClient); #endif close(sockClient); pthread_exit(NULL); } } shutdown(sockClient, SHUT_RDWR); pthread_exit(NULL); break; case STATUS: #ifdef DEBUG_SERVEUR_IMPL printf("STATUS\n"); #endif ident = get_my_idConnexion(); envoyerIdent(ident, sockClient); break; break; default: printf("Erreur:talk_to_client:Message inconnu\n"); break; } pthread_mutex_lock(&MUTEX_NB_JOBS); NB_JOBS--; if (NB_JOBS == 0) { pthread_cond_signal(&COND_NB_JOBS); } pthread_mutex_unlock(&MUTEX_NB_JOBS); } }