Routing Table Calculation and Fib Manipulation Complete
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);
+}
+