blob: 85b1da09440a5cb6ee5f77ed2d8d543854317b7a [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;
akmhoque157b0a42014-05-13 00:26:37 -050017 m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator,
18 m_syncFace,
19 ndn::bind(&SyncLogicHandler::nsyncUpdateCallBack,
20 this, _1, _2, ndn::ref(pnlsr)),
21 ndn::bind(&SyncLogicHandler::nsyncRemoveCallBack,
22 this, _1, ndn::ref(pnlsr)));
akmhoque53353462014-04-22 08:43:45 -050023}
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();
akmhoque157b0a42014-05-13 00:26:37 -050031 for (int32_t i = 0; i < n; i++){
akmhoque53353462014-04-22 08:43:45 -050032 std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() <<
33 endl;
34 processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr);
35 }
36}
37
38void
39SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr)
40{
41 std::cout << "nsyncRemoveCallBack called ...." << std::endl;
42}
43
44void
akmhoque31d1d4b2014-05-05 22:08:14 -050045SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix)
akmhoque53353462014-04-22 08:43:45 -050046{
47}
48
49void
akmhoque31d1d4b2014-05-05 22:08:14 -050050SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName,
akmhoque53353462014-04-22 08:43:45 -050051 uint64_t seqNo, Nlsr& pnlsr)
52{
akmhoque53353462014-04-22 08:43:45 -050053 string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -050054 int32_t lasPosition = util::getNameComponentPosition(updateName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -050055 if (lasPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050056 ndn::Name routerName = updateName.getSubName(lasPosition + 1);
akmhoque53353462014-04-22 08:43:45 -050057 processRoutingUpdateFromSync(routerName, seqNo, pnlsr);
akmhoque31d1d4b2014-05-05 22:08:14 -050058 return;
akmhoque53353462014-04-22 08:43:45 -050059 }
60}
61
62void
akmhoque31d1d4b2014-05-05 22:08:14 -050063SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName,
akmhoque53353462014-04-22 08:43:45 -050064 uint64_t seqNo, Nlsr& pnlsr)
65{
akmhoque31d1d4b2014-05-05 22:08:14 -050066 ndn::Name rName = routerName;
akmhoque157b0a42014-05-13 00:26:37 -050067 if (routerName != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -050068 SequencingManager sm(seqNo);
69 std::cout << sm;
70 std::cout << "Router Name: " << routerName << endl;
akmhoque157b0a42014-05-13 00:26:37 -050071 try {
72 if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050073 std::cout << "Updated Name LSA. Need to fetch it" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050074 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050075 interestName.append(routerName);
76 interestName.append("name");
77 interestName.appendNumber(sm.getNameLsaSeq());
78 pnlsr.getLsdb().expressInterest(interestName,
79 pnlsr.getConfParameter().getInterestResendTime());
80 }
81 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"),
akmhoque157b0a42014-05-13 00:26:37 -050082 sm.getAdjLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050083 std::cout << "Updated Adj LSA. Need to fetch it" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050084 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050085 interestName.append(routerName);
86 interestName.append("adjacency");
87 interestName.appendNumber(sm.getAdjLsaSeq());
88 pnlsr.getLsdb().expressInterest(interestName,
89 pnlsr.getConfParameter().getInterestResendTime());
90 }
91 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -050092 sm.getCorLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050093 std::cout << "Updated Cor LSA. Need to fetch it" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050094 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050095 interestName.append(routerName);
96 interestName.append("coordinate");
97 interestName.appendNumber(sm.getCorLsaSeq());
98 pnlsr.getLsdb().expressInterest(interestName,
99 pnlsr.getConfParameter().getInterestResendTime());
100 }
akmhoque53353462014-04-22 08:43:45 -0500101 }
akmhoque157b0a42014-05-13 00:26:37 -0500102 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 std::cerr << e.what() << std::endl;
104 return;
akmhoque53353462014-04-22 08:43:45 -0500105 }
106 }
107}
108
akmhoque53353462014-04-22 08:43:45 -0500109void
110SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 const ndn::Name& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500112{
113 sm.writeSeqNoToFile();
114 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
115}
116
akmhoque53353462014-04-22 08:43:45 -0500117void
akmhoque31d1d4b2014-05-05 22:08:14 -0500118SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix,
119 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500120{
121 std::cout << "Publishing Sync Update ......" << std::endl;
122 std::cout << "Update in prefix: " << updatePrefix << std::endl;
123 std::cout << "Seq No: " << seqNo << std::endl;
124 ndn::Name updateName(updatePrefix);
125 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
127 1000,
akmhoque53353462014-04-22 08:43:45 -0500128 seqNo);
129}
130
131}//namespace nlsr