Removing nlsr-sync folder and Adding marker in Content Name
diff --git a/nlsr_route.c b/nlsr_route.c
index 8537b2e..573fcf9 100644
--- a/nlsr_route.c
+++ b/nlsr_route.c
@@ -1,6 +1,7 @@
-#include<stdio.h>
-#include<string.h>
-#include<stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <sys/time.h>
@@ -75,47 +76,121 @@
 			print_adj_matrix(adj_matrix,map_element);
 
 		long int source=get_mapping_no(nlsr->router_name);
-		long int *parent=(long int *)malloc(map_element * sizeof(long int));
-		long int *dist=(long int *)malloc(map_element * sizeof(long int));
-
 		int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
-		
-		if ( (num_link == 0) || (nlsr->multi_path_face_num <= 1 ) )
-		{	
-			calculate_path(adj_matrix,parent,dist, map_element, source);		
-			print_all_path_from_source(parent,source);
-			print_all_next_hop(parent,source);		
-			update_routing_table_with_new_route(parent, dist,source);
-		}
-		else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
+
+		if ( nlsr->is_hyperbolic_calc == 1)
 		{
 			long int *links=(long int *)malloc(num_link*sizeof(long int));
 			long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
 			get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
-			for ( i=0 ; i < num_link; i++)
+
+			struct hashtb_enumerator ee;
+			struct hashtb_enumerator *e = &ee;
+			for (hashtb_start(nlsr->rev_map, e); e->key != NULL; hashtb_next(e)) 
 			{
-				adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
+				struct map_entry *me=e->data;
+				if ( me->mapping != source )
+				{
+					long int *faces=(long int *)calloc(num_link,sizeof(long int));
+					double *nbr_dist=(double *)calloc(num_link,sizeof(double));
+					double *nbr_to_dest=(double *)calloc(num_link,sizeof(double));
+					for ( i=0 ; i < num_link; i++)
+					{
+						int face=get_next_hop_face_from_adl(get_router_from_rev_map(links[i]));
+						double dist_to_nbr=get_hyperbolic_distance(source,links[i]);
+						double dist_to_dest_from_nbr=get_hyperbolic_distance(links[i],me->mapping);
+						faces[i]=face;
+						nbr_dist[i]=dist_to_nbr;
+						nbr_to_dest[i]=	dist_to_dest_from_nbr;	
+
+						
+					}
+					sort_hyperbolic_route(nbr_to_dest,nbr_dist, faces,0,num_link);
+					if (nlsr->max_faces_per_prefix == 0 )
+					{
+						for ( i=0 ; i < num_link; i++)
+						{
+							update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
+						}				
+					}
+					else if ( nlsr->max_faces_per_prefix > 0 )
+					{
+						if ( num_link <= nlsr->max_faces_per_prefix )
+						{
+							for ( i=0 ; i < num_link; i++)
+							{
+								update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
+							}
+						}
+						else if (num_link > nlsr->max_faces_per_prefix)
+						{
+							for ( i=0 ; i < nlsr->max_faces_per_prefix; i++)
+							{
+								update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
+							}
+						}
+
+					}
+					free(faces);
+					free(nbr_dist);
+					free(nbr_to_dest);
+				}
+			}
+			hashtb_end(e);
+
+			
+			free(links);
+			free(link_costs);
+		}
+		else if (nlsr->is_hyperbolic_calc == 0 )
+		{
+
+			long int *parent=(long int *)malloc(map_element * sizeof(long int));
+			long int *dist=(long int *)malloc(map_element * sizeof(long int));
+			
+		
+			if ( (num_link == 0) || (nlsr->max_faces_per_prefix == 1 ) )
+			{	
 				calculate_path(adj_matrix,parent,dist, map_element, source);		
 				print_all_path_from_source(parent,source);
 				print_all_next_hop(parent,source);		
 				update_routing_table_with_new_route(parent, dist,source);
 			}
+			else if ( (num_link != 0) && (nlsr->max_faces_per_prefix == 0 || nlsr->max_faces_per_prefix > 1 ) )
+			{
+				long int *links=(long int *)malloc(num_link*sizeof(long int));
+				long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
+				get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
+				for ( i=0 ; i < num_link; i++)
+				{
+					adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
+					calculate_path(adj_matrix,parent,dist, map_element, source);		
+					print_all_path_from_source(parent,source);
+					print_all_next_hop(parent,source);		
+					update_routing_table_with_new_route(parent, dist,source);
+				}
 
-			free(links);
-			free(link_costs);
+				free(links);
+				free(link_costs);
+			}
+			free(parent);
+			free(dist);
 		}
+		
+		print_routing_table();
+		print_npt();
 
 		update_npt_with_new_route();
 
 		print_routing_table();
 		print_npt();
 
+
 		for(i = 0; i < map_element; i++)
 		{
 			free(adj_matrix[i]);
 		}
-		free(parent);
-		free(dist);
+		
 		free(adj_matrix);
 		hashtb_destroy(&nlsr->map);
 		hashtb_destroy(&nlsr->rev_map);
@@ -136,7 +211,7 @@
 	//long int *dist=(long int *)malloc(V * sizeof(long int));
 	long int *Q=(long int *)malloc(V * sizeof(long int));
 	long int head=0;
-	/* Initial the Parent */
+	/* Initiate the Parent */
 	for (i = 0 ; i < V; i++)
 	{
 		parent[i]=EMPTY_PARENT;
@@ -180,8 +255,7 @@
 			sort_queue_by_distance(Q,dist,head,V);
 		}
 	}
-	free(Q);
-	//free(dist);	
+	free(Q);	
 }
 
 void
@@ -277,7 +351,9 @@
 {
 	if (parent[dest] != EMPTY_PARENT )
 		print_path(parent,parent[dest]);
-	printf(" %ld",dest);
+
+	if ( nlsr->debugging )
+		printf(" %ld",dest);
 }
 
 int 
@@ -317,6 +393,50 @@
 	
 }
 
+void 
+sort_hyperbolic_route(double *dist_dest,double *dist_nbr, long int *faces,long int start,long int element)
+{
+	long int i,j;
+	double temp_dist;
+	long int temp_face;
+
+	for ( i=start ; i < element ; i ++) 
+	{
+		for( j=i+1; j<element; j ++)
+		{
+			if (dist_dest[i] < dist_dest[j])
+			{
+				temp_dist=dist_dest[j];
+				dist_dest[j]=dist_dest[i];
+				dist_dest[i]=temp_dist;
+
+				temp_dist=dist_nbr[j];
+				dist_nbr[j]=dist_nbr[i];
+				dist_nbr[i]=temp_dist;
+
+				temp_face=faces[j];
+				faces[j]=faces[i];
+				faces[i]=temp_face;
+			}
+			if ( (dist_dest[i] == dist_dest[j]) && (dist_nbr[i] < dist_nbr[j]) )
+			{
+				temp_dist=dist_dest[j];
+				dist_dest[j]=dist_dest[i];
+				dist_dest[i]=temp_dist;
+
+				temp_dist=dist_nbr[j];
+				dist_nbr[j]=dist_nbr[i];
+				dist_nbr[i]=temp_dist;
+
+				temp_face=faces[j];
+				faces[j]=faces[i];
+				faces[i]=temp_face;
+			}
+		}
+	}
+	
+}
+
 void
 print_map(void)
 {
@@ -874,6 +994,12 @@
 void 
 update_routing_table(char * dest_router,int next_hop_face, int route_cost)
 {
+	if ( nlsr->debugging )
+	{
+		printf("update_routing_table called \n");
+		printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
+	}
+
 	int res,res1;
 	struct routing_table_entry *rte;
 
@@ -906,20 +1032,6 @@
 			fle->route_cost=route_cost;
 		}
 		hashtb_end(ef);
-		
-		/*
-		//updating the face for the router prefix itself
-		if ( (rte->next_hop_face != NO_FACE  || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
-		{
-			add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
-		}
-		if ( (next_hop_face != NO_FACE  || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
-		{
-			add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
-		}
-
-		rte->next_hop_face=next_hop_face;
-		*/
 	}
 	else if ( res == HT_OLD_ENTRY )
 	{
@@ -984,7 +1096,7 @@
 			{
 				fle=ef->data;
 				if ( nlsr->debugging )
-					printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
+					printf(" 	Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
 				if ( nlsr->detailed_logging )
 					writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
 				hashtb_next(ef);	
@@ -1016,13 +1128,14 @@
 	{
 		writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
 		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
-		//writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
 	}
 	
 	if(flags == CCN_SCHEDULE_CANCEL)
 	{
  	 	return -1;
 	}
+
+	nlsr_lock();
 	int res;
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee;
@@ -1040,6 +1153,8 @@
 	}
 
 	print_routing_table();
+
+	nlsr_unlock();
 	
 	return 0;
 }
@@ -1112,6 +1227,28 @@
 	hashtb_end(e);	
 }
 
+void 
+update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist)
+{
+	if ( nlsr->debugging )
+		printf("update_routing_table_with_new_hyperbolic_route called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n");
+	
+	char *orig_router=get_router_from_rev_map(dest_router_rev_map_index);
+
+	if (face != NO_NEXT_HOP && face != NO_FACE )
+	{
+		update_routing_table(orig_router,face,nbr_to_dest_dist);
+		if ( nlsr->debugging )
+			printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
+					
+	}
+	
+
+}
 
 
 void 
@@ -1140,10 +1277,8 @@
 			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);
 					if ( nlsr->debugging )
 						printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
 					if ( nlsr->detailed_logging )
@@ -1152,9 +1287,7 @@
 				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);
 					update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
 					if ( nlsr->debugging )
 						printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
@@ -1174,9 +1307,15 @@
 int 
 does_face_exist_for_router(char *dest_router, int face_id)
 {
+	if (nlsr->debugging)
+	{
+		printf("does_face_exist_for_router called\n");
+		printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
+	}
+
 	int ret=0;
 
-	int res,res1;
+	int res;
 	struct routing_table_entry *rte;
 
 	struct hashtb_enumerator ee;
@@ -1188,22 +1327,12 @@
 	if( res == HT_OLD_ENTRY )
 	{
 		rte=e->data;
-		struct hashtb_enumerator eef;
-    		struct hashtb_enumerator *ef = &eef;
-    	
-    		hashtb_start(rte->face_list, ef);
-		res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);	
-		if( res1 == HT_OLD_ENTRY)
-		{
-			ret=1;									
-		}
-		else if ( res1 == HT_OLD_ENTRY )
-		{
-			hashtb_delete(ef);
-		}
-		hashtb_end(ef);
+		unsigned *v;
+		v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
+		if (v != NULL)
+			ret = 1;
 	}
-	else if( res == HT_NEW_ENTRY )
+	else
 	{
 		hashtb_delete(e);
 	} 
@@ -1212,3 +1341,27 @@
 
 	return ret;
 }
+
+double 
+get_hyperbolic_distance(long int source, long int dest)
+{
+	double distance;
+	char *src_router=get_router_from_rev_map(source);
+	char *dest_router=get_router_from_rev_map(dest);
+
+	double src_r=get_hyperbolic_r(src_router);
+	double src_theta=get_hyperbolic_r(src_router);	
+
+	double dest_r=get_hyperbolic_r(dest_router);
+	double dest_theta=get_hyperbolic_r(dest_router);
+	if ( src_r != -1 && dest_r != -1 )
+	{
+		distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta));
+	}
+	else 
+	{
+		distance= -1.0;
+	}
+	
+	return distance;
+}