blob: 10a40ecebbf51a96687b2f1d9200fc949cf33dfa [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include "nlsr.hpp"
2#include "sync-logic-handler.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -05003#include "utility/name-helper.hpp"
4#include "lsa.hpp"
akmhoque53353462014-04-22 08:43:45 -05005
6
7namespace nlsr {
8
akmhoquefdbddb12014-05-02 18:35:19 -05009using namespace ndn;
10using namespace std;
11
akmhoque53353462014-04-22 08:43:45 -050012void
13SyncLogicHandler::createSyncSocket(Nlsr& pnlsr)
14{
15 std::cout << "Creating Sync socket ......" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050016 std::cout << "Sync prefix: " << m_syncPrefix << std::endl;
akmhoque53353462014-04-22 08:43:45 -050017 m_syncSocket = make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator,
18 m_syncFace,
19 bind(&SyncLogicHandler::nsyncUpdateCallBack, this,
20 _1, _2, boost::ref(pnlsr)),
21 bind(&SyncLogicHandler::nsyncRemoveCallBack, this,
22 _1, boost::ref(pnlsr)));
23}
24
25void
26SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v,
27 Sync::SyncSocket* socket, Nlsr& pnlsr)
28{
29 std::cout << "nsyncUpdateCallBack called ...." << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -050030 int32_t n = v.size();
31 for (int32_t i = 0; i < n; i++)
akmhoque53353462014-04-22 08:43:45 -050032 {
33 std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() <<
34 endl;
35 processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr);
36 }
37}
38
39void
40SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr)
41{
42 std::cout << "nsyncRemoveCallBack called ...." << std::endl;
43}
44
45void
akmhoque31d1d4b2014-05-05 22:08:14 -050046SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix)
akmhoque53353462014-04-22 08:43:45 -050047{
48}
49
50void
akmhoque31d1d4b2014-05-05 22:08:14 -050051SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName,
akmhoque53353462014-04-22 08:43:45 -050052 uint64_t seqNo, Nlsr& pnlsr)
53{
akmhoque31d1d4b2014-05-05 22:08:14 -050054 //const ndn::Name name(updateName);
akmhoque53353462014-04-22 08:43:45 -050055 string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -050056 int32_t lasPosition = util::getNameComponentPosition(updateName, chkString);
57 if (lasPosition >= 0)
akmhoque53353462014-04-22 08:43:45 -050058 {
akmhoque31d1d4b2014-05-05 22:08:14 -050059 ndn::Name routerName = updateName.getSubName(lasPosition + 1);
akmhoque53353462014-04-22 08:43:45 -050060 processRoutingUpdateFromSync(routerName, seqNo, pnlsr);
akmhoque31d1d4b2014-05-05 22:08:14 -050061 return;
akmhoque53353462014-04-22 08:43:45 -050062 }
63}
64
65void
akmhoque31d1d4b2014-05-05 22:08:14 -050066SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName,
akmhoque53353462014-04-22 08:43:45 -050067 uint64_t seqNo, Nlsr& pnlsr)
68{
akmhoque31d1d4b2014-05-05 22:08:14 -050069 ndn::Name rName = routerName;
akmhoque53353462014-04-22 08:43:45 -050070 if (routerName != pnlsr.getConfParameter().getRouterPrefix())
71 {
72 SequencingManager sm(seqNo);
73 std::cout << sm;
74 std::cout << "Router Name: " << routerName << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050075 try
akmhoque53353462014-04-22 08:43:45 -050076 {
akmhoque31d1d4b2014-05-05 22:08:14 -050077 if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq()))
78 {
79 std::cout << "Updated Name LSA. Need to fetch it" << std::endl;
80 ndn::Name interestName(pnlsr.getConfParameter().getChronosyncLsaPrefix());
81 interestName.append(routerName);
82 interestName.append("name");
83 interestName.appendNumber(sm.getNameLsaSeq());
84 pnlsr.getLsdb().expressInterest(interestName,
85 pnlsr.getConfParameter().getInterestResendTime());
86 }
87 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"),
88 sm.getAdjLsaSeq()))
89 {
90 std::cout << "Updated Adj LSA. Need to fetch it" << std::endl;
91 ndn::Name interestName(pnlsr.getConfParameter().getChronosyncLsaPrefix());
92 interestName.append(routerName);
93 interestName.append("adjacency");
94 interestName.appendNumber(sm.getAdjLsaSeq());
95 pnlsr.getLsdb().expressInterest(interestName,
96 pnlsr.getConfParameter().getInterestResendTime());
97 }
98 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
99 sm.getCorLsaSeq()))
100 {
101 std::cout << "Updated Cor LSA. Need to fetch it" << std::endl;
102 ndn::Name interestName(pnlsr.getConfParameter().getChronosyncLsaPrefix());
103 interestName.append(routerName);
104 interestName.append("coordinate");
105 interestName.appendNumber(sm.getCorLsaSeq());
106 pnlsr.getLsdb().expressInterest(interestName,
107 pnlsr.getConfParameter().getInterestResendTime());
108 }
akmhoque53353462014-04-22 08:43:45 -0500109 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500110 catch (std::exception& e)
akmhoque53353462014-04-22 08:43:45 -0500111 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500112 std::cerr << e.what() << std::endl;
113 return;
akmhoque53353462014-04-22 08:43:45 -0500114 }
115 }
116}
117
akmhoque53353462014-04-22 08:43:45 -0500118void
119SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoque31d1d4b2014-05-05 22:08:14 -0500120 const ndn::Name& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500121{
122 sm.writeSeqNoToFile();
123 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
124}
125
akmhoque53353462014-04-22 08:43:45 -0500126void
akmhoque31d1d4b2014-05-05 22:08:14 -0500127SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix,
128 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500129{
130 std::cout << "Publishing Sync Update ......" << std::endl;
131 std::cout << "Update in prefix: " << updatePrefix << std::endl;
132 std::cout << "Seq No: " << seqNo << std::endl;
133 ndn::Name updateName(updatePrefix);
134 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500135 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
136 1000,
akmhoque53353462014-04-22 08:43:45 -0500137 seqNo);
138}
139
140}//namespace nlsr