Configured Variable Validation and LSDB Refreshing
diff --git a/macbook.conf b/macbook.conf
index b4d4791..da4dfe1 100644
--- a/macbook.conf
+++ b/macbook.conf
@@ -1,5 +1,5 @@
 router-name /ndn/memphis.edu/netlab/macbook
-ccnneighbor /ndn/memphis.edu/netlab/pollux face37 10
+ccnneighbor /ndn/memphis.edu/netlab/pollux face9 10
 ccnneighbor /ndn/memphis.edu/netlab/maia face35 15
 ccnname /ndn/memphis.edu/netlab/macbook/name1
 ccnname /ndn/memphis.edu/netlab/macbook/test
diff --git a/nlsr.c b/nlsr.c
index b2b7eb9..70a99a6 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -34,6 +34,7 @@
 {           \
     if ((resval) < 0) { \
         nlsr_destroy(); \
+	exit(1);\
     } \
 }
 
@@ -41,7 +42,7 @@
 #define ON_ERROR_EXIT(resval) \
 {           \
     if ((resval) < 0) { \
-        exit(-1); \
+        exit(1); \
     } \
 }
 
@@ -81,12 +82,24 @@
     NULL
 };
 
+void
+nlsr_lock(void)
+{
+	nlsr->semaphor=NLSR_LOCKED;
+}
+
+void
+nlsr_unlock(void)
+{
+	nlsr->semaphor=NLSR_UNLOCKED;
+}
 
 void 
 nlsr_stop_signal_handler(int sig)
 {
 	signal(sig, SIG_IGN);
- 	nlsr_destroy();	
+ 	nlsr_destroy();
+	exit(0);	
 }
 
 void 
@@ -220,7 +233,10 @@
 	}
 
 	seconds=atoi(secs);
-	nlsr->lsdb_synch_interval=seconds;
+	if ( seconds >= 120 && seconds <= 3600 )
+	{
+		nlsr->lsdb_synch_interval=seconds;
+	}
 
 }
 
@@ -235,18 +251,21 @@
 	}
 	char *rem;
 	const char *sep=" \t\n";
-	char *secs;
-	long int seconds;
+	char *retry;
+	long int retry_number;
 	
-	secs=strtok_r(command,sep,&rem);
-	if(secs==NULL)
+	retry=strtok_r(command,sep,&rem);
+	if(retry==NULL)
 	{
 		printf(" Wrong Command Format ( interest-retry number)\n");
 		return;
 	}
 
-	seconds=atoi(secs);
-	nlsr->interest_retry=seconds;
+	retry_number=atoi(retry);
+	if ( retry_number >= 1 && retry_number<=10 )
+	{
+		nlsr->interest_retry=retry_number;
+	}
 
 }
 
@@ -271,8 +290,10 @@
 	}
 
 	seconds=atoi(secs);
-	nlsr->interest_resend_time=seconds;
-
+	if ( seconds <= 60 && seconds >= 1 )
+	{
+		nlsr->interest_resend_time=seconds;
+	}
 }
 
 
@@ -459,7 +480,7 @@
 	nlsr->interest_retry = INTEREST_RETRY;
 	nlsr->interest_resend_time = INTEREST_RESEND_TIME;
 
-	nlsr->semaphor=0;
+	nlsr->semaphor=NLSR_UNLOCKED;
 
 	return 0;
 }
@@ -470,7 +491,7 @@
 {
     	int res, ret;
     	char *config_file;
-	int daemon_mode;
+	//int daemon_mode;
 
 	ret=init_nlsr();	
     	ON_ERROR_EXIT(ret);
@@ -480,7 +501,7 @@
         	switch (res) 
 		{
 			case 'd':
-				daemon_mode = 1;
+				//daemon_mode = 1;
 				break;
 			case 'f':
 				config_file = optarg;
@@ -528,20 +549,24 @@
 
 	nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
 	nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
+	nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
 
 	while(1)
 	{	
-		if( nlsr->sched != NULL )
+		if ( nlsr->semaphor == NLSR_UNLOCKED  )
 		{
-			ccn_schedule_run(nlsr->sched);
-		}
-		if(nlsr->ccn != NULL)
-		{
-        		res = ccn_run(nlsr->ccn, 500);
-		}
-		if (!(nlsr->sched && nlsr->ccn))
-		{	      
-			break;
+			if( nlsr->sched != NULL )
+			{
+				ccn_schedule_run(nlsr->sched);
+			}
+			if(nlsr->ccn != NULL)
+			{
+        			res = ccn_run(nlsr->ccn, 500);
+			}
+			if (!(nlsr->sched && nlsr->ccn))
+			{	      
+				break;
+			}
 		}
 
 	}
diff --git a/nlsr.h b/nlsr.h
index 1bfc5b6..95087d3 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -5,6 +5,8 @@
 #define LSDB_SYNCH_INTERVAL 300
 #define INTEREST_RETRY 3
 #define INTEREST_RESEND_TIME 15
+#define NLSR_LOCKED 1
+#define NLSR_UNLOCKED 0
 
 struct name_prefix
 {
@@ -79,6 +81,9 @@
 void process_conf_command(char *command);
 int readConfigFile(const char *filename);
 
+void nlsr_lock(void);
+void nlsr_unlock(void);
+
 int init_nlsr(void);
 void nlsr_destroy( void );
 void nlsr_stop_signal_handler(int sig);
diff --git a/nlsr_lsdb.c b/nlsr_lsdb.c
index f271e50..f80a2b7 100644
--- a/nlsr_lsdb.c
+++ b/nlsr_lsdb.c
@@ -137,92 +137,186 @@
 void 
 install_name_lsa(struct nlsa *name_lsa)
 {
-	
-	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 *time_stamp=(char *)malloc(20);
+	memset(time_stamp,0,20);
+	get_current_timestamp_micro(time_stamp);
+	long int lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
+
+	printf("time difference: %ld \n",lsa_life_time);
+	
+
+		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);
+		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));
+		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));
 
-	struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
+		struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
 
-	struct hashtb_enumerator ee;
-    	struct hashtb_enumerator *e = &ee; 	
-    	int res;
+		struct hashtb_enumerator ee;
+    		struct hashtb_enumerator *e = &ee; 	
+    		int res;
 
-   	hashtb_start(nlsr->lsdb->name_lsdb, e);
-    	res = hashtb_seek(e, key, strlen(key), 0);
+   		hashtb_start(nlsr->lsdb->name_lsdb, e);
+    		res = hashtb_seek(e, key, strlen(key), 0);
 
-	if(res == HT_NEW_ENTRY )
-	{
-		printf("New Name LSA... Adding to LSDB\n");
-		new_name_lsa = e->data;
-
-		new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
-		new_name_lsa->header->ls_type=name_lsa->header->ls_type;
-
-		new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
-		memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
-		memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
-
-		new_name_lsa->header->ls_id=name_lsa->header->ls_id;
-		new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
-		new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
-		memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
-		new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
-		new_name_lsa->header->isValid=name_lsa->header->isValid;
-
-	
-		new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
-		new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
-		memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
-		new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
-
-		printf("New Name LSA Added....\n");	
-
-		printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
-		set_new_lsdb_version();	
-		printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);	
-
-		int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
-		if ( next_hop == NO_NEXT_HOP )
+		if(res == HT_NEW_ENTRY )
 		{
-			int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
-			if ( check == HT_NEW_ENTRY )
+				
+	
+		
+		
+			printf("New Name LSA... Adding to LSDB\n");
+			new_name_lsa = e->data;
+
+			new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
+			new_name_lsa->header->ls_type=name_lsa->header->ls_type;
+
+			new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
+			memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
+			memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
+
+			new_name_lsa->header->ls_id=name_lsa->header->ls_id;
+			new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
+			new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
+			memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
+			new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
+			new_name_lsa->header->isValid=name_lsa->header->isValid;
+
+	
+			new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
+			new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
+			memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
+			new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
+
+			printf("New Name LSA Added....\n");	
+
+			printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+			set_new_lsdb_version();	
+			printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);	
+
+			int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
+			if ( next_hop == NO_NEXT_HOP )
 			{
-				printf("Added in npt \n");
+				int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
+				if ( check == HT_NEW_ENTRY )
+				{
+					printf("Added in npt \n");
+				}
 			}
-		}
-		else 
-		{
-			int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
-			if ( check == HT_NEW_ENTRY )
+			else 
 			{
-				printf("Added in npt \n");
+				int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
+				if ( check == HT_NEW_ENTRY )
+				{
+					printf("Added in npt \n");
+				}
+
 			}
 
+		
+		free(time_stamp);
+
+		}
+		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)
+			{
+				printf("Older/Duplicate Adj LSA. Discarded...\n");
+			}
+			else 
+			{
+				if ( name_lsa->header->isValid == 0 )
+				{
+					// have to call to delete npt table entry
+					delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
+				
+					if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
+					{
+						hashtb_delete(e);
+						printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
+					}
+					else 
+					{
+						new_name_lsa->header->isValid=name_lsa->header->isValid;
+						free(new_name_lsa->header->orig_time);
+						new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
+						memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
+						memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
+					}
+					printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+					set_new_lsdb_version();	
+					printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+				}
+				else
+				{
+					int is_npt_update=0;
+					if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
+					{
+						is_npt_update=1;
+						delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
+					}
+
+					// copying LSA content with header
+
+					free(new_name_lsa->header->orig_time);
+					new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
+					memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
+					memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
+				
+					new_name_lsa->header->isValid=name_lsa->header->isValid;
+
+					free(new_name_lsa->name_prefix->name);
+					free(new_name_lsa->name_prefix);
+					new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+					new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
+					memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
+					new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
+
+					printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+					set_new_lsdb_version();	
+					printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+
+					if( is_npt_update == 1 )
+					{
+						int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
+						if ( next_hop == NO_NEXT_HOP )
+						{
+							int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
+							if ( check == HT_NEW_ENTRY )
+							{
+								printf("Added in npt \n");
+							}
+						}
+						else 
+						{
+							int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
+							if ( check == HT_NEW_ENTRY )
+							{
+								printf("Added in npt \n");
+							}
+						}
+					}
+				}
+			}
+		
 		}
 
-	}
-	else if(res == HT_OLD_ENTRY)
-	{
-		printf("Duplicate Name LSA. Discarded...\n");
+    		hashtb_end(e);
 
-	}
-
-    	hashtb_end(e);
-
-	free(key);
+		free(key);
 }
 
 
@@ -320,6 +414,8 @@
 }
 
 
+
+
 void 
 make_adj_lsa_key(char *key,struct alsa *adj_lsa)
 {
@@ -334,6 +430,13 @@
 int
 build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
+	if(flags == CCN_SCHEDULE_CANCEL)
+	{
+ 	 	return -1;
+	}
+
+	nlsr_lock();
+
 	printf("build_and_install_adj_lsa called \n");
 
 	printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
@@ -362,6 +465,9 @@
 		}
 	}
 	nlsr->is_build_adj_lsa_sheduled=0;
+
+	nlsr_unlock();
+
 	return 0;
 }
 
@@ -397,40 +503,6 @@
 	/* Filling Up Body Data */
 
 	adj_lsa->no_link=no_link;
-/*
-	struct link *templ=(struct link *)malloc(2*sizeof(struct link));
-	adj_lsa->links=templ;
-
-	int i, adl_element;
-	struct ndn_neighbor *nbr;
-	struct hashtb_enumerator ee;
-    	struct hashtb_enumerator *e = &ee;
-    	hashtb_start(nlsr->adl, e);
-	adl_element=hashtb_n(nlsr->adl);
-
-	for(i=0;i<adl_element;i++)
-	{
-		nbr=e->data;
-		if( nbr->status	== 1 )
-		{
-			struct link *temp=(struct link *)malloc(sizeof(struct link));
-			temp->nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix));
-			temp->nbr->name=(char *)malloc(nbr->neighbor->length);
-			memset(temp->nbr->name,0,nbr->neighbor->length);		
-			memcpy(temp->nbr->name,nbr->neighbor->name,nbr->neighbor->length);
-
-			temp->nbr->length=nbr->neighbor->length;
-			temp->face=nbr->face;
-			temp->metric=nbr->metric;
-
-			templ=temp;
-			templ++;
-		}	
-		hashtb_next(e);		
-	}
-
-	hashtb_end(e);
-*/
 
 
 	struct ccn_charbuf *c=ccn_charbuf_create();
@@ -461,68 +533,48 @@
 {
 	printf("install_adj_lsa called \n");
 
-	char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
-	memset(key,0,adj_lsa->header->orig_router->length+2);
-	make_adj_lsa_key(key,adj_lsa);
-	printf("Adjacent LSA key: %s \n",key);
 
-	struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
+	char *time_stamp=(char *)malloc(20);
+	memset(time_stamp,0,20);
+	get_current_timestamp_micro(time_stamp);
+	long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
 
-	struct hashtb_enumerator ee;
-    	struct hashtb_enumerator *e = &ee; 	
-    	int res;
+	printf("time difference: %ld \n",lsa_life_time);
 
-   	hashtb_start(nlsr->lsdb->adj_lsdb, e);
-    	res = hashtb_seek(e, key, strlen(key), 0);
+
+		char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
+		memset(key,0,adj_lsa->header->orig_router->length+2);
+		make_adj_lsa_key(key,adj_lsa);
+		printf("Adjacent LSA key: %s \n",key);
+
+		struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
+
+		struct hashtb_enumerator ee;
+    		struct hashtb_enumerator *e = &ee; 	
+    		int res;
+
+   		hashtb_start(nlsr->lsdb->adj_lsdb, e);
+    		res = hashtb_seek(e, key, strlen(key), 0);
 
 
 
-	if(res == HT_NEW_ENTRY )
-	{
-		printf("New ADJ LSA... Adding to LSDB\n");
-		new_adj_lsa = e->data;
-
-		new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
-		new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
-		new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
-		memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);		
-
-		new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
-		new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
-		memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
-		new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
-
-		new_adj_lsa->no_link=adj_lsa->no_link;
-		
-		new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
-		memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
-		memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
-
-		printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
-		set_new_lsdb_version();	
-		printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
-
-		add_next_hop_router(new_adj_lsa->header->orig_router->name);
-
-		add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
-	}
-	else if(res == HT_OLD_ENTRY)
-	{
-		new_adj_lsa = e->data;
-		if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
+		if(res == HT_NEW_ENTRY )
 		{
-			printf("Older/Duplicate Adj LSA. Discarded...\n");
-		}
-		else
-		{
+			printf("New ADJ LSA... Adding to LSDB\n");
 			new_adj_lsa = e->data;
 
-			free(new_adj_lsa->header->orig_time);
+			new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
+			new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
 			new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
-			memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
+			memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);		
+
+			new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+			new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
+			memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
+			new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
 
 			new_adj_lsa->no_link=adj_lsa->no_link;
-			
+		
 			new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
 			memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
 			memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
@@ -531,19 +583,52 @@
 			set_new_lsdb_version();	
 			printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
 
-			add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
+			add_next_hop_router(new_adj_lsa->header->orig_router->name);
 
+			add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
+		}
+		else if(res == HT_OLD_ENTRY)
+		{
+			new_adj_lsa = e->data;
+			if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
+			{
+				printf("Older/Duplicate Adj LSA. Discarded...\n");
+			}
+			else
+			{
+				new_adj_lsa = e->data;
+
+				free(new_adj_lsa->header->orig_time);
+				new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
+				memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
+
+				new_adj_lsa->no_link=adj_lsa->no_link;
+				
+				new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
+				memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
+				memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
+
+				printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+				set_new_lsdb_version();	
+				printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+
+				add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
+
+			}
+	
+		}
+	    	hashtb_end(e);
+
+		if ( !nlsr->is_route_calculation_scheduled )
+		{
+			nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
+			nlsr->is_route_calculation_scheduled=1;
 		}
 
-	}
-    	hashtb_end(e);
 
-	if ( !nlsr->is_route_calculation_scheduled )
-	{
-		nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
-		nlsr->is_route_calculation_scheduled=1;
-	}
-	free(key);
+		free(key);
+
+	free(time_stamp);
 }
 
 void 
@@ -1065,3 +1150,101 @@
 
 	hashtb_end(e);
 }
+
+void
+refresh_name_lsdb(void)
+{
+	printf("refresh_name_lsdb called \n");
+
+	char *time_stamp=(char *)malloc(20);
+	memset(time_stamp,0,20);
+	get_current_timestamp_micro(time_stamp);
+	
+	long int lsa_life_time;
+		
+	int i, name_lsdb_element;
+	struct nlsa *name_lsa;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->lsdb->name_lsdb, e);
+	name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
+
+	for(i=0;i<name_lsdb_element;i++)
+	{
+		name_lsa=e->data;
+
+		lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
+		printf("LSA Life Time: %ld \n",lsa_life_time);			
+
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+	
+	free(time_stamp);
+	
+}
+
+int
+refresh_adj_lsdb(void)
+{
+	printf("refresh_adj_lsdb called \n");
+
+	char *time_stamp=(char *)malloc(20);
+	memset(time_stamp,0,20);
+	get_current_timestamp_micro(time_stamp);
+	
+	long int lsa_life_time;
+		
+	int i, adj_lsdb_element;
+	struct alsa *adj_lsa;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->lsdb->adj_lsdb, e);
+	adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
+
+	for(i=0;i<adj_lsdb_element;i++)
+	{
+		adj_lsa=e->data;
+
+		lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
+		printf("LSA Life Time: %ld \n",lsa_life_time);			
+
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+	
+	free(time_stamp);
+
+	return 0;
+}
+
+int
+refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
+{
+	if(flags == CCN_SCHEDULE_CANCEL)
+	{
+ 	 	return -1;
+	}
+
+	nlsr_lock();
+	printf("refresh_lsdb called \n");
+	
+	refresh_name_lsdb();
+	refresh_adj_lsdb();
+
+	if ( !nlsr->is_route_calculation_scheduled )
+	{
+		nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
+		nlsr->is_route_calculation_scheduled=1;
+	}
+
+	nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
+	nlsr_unlock();
+	return 0;
+}
diff --git a/nlsr_lsdb.h b/nlsr_lsdb.h
index 87c4013..75f1886 100644
--- a/nlsr_lsdb.h
+++ b/nlsr_lsdb.h
@@ -54,6 +54,7 @@
 void build_and_install_others_name_lsa(char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np);
 void build_others_name_lsa(struct nlsa *name_lsa, char *orig_router,int ls_type,long int ls_id, char *orig_time, int isValid, char *np);
 void make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id);
+int refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags);
 
 int build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags);
 void build_adj_lsa(struct alsa * adj_lsa);
diff --git a/nlsr_ndn.c b/nlsr_ndn.c
index bd59459..19cf893 100644
--- a/nlsr_ndn.c
+++ b/nlsr_ndn.c
@@ -192,7 +192,9 @@
 incoming_interest(struct ccn_closure *selfp,
         enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
 {
-    
+
+    nlsr_lock();    
+
     switch (kind) {
         case CCN_UPCALL_FINAL:
             break;
@@ -213,6 +215,8 @@
             break;
     }
 
+     nlsr_unlock();
+
     return CCN_UPCALL_RESULT_OK;
 }
 
@@ -502,6 +506,7 @@
         enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
 {
 
+     nlsr_lock();
 
     switch(kind) {
         case CCN_UPCALL_FINAL:
@@ -531,8 +536,11 @@
 	    break;
         default:
             fprintf(stderr, "Unexpected response of kind %d\n", kind);
-            return CCN_UPCALL_RESULT_ERR;
+            //return CCN_UPCALL_RESULT_ERR;
+	    break;
     }
+    
+     nlsr_unlock();
 
     return CCN_UPCALL_RESULT_OK;
 }
@@ -896,6 +904,13 @@
 int
 send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
+	if(flags == CCN_SCHEDULE_CANCEL)
+	{
+ 	 	return -1;
+	}
+
+         nlsr_lock();
+
 	printf("send_info_interest called \n");
 	printf("\n");
 
@@ -916,6 +931,8 @@
 	}
 	hashtb_end(e);
 
+	 nlsr_unlock();
+
 	return 0;
 }
 
@@ -991,6 +1008,8 @@
  	 	return -1;
 	}
 
+	 nlsr_lock();
+
 	int i, adl_element;
 	struct ndn_neighbor *nbr;
 
@@ -1024,6 +1043,8 @@
 	hashtb_end(e);
 	nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 30000000, &send_lsdb_interest, NULL, 0);
 
+	 nlsr_unlock();
+
 	return 0;
 }
 
diff --git a/nlsr_npt.c b/nlsr_npt.c
index 9841da6..9fe4565 100644
--- a/nlsr_npt.c
+++ b/nlsr_npt.c
@@ -126,6 +126,59 @@
 	return res;
 }
 
+int 
+delete_npt_entry(char *orig_router, char *name_prefix)
+{
+	if ( strcmp(orig_router,nlsr->router_name)== 0)
+	{
+		return -1;
+	}
+
+	struct npt_entry *ne;
+	
+	int res,res_nle;
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    
+
+   	hashtb_start(nlsr->npt, e);
+    	res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
+
+	if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+		return -1;
+	}
+	else if (res == HT_OLD_ENTRY)
+	{
+		ne=e->data;
+
+		struct hashtb_enumerator eenle;
+    		struct hashtb_enumerator *enle = &eenle;
+
+		hashtb_start(ne->name_list, enle);
+		res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
+
+		if(res_nle == HT_NEW_ENTRY )
+		{
+			hashtb_delete(enle);
+		}
+		else if(res_nle == HT_OLD_ENTRY )
+		{
+			if (ne->next_hop_face != NO_FACE )
+			{
+				add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_UNREG, ne->next_hop_face);
+			}
+			hashtb_delete(enle);
+		}
+
+		hashtb_end(enle);
+	}
+	
+	hashtb_end(e);
+
+	return 0;
+}
 
 void 
 print_npt(void)
@@ -289,11 +342,7 @@
 
 	for(i=0;i<npt_element;i++)
 	{
-		//printf("\n");
-		//printf("----------NPT ENTRY %d------------------\n",i+1);
 		ne=e->data;
-		//printf(" Origination Router: %s \n",ne->orig_router);
-		//ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face);
 		
 		int j, nl_element;
 		struct name_list_entry *nle;		
@@ -310,7 +359,7 @@
 			{
 				add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
 			}
-			//printf(" Name Prefix: %s \n",nle->name);
+			//printf(" Name Prefix: %s Face: %d \n",nle->name,ne->next_hop_face);
 			hashtb_next(enle);
 		}
 		hashtb_end(enle);
diff --git a/nlsr_npt.h b/nlsr_npt.h
index d564312..f469f24 100644
--- a/nlsr_npt.h
+++ b/nlsr_npt.h
@@ -17,6 +17,7 @@
 
 
 int add_npt_entry(char *orig_router, char *name_prefix, int face);
+int delete_npt_entry(char *orig_router, char *name_prefix);
 void print_npt(void);
 void delete_orig_router_from_npt(char *orig_router,int next_hop_face);
 void update_npt_with_new_route(char * orig_router,int next_hop_face);
diff --git a/nlsr_route.c b/nlsr_route.c
index 60e9c76..85a9b03 100644
--- a/nlsr_route.c
+++ b/nlsr_route.c
@@ -28,6 +28,14 @@
 int
 route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
+
+	if(flags == CCN_SCHEDULE_CANCEL)
+	{
+ 	 	return -1;
+	}
+
+	nlsr_lock();
+
 	printf("route_calculate called\n");
 
 	if( ! nlsr->is_build_adj_lsa_sheduled )
@@ -69,6 +77,8 @@
 
 		update_routing_table_with_new_route(parent,source);
 
+		print_routing_table();
+		print_npt();
 
 		for(i = 0; i < map_element; i++)
 		{
@@ -82,6 +92,8 @@
 	}
 	nlsr->is_route_calculation_scheduled=0;
 
+	nlsr_unlock();
+
 	return 0;
 }
 
@@ -696,6 +708,32 @@
 }
 
 void 
+update_routing_table(char * dest_router,int next_hop_face)
+{
+	int res;
+	struct routing_table_entry *rte;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->routing_table, e);
+	res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
+
+	if( res == HT_OLD_ENTRY )
+	{
+		rte=e->data;
+		rte->next_hop_face=next_hop_face;
+	}
+	else if ( res == HT_OLD_ENTRY )
+	{
+		hashtb_delete(e);
+	}
+	
+	hashtb_end(e);
+	
+}
+
+void 
 print_routing_table(void)
 {
 	printf("\n");
@@ -789,7 +827,7 @@
 					//printf("Next hop router name: %s \n",next_hop_router);
 					int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
 					update_npt_with_new_route(orig_router,next_hop_face);
-
+					update_routing_table(orig_router,next_hop_face);
 					printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
 
 				}
diff --git a/utility.c b/utility.c
index 33f3b2e..49ad52c 100644
--- a/utility.c
+++ b/utility.c
@@ -65,11 +65,33 @@
 {
 	struct timeval now; 
 	gettimeofday(&now, NULL);
-
-	//char *microSec=(char *)malloc(20);
 	sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
-	//microSec[strlen(microSec)]='\0';
-	//return microSec;
+}
 
+
+long int
+get_time_diff(char *time1, char *time2)
+{
+	long int diff_secs;
+
+	long int time1_in_sec,	time2_in_sec;
+
+	char *time1_sec=(char *)malloc(strlen(time1)-6+1);
+	memset(time1_sec,0,strlen(time1)-6+1);
+	memcpy(time1_sec,time1,strlen(time1)-6);
+
+	char *time2_sec=(char *)malloc(strlen(time2)-6+1);
+	memset(time2_sec,0,strlen(time2)-6+1);
+	memcpy(time2_sec,time2,strlen(time2)-6);
+
+	time1_in_sec=strtol(time1_sec,NULL,10);
+	time2_in_sec=strtol(time2_sec,NULL,10);
+
+	diff_secs=time1_in_sec-time2_in_sec;
+
+	free(time1_sec);
+	free(time2_sec);
+
+	return diff_secs;
 }
 
diff --git a/utility.h b/utility.h
index fb699d6..75bf5ab 100644
--- a/utility.h
+++ b/utility.h
@@ -5,5 +5,6 @@
 char * getGmTimeStamp(void);
 long int get_current_time_sec(void);
 void get_current_timestamp_micro(char * microSec);
+long int get_time_diff(char *time1, char *time2);
 
 #endif