-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaodvugd_route_discovery.cpp
More file actions
1291 lines (1171 loc) · 51 KB
/
Copy pathaodvugd_route_discovery.cpp
File metadata and controls
1291 lines (1171 loc) · 51 KB
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "_legacyapps_enable_cmake.h"
#ifdef ENABLE_AODVUGD
#include "legacyapps/aodvugd/aodvugd_logger.h"
//#include "legacyapps/aodvugd/aodvugd_route_discovery.h"
//#include "legacyapps/aodvugd/aodvugd_message.h"
#include "legacyapps/aodvugd/aodvugd_observer_estado_ruta.h"
//#include "legacyapps/aodvugd/aodvugd_processor.h"
//#include "legacyapps/aodvugd/aodvugd_message.h"
//#include "sys/simulation/simulation_controller.h"
//#include "sys/node.h"
#include <sstream> //tostring
#include <algorithm> //std::max
#include <math.h> //round
namespace aodvugd
{
// clase AodvUgdRouteDiscovery----------------------------------------------------------------------
/* AodvUgdRouteDiscovery::
AodvUgdRouteDiscovery()
{}*/
// ----------------------------------------------------------------------
AodvUgdRouteDiscovery::
AodvUgdRouteDiscovery( unsigned int contadorRREQ_ID,
unsigned int contadorRREQ_sent_seg,
ConfiguracionDiscovery unaConfiguracionDiscovery,
AodvUgdProcessor& procesoAodv )
:
contadorRREQ_ID_ {contadorRREQ_ID},
contadorRREQ_sent_seg_ {contadorRREQ_sent_seg},
unaConfiguracionDiscovery_ {unaConfiguracionDiscovery},
procesoAodv_ {&procesoAodv}
{
}
// ----------------------------------------------------------------------
AodvUgdRouteDiscovery::
~AodvUgdRouteDiscovery() {}
// get set----------------------------------------------------------------------
unsigned int
AodvUgdRouteDiscovery::
getContadorRREQ_ID (void) throw()
{
return contadorRREQ_ID_;
}
// metodos----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
timeout( shawn::EventScheduler&, shawn::EventScheduler::EventHandle,
double, shawn::EventScheduler::EventTagHandle& unEventTagHandle )
throw()
{
//*********************************
//evento DiscoveryEvent
const DiscoveryEventTag* unDiscoveryEventTag =
dynamic_cast<const DiscoveryEventTag*> ( unEventTagHandle.get() );
/* si se disparo el evento unDiscoveryEventTag,
es porque expiro el tiempo de espera por un RREP*/
if ( unDiscoveryEventTag != NULL){
std::cout<< "*************** TIMEOUT unEventTagHandle: " << std::endl;
unDiscoveryEventTag->show();
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Timeout: "<< unDiscoveryEventTag->toString() ;
LoggerAodv::Instance()->logCsvPingDetalle( procesoAodv_->owner().label() ,
"" /*origen*/,unDiscoveryEventTag->getDestino() ,
"Discovery", mensaje.str() );
//**************** FIN LOG ****************
handle_EventExpireTimeDiscovery ( unDiscoveryEventTag->getDestino () );
}
//*********************************
//evento ResetRREQ_limit
const DiscoveryEventResetRREQ_limit* unDiscoveryEventResetRREQ_limit =
dynamic_cast<const DiscoveryEventResetRREQ_limit*> ( unEventTagHandle.get() );
if ( unDiscoveryEventResetRREQ_limit != NULL)
handleEventoResetContadorRREQ();
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
aumentarContadorRREQ_ID() throw()
{
contadorRREQ_ID_++;
}
// ----------------------------------------------------------------------
/////////////////////////////////////////////////////////////////
/********************* AvisoEstadoRuta (observer) *************/
/////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
observarUnaRuta( std::string destino, std::string estado )
throw()
{
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Escucha por Ruta: "<<estado<<".. Destino: "<<destino;
LoggerAodv::Instance()->logCsvPingDetalle( procesoAodv_->owner().label() ,
"" /*origen*/ , destino ,"Discovery", mensaje.str() );
//**************** FIN LOG ****************
procesoAodv_->agregarObservadorTabla ( this , destino , estado );
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
cancelarObservarUnaRuta( std::string destino, std::string estado )
throw()
{
procesoAodv_->quitarObservadorTabla ( this , destino , estado );
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
handleAvisoRutaActiva ( std::string destino ) throw()
{
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Conoce nueva Ruta Activa.. Destino: "<<destino;
LoggerAodv::Instance()->logCsvPingDetalle(procesoAodv_->owner().label() ,
"" /*origen*/ , destino , "Discovery", mensaje.str() );
//**************** FIN LOG ****************
//verifico que este el decubrimiento todavia por las dudas
if( existeDescubrimientoIniciado(destino) )
{
cancelarDiscovery(destino);
//enviar paquetes pendientes a destino
}
else
{
//TODO avisar log
}
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
cancelarDiscovery (std::string destino ) throw()
{
std::map <std::string , infoDiscoveryRoute*> ::iterator it;
it=discoverysMap.find( destino );
infoDiscoveryRoute *pUnaInfoDiscoveryRoute = it->second;
//cancelar Tiempo de espera
cancelarEventoTiempoEspera(*pUnaInfoDiscoveryRoute);
//elimino el info del discovery
discoverysMap.erase (destino);
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
cancelarEventoTiempoEspera( infoDiscoveryRoute &pUnaInfoDiscoveryRoute )
throw()
{
shawn::EventScheduler::EventHandle pEventHandle =
pUnaInfoDiscoveryRoute.getEventoTiempoEspera();
procesoAodv_->owner_w().world_w().scheduler_w().delete_event ( pEventHandle );
}
/////////////////////////////////////////////////////////////////
/***************** FIN AvisoEstadoRuta (observer) *************/
/////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------
bool
AodvUgdRouteDiscovery::
existeDescubrimientoIniciado ( std::string dest_) throw()
{
bool existe = false;
std::map<const std::string , infoDiscoveryRoute*> ::iterator it;
it = discoverysMap.find( dest_ );
if ( it != discoverysMap.end() )
{
existe = true;
}
return existe;
}
// ----------------------------------------------------------------------
unsigned int
AodvUgdRouteDiscovery::
obtenerRREQ_ReintentosSentDestino( std::string destino ) throw()
{
unsigned int RREQ_Sent = 0;
std::map<const std::string , infoDiscoveryRoute*> ::iterator it;
it = discoverysMap.find( destino );
//si existe el descubrimiento,
//no se usa el metodo existeDescubrimientoIniciado para optimizar
if ( it != discoverysMap.end() )
{
RREQ_Sent = it -> second-> getRREQ_sent_by_this_discovery() ;
}
return RREQ_Sent;
}
// ----------------------------------------------------------------------
AodvUgdRREQ*
AodvUgdRouteDiscovery::
armarRREQ_reintento( std::string destino, unsigned int newTtl ) throw()
{
/*const_iterator si o si*/
std::map<const std::string , infoDiscoveryRoute*> ::const_iterator it;
it = discoverysMap.find( destino );
//si existe el descubrimiento,
//no se usa el metodo existeDescubrimientoIniciado para optimizar
if ( it != discoverysMap.end() )
{
//return it -> second->getUltimoRREQ() ;
const infoDiscoveryRoute *uninfoDiscoveryRoute = it->second;
return new AodvUgdRREQ(
uninfoDiscoveryRoute->getDestino() ,
uninfoDiscoveryRoute->getOrigen() ,
uninfoDiscoveryRoute->getDestSequNumb() ,
uninfoDiscoveryRoute->getOrigenSequNumb() ,
uninfoDiscoveryRoute->getJoinFlag() ,
uninfoDiscoveryRoute->getRepairFlag() ,
uninfoDiscoveryRoute->getRREP_gFlag() ,
uninfoDiscoveryRoute->getDestOnlyFlag() ,
uninfoDiscoveryRoute->getUnknownSequNum() ,
uninfoDiscoveryRoute->getHops() ,
contadorRREQ_ID_ ,// esta sumado!!
procesoAodv_-> owner().label() /*ipOrigen*/,
newTtl );
}
return NULL;
}
// ----------------------------------------------------------------------
unsigned int
AodvUgdRouteDiscovery::
obtenerTtlAnterrior( std::string destino )const throw()
{
/*const_iterator si o si*/
std::map<const std::string , infoDiscoveryRoute*> ::const_iterator it;
it = discoverysMap.find( destino );
//si existe el descubrimiento,
//no se usa el metodo existeDescubrimientoIniciado para optimizar
if ( it != discoverysMap.end() )
{
const infoDiscoveryRoute *uninfoDiscoveryRoute = it->second;
return uninfoDiscoveryRoute->getTtl();
}
return 0;
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
handle_EventExpireTimeDiscovery( std::string destino ) throw()
{
/* si llega hasta aca es porque no se encontraron rutas activas todavia, porque
se monitorea todo el tiempo la ruta y si se encuentra se cancela el envento*/
/*verificar por las dudas para hacer una assert que la ruta no existe*/
std::cout<< "Evento: Tiempo de espera sin encontrar Ruta " << std::endl;
/*TODO verificar en una funcion si se puede reintentar*/
/*TODO ver segun ERS o otro metodo activado*/
bool seEnvioNuevoReintento = reintentarProcesoRuteDescovery(destino);
/*at the end of the discovery period, the repairing node has not received
a RREP (or other control message creating or updating the route) for
that destination, it proceeds as described in Section 6.11 by
transmitting a RERR message for that destination.*/
//si por algun motivo no se realizan mas reintentos se supone que no se pudo
//reparar la ruta..
if (!seEnvioNuevoReintento)
{
bool descubrimientoLocalRepair = isDescubrimientoLocalRepair (destino);
TipoLocalRepair unTipoLocalRepair = getTipoLocalRepairInfoDiscovery (destino);
//si el RREQ.repairFlag_ = false es porque el descubrimiento no usa LocalREpair
assert (!descubrimientoLocalRepair && (unTipoLocalRepair == TipoLocalRepair::None) );
//si el descubrimiento era por que se intentaba reparar la ruta, hay que manejar el error!
if(descubrimientoLocalRepair)
{
//se intento reparar sin exito y se envia el RERR
if(unTipoLocalRepair == TipoLocalRepair::LinkBreak)
procesoAodv_-> rerrDetectesLinkBreak (destino);
if(unTipoLocalRepair == TipoLocalRepair::PacketSinRutaActivaParaReenviar)
procesoAodv_->rerrDataPacketSinRutaActivaParaReenviar (destino);
}
//sin importar si se usa local repair o no..
//si no hago mas reintentos hay que hacer drop de los mensajes pendientes en el buffer
procesoAodv_->handleNotFindRoute(destino);
}
else
{
//TODO log no se encontro la ruta luego de todos los rintentos
}
}
// ----------------------------------------------------------------------
bool
AodvUgdRouteDiscovery::
isDescubrimientoLocalRepair( std::string destino ) throw()
{
bool repairFlag = false;
std::map<const std::string , infoDiscoveryRoute*> ::iterator it;
it = discoverysMap.find( destino );
//si existe el descubrimiento,
//no se usa el metodo existeDescubrimientoIniciado para optimizar
if ( it != discoverysMap.end() )
{
repairFlag = it -> second-> getRREP_gFlag() ;
}
return repairFlag;
}
// ----------------------------------------------------------------------
const TipoLocalRepair
AodvUgdRouteDiscovery::
getTipoLocalRepairInfoDiscovery( std::string destino ) throw()
{
std::map<const std::string , infoDiscoveryRoute*> ::iterator it;
it = discoverysMap.find( destino );
//si existe el descubrimiento,
//no se usa el metodo existeDescubrimientoIniciado para optimizar
if ( it != discoverysMap.end() )
{
return it -> second-> getTipoLocalRepair() ;
}
return TipoLocalRepair::None ;
}
// ----------------------------------------------------------------------
shawn::EventScheduler::EventHandle
AodvUgdRouteDiscovery::
generarPrimerTiempoEspera (unsigned int ultimoTtl , double tiempoDeEspera ,
std::string destino )
throw()
{
shawn::EventScheduler& pEventScheduler = procesoAodv_->owner_w().world_w().scheduler_w();
DiscoveryEventTag* pDiscoveryEventTag=new DiscoveryEventTag
( destino , ultimoTtl, 0 /*reintento*/ );
double tiempoEvento=tiempoDeEspera + procesoAodv_->owner().current_time();
return pEventScheduler.new_event ( *this , tiempoEvento , pDiscoveryEventTag );
//**********
//LOGGGGG!
/* std::cout<< "Evento Tiempo Espera Route Dicovery en : " << procesoAodv_->owner().label()
<<" programado para: "<< tiempoEvento
<< std::endl;
std::stringstream mensaje;
mensaje <<"generarPrimerTiempoEspera programado: "<< tiempoEvento;
LoggerAodv::Instance()->logCsvRREQ( pUltimoRREQ ,procesoAodv_->owner().label() ,
mensaje.str() ); */
}
// ----------------------------------------------------------------------
shawn::EventScheduler::EventHandle
AodvUgdRouteDiscovery::
generarEventoTiempoEspera (unsigned int ultimoTtl , double tiempoDeEspera ,
unsigned int contadorReintentos , std::string destino )
throw()
{
shawn::EventScheduler& pEventScheduler = procesoAodv_->owner_w().world_w().scheduler_w();
DiscoveryEventTag* pDiscoveryEventTag=new DiscoveryEventTag
( destino , ultimoTtl , contadorReintentos /*reintento*/);
double tiempoEvento = tiempoDeEspera + procesoAodv_->owner().current_time();
shawn::EventScheduler::EventHandle pEventHandle= pEventScheduler.new_event ( *this , tiempoEvento , pDiscoveryEventTag );
return pEventHandle;
//*************
//LOGGGGGGGGGGG
/* std::cout<< "Evento: Reintento-Tiempo Espera Route Dicovery en : "
<< procesoAodv_->owner().label()
<<" programado para: "<< tiempoEvento
<< std::endl;
std::stringstream mensaje;
mensaje <<"generarUnNuevoEventoTiempoEsperaReintento programado: "<< tiempoEvento;
LoggerAodv::Instance()->logCsvRREQ( pUltimoRREQ ,procesoAodv_->owner().label() ,
mensaje.str() ); */
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
guardarPrimeraInfoDiscoveryRoute (const AodvUgdRREQ &pUltimoRREQ ,
shawn::EventScheduler::EventHandle pEventHandle ,
const TipoLocalRepair &unaTipoLocalRepair) throw()
{
infoDiscoveryRoute* pUnaInfoDiscoveryRoute=new infoDiscoveryRoute
( pUltimoRREQ /*&ultimoRREQ*/,
0 /*RREQ_sent_by_this_discovery*/,
pEventHandle /*EventHandler &eventoTiempoEspera*/,
unaTipoLocalRepair );
discoverysMap.insert(std::pair<std::string,infoDiscoveryRoute*>
( pUltimoRREQ.getDestino() , pUnaInfoDiscoveryRoute));
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
guardarReintentoInfoDiscoveryRoute (const AodvUgdRREQ &pUltimoRREQ ,
shawn::EventScheduler::EventHandle pEventHandle ,
unsigned int contadorReintentos ) throw()
{
//tengo que traer el tipoLocalRepair anterrior
TipoLocalRepair unaTipoLocalRepair = getTipoLocalRepairInfoDiscovery( pUltimoRREQ.getDestino() );
infoDiscoveryRoute* pUnaInfoDiscoveryRoute = new infoDiscoveryRoute
( pUltimoRREQ /*&ultimoRREQ*/,
contadorReintentos /*RREQ_sent_by_this_discovery*/,
pEventHandle /*EventHandler &eventoTiempoEspera*/,
unaTipoLocalRepair );
modificarInfoDiscoveryRoute ( pUltimoRREQ.getDestino() , *pUnaInfoDiscoveryRoute );
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
modificarInfoDiscoveryRoute (const std::string dest , infoDiscoveryRoute &pUnaInfoDiscoveryRoute ) throw()
{
std::map <std::string , infoDiscoveryRoute*> ::iterator it;
it=discoverysMap.find( dest );
it->second = &pUnaInfoDiscoveryRoute ; //utilizo un puntero para modificar directamente
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
modificarTipoRepairInfoDiscoveryRoute (const std::string dest , TipoLocalRepair unTipoLocalRepair) throw()
{
std::map <std::string , infoDiscoveryRoute*> ::iterator it;
it=discoverysMap.find( dest );
it->second->setTipoLocalRepair(unTipoLocalRepair);
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
sendDiscoveryRREQ ( AodvUgdRREQ* pRREQ)
//creo un metodo para enviar los dicoverys, asi se suma el contador y si
//en algun momento cambio la forma de enviar modifico unicamente aca :)
{
contadorRREQ_sent_seg_++;
//por ahora envio por el proceso principal
procesoAodv_->sendRREQ (pRREQ) ;
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
comenzarProcesoRuteDescovery(std::string destino , std::string origen )
throw()
{
/*se supone que antes de comenzar el descubrimiento se busco una ruta activa*/
/*TODO existe un descubrimiento iniciado con el mismo destino?*/
if (existeDescubrimientoIniciado (destino) )
/*TODO ver si se mueve algun evento*/
return;
/*verificar si se supera RREQ_RATELIMIT, la cantidad de RREQ enviado en un seg*/
/*A node SHOULD NOT originate more than RREQ_RATELIMIT RREQ messages
per second.*/
bool alcanzoRREQ_RATELIMIT = verificarRateLimit();
/*un evento que cada segundo renicie el contador..*/
if ( alcanzoRREQ_RATELIMIT )
return;
/*The Originator Sequence Number in the RREQ message is the
node's own sequence number, which is incremented prior to insertion
in a RREQ.*/
procesoAodv_->aumentarContadorNodoSeqNum() ;
unsigned int nodoSeqNum = procesoAodv_-> getContadorNodoSeqNum();
/*The RREQ ID field is incremented by one from the last
RREQ ID used by the current node.*/
contadorRREQ_ID_++;
//Guarda en el buffer rreqID+origen (su propia direccion)
/*Before broadcasting the RREQ, the originating node buffers the RREQ
ID and the Originator IP address (its own address) of the RREQ for
PATH_DISCOVERY_TIME. In this way, when the node receives the packet
again from its neighbors, it will not reprocess and re-forward the
packet*/
procesoAodv_->agregarRREQ_Buffer ( contadorRREQ_ID_ , origen);
/*TODO RREQ_sent_in_a_second+1 ver con un observe o verificando la variable nomas,
un observe que asigne 0 cada 1 segundo*/
unsigned int ttlInicial = unaConfiguracionDiscovery_.getTTL_START();
AodvUgdRREQ *UnPrimerRREQ =
new AodvUgdRREQ ( destino , origen , 0 /*destSequNumb*/ ,
nodoSeqNum /*origenSequNumb*/ , false /*joinFlag*/ ,
false /*repairFlag*/ ,
unaConfiguracionDiscovery_.getUseGratuitousRREP() /*rrep_gFlag*/ ,
unaConfiguracionDiscovery_.getUseDestOnly() /*destOnlyFlag*/ ,
true /*UnknownSequNum*/,
0 /*hops*/ , contadorRREQ_ID_/*RREQ_ID*/ ,
procesoAodv_-> owner().label() /*ipOrigen*/ ,
ttlInicial /*ttl*/ );
/*TODO si RREQ_sent_in_a_second> se puede enviar*/
//en el primer RREQ no hay que verificar TTL_THRESHOLD
/*If the RREQ times out
without a corresponding RREP, the originator broadcasts the RREQ
again with the TTL incremented by TTL_INCREMENT. This continues
until the TTL set in the RREQ reaches TTL_THRESHOLD, beyond which a
TTL = NET_DIAMETER is used for each attempt.*/
double tiempoDeEspera = unaConfiguracionDiscovery_.calcularRING_TRAVERSAL_TIME
( ttlInicial );
//calcularTiempoEsperaERS; no se usa en el primero
/*programar evento: esperar TIMEOUT*/
shawn::EventScheduler::EventHandle pEventHandle = generarEventoTiempoEspera (
ttlInicial /*ultimoTtl*/ ,
tiempoDeEspera ,
0 /*contadorReintentos*/ , destino );
//guardar info del descubrimiento para el proximo reintento
guardarPrimeraInfoDiscoveryRoute (*UnPrimerRREQ/*pUltimoRREQ*/ ,
pEventHandle ,
TipoLocalRepair::None );
//observo la ruta para saber cuando esta activa, sea porque me responden el RREP
//o porque recibo un mensaje que actualiza la tabla.
observarUnaRuta ( destino , "active" /*estado*/ );
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Busqueda: Nueva TTL: "<<UnPrimerRREQ->getTtl()<< " Timeout: "<< tiempoDeEspera;
LoggerAodv::Instance()->logCsvPingDetalle( procesoAodv_->owner().label() ,
UnPrimerRREQ->getOrigen() , UnPrimerRREQ->getDestino() ,
"Discovery", mensaje.str() );
//**************** FIN LOG ****************
//sumo el contador global de RREQ enviados cada vez que llamo sendDiscoveryRREQ
sendDiscoveryRREQ (UnPrimerRREQ);
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
comenzarProcesoRuteDescoveryEntradaExistente (std::string destino , std::string origen,
const AodvUgdTableEntery& unaEntrada)
throw()
{
/*se supone que antes de comenzar el descubrimiento se busco una ruta activa*/
/*TODO existe un descubrimiento iniciado con el mismo destino?*/
if (existeDescubrimientoIniciado (destino) )
/*TODO ver si se mueve algun evento*/
return;
/*verificar si se supera RREQ_RATELIMIT, la cantidad de RREQ enviado en un seg*/
/*A node SHOULD NOT originate more than RREQ_RATELIMIT RREQ messages
per second.*/
bool alcanzoRREQ_RATELIMIT = verificarRateLimit();
/*un evento que cada segundo renicie el contador..*/
if ( alcanzoRREQ_RATELIMIT )
return;
/*The Originator Sequence Number in the RREQ message is the
node's own sequence number, which is incremented prior to insertion
in a RREQ.*/
procesoAodv_->aumentarContadorNodoSeqNum() ;
unsigned int nodoSeqNum = procesoAodv_-> getContadorNodoSeqNum();
/*The RREQ ID field is incremented by one from the last
RREQ ID used by the current node.*/
contadorRREQ_ID_++;
//Guarda en el buffer rreqID+origen (su propia direccion)
/*Before broadcasting the RREQ, the originating node buffers the RREQ
ID and the Originator IP address (its own address) of the RREQ for
PATH_DISCOVERY_TIME. In this way, when the node receives the packet
again from its neighbors, it will not reprocess and re-forward the
packet*/
procesoAodv_->agregarRREQ_Buffer ( contadorRREQ_ID_ , origen);
/*TODO RREQ_sent_in_a_second+1 ver con un observe o verificando la variable nomas,
un observe que asigne 0 cada 1 segundo*/
//TODO solo cambia aca de route descovery se puede factorizar colocando en funciones
//el resto de las funciones
AodvUgdRREQ *UnPrimerRREQ =
armarPrimerRREQRutaExistente( destino , origen ,
unaEntrada.getDestSequenceNumber()/*rutaDestSequNumb*/ ,
nodoSeqNum /*origenSequNumb*/ ,
unaEntrada.getValidDestSeqNum() /*rutaValidDestSeqNum*/,
unaEntrada.getHops() /*rutaDestHops*/ ,
contadorRREQ_ID_ /*RREQ_ID*/ );
/*TODO si RREQ_sent_in_a_second> se puede enviar*/
//en el primer RREQ no hay que verificar TTL_THRESHOLD
/*If the RREQ times out
without a corresponding RREP, the originator broadcasts the RREQ
again with the TTL incremented by TTL_INCREMENT. This continues
until the TTL set in the RREQ reaches TTL_THRESHOLD, beyond which a
TTL = NET_DIAMETER is used for each attempt.*/
double tiempoDeEspera = unaConfiguracionDiscovery_.calcularRING_TRAVERSAL_TIME
( UnPrimerRREQ->getTtl() );
//calcularTiempoEsperaERS; no se usa en el primero
/*programar evento: esperar TIMEOUT*/
shawn::EventScheduler::EventHandle pEventHandle = generarEventoTiempoEspera (
UnPrimerRREQ->getTtl() /*ultimoTtl*/ ,
tiempoDeEspera ,
0 /*contadorReintentos*/ , destino );
//guardar info del descubrimiento para el proximo reintento
guardarPrimeraInfoDiscoveryRoute (*UnPrimerRREQ/*pUltimoRREQ*/ ,
pEventHandle ,
TipoLocalRepair::None );
//observo la ruta para saber cuando esta activa, sea porque me responden el RREP
//o porque recibo un mensaje que actualiza la tabla.
observarUnaRuta ( destino , "active" /*estado*/ );
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Busqueda: Comienzo TTL: "<<UnPrimerRREQ->getTtl()<< " Timeout: "<< tiempoDeEspera;
LoggerAodv::Instance()->logCsvPingDetalle( procesoAodv_->owner().label() ,
UnPrimerRREQ->getOrigen() , UnPrimerRREQ->getDestino() ,
"Discovery", mensaje.str() );
//**************** FIN LOG ****************
//sumo el contador global de RREQ enviados cada vez que llamo sendDiscoveryRREQ
sendDiscoveryRREQ (UnPrimerRREQ);
}
// ----------------------------------------------------------------------
AodvUgdRREQ*
AodvUgdRouteDiscovery::
armarPrimerRREQRutaExistente( std::string dest , std::string origen ,
unsigned int rutaDestSequNumb , unsigned int origenSequNumb ,
bool rutaValidDestSeqNum , unsigned int rutaDestHops ,
unsigned int RREQ_ID )
throw()
{
unsigned int ttlInicial = rutaDestHops + unaConfiguracionDiscovery_.getTTL_INCREMENT();
/*The Destination Sequence Number field in the RREQ message is the last
known destination sequence number and is copied
from the Destination Sequence Number field in the routing table.*/
unsigned int RREQ_DestSeqNum = 0 ;
if ( rutaValidDestSeqNum )
RREQ_DestSeqNum = rutaDestSequNumb ;
return new AodvUgdRREQ
( dest , origen , RREQ_DestSeqNum ,
origenSequNumb , false /*joinFlag*/ , false /*repairFlag*/,
unaConfiguracionDiscovery_.getUseGratuitousRREP() /*rrep_gFlag*/ ,
unaConfiguracionDiscovery_.getUseDestOnly() /*destOnlyFlag*/ ,
! rutaValidDestSeqNum /*UnknownSequNum*/ ,
0 /*hops*/ , RREQ_ID ,
procesoAodv_-> owner().label() /*ipOrigen*/ , ttlInicial );
}
// ----------------------------------------------------------------------
bool
AodvUgdRouteDiscovery::
verificarRateLimit()
throw()
{
if (contadorRREQ_sent_seg_ > unaConfiguracionDiscovery_.getRREQ_RATELIMIT() )
return true;
return false;
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
comenzarProcesoRuteDescoveryLocalRepair(std::string destino , std::string origen,
AodvUgdTableEntery *unaEntrada ,
unsigned int ttlInicialLocalRepair ,
const TipoLocalRepair &unTipoLocalRepair )
throw()
{
/*se supone que antes de comenzar el descubrimiento se busco una ruta activa*/
/*TODO existe un descubrimiento iniciado con el mismo destino?*/
if (existeDescubrimientoIniciado (destino) )
/*TODO ver si se mueve algun evento*/
return;
/*verificar si se supera RREQ_RATELIMIT, la cantidad de RREQ enviado en un seg*/
/*A node SHOULD NOT originate more than RREQ_RATELIMIT RREQ messages
per second.*/
bool alcanzoRREQ_RATELIMIT = verificarRateLimit();
/* un evento que cada segundo renicie el contador..*/
if ( alcanzoRREQ_RATELIMIT )
return;
/*The Originator Sequence Number in the RREQ message is the
node's own sequence number, which is incremented prior to insertion
in a RREQ.*/
procesoAodv_->aumentarContadorNodoSeqNum() ;
unsigned int nodoSeqNum = procesoAodv_-> getContadorNodoSeqNum();
/*The RREQ ID field is incremented by one from the last
RREQ ID used by the current node.*/
contadorRREQ_ID_++;
//Guarda en el buffer rreqID+origen (su propia direccion)
/*Before broadcasting the RREQ, the originating node buffers the RREQ
ID and the Originator IP address (its own address) of the RREQ for
PATH_DISCOVERY_TIME. In this way, when the node receives the packet
again from its neighbors, it will not reprocess and re-forward the
packet*/
procesoAodv_->agregarRREQ_Buffer ( contadorRREQ_ID_ , origen);
/*TODO RREQ_sent_in_a_second+1 ver con un observe o verificando la variable nomas,
un observe que asigne 0 cada 1 segundo*/
/*To repair the link
break, the node increments the sequence number for the destination
and then broadcasts a RREQ for that destination.*/
unsigned int nuevoSeqNumRuta = unaEntrada->getDestSequenceNumber() + 1;
unaEntrada->setDestSequenceNumber ( nuevoSeqNumRuta );
AodvUgdRREQ *UnPrimerRREQ=
armarPrimerRREQRutaExistenteLocalRepair
( destino , origen ,
unaEntrada -> getDestSequenceNumber() /*rutaDestSequNumb*/ ,
nodoSeqNum /*origenSequNumb*/ ,
unaEntrada -> getValidDestSeqNum() /*rutaValidDestSeqNum*/,
unaEntrada -> getHops() /*rutaDestHops*/ ,
contadorRREQ_ID_/*RREQ_ID*/ , ttlInicialLocalRepair );
/*TODO si RREQ_sent_in_a_second> se puede enviar*/
//en el primer RREQ no hay que verificar TTL_THRESHOLD
/*If the RREQ times out
without a corresponding RREP, the originator broadcasts the RREQ
again with the TTL incremented by TTL_INCREMENT. This continues
until the TTL set in the RREQ reaches TTL_THRESHOLD, beyond which a
TTL = NET_DIAMETER is used for each attempt.*/
double tiempoDeEspera = unaConfiguracionDiscovery_.calcularRING_TRAVERSAL_TIME
( UnPrimerRREQ->getTtl() );
//calcularTiempoEsperaERS; no se usa en el primero
/*programar evento: esperar TIMEOUT*/
shawn::EventScheduler::EventHandle pEventHandle = generarEventoTiempoEspera (
UnPrimerRREQ->getTtl() /*ultimoTtl*/ ,
tiempoDeEspera ,
0 /*contadorReintentos*/ , destino );
//guardar info del descubrimiento para el proximo reintento
guardarPrimeraInfoDiscoveryRoute (*UnPrimerRREQ/*pUltimoRREQ*/ ,
pEventHandle ,
unTipoLocalRepair );
//observo la ruta para saber cuando esta activa, sea porque me responden el RREP
//o porque recibo un mensaje que actualiza la tabla.
observarUnaRuta ( destino , "active" /*estado*/ );
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Busqueda: Nueva Repair! TTL: "<<UnPrimerRREQ->getTtl()<< " Timeout: "<< tiempoDeEspera;
LoggerAodv::Instance()->logCsvPingDetalle( procesoAodv_->owner().label() ,
UnPrimerRREQ->getOrigen() , UnPrimerRREQ->getDestino() ,
"Discovery", mensaje.str() );
//**************** FIN LOG ****************
//sumo el contador global de RREQ enviados cada vez que llamo sendDiscoveryRREQ
sendDiscoveryRREQ (UnPrimerRREQ);
}
// ----------------------------------------------------------------------
AodvUgdRREQ*
AodvUgdRouteDiscovery::
armarPrimerRREQRutaExistenteLocalRepair( std::string dest , std::string origen ,
unsigned int rutaDestSequNumb , unsigned int origenSequNumb ,
bool rutaValidDestSeqNum , unsigned int rutaDestHops ,
unsigned int RREQ_ID , unsigned int ttlInicial )
throw()
{
/* To repair the link
break, the node increments the sequence number for the destination
and then broadcasts a RREQ for that destination. The TTL of the RREQ
should initially be set to the following value:
max(MIN_REPAIR_TTL, 0.5 * #hops) + LOCAL_ADD_TTL,*/
/*The Destination Sequence Number field in the RREQ message is the last
known destination sequence number and is copied
from the Destination Sequence Number field in the routing table.*/
unsigned int RREQ_DestSeqNum = 0 ;
if ( rutaValidDestSeqNum )
RREQ_DestSeqNum = rutaDestSequNumb ;
return new AodvUgdRREQ
( dest , origen , RREQ_DestSeqNum ,
origenSequNumb , false /*joinFlag*/ , true /*repairFlag*/ ,
unaConfiguracionDiscovery_.getUseGratuitousRREP() /*rrep_gFlag*/ ,
unaConfiguracionDiscovery_.getUseDestOnly() /*destOnlyFlag*/ ,
!rutaValidDestSeqNum /*UnknownSequNum*/ ,
0 /*hops*/ , RREQ_ID ,
procesoAodv_->owner().label() /*ipOrigen*/ , ttlInicial );
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
generarEventoResetContadorRREQ ( ) throw()
{
double tiempoDeEspera = 1000; //cada 1000 milisegundos
shawn::EventScheduler& pEventScheduler = procesoAodv_->owner_w().world_w().scheduler_w();
DiscoveryEventResetRREQ_limit* unDiscoveryEventResetRREQ_limit=new DiscoveryEventResetRREQ_limit
( procesoAodv_->owner().current_time() /*ultimoReset*/ );
double tiempoEvento=tiempoDeEspera + procesoAodv_->owner().current_time();
shawn::EventScheduler::EventHandle pEventHandle = pEventScheduler.new_event
( *this , tiempoEvento ,
unDiscoveryEventResetRREQ_limit );
}
// ----------------------------------------------------------------------
void
AodvUgdRouteDiscovery::
handleEventoResetContadorRREQ ( ) throw()
{
contadorRREQ_sent_seg_ = 0;
generarEventoResetContadorRREQ();
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
bool
AodvUgdRouteDiscovery::
reintentarProcesoRuteDescovery
( std::string destino )
throw()
{
assert ( existeDescubrimientoIniciado (destino) );
/*verificar si se supera RREQ_RATELIMIT, la cantidad de RREQ enviado en un seg*/
/*A node SHOULD NOT originate more than RREQ_RATELIMIT RREQ messages
per second.*/
bool alcanzoRREQ_RATELIMIT = verificarRateLimit();
/*un evento que cada segundo renicie el contador..*/
if ( alcanzoRREQ_RATELIMIT )
return false;
unsigned int reintentos = obtenerRREQ_ReintentosSentDestino ( destino );
//const AodvUgdRREQ* pRREQ_anterrior = obtenerUltimoRREQ_Destino ( destino );
unsigned int maxReintentos = unaConfiguracionDiscovery_.getRREQ_RETRIES();
unsigned int maxTTL = unaConfiguracionDiscovery_.getTTL_THRESHOLD();
unsigned int ttlincrement = unaConfiguracionDiscovery_.getTTL_INCREMENT();
/*the originator broadcasts the RREQ
again with the TTL incremented by TTL_INCREMENT. This continues
until the TTL set in the RREQ reaches TTL_THRESHOLD, beyond which a
TTL = NET_DIAMETER is used for each attempt. Each time, the timeout
for receiving a RREP is RING_TRAVERSAL_TIME*/
unsigned int newTTL = 0;
unsigned int oldTTL = obtenerTtlAnterrior(destino);
//se puede simplificar mucho el if pero no quedan claras las condiciones
if(maxReintentos>0) //se admiten reintentos
{
if ( (0 < reintentos) && (reintentos < maxReintentos) ){//comenzo los reintentos y todavia se pueden hacer mas
reintentos++;
newTTL = unaConfiguracionDiscovery_.getNET_DIAMETER ();}
if ( (0 < reintentos) && (reintentos >= maxReintentos) ){ //comenzo los reintentos, pero se alcanzo el maxReintentos
std::cout<< "se alcanzo maxReintentos: "<< maxReintentos
<<" contReintento: "<< reintentos <<std::endl;
return false;}
if ( reintentos==0 && //no comenzo el proceso de reintentos
oldTTL+ttlincrement < maxTTL ) //pero no hace falta enviar el 1er reintento
newTTL = oldTTL + ttlincrement;
if ( reintentos==0 && //no comenzo el proceso de reintentos
oldTTL + ttlincrement >= maxTTL ) { //se realiza el 1er reintento
reintentos++;
newTTL = unaConfiguracionDiscovery_.getNET_DIAMETER ();}
}else //no se admiten reintentos maxReintentos == 0
{
if( oldTTL+ttlincrement < maxTTL )
newTTL = oldTTL + ttlincrement;
else{
std::cout<< "se alcanzo maxTTl: "<< maxTTL
<<" y no se admiten reintentos: "<< reintentos <<std::endl;
return false; }
}
/*Each new attempt MUST increment and update the RREQ ID.*/
contadorRREQ_ID_++;
procesoAodv_->agregarRREQ_Buffer ( contadorRREQ_ID_ , procesoAodv_->owner().label() );
AodvUgdRREQ *pRREQ_Nuevo = armarRREQ_reintento( destino , newTTL );
/*el valor de RING_TRAVERSAL_TIME depende del ttlActual y se tiene que calcular*/
double tiempoDeEspera = unaConfiguracionDiscovery_.
calcularRING_TRAVERSAL_TIME ( newTTL );
/*programar evento: esperar TIMEOUT*/
shawn::EventScheduler::EventHandle pEventHandle = generarEventoTiempoEspera (
newTTL /*ultimoTtl*/ , tiempoDeEspera ,
reintentos /*contadorReintentos*/ , destino );
//guardar info del descubrimiento para el proximo reintento
guardarReintentoInfoDiscoveryRoute (*pRREQ_Nuevo/*pUltimoRREQ*/ ,
pEventHandle ,
reintentos /*contadorReintentos*/ );
LoggerAodv::Instance()->logCsvRREQ (*pRREQ_Nuevo, procesoAodv_->owner().label() ,
"reintentarProcesoRuteDescovery" );
LoggerAodv::Instance()->logCsvRREQ_Mensajes (*pRREQ_Nuevo,
"reintentarProcesoRuteDescovery" );
//**************** LOG *****************
std::ostringstream mensaje;
mensaje <<"Busqueda: Reintento TTL: "<<pRREQ_Nuevo->getTtl()<< " Timeout: "<< tiempoDeEspera;
LoggerAodv::Instance()->logCsvPingDetalle( procesoAodv_->owner().label() ,
pRREQ_Nuevo->getOrigen() , pRREQ_Nuevo->getDestino() ,
"Discovery", mensaje.str() );
LoggerAodv::Instance()->logCsvRoutingTable_comoTabla(procesoAodv_->owner().label() ,
procesoAodv_->unaTablaRuteo->getRoutingTableMap() );
//**************** FIN LOG ****************
//sumo el contador global de RREQ enviados cada vez que llamo sendDiscoveryRREQ
sendDiscoveryRREQ (pRREQ_Nuevo);
return true; //si se reaalizo el reintento aviso..
}
// ----------------------------------------------------------------------
/* void
AodvUgdRouteDiscovery::
calcularTiempoEsperaERS( unsigned int rreqTtl , std::string destino )
throw()
{
double tiempo_RREQ_espera_un_RREP
if( rreqTtl < TTL_THRESHOLD)
{
tiempo_RREQ_espera_un_RREP =
unaConfiguracionDiscovery.getRING_TRAVERSAL_TIME();
}
else
{
tiempo_RREQ_espera_un_RREP =
unaConfiguracionDiscovery.getNET_TRAVERSAL_TIME();
}
return = Tiempo_RREQ_espera_un_RREP;
}*/
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// clase DiscoveryEventTag----------------------------------------------------------------------
/*use como plantilla class RoutingEventTag*/
//constructor
DiscoveryEventTag::
DiscoveryEventTag( std::string destino, unsigned int ultimoTtl,
unsigned int reintento )
//inicializacion
:
destino_ {destino},
ultimoTtl_ {ultimoTtl},
reintento_ {reintento}
{}
//----------------------------------------------------------------------
//destructor
DiscoveryEventTag::
~DiscoveryEventTag(){}
// get set----------------------------------------------------------------------
/*unsigned int AodvUgdRREQ:: getHops ( void ) const throw()
{ return hops_;}
// ----------------------------------------------------------------------*/
// ----------------------------------------------------------------------
inline std::string
DiscoveryEventTag::
getDestino (void) const throw()
{
return destino_ ;
}
// ----------------------------------------------------------------------
// metods----------------------------------------------------------------------
std::string
DiscoveryEventTag::
toString(void) const
throw()
{
{
std::stringstream ss;
ss <<
"destino: " << destino_ <<' '<<
"ultimoTtl: " << ultimoTtl_ <<' '<<
"reintento: " << reintento_ <<' ';
return (ss.str());
}
}
// ----------------------------------------------------------------------
void
DiscoveryEventTag::
show (void) const throw()
{
std::cout << DiscoveryEventTag::toString()<< '\n';
}
// ----------------------------------------------------------------------
// clase DiscoveryEventResetRREQ_limit----------------------------------------------------------------------