Adding boost logging support for NLSR
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index f1b5d93..569a40a 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -95,7 +95,7 @@
nlsr.getConfParameter().buildRouterPrefix();
nlsr.getLsdb().setLsaRefreshTime(nlsr.getConfParameter().getLsaRefreshTime());
nlsr.getFib().setFibEntryRefreshTime(2*nlsr.getConfParameter().getLsaRefreshTime());
- nlsr.getFib().scheduleFibRefreshing(nlsr, 60);
+ //nlsr.getFib().scheduleFibRefreshing(nlsr, 60);
nlsr.getLsdb().setThisRouterPrefix(nlsr.getConfParameter().getRouterPrefix());
/* debugging purpose start */
diff --git a/src/nlsr.hpp b/src/nlsr.hpp
index 5ff2ef7..31655f4 100644
--- a/src/nlsr.hpp
+++ b/src/nlsr.hpp
@@ -15,6 +15,7 @@
#include "nlsr_rt.hpp"
#include "nlsr_npt.hpp"
#include "nlsr_fib.hpp"
+#include "nlsr_logger.hpp"
//testing
#include "nlsr_test.hpp"
@@ -45,6 +46,7 @@
, routingTable()
, npt()
, fib()
+ , nlsrLogger()
, nlsrTesting()
{
isDaemonProcess=false;
@@ -196,6 +198,11 @@
{
isRouteCalculationScheduled=ircs;
}
+
+ NlsrLogger& getNlsrLogger()
+ {
+ return nlsrLogger;
+ }
private:
ConfParameter confParam;
@@ -216,7 +223,7 @@
RoutingTable routingTable;
Npt npt;
Fib fib;
-
+ NlsrLogger nlsrLogger;
long int adjBuildCount;
int isBuildAdjLsaSheduled;
diff --git a/src/nlsr_fe.hpp b/src/nlsr_fe.hpp
index 8392dfb..2297315 100644
--- a/src/nlsr_fe.hpp
+++ b/src/nlsr_fe.hpp
@@ -3,6 +3,7 @@
#include<list>
#include <iostream>
+#include <ndn-cpp-dev/util/scheduler.hpp>
#include "nlsr_nexthop.hpp"
#include "nlsr_nhl.hpp"
@@ -15,6 +16,7 @@
FibEntry()
: name()
, timeToRefresh(0)
+ , feSeqNo(0)
{
}
@@ -43,11 +45,33 @@
timeToRefresh=ttr;
}
+ void setFeExpiringEventId(ndn::EventId feid)
+ {
+ feExpiringEventId=feid;
+ }
+
+ ndn::EventId getFeExpiringEventId()
+ {
+ return feExpiringEventId;
+ }
+
+ void setFeSeqNo(int fsn)
+ {
+ feSeqNo=fsn;
+ }
+
+ int getFeSeqNo()
+ {
+ return feSeqNo;
+ }
+
bool isEqualNextHops(Nhl &nhlOther);
private:
string name;
int timeToRefresh;
+ ndn::EventId feExpiringEventId;
+ int feSeqNo;
Nhl nhl;
};
diff --git a/src/nlsr_fib.cpp b/src/nlsr_fib.cpp
index 399584f..fd4b209 100644
--- a/src/nlsr_fib.cpp
+++ b/src/nlsr_fib.cpp
@@ -5,6 +5,7 @@
#include "nlsr.hpp"
using namespace std;
+using namespace ndn;
static bool
fibEntryNameCompare(FibEntry& fe, string name)
@@ -12,10 +13,28 @@
return fe.getName() == name ;
}
+void
+Fib::cancelScheduledFeExpiringEvent(nlsr& pnlsr, EventId eid)
+{
+ pnlsr.getScheduler().cancelEvent(eid);
+}
+ndn::EventId
+Fib::scheduleFibEntryRefreshing(nlsr& pnlsr, string name, int feSeqNum, int refreshTime)
+{
+ return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
+ ndn::bind(&Fib::refreshFibEntry,this,name,feSeqNum));
+}
+
+void
+Fib::refreshFibEntry(string name, int feSeqNum)
+{
+
+}
+
void
-Fib::removeFromFib(string name)
+Fib::removeFromFib(nlsr& pnlsr, string name)
{
std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
fibTable.end(), bind(&fibEntryNameCompare, _1, name));
@@ -26,13 +45,14 @@
{
//remove entry from NDN-FIB
}
+ cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
fibTable.erase(it);
}
}
void
-Fib::updateFib(string name, Nhl& nextHopList, int maxFacesPerPrefix)
+Fib::updateFib(nlsr& pnlsr,string name, Nhl& nextHopList, int maxFacesPerPrefix)
{
int startFace=0;
int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
@@ -56,6 +76,11 @@
(*it).setTimeToRefresh(fibEntryRefreshTime);
}
(*it).getNhl().sortNhl();
+ cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
+ (*it).setFeSeqNo((*it).getFeSeqNo()+1);
+ (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
+ (*it).getName() ,
+ (*it).getFeSeqNo(),fibEntryRefreshTime));
//update NDN-FIB
}
else
@@ -70,39 +95,18 @@
}
newEntry.getNhl().sortNhl();
newEntry.setTimeToRefresh(fibEntryRefreshTime);
+ newEntry.setFeSeqNo(1);
fibTable.push_back(newEntry);
+
+ //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId());
+
//Update NDN-FIB
}
}
-void
-Fib::refreshFib(nlsr& pnlsr)
-{
- for ( std::list<FibEntry >::iterator it = fibTable.begin() ;
- it != fibTable.end() ; ++it)
- {
- (*it).setTimeToRefresh((*it).getTimeToRefresh()-60);
- if( (*it).getTimeToRefresh() < 0 )
- {
- cout<<"Refreshing FIB entry : "<<endl;
- cout<<(*it)<<endl;
- (*it).setTimeToRefresh(fibEntryRefreshTime);
- //update NDN-FIB
- }
- }
- printFib();
- scheduleFibRefreshing(pnlsr,60);
-}
-void
-Fib::scheduleFibRefreshing(nlsr& pnlsr, int refreshTime)
-{
- pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
- ndn::bind(&Fib::refreshFib,this,boost::ref(pnlsr)));
-}
-
-void Fib::cleanFib()
+void Fib::cleanFib(nlsr& pnlsr)
{
for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
++it)
@@ -110,6 +114,7 @@
for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
{
+ cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId());
//remove entry from NDN-FIB
}
}
diff --git a/src/nlsr_fib.hpp b/src/nlsr_fib.hpp
index 453b7db..d02d040 100644
--- a/src/nlsr_fib.hpp
+++ b/src/nlsr_fib.hpp
@@ -7,6 +7,7 @@
class nlsr;
using namespace std;
+using namespace ndn;
class Fib
{
@@ -15,10 +16,9 @@
{
}
- void removeFromFib(string name);
- void updateFib(string name, Nhl& nextHopList, int maxFacesPerPrefix);
- void scheduleFibRefreshing(nlsr& pnlsr, int refreshTime);
- void cleanFib();
+ void removeFromFib(nlsr& pnlsr, string name);
+ void updateFib(nlsr& pnlsr, string name, Nhl& nextHopList, int maxFacesPerPrefix);
+ void cleanFib(nlsr& pnlsr);
void setFibEntryRefreshTime(int fert)
{
fibEntryRefreshTime=fert;
@@ -29,7 +29,10 @@
private:
void removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId);
int getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix);
- void refreshFib(nlsr& pnlsr);
+ ndn::EventId
+ scheduleFibEntryRefreshing(nlsr& pnlsr, string name, int feSeqNum, int refreshTime);
+ void cancelScheduledFeExpiringEvent(nlsr& pnlsr, EventId eid);
+ void refreshFibEntry(string name, int feSeqNum);
private:
std::list<FibEntry> fibTable;
diff --git a/src/nlsr_im.cpp b/src/nlsr_im.cpp
index 6ae0131..886aca8 100644
--- a/src/nlsr_im.cpp
+++ b/src/nlsr_im.cpp
@@ -147,7 +147,7 @@
void
interestManager::scheduleInfoInterest(nlsr& pnlsr, int seconds)
{
- pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
+ EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
ndn::bind(&interestManager::sendScheduledInfoInterest, this,
boost::ref(pnlsr),seconds));
}
diff --git a/src/nlsr_logger.cpp b/src/nlsr_logger.cpp
new file mode 100644
index 0000000..af263cd
--- /dev/null
+++ b/src/nlsr_logger.cpp
@@ -0,0 +1,58 @@
+#include "nlsr_logger.hpp"
+
+
+string
+NlsrLogger::getEpochTime()
+{
+ std::stringstream ss;
+ boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970,1,1));
+ boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
+ boost::posix_time::time_duration diff = now - time_t_epoch;
+ ss<<diff.total_seconds()<<"."<<boost::format("%06i")%(diff.total_microseconds()%1000000);
+ return ss.str();
+}
+
+string
+NlsrLogger::getUserHomeDirectory()
+{
+ string homeDirPath(getpwuid(getuid())->pw_dir);
+ if( homeDirPath.empty() )
+ {
+ homeDirPath = getenv("HOME");
+ }
+
+ return homeDirPath;
+}
+
+void
+NlsrLogger::initNlsrLogger(std::string dirPath)
+{
+ string logDirPath(dirPath);
+ if( dirPath.empty() )
+ {
+ logDirPath=getUserHomeDirectory();
+ }
+
+ typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
+ shared_ptr< file_sink > sink(new file_sink(
+ keywords::file_name = "NLSR%Y%m%d%H%M%S_%3N.log", // file name pattern
+ keywords::rotation_size = 128 * 1024 * 1024 ,// rotation size, in characters
+ keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0)
+ ));
+
+ sink->locked_backend()->set_file_collector(sinks::file::make_collector(
+ keywords::target = logDirPath, // where to store rotated files
+ keywords::max_size = 64 * 1024 * 1024 * 1024, // maximum total size of the stored files, in bytes
+ keywords::min_free_space = 128 * 1024 * 1024 // minimum free space on the drive, in bytes
+ ));
+
+ sink->set_formatter
+ (
+ expr::format("%1%: %2%")
+ % getEpochTime()
+ % expr::smessage
+ );
+
+ // Add it to the core
+ logging::core::get()->add_sink(sink);
+}
diff --git a/src/nlsr_logger.hpp b/src/nlsr_logger.hpp
new file mode 100644
index 0000000..119f172
--- /dev/null
+++ b/src/nlsr_logger.hpp
@@ -0,0 +1,59 @@
+#ifndef NLSR_LOGGER_HPP
+#define NLSR_LOGGER_HPP
+
+#define BOOST_LOG_DYN_LINK 1
+
+#include <stdexcept>
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <pwd.h>
+#include <cstdlib>
+#include <string>
+#include <unistd.h>
+#include <boost/format.hpp>
+#include <boost/smart_ptr/shared_ptr.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/date_time/local_time/local_time.hpp>
+#include <boost/log/common.hpp>
+#include <boost/log/expressions.hpp>
+#include <boost/log/attributes.hpp>
+#include <boost/log/sources/logger.hpp>
+#include <boost/log/sinks/sync_frontend.hpp>
+#include <boost/log/sinks/text_file_backend.hpp>
+
+namespace logging = boost::log;
+namespace attrs = boost::log::attributes;
+namespace src = boost::log::sources;
+namespace sinks = boost::log::sinks;
+namespace expr = boost::log::expressions;
+namespace keywords = boost::log::keywords;
+
+using boost::shared_ptr;
+using namespace std;
+
+
+
+class NlsrLogger
+{
+public:
+ NlsrLogger()
+ {
+ }
+
+ void initNlsrLogger(std::string dirPath);
+
+ boost::log::sources::logger& getLogger()
+ {
+ return mLogger;
+ }
+
+private:
+ string getEpochTime();
+ string getUserHomeDirectory();
+
+private:
+ boost::log::sources::logger mLogger;
+};
+
+#endif
diff --git a/src/nlsr_lsa.hpp b/src/nlsr_lsa.hpp
index 0f3e528..b80d5a9 100644
--- a/src/nlsr_lsa.hpp
+++ b/src/nlsr_lsa.hpp
@@ -1,11 +1,13 @@
#ifndef NLSR_LSA_HPP
#define NLSR_LSA_HPP
+#include <ndn-cpp-dev/util/scheduler.hpp>
#include "nlsr_adjacent.hpp"
#include "nlsr_npl.hpp"
#include "nlsr_adl.hpp"
using namespace std;
+using namespace ndn;
class Lsa{
public:
@@ -13,6 +15,7 @@
: origRouter()
, lsSeqNo()
, lifeTime()
+ , lsaExpiringEventId()
{
}
@@ -56,12 +59,24 @@
{
lifeTime=lt;
}
- //string getLsaKey();
+
+ void setLsaExpiringEventId(ndn::EventId leei)
+ {
+ lsaExpiringEventId=leei;
+ }
+
+ ndn::EventId getLsaExpiringEventId()
+ {
+ return lsaExpiringEventId;
+ }
+
+
protected:
string origRouter;
uint8_t lsType;
uint32_t lsSeqNo;
uint32_t lifeTime;
+ ndn::EventId lsaExpiringEventId;
};
class NameLsa:public Lsa{
diff --git a/src/nlsr_lsdb.cpp b/src/nlsr_lsdb.cpp
index 9e3ffe7..de1343a 100644
--- a/src/nlsr_lsdb.cpp
+++ b/src/nlsr_lsdb.cpp
@@ -5,6 +5,12 @@
using namespace std;
+void
+Lsdb::cancelScheduleLsaExpiringEvent(nlsr& pnlsr, EventId eid)
+{
+ pnlsr.getScheduler().cancelEvent(eid);
+}
+
static bool
nameLsaCompareByKey(NameLsa& nlsa1, string& key){
return nlsa1.getNameLsaKey()==key;
@@ -42,10 +48,10 @@
}
-void
+ndn::EventId
Lsdb::scheduleNameLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime)
{
- pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
+ return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
this,boost::ref(pnlsr), key, seqNo));
}
@@ -76,8 +82,8 @@
{
timeToExpire=nlsa.getLifeTime();
}
- scheduleNameLsaExpiration( pnlsr, nlsa.getNameLsaKey(),
- nlsa.getLsSeqNo(), timeToExpire);
+ nlsa.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
+ nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
}
else
{
@@ -131,8 +137,10 @@
{
timeToExpire=nlsa.getLifeTime();
}
- scheduleNameLsaExpiration( pnlsr, nlsa.getNameLsaKey(),
- nlsa.getLsSeqNo(), timeToExpire);
+ cancelScheduleLsaExpiringEvent(pnlsr,
+ chkNameLsa.first.getLsaExpiringEventId());
+ chkNameLsa.first.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
+ nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
}
}
@@ -242,10 +250,10 @@
return std::make_pair(boost::ref(clsa),false);
}
-void
+ndn::EventId
Lsdb::scheduleCorLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime)
{
- pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
+ return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
this,boost::ref(pnlsr),key,seqNo));
}
@@ -298,8 +306,11 @@
{
timeToExpire=clsa.getLifeTime();
}
- scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
- clsa.getLsSeqNo(), timeToExpire);
+ cancelScheduleLsaExpiringEvent(pnlsr,
+ chkCorLsa.first.getLsaExpiringEventId());
+ chkCorLsa.first.setLsaExpiringEventId(scheduleCorLsaExpiration(pnlsr,
+ clsa.getCorLsaKey(),
+ clsa.getLsSeqNo(), timeToExpire));
}
}
@@ -444,10 +455,10 @@
-void
+ndn::EventId
Lsdb::scheduleAdjLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime)
{
- pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
+ return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
this,boost::ref(pnlsr),key,seqNo));
}
@@ -489,8 +500,10 @@
{
timeToExpire=alsa.getLifeTime();
}
- scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
- alsa.getLsSeqNo(),timeToExpire);
+ cancelScheduleLsaExpiringEvent(pnlsr,
+ chkAdjLsa.first.getLsaExpiringEventId());
+ chkAdjLsa.first.setLsaExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
+ alsa.getAdjLsaKey(), alsa.getLsSeqNo(),timeToExpire));
}
}
diff --git a/src/nlsr_lsdb.hpp b/src/nlsr_lsdb.hpp
index 62c91ee..a9c8fb3 100644
--- a/src/nlsr_lsdb.hpp
+++ b/src/nlsr_lsdb.hpp
@@ -54,16 +54,21 @@
bool addAdjLsa(AdjLsa &alsa);
bool doesAdjLsaExist(string key);
-
- void scheduleNameLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime);
+
+ ndn::EventId
+ scheduleNameLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime);
void exprireOrRefreshNameLsa(nlsr& pnlsr, string lsaKey, int seqNo);
- void scheduleAdjLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime);
+ ndn::EventId
+ scheduleAdjLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime);
void exprireOrRefreshAdjLsa(nlsr& pnlsr, string lsaKey, int seqNo);
- void scheduleCorLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime);
+ ndn::EventId
+ scheduleCorLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime);
void exprireOrRefreshCorLsa(nlsr& pnlsr, string lsaKey, int seqNo);
private:
+ void cancelScheduleLsaExpiringEvent(nlsr& pnlsr, EventId eid);
+
std::list<NameLsa> nameLsdb;
std::list<AdjLsa> adjLsdb;
std::list<CorLsa> corLsdb;
diff --git a/src/nlsr_npt.cpp b/src/nlsr_npt.cpp
index ba516e5..697d11c 100644
--- a/src/nlsr_npt.cpp
+++ b/src/nlsr_npt.cpp
@@ -28,7 +28,7 @@
newEntry.generateNhlfromRteList();
npteList.push_back(newEntry);
// update FIB here with nhl list newEntry.getNhl()
- pnlsr.getFib().updateFib(name,newEntry.getNhl(),
+ pnlsr.getFib().updateFib(pnlsr, name,newEntry.getNhl(),
pnlsr.getConfParameter().getMaxFacesPerPrefix());
}
else
@@ -36,7 +36,7 @@
(*it).addRoutingTableEntry(rte);
(*it).generateNhlfromRteList();
// update FIB here with nhl list from (*it).getNhl()
- pnlsr.getFib().updateFib(name,(*it).getNhl() ,
+ pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl() ,
pnlsr.getConfParameter().getMaxFacesPerPrefix());
}
}
@@ -58,14 +58,14 @@
{
npteList.erase(it); // remove entry from NPT
// remove FIB entry with this name
- pnlsr.getFib().removeFromFib(name);
+ pnlsr.getFib().removeFromFib(pnlsr,name);
}
else
{
(*it).generateNhlfromRteList();
// update FIB entry with new NHL
- pnlsr.getFib().updateFib(name,(*it).getNhl(),
+ pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl(),
pnlsr.getConfParameter().getMaxFacesPerPrefix());
}
}
diff --git a/src/nlsr_params.hpp b/src/nlsr_params.hpp
deleted file mode 100644
index a5f11c5..0000000
--- a/src/nlsr_params.hpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef NLSR_PARAMS_HPP
-#define NLSR_PARAMS_HPP
-
-class nlsrParams{
-
-
-
-
-
-};
-
-#endif