LSDB Operation Complete - Refresh
diff --git a/nlsr_adjacent.cpp b/nlsr_adjacent.cpp
index c8821c1..8a1c048 100644
--- a/nlsr_adjacent.cpp
+++ b/nlsr_adjacent.cpp
@@ -1,5 +1,7 @@
 #include<iostream>
 #include<string>
+#include<cmath>
+#include<limits>
 
 #include "nlsr_adjacent.hpp"
 
@@ -13,6 +15,15 @@
 	interestTimedOutNo=iton;
 }
 
+bool 
+Adjacent::isAdjacentEqual(Adjacent& adj)
+{
+	return ( adjacentName == adj.getAdjacentName() ) && 
+	       ( connectingFace == adj.getConnectingFace() ) &&
+	       (std::abs(linkCost - adj.getLinkCost()) < 
+	                                    std::numeric_limits<double>::epsilon()) ;
+}
+
 std::ostream&
 operator << (std::ostream &os, Adjacent &adj){
 	cout<<"Adjacent : "<< adj.getAdjacentName()	<< endl;
diff --git a/nlsr_adjacent.hpp b/nlsr_adjacent.hpp
index 2f5106f..910b8a9 100644
--- a/nlsr_adjacent.hpp
+++ b/nlsr_adjacent.hpp
@@ -65,6 +65,8 @@
 		void setInterestTimedOutNo(int iton){
 			interestTimedOutNo=iton;
 		}
+
+		bool isAdjacentEqual(Adjacent& adj);
 	private:
 		string adjacentName;
 		int connectingFace;
diff --git a/nlsr_adl.cpp b/nlsr_adl.cpp
index 055a792..eaf0699 100644
--- a/nlsr_adl.cpp
+++ b/nlsr_adl.cpp
@@ -28,6 +28,17 @@
 	adjList.push_back(adj);
 	return 0;
 }
+
+void 
+Adl::addAdjacentsFromAdl(Adl& adl)
+{
+	for(std::list<Adjacent >::iterator it=adl.getAdjList().begin();
+	                                             it!=adl.getAdjList().end(); ++it)
+	{
+		insert((*it));
+	}
+}
+
 int
 Adl::updateAdjacentStatus(string adjName, int s){
 	Adjacent adj(adjName);
@@ -62,6 +73,37 @@
 	return adj;
 }
 
+
+bool 
+Adl::isAdlEqual(Adl &adl)
+{
+	if ( getAdlSize() != adl.getAdlSize() )
+	{
+		return false;
+	}
+
+	adjList.sort(adjacent_compare);	
+	adl.getAdjList().sort(adjacent_compare);
+	int equalAdjCount=0;
+
+	std::list< Adjacent > adjList2=adl.getAdjList();
+
+	std::list<Adjacent>::iterator it1;
+	std::list<Adjacent>::iterator it2;
+	for(it1=adjList.begin() , it2=adjList2.begin() ; 
+	                                              it1!=adjList.end(); it1++,it2++)
+	{
+		if ( !(*it1).isAdjacentEqual((*it2)) )
+		{
+			break;
+		}
+		equalAdjCount++;
+	}
+
+	return equalAdjCount==getAdlSize();
+}
+
+
 int 
 Adl::updateAdjacentLinkCost(string adjName, double lc){
 	Adjacent adj(adjName);
diff --git a/nlsr_adl.hpp b/nlsr_adl.hpp
index ec5cf96..0d868ae 100644
--- a/nlsr_adl.hpp
+++ b/nlsr_adl.hpp
@@ -24,16 +24,27 @@
 	int getStatusOfNeighbor(string& neighbor);
 	void setStatusOfNeighbor(string& neighbor, int status);
 	void setTimedOutInterestCount(string& neighbor, int count);
+	void addAdjacentsFromAdl(Adl& adl);
 
 	bool isAdjLsaBuildable(nlsr& pnlsr);
 	int getNumOfActiveNeighbor();
 	Adjacent getAdjacent(string adjName);
 
+	bool isAdlEqual(Adl &adl);
+
 	int getAdlSize()
 	{
 		return adjList.size();
 	}
 
+	void resetAdl()
+	{
+		if( adjList.size() > 0 )
+		{
+			adjList.clear();
+		}
+	}
+
 	void printAdl();
 
 private:	
diff --git a/nlsr_fib.cpp b/nlsr_fib.cpp
index a1e4968..2bfc568 100644
--- a/nlsr_fib.cpp
+++ b/nlsr_fib.cpp
@@ -58,6 +58,7 @@
   				(*it).getNhl().addNextHop((*nhit));
   			}
   		}
+  		(*it).getNhl().sortNhl();
   }
   else
   {
@@ -67,6 +68,7 @@
   		{
   			newEntry.getNhl().addNextHop((*nhit));
   		}
+  		newEntry.getNhl().sortNhl();
   		fibTable.push_back(newEntry);	
   }
 }
@@ -127,6 +129,7 @@
 	for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
 	                                                                         ++it)
 	{
+		//(*it).getNhl().sortNhl();
 		cout<<(*it);
 	}
 }
diff --git a/nlsr_lsa.cpp b/nlsr_lsa.cpp
index 38d3fd5..6eebb16 100644
--- a/nlsr_lsa.cpp
+++ b/nlsr_lsa.cpp
@@ -1,10 +1,13 @@
 #include<string>
 #include<iostream>
 #include<algorithm>
+#include<cmath>
+#include<limits>
 
 #include "nlsr_lsa.hpp"
 #include "nlsr_npl.hpp"
 #include "nlsr_adjacent.hpp"
+#include "nlsr.hpp"
 
 using namespace std;
 
@@ -93,6 +96,15 @@
 	return key;
 }
 
+bool 
+CorLsa::isLsaContentEqual(CorLsa& clsa)
+{
+	return (std::abs(corRad - clsa.getCorRadius()) < 
+	                                    std::numeric_limits<double>::epsilon()) &&
+	       (std::abs(corTheta - clsa.getCorTheta()) < 
+	                                    std::numeric_limits<double>::epsilon());
+}
+
 string 
 CorLsa::getCorLsaData()
 {
@@ -149,6 +161,13 @@
 	return key;
 }
 
+bool 
+AdjLsa::isLsaContentEqual(AdjLsa& alsa)
+{
+	return adl.isAdlEqual(alsa.getAdl());
+}
+
+
 string 
 AdjLsa::getAdjLsaData(){
 	string adjLsaData;
@@ -172,6 +191,29 @@
 	return adjLsaData;
 }
 
+
+void 
+AdjLsa::addNptEntriesForAdjLsa(nlsr& pnlsr)
+{
+	if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
+	{
+		pnlsr.getNpt().addNpte(getOrigRouter(), getOrigRouter(),pnlsr);
+	}
+
+}
+
+
+void 
+AdjLsa::removeNptEntriesForAdjLsa(nlsr& pnlsr)
+{
+	if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
+	{
+		pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(),pnlsr);
+	}
+}
+
+
+
 std::ostream& 
 operator<<(std::ostream& os, AdjLsa& aLsa) 
 {
diff --git a/nlsr_lsa.hpp b/nlsr_lsa.hpp
index a75f572..0f3e528 100644
--- a/nlsr_lsa.hpp
+++ b/nlsr_lsa.hpp
@@ -127,6 +127,10 @@
 		return noLink;
 	}
 
+	bool isLsaContentEqual(AdjLsa& alsa);
+	void addNptEntriesForAdjLsa(nlsr& pnlsr);
+	void removeNptEntriesForAdjLsa(nlsr& pnlsr);
+
 private:
 	uint32_t noLink;
 	Adl adl;
@@ -173,6 +177,8 @@
 	void setCorTheta(double ct){
 		corTheta=ct;
 	}
+
+	bool isLsaContentEqual(CorLsa& clsa);
 private:
 	double corRad;
 	double corTheta;
diff --git a/nlsr_lsdb.cpp b/nlsr_lsdb.cpp
index 3177fc8..da7d86e 100644
--- a/nlsr_lsdb.cpp
+++ b/nlsr_lsdb.cpp
@@ -57,7 +57,10 @@
 			std::list<string> nameList=nlsa.getNpl().getNameList();
 			for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end();it++)
 			{
-				pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
+				if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
+				{
+					pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
+				}
 			}
 		} 
 	}
@@ -83,7 +86,10 @@
       		chkNameLsa.first.addNameToLsa((*it));
       		if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
       		{
-      			pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
+      			if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
+      			{
+      				pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
+      			}
       		}
       }
                           
@@ -99,7 +105,10 @@
       		chkNameLsa.first.removeNameFromLsa((*it));
       		if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
       		{
-      			pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
+      			if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
+      			{
+      				pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
+      			}
       		}
       }  
 			
@@ -125,13 +134,26 @@
 }
 
 bool 
-Lsdb::removeNameLsa(string& key)
+Lsdb::removeNameLsa(nlsr& pnlsr, string& key)
 {
 	std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), 
 																		nameLsdb.end(),	
    																	bind(nameLsaCompareByKey, _1, key));
   if ( it != nameLsdb.end() )
   {
+  		if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()  )
+  		{
+  			pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);	
+  			for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
+  			                           nit!=(*it).getNpl().getNameList().end(); ++nit)
+  			{
+  				if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
+  				{
+  					pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
+  				}
+  			} 
+  			
+  		}
 		nameLsdb.erase(it);
 		return true;
   }
@@ -164,12 +186,8 @@
 }
 
 // Cor LSA and LSDB related Functions start here
-/*
-static bool
-corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){
-	return clsa1.getLsaKey()==clsa1.getLsaKey();
-}
-*/
+
+
 static bool
 corLsaCompareByKey(CorLsa& clsa, string& key){
 	return clsa.getCorLsaKey()==key;
@@ -210,15 +228,12 @@
 	std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
 	if ( !chkCorLsa.second )
 	{
-		// add cor LSA
 		addCorLsa(clsa);
 		printCorLsdb(); //debugging purpose
 		if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
 		{
 			pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr);
 		}
-		//schedule routing table calculation only if 
-		//hyperbolic calculation is scheduled
 		if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
 		{
 			if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
@@ -233,8 +248,28 @@
 	}
 	else
 	{
-		// check for newer cor LSA
-		//CorLsa oldCorLsa=getCorLsa(clsa.getCorLsaKey());
+		if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
+		{
+			chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
+			chkCorLsa.first.setLifeTime(clsa.getLifeTime());
+			if ( !chkCorLsa.first.isLsaContentEqual(clsa) )
+			{
+				chkCorLsa.first.setCorRadius(clsa.getCorRadius());
+				chkCorLsa.first.setCorTheta(clsa.getCorTheta());
+
+				if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
+				{
+					if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
+					{
+						pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
+								ndn::bind(&RoutingTable::calculate, 
+								&pnlsr.getRoutingTable(),boost::ref(pnlsr)));
+						pnlsr.setIsRouteCalculationScheduled(1);
+					}	
+				}
+				
+			}
+		}
 		
 	}
 	
@@ -257,13 +292,17 @@
 }
 
 bool 
-Lsdb::removeCorLsa(string& key)
+Lsdb::removeCorLsa(nlsr& pnlsr, string& key)
 {
 	std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), 
 																		corLsdb.end(),	
    																	bind(corLsaCompareByKey, _1, key));
   if ( it != corLsdb.end() )
   {
+  		if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
+		{
+			pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
+		}
 		corLsdb.erase(it);
 		return true;
   }
@@ -329,7 +368,7 @@
 			{
 				//remove if there is any adj lsa in LSDB
 				string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
-				removeAdjLsa(key);
+				removeAdjLsa(pnlsr,key);
 				// Remove alll fib entries as per NPT
 			}
 			pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
@@ -381,19 +420,11 @@
 bool 
 Lsdb::installAdjLsa(nlsr& pnlsr, AdjLsa &alsa)
 {
-	//bool doesLsaExist_ = doesAdjLsaExist(alsa.getAdjLsaKey());
-	//if ( !doesLsaExist_ )
 	std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
 	if ( !chkAdjLsa.second )
 	{
-		// add Adj LSA
 		addAdjLsa(alsa);
-		// adding a NPT entry for router itself
-		if ( alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
-		{
-			pnlsr.getNpt().addNpte(alsa.getOrigRouter(),alsa.getOrigRouter(),pnlsr);
-		}
-		// schedule routing table calculation
+		alsa.addNptEntriesForAdjLsa(pnlsr);
 		if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
 		{
 			pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
@@ -404,8 +435,24 @@
 	}
 	else
 	{
-		// check for newer name LSA
-		//AdjLsa oldAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
+		if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
+		{
+			chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
+			chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
+
+			if ( !	chkAdjLsa.first.isLsaContentEqual(alsa))
+			{
+				chkAdjLsa.first.getAdl().resetAdl();
+				chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
+				if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
+				{
+					pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
+								ndn::bind(&RoutingTable::calculate, 
+								&pnlsr.getRoutingTable(),boost::ref(pnlsr)));
+					pnlsr.setIsRouteCalculationScheduled(1);
+				}	
+			}
+		}
 		
 	}
 
@@ -428,13 +475,14 @@
 }
 
 bool 
-Lsdb::removeAdjLsa(string& key)
+Lsdb::removeAdjLsa(nlsr& pnlsr, string& key)
 {
 	std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), 
 																		adjLsdb.end(),	
    																	bind(adjLsaCompareByKey, _1, key));
   if ( it != adjLsdb.end() )
   {
+  		(*it).removeNptEntriesForAdjLsa(pnlsr);
 		adjLsdb.erase(it);
 		return true;
   }
@@ -462,6 +510,7 @@
 		return adjLsdb;
 }
 
+
 void 
 Lsdb::printAdjLsdb()
 {
diff --git a/nlsr_lsdb.hpp b/nlsr_lsdb.hpp
index 5d278ea..9ba0729 100644
--- a/nlsr_lsdb.hpp
+++ b/nlsr_lsdb.hpp
@@ -20,20 +20,20 @@
 	bool buildAndInstallOwnNameLsa(nlsr& pnlsr);
 	std::pair<NameLsa&, bool>  getNameLsa(string key);
 	bool installNameLsa(nlsr& pnlsr, NameLsa &nlsa);
-	bool removeNameLsa(string& key);
+	bool removeNameLsa(nlsr& pnlsr, string& key);
 	void printNameLsdb(); //debugging
 
 	//function related to Cor LSDB
 	bool buildAndInstallOwnCorLsa(nlsr& pnlsr);
 	std::pair<CorLsa&, bool> getCorLsa(string key);
 	bool installCorLsa(nlsr& pnlsr, CorLsa &clsa);
-	bool removeCorLsa(string& key);
+	bool removeCorLsa(nlsr& pnlsr, string& key);
 	void printCorLsdb(); //debugging
 
 	//function related to Adj LSDB
 	void scheduledAdjLsaBuild(nlsr& pnlsr);
 	bool buildAndInstallOwnAdjLsa(nlsr& pnlsr);
-	bool removeAdjLsa(string& key);
+	bool removeAdjLsa(nlsr& pnlsr, string& key);
 	bool installAdjLsa(nlsr& pnlsr, AdjLsa &alsa);
 	std::pair<AdjLsa& , bool> getAdjLsa(string key);
 	std::list<AdjLsa>& getAdjLsdb();
@@ -43,6 +43,7 @@
 	bool addNameLsa(NameLsa &nlsa);
 	bool doesNameLsaExist(string key);
 	
+	
 	bool addCorLsa(CorLsa& clsa);
 	bool doesCorLsaExist(string key);