Randomization of LSDB Interest and isValid Check in Name LSA
diff --git a/nlsr.conf b/nlsr.conf
index eef6119..ced844f 100644
--- a/nlsr.conf
+++ b/nlsr.conf
@@ -1,6 +1,6 @@
 router-name /ndn/memphis.edu/netlab/macbook
-ccnneighbor /ndn/memphis.edu/netlab/pollux face2
-ccnneighbor /ndn/memphis.edu/netlab/castor face3
+ccnneighbor /ndn/memphis.edu/netlab/pollux face2 10
+ccnneighbor /ndn/memphis.edu/netlab/castor face3 8
 ccnname /ndn/memphis.edu/netlab/macbook/name1
 ccnname /ndn/memphis.edu/netlab/macbook/test
 lsdb-synch-interval 310
diff --git a/nlsr.h b/nlsr.h
index f935951..941e357 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -10,6 +10,11 @@
 #define LSA_REFRESH_TIME 600
 #define ROUTER_DEAD_INTERVAL 900
 
+#define LINK_METRIC 10
+
+#define NAME_LSA_VALID 1
+#define NAME_LSA_INVALID 0
+
 
 struct name_prefix
 {
diff --git a/nlsr_adl.c b/nlsr_adl.c
index a22ecde..25af57d 100644
--- a/nlsr_adl.c
+++ b/nlsr_adl.c
@@ -47,8 +47,10 @@
 		nbr->face=face;
 		nbr->status=NBR_DOWN;
 		nbr->info_interest_timed_out=0;
-		nbr->lsdb_synch_interval=300;
-		nbr->metric=10;
+		nbr->lsdb_interest_timed_out=0;
+		nbr->lsdb_random_time_component=(int)(LSDB_SYNCH_INTERVAL/2);
+		nbr->lsdb_synch_interval=LSDB_SYNCH_INTERVAL;
+		nbr->metric=LINK_METRIC;
 		nbr->is_lsdb_send_interest_scheduled=0;
 		
 
@@ -82,7 +84,9 @@
 	printf("	LSDB Version: %s \n",nbr->last_lsdb_version);
 	printf("	Info Version: %s \n",nbr->last_info_version);
 	printf("	Info Interest Timed Out : %d \n",nbr->info_interest_timed_out);
+	printf("	LSDB Interest Timed Out : %d \n",nbr->lsdb_interest_timed_out);
 	printf("	LSDB Synch Interval     : %ld \n",nbr->lsdb_synch_interval);
+	printf("	LSDB Random Time comp   : %d \n",nbr->lsdb_random_time_component);
 	printf("	Las Time LSDB Requested: %ld \n",nbr->last_lsdb_requested);
 	printf("	IS_lsdb_send_interest_scheduled : %d \n",nbr->is_lsdb_send_interest_scheduled);
 
@@ -175,6 +179,34 @@
 	return ret;	
 }
 
+int 
+get_lsdb_interest_timed_out_number(struct name_prefix *nbr)
+{
+	printf("get_timed_out_number called \n");
+
+	int res,ret=-1;
+	struct ndn_neighbor *nnbr;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = ⅇ
+
+	hashtb_start(nlsr->adl, e);
+	res = hashtb_seek(e, nbr->name, nbr->length, 0);
+
+	if( res == HT_OLD_ENTRY )
+	{
+		nnbr=e->data;
+		ret=nnbr->lsdb_interest_timed_out;
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+	}
+
+	hashtb_end(e);
+
+	return ret;	
+}
 
 void 
 update_adjacent_timed_out_to_adl(struct name_prefix *nbr, int increment)
@@ -212,6 +244,43 @@
 
 }
 
+
+void 
+update_lsdb_interest_timed_out_to_adl(struct name_prefix *nbr, int increment)
+{
+	printf("update_adjacent_timed_out_to_adl called \n");
+
+	int res;
+	struct ndn_neighbor *nnbr;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = ⅇ
+
+	hashtb_start(nlsr->adl, e);
+	res = hashtb_seek(e, nbr->name, nbr->length, 0);
+
+	if( res == HT_OLD_ENTRY )
+	{
+		nnbr=e->data;
+		nnbr->lsdb_interest_timed_out += increment;
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+	}
+
+	hashtb_end(e);
+}
+
+void 
+update_lsdb_interest_timed_out_zero_to_adl(struct name_prefix *nbr)
+{
+	printf("update_adjacent_timed_out_zero_to_adl called \n");
+	int time_out_number=get_lsdb_interest_timed_out_number(nbr);
+	update_lsdb_interest_timed_out_to_adl(nbr,-time_out_number);
+
+}
+
 void 
 update_adjacent_status_to_adl(struct name_prefix *nbr, int status)
 {
@@ -265,6 +334,7 @@
 		if ( nnbr->lsdb_synch_interval!= interval )
 		{
 			nnbr->lsdb_synch_interval=interval;
+			nnbr->lsdb_random_time_component=(int)(interval/2);
 
 		}
 	}
@@ -452,6 +522,38 @@
 	return last_lsdb_requested;
 }
 
+
+int 
+get_nbr_random_time_component(char *nbr)
+{
+	printf("get_timed_out_number called \n");
+
+	int time=0;
+
+	int res;
+	struct ndn_neighbor *nnbr;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = ⅇ
+
+	hashtb_start(nlsr->adl, e);
+	res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
+
+	if (res == HT_OLD_ENTRY)
+	{
+		nnbr=e->data;
+		time=nnbr->lsdb_random_time_component * nnbr->lsdb_interest_timed_out;
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+	}
+
+	hashtb_end(e);
+
+	return time;
+}
+
 long int 
 get_lsdb_synch_interval(char *nbr)
 {
diff --git a/nlsr_adl.h b/nlsr_adl.h
index f0b3c4e..ac6c066 100644
--- a/nlsr_adl.h
+++ b/nlsr_adl.h
@@ -12,6 +12,8 @@
 	char * last_lsdb_version;
 	char * last_info_version;
 	int info_interest_timed_out;
+	int lsdb_interest_timed_out;
+	int lsdb_random_time_component;
 	long int lsdb_synch_interval;
 	long int last_lsdb_requested;
 	int is_lsdb_send_interest_scheduled;
@@ -23,8 +25,11 @@
 void print_adjacent_from_adl(void);
 int get_adjacent_status(struct name_prefix *nbr);
 int get_timed_out_number(struct name_prefix *nbr);
+int get_lsdb_interest_timed_out_number(struct name_prefix *nbr);
 void update_adjacent_timed_out_to_adl(struct name_prefix *nbr, int increment);
 void update_adjacent_timed_out_zero_to_adl(struct name_prefix *nbr);
+void update_lsdb_interest_timed_out_to_adl(struct name_prefix *nbr, int increment);
+void update_lsdb_interest_timed_out_zero_to_adl(struct name_prefix *nbr);
 void update_adjacent_status_to_adl(struct name_prefix *nbr, int status);
 void update_lsdb_synch_interval_to_adl(struct name_prefix *nbr, long int interval);
 int no_active_nbr(void);
@@ -34,6 +39,7 @@
 long int get_nbr_last_lsdb_requested(char *nbr);
 long int get_nbr_last_lsdb_requested(char *nbr);
 long int get_lsdb_synch_interval(char *nbr);
+int get_nbr_random_time_component(char *nbr);
 char * get_nbr_lsdb_version(char *nbr);
 void update_adjacent_last_lsdb_requested_to_adl(char *nbr, long int timestamp);
 void set_is_lsdb_send_interest_scheduled_to_zero(char *nbr);
diff --git a/nlsr_lsdb.c b/nlsr_lsdb.c
index b145586..49b033f 100644
--- a/nlsr_lsdb.c
+++ b/nlsr_lsdb.c
@@ -173,9 +173,6 @@
 
 		if(res == HT_NEW_ENTRY )
 		{
-				
-	
-		
 		
 			printf("New Name LSA... Adding to LSDB\n");
 			new_name_lsa = e->data;
@@ -232,9 +229,13 @@
 		else if(res == HT_OLD_ENTRY)
 		{
 			new_name_lsa=e->data;
-			if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<=0)
+			if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
 			{
-				printf("Older/Duplicate Adj LSA. Discarded...\n");
+				printf("Older Adj LSA. Discarded...\n");
+			}
+			else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
+			{
+				printf("Duplicate Adj LSA. Discarded...\n");
 			}
 			else 
 			{
@@ -1283,17 +1284,40 @@
 		{
 			if ( lsa_life_time > nlsr->lsa_refresh_time )
 			{
-				printf("Own Name LSA need to be refrshed\n");
-				char *current_time_stamp=(char *)malloc(20);
-				memset(current_time_stamp,0,20);
-				get_current_timestamp_micro(current_time_stamp);
+				if ( name_lsa->header->isValid == NAME_LSA_VALID )					
+				{
+					printf("Own Name LSA need to be refrshed\n");
+					char *current_time_stamp=(char *)malloc(20);
+					memset(current_time_stamp,0,20);
+					get_current_timestamp_micro(current_time_stamp);
 
-				free(name_lsa->header->orig_time);
-				name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free 
-				memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
-				memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
+					free(name_lsa->header->orig_time);
+					name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free 
+					memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
+					memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
 	
-				free(current_time_stamp);
+					free(current_time_stamp);
+				}
+				else 
+				{
+					char lst[2];
+					memset(lst,0,2);
+					sprintf(lst,"%d",name_lsa->header->ls_type);	
+
+					char lsid[10];
+					memset(lsid,0,10);
+					sprintf(lsid,"%ld",name_lsa->header->ls_id);
+	
+	
+					char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
+					memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
+
+
+					make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);	
+					printf("Key:%s Length:%d\n",key,(int)strlen(key));
+				
+					nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
+				}
 
 				printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
 				set_new_lsdb_version();	
diff --git a/nlsr_ndn.c b/nlsr_ndn.c
index 19cf893..006692f 100644
--- a/nlsr_ndn.c
+++ b/nlsr_ndn.c
@@ -892,6 +892,30 @@
 process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
 	printf("process_incoming_timed_out_interest_lsdb called \n");
+
+	struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+	get_nbr(nbr,selfp,info);
+
+	printf("LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+
+	int interst_timed_out_num=get_lsdb_interest_timed_out_number(nbr);
+
+	if( interst_timed_out_num < nlsr->interest_retry )
+	{
+		update_lsdb_interest_timed_out_to_adl(nbr,1);
+	}
+	else
+	{
+		update_lsdb_interest_timed_out_zero_to_adl(nbr);
+		update_adjacent_status_to_adl(nbr,NBR_DOWN);
+		if(!nlsr->is_build_adj_lsa_sheduled)
+		{
+			nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
+			nlsr->is_build_adj_lsa_sheduled=1;		
+		}
+	}
+	free(nbr->name);
+	free(nbr);
 }
 
 void
@@ -1030,7 +1054,7 @@
 				long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);	
 				printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);		
 
-				if(time_diff>=get_lsdb_synch_interval(nbr->neighbor->name))
+				if( time_diff >= ( get_lsdb_synch_interval(nbr->neighbor->name) + get_nbr_random_time_component(nbr->neighbor->name) ) )
 				{
 					nbr->is_lsdb_send_interest_scheduled=1;
 					send_lsdb_interest_to_nbr(nbr->neighbor);
diff --git a/pollux.conf b/pollux.conf
index d6cbfbc..495378e 100644
--- a/pollux.conf
+++ b/pollux.conf
@@ -1,6 +1,6 @@
 router-name /ndn/memphis.edu/netlab/pollux
-ccnneighbor /ndn/memphis.edu/netlab/macbook face2
-ccnneighbor /ndn/memphis.edu/netlab/castor face3
+ccnneighbor /ndn/memphis.edu/netlab/macbook face2 10
+ccnneighbor /ndn/memphis.edu/netlab/castor face3 8
 ccnname /ndn/memphis.edu/netlab/pollux/name1
 ccnname /ndn/memphis.edu/netlab/pollux/test
 lsdb-synch-interval 320