-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-emitter.c
589 lines (503 loc) · 13.6 KB
/
event-emitter.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#include "event-emitter.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
void error(int n){
printf("\n ERROR: ");
switch(n){
case 0:
printf("Para crear un centro se debe de especificar un nombre");
break;
case 1:
printf("Uno de los centros no existe ");
break;
case 2:
printf("El evento es nulo o no existe ");
break;
case 3:
printf("El cliente no puede ser NULL ");
break;
case 4:
printf("El sender no tiene suscriptores ");
break;
}
printf("\n");
}
Centro *CENTROS;
void deleteList_event(node_e *event){
if(event == NULL) return;
deleteList_event(event->next);
free(event);
}
void deleteList_sender(node_s *sender){
if(sender == NULL) return;
deleteList_sender(sender->next);
free(sender);
}
void deleteList_client(node_c *client){
if(client == NULL) return;
deleteList_client(client->next);
free(client);
}
void deleteList_nodeCenter(node_center *c){
if(c == NULL) return;
deleteList_nodeCenter(c->next);
free(c);
}
void removeFromList_c(node_c **list, void *del) {
if(!*list) return;
node_c *aux = *list;
node_c *prev = *list;
while(aux && aux->client != del){
prev = aux;
aux = aux->next;
}
if(aux == *list) *list = (*list)->next;
if(aux && aux->client == del){
if(prev) prev->next = aux->next;
free(aux);
}
}
void removeFromList_center(node_center **list, Centro *del){
if(!*list) return;
node_center *aux = *list;
node_center *prev = *list;
while(aux && aux->center != del){
prev = aux;
aux = aux->next;
}
if(aux == *list) *list = (*list)->next;
if(aux && aux->center == del){
if(prev) prev->next = aux->next;
free(aux);
}
}
void imprimirCentros(void) {
node_center *aux1;
Centro *aux;
node_e *aux2;
node_s *aux3;
node_c *aux4;
aux = CENTROS;
while(aux){
printf("\n Centros asociados a %s \n", aux->name);
if(aux->events){
// aux = aux->next;
// continue;
aux2 = aux->events;
while(aux2){
if(aux2->centers) printf("-->Centros registrados al evento %s\n", aux2->name);
aux1 = aux2->centers;
while(aux1){
printf("\n---->%s \n", aux1->center->name);
//printf("\n %s \n", (aux1->center) == (aux1->next->center)?"iguales":"distintos");
aux1 = aux1->next;
}
if(aux2->senders || aux2->clients) printf("\n-->Clientes registrados al evento %s \n", aux2->name);
if(aux2->senders){
aux3 = aux2->senders;
while(aux3){
printf("\n----> Sender %p\n", aux3->sender);
aux4 = aux3->clients;
while(aux4){
printf("\n------> %p\n", aux4->client);
//aux4->f(aux4->client);
aux4 = aux4->next;
}
aux3 = aux3->next;
}
}
if(aux2->clients){
aux4 = aux2->clients;
while(aux4){
printf("\n----> %p\n", aux4->client);
aux4 = aux4->next;
}
}
aux2 = aux2->next;
}
}
aux = aux->next;
}
}
Centro * locateCenter(char *center){
Centro *aux = CENTROS;
while(aux && strcmp(center, aux->name)) aux = aux->next;
return aux;
}
node_e * locateEvent(Centro *center, char *event){
node_e *aux = center->events;
while(aux && strcmp(event, aux->name)) aux = aux->next;
return aux;
}
node_s * locateSender(node_e *event, void *sender){
node_s *aux = event->senders;
while(aux && aux->sender != sender) aux = aux->next;
return aux;
}
void newCenter(char *name) {
if(!name) {
error(0);
return;
}
if(locateCenter(name)) return; // SI EL CENTRO YA EXISTE NO LO AGREGAMOS
Centro *new = (Centro *) malloc(sizeof(Centro));
if(!CENTROS){ // NOS ASEGURAMOS QUE EXISTA ALGUIEN EN LA LISTA DE CENTROS
CENTROS = new;
strcpy(new->name, name);
return;
}
Centro *aux = CENTROS;
strcpy(new->name, name);
aux = CENTROS;
while(aux->next) aux = aux->next;
aux->next = new;
new->events = NULL;
new->next = NULL;
}
// Centro B observa al centro A
void observeCenterFromCenter (char *centerA, char *centerB, char *event){
if(!centerA || !centerB || !event) return;
Centro *cA = locateCenter(centerA);
Centro *cB = locateCenter(centerB);
if(!cA || !cB){
error(1);
return;
}
if(cA == cB){
printf("\n los centros no pueden ser iguales \n");
return;
}
node_e *aux = NULL;
node_e *eventToSub = locateEvent(cA, event);
node_center *aux1 = NULL;
// ASOCIAMOS EL EVENTO AL CENTRO A
aux = cA->events;
if(!eventToSub){
eventToSub = (node_e *)malloc(sizeof(node_e));
strcpy(eventToSub->name, event);
if(!aux) cA->events = eventToSub;
else {
while(aux->next) aux = aux->next;
aux->next = eventToSub;
}
eventToSub->senders = NULL;
eventToSub->emitToClients = 0;
eventToSub->centers = NULL;
eventToSub->clients = NULL;
eventToSub->next = NULL;
}
// ASOCIAMOS EL CENTRO B AL EVENTO
aux1 = eventToSub->centers;
if(!aux1){
aux1 = (node_center *)malloc(sizeof(node_center));
aux1->center = cB;
eventToSub->centers = aux1;
}else{
if(aux1->center == cB) return; //CHEQUEAMOS QUE EL PRIMER CENTRO DE LA LISTA NO SEA EL MISMO QUE VAMOS A AGREGAR
while(aux1->next){
if(aux1->center == cB) return; // CHEQUEAMOS QUE EL CENTRO QUE QUEREMOS AGREGR NO SE ENCUENTRE EN LA LISTA
aux1 = aux1->next;
}
if(aux1->center == cB) return;
aux1->next = (node_center *) malloc(sizeof(node_center));
aux1->next->center = cB;
aux1->next->next = NULL;
}
}
void observeCenterFromClient(char *centerA, void *client, void *sender, char *event, void (* methodToCall)(void *)){
if(!centerA || !methodToCall || !event ) return;
if (!client){
error(3);
return;
}
Centro *c = locateCenter(centerA);
if(!c){
error(1);
return;
}
node_e *eventToSub = locateEvent(c, event);
node_s *senderToSub = NULL;
node_s *p_sender = NULL;
node_c *p_client = NULL;
node_c *clientToSub = (node_c *) malloc(sizeof(node_c));
clientToSub->client = client;
clientToSub->f = methodToCall;
clientToSub->next = NULL;
// SI EL EVENTO NO EXISTE, LO AGREGAMOS A LA LISTA DE EVENTOS EN EL CENTRO
if (!eventToSub){
eventToSub = (node_e *) malloc(sizeof(node_e));
strcpy(eventToSub->name, event);
node_e *aux = c->events;
if(!aux) c->events = eventToSub;
else {
while(aux->next) aux = aux->next;
aux->next = eventToSub;
}
eventToSub->senders = NULL;
eventToSub->emitToClients = 0;
eventToSub->centers = NULL;
eventToSub->clients = NULL;
eventToSub->next = NULL;
}
// SI HAY UN SENDER VINCULADO AL EVENTO, AGREGAMOS EL SENDER EN EL EVENTO. EN CASO DE QUE
// EL SENDER SEA (NULL) AGREGAMOS EL CLIENTE A LA LISTA DE CLIENTES
if(sender){
// YA QUE AGREGAMOS EL EVENTO AL CENTRO, CREAMOS LA LISTA DE ESTE SENDER
senderToSub = locateSender(eventToSub, sender);
// SI EL SENDER NO EXISTE DENTRO DEL EVENTO, LO AGREGAMOS A LA LISTA DE SENDERS DEL EVENTO
if(!senderToSub) {
senderToSub = (node_s *)malloc(sizeof(node_s));
senderToSub->sender = sender;
p_sender = eventToSub->senders;
if(!p_sender) eventToSub->senders = senderToSub;
else {
while(p_sender->next) p_sender = p_sender->next;
p_sender->next = senderToSub;
}
senderToSub->clients = NULL;
}
//AGREGAMOS EL CLIENTE A LA LISTA DEl SENDER
p_client = senderToSub->clients;
if(!p_client){
senderToSub->clients = clientToSub;
return;
}
} else{
p_client = eventToSub->clients; //AGREGAMOS EL CLIENTE A LA LISTA DEL EVENTO
if(!p_client) {
eventToSub->clients = clientToSub;
return;
}
}
// CHEQUEAMOS QUE EL PRIMER ELEMENTO DE LA LISTA NO SEA IGUAL AL CLIENTE
if(p_client->client == client){
free(clientToSub);
return;
}
// AHORA AGREGAMOS EL CLIENTE QUE ESCUCHA AL SENDER EN LA LISTA CORRESPONDIENTE
while(p_client->next){
if(p_client->client == client){
free(clientToSub);
return; // CHEQUEAMOS QUE EL CLIENTE NO ESTE EN LA LISTA
}
p_client = p_client->next;
}
if(p_client->client == client){
free(clientToSub);
return; // CHEQUEAMOS QUE EL CLIENTE NO ESTE EN LA LISTA
}
p_client->next = clientToSub;
}
void removeObserver(char *center, void *client, char *event){
if(!center || !client || !event) return;
Centro *c = locateCenter(center);
if(!c) {
error(1);
return;
}
node_e *e = locateEvent(c, event);
if(!e) {
error(2);
return;
}
// ELIMINAMOS DE LA LISTA DE CLIENTES QUE ESTAN SUSCRITOS SIN NINGUN SENDER EN ESPECIFICO
removeFromList_c(&e->clients, client);
// ELIMINAMOS DE LA LISTA DE CADA UNO DE LOS SENDERS A LOS QUE ESTA SUSCRITO
node_s *p_sender = e->senders;
node_s *prev_sender = e->senders;
while(p_sender) {
removeFromList_c(&p_sender->clients, client);
// SI EL SENDER NO TIENE CLIENTES LO ELIMINAMOS DE LA LISTA
if(!p_sender->clients) {
if(p_sender == e->senders) {
e->senders = p_sender->next;
free(p_sender);
p_sender = e->senders;
continue;
}
else {
prev_sender->next = p_sender->next;
free(p_sender);
p_sender = prev_sender->next;
continue;
}
}
prev_sender = p_sender;
p_sender = p_sender->next;
}
}
void removeObservers(char *center, void *client) {
if(!center || !client) return;
Centro * c = locateCenter(center);
if(!c) {
error(1);
return;
}
node_e *p_event = c->events;
node_e *prev = c->events;
// RECORREMOS TODOS LOS EVENTOS DEL CENTRO Y VAMOS ELIMINANDO EL CLIENTE DE CADA UNO
// DE ELLOS CON LA FUNCION removeObserver
while(p_event){
removeObserver(center, client, p_event->name);
// SI EL EVENTO NO TIENE CLIENTES NI SENDERS NI CENTROS LO ELIMINAMOS DE LA LISTA
if(!p_event->clients && !p_event->senders && !p_event->centers) {
if(p_event == c->events) {
c->events = p_event->next;
free(p_event);
p_event = c->events;
continue;
}
else {
prev->next = p_event->next;
free(p_event);
p_event = prev->next;
continue;
}
}
prev = p_event;
p_event = p_event->next;
}
}
void removeAllObservers(void * client){
if(!client) return;
Centro *c = CENTROS;
while(c){
removeObservers(c->name, client);
c = c->next;
}
}
// Desasociar Centro B de un evento en el Centro A
void removeObserverFromCenter(char *centerA, char *centerB, char *event) {
if(!centerA || !centerB || !event) return;
Centro *cA = locateCenter(centerA);
Centro *cB = locateCenter(centerB);
if(!cA || !cB) {
error(1);
return;
}
node_e *p_event = locateEvent(cA, event);
if(!p_event) {
error(2);
return;
}
removeFromList_center(&p_event->centers, cB);
}
void removeObserversFromCenter(char * centerA, char * centerB){
if(!centerA || !centerB) return;
Centro *cA = locateCenter(centerA);
if(!cA){
error(1);
return;
}
node_e *p_event = cA->events;
while(p_event){
removeObserverFromCenter(centerA, centerB, p_event->name);
p_event = p_event->next;
}
}
void removeCenter (char *center) {
if(!center) return;
if(!locateCenter(center)){
error(1);
return;
}
Centro *c, *prev;
c = prev = CENTROS;
// ELIMINAMOS EL CENTRO DE LA LISTA GLOBAL
if(!strcmp(c->name, center)) {
CENTROS = CENTROS->next;
return;
}else {
while(c && strcmp(c->name, center)){
prev = c;
c = c->next;
}
prev->next = c->next;
}
// LIBERAMOS TODA SU INFORMACION
deleteList_client(c->events->clients); // CLIENTES CON SENDER NULL
deleteList_nodeCenter(c->events->centers); // CENTROS
node_s *aux = c->events->senders;
while(aux) { // CLIENTES DE LOS SENDERS
deleteList_client(aux->clients);
aux = aux->next;
}
deleteList_sender(c->events->senders); // SENDERS
deleteList_event(c->events); // EVENTOS
free(c);
}
void post(char *center, char *event, void *sender, void *params){
if(!center || !event || !sender) return;
Centro *c = locateCenter(center);
if(!c) {
error(1);
return;
}
c->events->emitToClients = 1;
node_e *e = locateEvent(c, event);
if(!e) {
error(2);
return;
}
node_s *s = locateSender(e, sender);
node_c *client = e->clients;
while(client){
client->f(params);
client = client->next;
}
if(s){
client = s->clients;
while(client){
client->f(params);
client = client->next;
}
}
node_center *centers = e->centers;
while(centers){
if(!centers->center->events->emitToClients) post(centers->center->name, event, sender, params);
centers = centers->next;
}
c->events->emitToClients = 0;
}
// IMPLEMENTACION CON THREADS INDEPENDIENTES
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; // USAMOS MUTEX PARA EVITAR QUE SE DA;E LA LISTA GLOBAL DE CENTROS
struct postParams {
char center[100];
char event[100];
void *sender;
void *params;
int miliseconds;
};
void *executioner(void *a){
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 0;
struct postParams *args = (struct postParams *)a;
int remaining = args->miliseconds;
if((remaining / 1000) > 0){
tim.tv_sec = (remaining / 1000);
remaining = (tim.tv_sec*1000) - remaining;
}
tim.tv_nsec = (remaining) * 1000000L;
nanosleep(&tim, &tim2);
pthread_mutex_lock(&lock);
post(args->center, args->event, args->sender, args->params);
free(args); // HACEMOS FREE DE LOS ARGS PARA NO EMBASURAR LA MEMORIA
pthread_mutex_unlock(&lock);
}
void postDelayed(char *center, char *event, void *sender, void *params, int miliseconds){
struct postParams *args = (struct postParams *) malloc(sizeof(struct postParams));
int err;
strcpy(args->center, center);
strcpy(args->event, event);
args->params = params;
args->sender = sender;
args->miliseconds = miliseconds;
pthread_t thread;
err = pthread_create( &thread, NULL, executioner, (void*) args);
if (err != 0) printf("\nNo se pudo crear el nuevo hilo :[%s]", strerror(err));
}