Routing Table Calculation and Fib Manipulation Complete
diff --git a/macbook.conf b/macbook.conf
index 3ddbd23..b4d4791 100644
--- a/macbook.conf
+++ b/macbook.conf
@@ -1,6 +1,6 @@
 router-name /ndn/memphis.edu/netlab/macbook
-ccnneighbor /ndn/memphis.edu/netlab/pollux face2 10
-ccnneighbor /ndn/memphis.edu/netlab/maia face4 15
+ccnneighbor /ndn/memphis.edu/netlab/pollux face37 10
+ccnneighbor /ndn/memphis.edu/netlab/maia face35 15
 ccnname /ndn/memphis.edu/netlab/macbook/name1
 ccnname /ndn/memphis.edu/netlab/macbook/test
 lsdb-synch-interval 310
diff --git a/nlsr.c b/nlsr.c
index 935b686..c896fb3 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -340,7 +340,9 @@
 {
 
 	printf("Freeing Allocated Memory....\n");	
-	
+	/* Destroying all face created by nlsr in CCND */
+	destroy_all_face_by_nlsr();	
+
 	/* Destroying every hash table attached to each neighbor in ADL before destorying ADL */	
 	hashtb_destroy(&nlsr->npl);
 	hashtb_destroy(&nlsr->adl);	
diff --git a/nlsr.h b/nlsr.h
index 65500fa..1bfa8a1 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -42,6 +42,7 @@
 	struct hashtb *npl;
 	struct hashtb *pit_alsa;
 	struct hashtb *map;
+	struct hashtb *rev_map;
 	struct hashtb *npt;
 	struct hashtb *routing_table;
 
diff --git a/nlsr_adl.c b/nlsr_adl.c
index c04aeda..a22ecde 100644
--- a/nlsr_adl.c
+++ b/nlsr_adl.c
@@ -21,6 +21,7 @@
 #include "nlsr_npl.h"
 #include "nlsr_adl.h"
 #include "utility.h"
+#include "nlsr_npt.h"
 
 void 
 add_nbr_to_adl(struct name_prefix *new_nbr,int face)
@@ -632,4 +633,32 @@
 
 }
 
+int 
+get_next_hop_face_from_adl(char *nbr)
+{
+	int res;
+	int connecting_face=NO_FACE;
+	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;
+		connecting_face=nnbr->face;
+		
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+	}
+
+	hashtb_end(e);
+	return connecting_face;
+}
+
 
diff --git a/nlsr_adl.h b/nlsr_adl.h
index 64d19d6..f0b3c4e 100644
--- a/nlsr_adl.h
+++ b/nlsr_adl.h
@@ -39,4 +39,5 @@
 void set_is_lsdb_send_interest_scheduled_to_zero(char *nbr);
 void update_adjacent_lsdb_version_to_adl(struct name_prefix *nbr, char *version);
 void adjust_adjacent_last_lsdb_requested_to_adl(char *nbr, long int sec);
+int get_next_hop_face_from_adl(char *nbr);
 #endif
diff --git a/nlsr_npt.c b/nlsr_npt.c
index bfce084..9841da6 100644
--- a/nlsr_npt.c
+++ b/nlsr_npt.c
@@ -221,3 +221,104 @@
 	}
 	hashtb_end(e);	
 }
+
+
+void 
+update_npt_with_new_route(char * orig_router,int next_hop_face)
+{
+	int res;
+	struct npt_entry *ne;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = ⅇ
+    	
+    	hashtb_start(nlsr->npt, e);
+	res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
+
+	if ( res == HT_OLD_ENTRY )
+	{
+		ne=e->data;
+
+		if ( next_hop_face != ne->next_hop_face )
+		{
+			int j, nl_element;
+			struct name_list_entry *nle;		
+			struct hashtb_enumerator eenle;
+    			struct hashtb_enumerator *enle = &eenle;
+
+			hashtb_start(ne->name_list, enle);
+			nl_element=hashtb_n(ne->name_list);	
+
+			for (j=0;j<nl_element;j++)
+			{
+				nle=enle->data;
+				if (ne->next_hop_face != NO_FACE )
+				{
+					add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
+				}
+				if (next_hop_face != NO_FACE )
+				{
+					add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face);
+				}
+				hashtb_next(enle);
+			}
+			hashtb_end(enle);
+			ne->next_hop_face=next_hop_face;	
+		}
+		
+	}
+	else if ( res == HT_NEW_ENTRY )
+	{
+		hashtb_delete(e);
+	}
+	hashtb_end(e);
+}
+
+void 
+destroy_all_face_by_nlsr(void)
+{
+	int i, npt_element;
+	
+	struct npt_entry *ne;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->npt, e);
+	npt_element=hashtb_n(nlsr->npt);
+
+	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;		
+		struct hashtb_enumerator eenle;
+    		struct hashtb_enumerator *enle = &eenle;
+
+		hashtb_start(ne->name_list, enle);
+		nl_element=hashtb_n(ne->name_list);	
+
+		for (j=0;j<nl_element;j++)
+		{
+			nle=enle->data;
+			if ( ne->next_hop_face != NO_FACE)
+			{
+				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);
+			hashtb_next(enle);
+		}
+		hashtb_end(enle);
+			
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+
+	printf("\n");
+}
diff --git a/nlsr_npt.h b/nlsr_npt.h
index 997cb71..d564312 100644
--- a/nlsr_npt.h
+++ b/nlsr_npt.h
@@ -19,5 +19,7 @@
 int add_npt_entry(char *orig_router, char *name_prefix, int face);
 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);
+void destroy_all_face_by_nlsr(void);
 
 #endif
diff --git a/nlsr_route.c b/nlsr_route.c
index 47384d8..60e9c76 100644
--- a/nlsr_route.c
+++ b/nlsr_route.c
@@ -23,6 +23,7 @@
 #include "nlsr_route.h"
 #include "nlsr_lsdb.h"
 #include "nlsr_npt.h"
+#include "nlsr_adl.h"
 
 int
 route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
@@ -37,11 +38,12 @@
 
 		struct hashtb_param param_me = {0};
 		nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
+		nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
 		make_map();
 		assign_mapping_number();		
 		print_map();
+		print_rev_map();
 
-		 
 		do_old_routing_table_updates();
 	
 
@@ -63,8 +65,11 @@
 		
 		print_all_path_from_source(parent,source);
 
-		
-		
+		print_all_next_hop(parent,source);		
+
+		update_routing_table_with_new_route(parent,source);
+
+
 		for(i = 0; i < map_element; i++)
 		{
 			free(adj_matrix[i]);
@@ -72,6 +77,7 @@
 		free(parent);
 		free(adj_matrix);
 		hashtb_destroy(&nlsr->map);
+		hashtb_destroy(&nlsr->rev_map);
 		
 	}
 	nlsr->is_route_calculation_scheduled=0;
@@ -160,6 +166,53 @@
 
 }
 
+void 
+print_all_next_hop(long int *parent,long int source)
+{
+	int i, map_element;
+	struct map_entry *me;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->map, e);
+	map_element=hashtb_n(nlsr->map);
+
+	for(i=0;i<map_element;i++)
+	{
+		me=e->data;
+		if(me->mapping != source)
+		{
+			
+			printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
+		}
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+
+}
+
+long int
+get_next_hop_from_calculation(long int *parent, long int dest,long int source)
+{
+	long int next_hop;
+	while ( parent[dest] != EMPTY_PARENT )
+	{
+		next_hop=dest;
+		dest=parent[dest];
+
+	}
+
+	if ( dest != source )
+	{
+		next_hop=NO_NEXT_HOP;	
+	}
+	
+	return next_hop;
+	
+}
+
 void
 print_path(long int *parent, long int dest)
 {
@@ -245,6 +298,7 @@
 	{
 		me=e->data;
 		me->mapping=i;
+		add_rev_map_entry(i,me->router);
 		hashtb_next(e);		
 	}
 
@@ -373,6 +427,86 @@
 
 }
 
+
+void 
+add_rev_map_entry(long int mapping_number, char *router)
+{
+
+	struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->rev_map, e);
+    	res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
+
+	if(res == HT_NEW_ENTRY)
+	{
+		me=e->data;
+		me->router=(char *)malloc(strlen(router)+1);
+		memset(me->router,0,strlen(router)+1);
+		memcpy(me->router,router,strlen(router));
+		me->mapping=mapping_number;
+	}
+
+	hashtb_end(e);
+	
+}
+
+
+
+char * 
+get_router_from_rev_map(long int mapping_number)
+{
+
+	struct map_entry *me;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->rev_map, e);
+    	res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
+
+	if(res == HT_OLD_ENTRY)
+	{
+		me=e->data;
+		hashtb_end(e);
+		return me->router;
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+		hashtb_end(e);
+	}
+	
+	return NULL;
+}
+
+void
+print_rev_map(void)
+{
+	int i, map_element;
+	struct map_entry *me;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->map, e);
+	map_element=hashtb_n(nlsr->map);
+
+	for(i=0;i<map_element;i++)
+	{
+		me=e->data;
+		printf("Mapping Number: %d Router: %s  \n",me->mapping,me->router);
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+}
+
+
 void
 assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
 {
@@ -620,3 +754,50 @@
 	hashtb_end(e);	
 }
 
+void 
+update_routing_table_with_new_route(long int *parent, long int source)
+{
+	printf("update_routing_table_with_new_route called\n");
+	int i, map_element;
+	struct map_entry *me;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->rev_map, e);
+	map_element=hashtb_n(nlsr->rev_map);
+
+	for(i=0;i<map_element;i++)
+	{
+		me=e->data;
+		if(me->mapping != source)
+		{
+			
+			char *orig_router=get_router_from_rev_map(me->mapping);
+			if (orig_router != NULL )
+			{
+				int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
+				//printf(" Next hop router Num: %d ",next_hop_router_num);
+				if ( next_hop_router_num == NO_NEXT_HOP )
+				{
+					update_npt_with_new_route(orig_router,NO_FACE);
+					printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
+				}
+				else 
+				{
+					char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
+					//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);
+
+					printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
+
+				}
+			}
+		}
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+}
+
diff --git a/nlsr_route.h b/nlsr_route.h
index 762efa4..5361deb 100644
--- a/nlsr_route.h
+++ b/nlsr_route.h
@@ -36,6 +36,9 @@
 int is_not_explored(long int *Q, long int u,long int start, long int element);
 void print_path(long int *parent, long int dest);
 void print_all_path_from_source(long int *parent,long int source);
+void add_rev_map_entry(long int mapping_number, char *router);
+void print_rev_map(void);
+char * get_router_from_rev_map(long int mapping_number);
 
 /* Routing Table Relates function */
 
@@ -44,5 +47,9 @@
 void add_next_hop_from_lsa_adj_body(char *body, int no_link);
 void print_routing_table(void);
 void do_old_routing_table_updates();
+void update_routing_table_with_new_route(long int *parent, long int source);
+
+long int get_next_hop_from_calculation(long int *parent, long int dest,long int source);
+void print_all_next_hop(long int *parent,long int source);
 
 #endif