-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathicl_hash.c
501 lines (398 loc) · 11.4 KB
/
icl_hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* @file icl_hash.c
*
* Dependency free hash table implementation.
*
* This simple hash table implementation should be easy to drop into
* any other peice of code, it does not depend on anything else :-)
*
* @author Jakub Kurzak
* Alcune funzioni sono state modificate ed altre aggiunte
*/
/* $Id: icl_hash.c 2838 2011-11-22 04:25:02Z mfaverge $ */
/* $UTK_Copyright: $ */
/**
* @file icl_hash.c
* @brief Implementazione di una tabella hash
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "icl_hash.h"
#include "config.h"
#include "message.h"
#include <limits.h>
#include "codamessaggi.h"
#include <pthread.h>
#define BITS_IN_int ( sizeof(int) * CHAR_BIT )
#define THREE_QUARTERS ((int) ((BITS_IN_int * 3) / 4))
#define ONE_EIGHTH ((int) (BITS_IN_int / 8))
#define HIGH_BITS ( ~((unsigned int)(~0) >> ONE_EIGHTH ))
/**
* A simple string hash.
*
* An adaptation of Peter Weinberger's (PJW) generic hashing
* algorithm based on Allen Holub's version. Accepts a pointer
* to a datum to be hashed and returns an unsigned integer.
* From: Keith Seymour's proxy library code
*
* @param[in] key -- the string to be hashed
*
* @returns the hash index
*/
static unsigned int
hash_pjw(void* key)
{
char *datum = (char *)key;
unsigned int hash_value, i;
if(!datum) return 0;
for (hash_value = 0; *datum; ++datum) {
hash_value = (hash_value << ONE_EIGHTH) + *datum;
if ((i = hash_value & HIGH_BITS) != 0)
hash_value = (hash_value ^ (i >> THREE_QUARTERS)) & ~HIGH_BITS;
}
return (hash_value);
}
static int string_compare(void* a, void* b)
{
return (strcmp( (char*)a, (char*)b ) == 0);
}
/**
* Create a new hash table.
*
* @param[in] nbuckets -- number of buckets to create
* @param[in] hash_function -- pointer to the hashing function to be used
* @param[in] hash_key_compare -- pointer to the hash key comparison function to be used
*
* @returns pointer to new hash table.
*/
icl_hash_t *
icl_hash_create( int nbuckets, unsigned int (*hash_function)(void*), int (*hash_key_compare)(void*, void*) )
{
icl_hash_t *ht;
int i;
ht = (icl_hash_t*) malloc(sizeof(icl_hash_t));
if(!ht) return NULL;
ht->nentries = 0;
ht->buckets = (icl_entry_t**)malloc(nbuckets * sizeof(icl_entry_t*));
if(!ht->buckets) return NULL;
ht->nbuckets = nbuckets;
for(i=0;i<ht->nbuckets;i++)
ht->buckets[i] = NULL;
ht->hash_function = hash_function ? hash_function : hash_pjw;
ht->hash_key_compare = hash_key_compare ? hash_key_compare : string_compare;
return ht;
}
/**
* Search for an entry in a hash table.
*
* @param ht -- the hash table to be searched
* @param key -- the key of the item to search for
*
* @returns pointer to the data corresponding to the key.
* If the key was not found, returns NULL.
*/
void *
icl_hash_find(icl_hash_t *ht, void* key)
{
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return NULL;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
//printf("ACCESSO DA FIND: %d\n",hash_val);
//printf("stampa icl: %d %s\n",hash_val,(char*)key);
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( strcmp(curr->data, key)==0)
return(curr->data);
return NULL;
}
/**
* Insert an item into the hash table.
*
* @param ht -- the hash table
* @param key -- the key of the new item
* @param data -- pointer to the new item's data
*
* @returns pointer to the new item. Returns NULL on error.
*/
icl_entry_t *
icl_hash_insert(icl_hash_t *ht, int fd, void * data, int dimCoda)
{
icl_entry_t *curr;
unsigned int hash_val;
if(!ht || !data) return NULL;
// printf("%d\n",ht->nbuckets);
hash_val = (*ht->hash_function)(data) % ht->nbuckets;
// printf("INSER hash val: %d %s\n",hash_val, data);
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if ( strcmp(curr->data, data)==0)
return(NULL); /* key already exists */
}
/* if key was not found */
curr = (icl_entry_t*)malloc(sizeof(icl_entry_t));
if(!curr) return NULL;
curr->data=strndup(data,strlen(data)+1);
curr->next = ht->buckets[hash_val]; /* add at start */
curr->fd= fd;
curr->online=1;
curr->coda=creacodaMessaggi(dimCoda);
ht->buckets[hash_val] = curr;
ht->nentries++;
return curr;
}
/**
* Aggiorna l'id di un certo utente
*
* @param ht -- hash table
* @param key -- il nome dell'utente che vogliamo modificare
* @param id -- il nuovo ID
*
* @ Restituisce 0 se non ci sono errori e -1 in caso di errori
*/
int
icl_hash_update_id(icl_hash_t *ht, void * key, int id)
{
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( strcmp(curr->data, key)==0){
curr->id=id;
return 0;
}
return -1;
}
/**
* Free one hash table entry located by key (key and data are freed using functions).
*
* @param ht -- the hash table to be freed
* @param key -- the key of the new item
* @param free_key -- pointer to function that frees the key
* @param free_data -- pointer to function that frees the data
*
* @returns 0 on success, -1 on failure.
*/
int icl_hash_delete(icl_hash_t *ht, void* key, void (*free_key)(void*), void (*free_data)(void*))
{
icl_entry_t *curr, *prev;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
prev = NULL;
for (curr=ht->buckets[hash_val]; curr != NULL; ) {
if ( strcmp(curr->data, key)==0) {
if (prev == NULL) {
ht->buckets[hash_val] = curr->next;
} else {
prev->next = curr->next;
}
free(curr->data);
free(curr);
ht->nentries--;
return 0;
}
prev = curr;
curr = curr->next;
}
return -1;
}
/**
* Free hash table structures (key and data are freed using functions).
*
* @param ht -- the hash table to be freed
* @param free_key -- pointer to function that frees the key
* @param free_data -- pointer to function that frees the data
*
* @returns 0 on success, -1 on failure.
*/
int
icl_hash_destroy(icl_hash_t *ht, void (*free_key)(void*), void (*free_data)(void*))
{
icl_entry_t *bucket, *curr, *next;
int i;
if(!ht) return -1;
for (i=0; i<ht->nbuckets; i++) {
bucket = ht->buckets[i];
for (curr=bucket; curr!=NULL; ) {
next=curr->next;
free(curr->data);
free(curr);
curr=next;
}
}
if(ht->buckets) free(ht->buckets);
if(ht) free(ht);
return 0;
}
/**
* Dump the hash table's contents to the given file pointer.
*
* @param stream -- the file to which the hash table should be dumped
* @param ht -- the hash table to be dumped
*
* @returns 0 on success, -1 on failure.
*/
int
icl_hash_dump(FILE* stream, icl_hash_t* ht)
{
icl_entry_t *bucket, *curr;
int i;
if(!ht) return -1;
for(i=0; i<ht->nbuckets; i++) {
bucket = ht->buckets[i];
for(curr=bucket; curr!=NULL; ) {
if(curr->data)
fprintf(stream, "icl_hash_dump: %s\n", (char *) curr->data);
curr=curr->next;
}
}
return 0;
}
/**
* Aggiorna il file descriptor assegnato all'utente
*
* @param fd -- tabella hash
* @param fd -- il nuovo file descriptor
* @param key -- il nome utente da aggiornare
*
* @returns 0 in caso di successo, altrimenti NULL
*/
void * icl_hash_updatefd(icl_hash_t *ht,int fd, void * key){
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return NULL;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( strcmp(curr->data, key)==0){
curr->fd=fd;
}
return 0;
}
/**
* Aggiunta di un messaggio all'interno della coda messaggi di un certo utente
*
* @param ht -- tabella hash
* @param key -- il nome utente da aggiornare
* @param msg -- il messaggio da inserire in codamessaggi
*
* @returns 0 in caso di successo, altrimenti -1
*/
int icl_add_message(icl_hash_t *ht, void * key, message_t msg){
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if (strcmp(curr->data, key)==0){
inserimentoMessaggi(curr->coda, msg);
return 0;
}
}
return -1;
}
/**
* Controlla se un utente è online o meno
*
* @param ht -- tabella hash
* @param key -- il nome utente da controllare
*
* @ Restituisce il FD dell'utente in caso di successo, altrimenti -1
*/
int isOnline(icl_hash_t *ht, void * key){
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if (strcmp(curr->data, key)==0 && curr->online==1){
return curr->fd;
}
}
return -1;
}
/**
* Controlla se un utente è registrato o meno
*
* @param ht -- tabella hash
* @param key -- il nome utente da controllare
*
* @ Restituisce 0 in caso di successo, altrimenti -1
*/
int isRegistrato(icl_hash_t *ht, void * key){
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if (strcmp(curr->data, key)==0){
return 0;
}
}
return -1;
}
/**
* Manda online un utente che ne fa richiesta
*
* @param ht -- tabella hash
* @param key -- il nome utente da connettere
* @param fd -- file descriptor dell'utente che si deve connettere
*
* @ Restituisce 0 in caso di successo, altrimenti -1
*/
int goOnline(icl_hash_t *ht, void * key,int fd){
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if (strcmp(curr->data, key)==0){
curr->online=1;
curr->fd=fd;
return 0;
}
}
return -1;
}
/**
* Manda offline un utente che ne fa richiesta
*
* @param ht -- tabella hash
* @param key -- il nome utente da disconnettere
*
* @ Restituisce 0 in caso di successo, altrimenti -1
*/
int goOffline(icl_hash_t *ht, void * key){
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return -1;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if (strcmp(curr->data, key)==0 && curr->online==1){
curr->online=0;
curr->fd=-1;
return 0;
}
}
return -1;
}
/**
* Serve per ottenere l'ID di un certo utente
*
* @param ht -- tabella hash
* @param key -- il nome utente per cui vogliamo ottenere l'id
*
* @ Restituisce l'id dell'utente in caso di successo, altrimenti -1
*/
int icl_get_id(icl_hash_t *ht, char * user){
icl_entry_t *curr;
unsigned int hash_val;
if(!ht || !user) return -1;
hash_val = (*ht->hash_function)(user) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next){
if ( strcmp(curr->data, user)==0)
return curr->id;
}
return -1;
}