Multi Path Routing Complete
diff --git a/nlsr_npt.c b/nlsr_npt.c
index 5f6ee26..d5ce2cc 100644
--- a/nlsr_npt.c
+++ b/nlsr_npt.c
@@ -24,6 +24,7 @@
 #include "nlsr_npt.h"
 #include "nlsr_fib.h"
 #include "nlsr_route.h"
+#include "nlsr_adl.h"
 
 int
 add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs)
@@ -32,7 +33,7 @@
 	{
 		return -1;
 	}
-	
+
 	struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry ));
 	
 	int res,res_nle,res_fle;
@@ -87,7 +88,7 @@
     			hashtb_start(ne->face_list, ef);
 			int i;						
 
-			for ( i=0; i< num_face ; i ++)
+			for ( i=0; i < num_face ; i++)
 			{
 				int face=faces[i];
 				res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
@@ -104,15 +105,6 @@
 			hashtb_end(ef);
 		}
 		
-
-		//ne->next_hop_face=face;
-
-		/*
-		if ( face != NO_FACE )
-		{
-			add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face);
-		}
-		*/
 	}
 	else if (res == HT_OLD_ENTRY)
 	{
@@ -134,12 +126,6 @@
 			nle->name=(char *)malloc(strlen(name_prefix)+1);
 			memset(nle->name,0,strlen(name_prefix)+1);
 			memcpy(nle->name,name_prefix,strlen(name_prefix));
-			/*
-			if ( face != NO_FACE )
-			{
-				add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face);
-			}
-			*/
 		}
 		else if(res_nle == HT_OLD_ENTRY )
 		{
@@ -175,11 +161,106 @@
 
 	}
 	hashtb_end(e);
+
+	update_ccnd_fib_for_orig_router(orig_router);
+
 	return res;
 }
 
+void 
+update_ccnd_fib_for_orig_router(char *orig_router)
+{
+
+	int res;	
+	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);
+	}
+	else if ( res == HT_OLD_ENTRY )
+	{
+		struct npt_entry *ne;
+		ne=e->data;
+		int num_face=hashtb_n(ne->face_list);
+		int last_face,first_face;
+
+		int *faces=(int *)malloc(num_face*sizeof(int));
+		int *route_costs=(int *)malloc(num_face*sizeof(int));
+		
+		get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
+		sort_faces_by_distance(faces,route_costs,0,num_face);
+		
+
+		first_face=num_face-1;		
+	
+		if ( nlsr->multi_path_face_num == 0 )
+		{
+			last_face=first_face;
+		}
+		else 
+		{
+			if ( num_face <= nlsr->multi_path_face_num)
+			{
+				last_face=0;
+			}
+			else if ( nlsr->multi_path_face_num == 0)
+			{
+				last_face=num_face-nlsr->multi_path_face_num;
+			}
+		}
+
+		int i,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 (i=0;i<nl_element;i++)
+		{
+			nle=enle->data;
+			
+			for( j=first_face; j>= last_face; j--)
+			{
+				if ( j == last_face )
+				{
+					if( is_neighbor(nle->name) == 0 )
+					{
+						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
+					}
+				}
+				else 
+				{
+					if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
+					{
+						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
+					}
+				}
+			}
+
+			hashtb_next(enle);
+		}
+		hashtb_end(enle);
+		
+
+
+		free(faces);
+		free(route_costs);
+
+	}
+	hashtb_end(e);
+
+}
+
 int 
-delete_npt_entry(char *orig_router, char *name_prefix)
+delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
 {
 	if ( strcmp(orig_router,nlsr->router_name)== 0)
 	{
@@ -217,12 +298,60 @@
 		}
 		else if(res_nle == HT_OLD_ENTRY )
 		{
-			/*
-			if (ne->next_hop_face != NO_FACE )
+			struct name_list_entry *nle;
+
+			nle=enle->data;
+	
+			int j;
+			int num_face=hashtb_n(ne->face_list);
+			int last_face,first_face;
+
+			int *faces=(int *)malloc(num_face*sizeof(int));
+			int *route_costs=(int *)malloc(num_face*sizeof(int));
+		
+			get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
+			sort_faces_by_distance(faces,route_costs,0,num_face);
+		
+
+			first_face=num_face-1;		
+	
+			if ( nlsr->multi_path_face_num == 0 )
 			{
-				add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_UNREG, ne->next_hop_face);
+				last_face=first_face;
 			}
-			*/
+			else 
+			{
+				if ( num_face <= nlsr->multi_path_face_num)
+				{
+					last_face=0;
+				}
+				else if ( nlsr->multi_path_face_num == 0)
+				{
+					last_face=num_face-nlsr->multi_path_face_num;
+				}
+			}			
+
+			for( j=first_face; j>= last_face; j--)
+			{
+				if ( j == last_face )
+				{
+					if( is_neighbor(nle->name) == 0 )
+					{
+						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
+					}
+				}
+				else 
+				{
+					if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
+					{
+						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
+					}
+				}
+			}
+			
+			
+			
+
 			hashtb_delete(enle);
 		}
 
@@ -342,26 +471,9 @@
 				for (j=0;j<nl_element;j++)
 				{
 					nle=enle->data;
-		
-					int k,face_list_element;
-					struct face_list_entry *fle;
-
-					struct hashtb_enumerator eef;
-    					struct hashtb_enumerator *ef = &eef;
-    	
-    					hashtb_start(ne->face_list, ef);
-					face_list_element=hashtb_n(ne->face_list);
-					for(k=0;k<face_list_element;k++)
-					{
-						fle=ef->data;
-						//printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
-						//add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face);
-						hashtb_next(ef);	
-					}
-					hashtb_end(ef);
-
-						//add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face);
 					
+					delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);		
+	
 					hashtb_next(enle);
 				}
 				hashtb_end(enle);				
@@ -380,61 +492,6 @@
 	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 = &ee;
-    	
-    	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
 add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
@@ -491,7 +548,7 @@
 
 
 void
-add_new_fib_entries_to_npt()
+add_new_fib_entries_to_npt(void)
 {
 	printf("add_new_fib_entries_to_npt called\n");
 	int i,j, rt_element,face_list_element;
@@ -506,10 +563,7 @@
 
 	for(i=0;i<rt_element;i++)
 	{
-		//printf("----------Routing Table Entry %d------------------\n",i+1);
 		rte=e->data;
-		//printf(" Destination Router: %s \n",rte->dest_router);
-		//rte->next_hop_face == NO_NEXT_HOP ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", rte->next_hop_face);
 
 		struct face_list_entry *fle;
 
@@ -527,7 +581,6 @@
 			for(j=0;j<face_list_element;j++)
 			{
 				fle=ef->data;
-				//printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
 				add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
 				hashtb_next(ef);	
 			}
@@ -622,7 +675,7 @@
 }
 
 void 
-clean_old_fib_entries_from_npt()
+clean_old_fib_entries_from_npt(void)
 {
 	
 	printf("clean_old_fib_entries_from_npt called\n\n");
@@ -671,7 +724,12 @@
 					{
 						nle=enle->data;
 
-						//delete all the fib entries here		
+						//delete all the fib entries here
+						if( is_neighbor(nle->name) == 0 )
+						{
+							add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
+						}						
+		
 
 						hashtb_next(enle);
 					}
@@ -704,10 +762,193 @@
 }
 
 void
-update_npt_with_new_route()
+update_npt_with_new_route(void)
 {
 	clean_old_fib_entries_from_npt();
 	add_new_fib_entries_to_npt();
+	
+	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++)
+	{
+	
+		ne=e->data;
+		update_ccnd_fib_for_orig_router(ne->orig_router);
+		hashtb_next(e);
+	}
+	
+	hashtb_end(e);
+}
+
+
+
+void 
+sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
+{
+	int i,j;
+	int temp_cost;
+	int temp_face;
+
+	for ( i=start ; i < element ; i ++) 
+	{
+		for( j=i+1; j<element; j ++)
+		{
+			if (route_costs[j] < route_costs[i] )
+			{
+				temp_cost=route_costs[j];
+				route_costs[j]=route_costs[i];
+				route_costs[i]=temp_cost;
+
+				temp_face=faces[j];
+				faces[j]=faces[i];
+				faces[i]=temp_face;
+			}
+		}
+	}
+	
+}
+
+void
+get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
+{
+
+	int res,face_list_element,j;	
+	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);
+	}
+	else if ( res == HT_OLD_ENTRY )
+	{
+		struct npt_entry *ne;
+		ne=e->data;
+		
+		struct face_list_entry *fle;
+
+		struct hashtb_enumerator eef;
+    		struct hashtb_enumerator *ef = &eef;
+    	
+    		hashtb_start(ne->face_list, ef);
+		face_list_element=hashtb_n(ne->face_list);
+		for(j=0;j<face_list_element;j++)
+		{
+			fle=ef->data;
+			faces[j]=fle->next_hop_face;
+			route_costs[j]=fle->route_cost;
+			hashtb_next(ef);	
+		}
+		hashtb_end(ef);
+		
+
+	}
+	hashtb_end(e);
+
+}
+
+void 
+destroy_faces_by_orig_router(char *orig_router)
+{
+
+	int res;	
+	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);
+	}
+	else if ( res == HT_OLD_ENTRY )
+	{
+		struct npt_entry *ne;
+		ne=e->data;
+		int num_face=hashtb_n(ne->face_list);
+		int last_face,first_face;
+
+		int *faces=(int *)malloc(num_face*sizeof(int));
+		int *route_costs=(int *)malloc(num_face*sizeof(int));
+		
+		get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
+		sort_faces_by_distance(faces,route_costs,0,num_face);
+		
+
+		first_face=num_face-1;		
+	
+		if ( nlsr->multi_path_face_num == 0 )
+		{
+			last_face=first_face;
+		}
+		else 
+		{
+			if ( num_face <= nlsr->multi_path_face_num)
+			{
+				last_face=0;
+			}
+			else if ( nlsr->multi_path_face_num == 0)
+			{
+				last_face=num_face-nlsr->multi_path_face_num;
+			}
+		}
+
+		int i,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 (i=0;i<nl_element;i++)
+		{
+			nle=enle->data;
+			
+			for( j=first_face; j>= last_face; j--)
+			{
+				if ( j == last_face )
+				{
+					if( is_neighbor(nle->name) == 0 )
+					{
+						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
+					}
+				}
+				else 
+				{
+					if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
+					{
+						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
+					}
+				}
+			}
+
+			hashtb_next(enle);
+		}
+		hashtb_end(enle);
+		
+
+
+		free(faces);
+		free(route_costs);
+
+	}
+	hashtb_end(e);
+
 }
 
 void 
@@ -726,29 +967,7 @@
 	for(i=0;i<npt_element;i++)
 	{
 		ne=e->data;
-		
-		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 Face: %d \n",nle->name,ne->next_hop_face);
-			hashtb_next(enle);
-		}
-		hashtb_end(enle);
-			
+		destroy_faces_by_orig_router(ne->orig_router);
 		hashtb_next(e);		
 	}