blob: 05f3e872c52df0fb24454ab407ebe55d0af7f4b3 [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"
akmhoque674b0b12014-05-20 14:33:28 -05005#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -05006
7namespace nlsr {
8
akmhoque674b0b12014-05-20 14:33:28 -05009INIT_LOGGER("SyncLogicHandler");
10
akmhoquefdbddb12014-05-02 18:35:19 -050011using namespace ndn;
12using namespace std;
13
akmhoque53353462014-04-22 08:43:45 -050014void
15SyncLogicHandler::createSyncSocket(Nlsr& pnlsr)
16{
akmhoque674b0b12014-05-20 14:33:28 -050017 _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
akmhoque53353462014-04-22 08:43:45 -050018 std::cout << "Creating Sync socket ......" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050019 std::cout << "Sync prefix: " << m_syncPrefix << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050020 m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator,
21 m_syncFace,
22 ndn::bind(&SyncLogicHandler::nsyncUpdateCallBack,
23 this, _1, _2, ndn::ref(pnlsr)),
24 ndn::bind(&SyncLogicHandler::nsyncRemoveCallBack,
25 this, _1, ndn::ref(pnlsr)));
akmhoque53353462014-04-22 08:43:45 -050026}
27
28void
29SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v,
30 Sync::SyncSocket* socket, Nlsr& pnlsr)
31{
akmhoque674b0b12014-05-20 14:33:28 -050032 _LOG_DEBUG("nsyncUpdateCallBack called ....");
akmhoque53353462014-04-22 08:43:45 -050033 std::cout << "nsyncUpdateCallBack called ...." << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -050034 int32_t n = v.size();
akmhoque157b0a42014-05-13 00:26:37 -050035 for (int32_t i = 0; i < n; i++){
akmhoque674b0b12014-05-20 14:33:28 -050036 _LOG_DEBUG("Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq());
akmhoque53353462014-04-22 08:43:45 -050037 std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() <<
38 endl;
39 processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr);
40 }
41}
42
43void
44SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr)
45{
akmhoque674b0b12014-05-20 14:33:28 -050046 _LOG_DEBUG("nsyncRemoveCallBack called ....");
akmhoque53353462014-04-22 08:43:45 -050047 std::cout << "nsyncRemoveCallBack called ...." << std::endl;
48}
49
50void
akmhoque31d1d4b2014-05-05 22:08:14 -050051SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix)
akmhoque53353462014-04-22 08:43:45 -050052{
53}
54
55void
akmhoque31d1d4b2014-05-05 22:08:14 -050056SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName,
akmhoque53353462014-04-22 08:43:45 -050057 uint64_t seqNo, Nlsr& pnlsr)
58{
akmhoque53353462014-04-22 08:43:45 -050059 string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -050060 int32_t lasPosition = util::getNameComponentPosition(updateName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -050061 if (lasPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050062 ndn::Name routerName = updateName.getSubName(lasPosition + 1);
akmhoque53353462014-04-22 08:43:45 -050063 processRoutingUpdateFromSync(routerName, seqNo, pnlsr);
akmhoque31d1d4b2014-05-05 22:08:14 -050064 return;
akmhoque53353462014-04-22 08:43:45 -050065 }
66}
67
68void
akmhoque31d1d4b2014-05-05 22:08:14 -050069SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName,
akmhoque53353462014-04-22 08:43:45 -050070 uint64_t seqNo, Nlsr& pnlsr)
71{
akmhoque31d1d4b2014-05-05 22:08:14 -050072 ndn::Name rName = routerName;
akmhoque157b0a42014-05-13 00:26:37 -050073 if (routerName != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -050074 SequencingManager sm(seqNo);
75 std::cout << sm;
akmhoque674b0b12014-05-20 14:33:28 -050076 //std::cout << "Router Name: " << routerName << endl;
akmhoque157b0a42014-05-13 00:26:37 -050077 try {
78 if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050079 std::cout << "Updated Name LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050080 _LOG_DEBUG("Updated Name LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -050081 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050082 interestName.append(routerName);
83 interestName.append("name");
84 interestName.appendNumber(sm.getNameLsaSeq());
85 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -050086 pnlsr.getConfParameter().getInterestResendTime(),
87 0);
akmhoque31d1d4b2014-05-05 22:08:14 -050088 }
89 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"),
akmhoque157b0a42014-05-13 00:26:37 -050090 sm.getAdjLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050091 std::cout << "Updated Adj LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050092 _LOG_DEBUG("Updated Adj LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -050093 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050094 interestName.append(routerName);
95 interestName.append("adjacency");
96 interestName.appendNumber(sm.getAdjLsaSeq());
97 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -050098 pnlsr.getConfParameter().getInterestResendTime(),
99 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 }
101 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -0500102 sm.getCorLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 std::cout << "Updated Cor LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500104 _LOG_DEBUG("Updated Cor LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500105 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 interestName.append(routerName);
107 interestName.append("coordinate");
108 interestName.appendNumber(sm.getCorLsaSeq());
109 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500110 pnlsr.getConfParameter().getInterestResendTime(),
111 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500112 }
akmhoque53353462014-04-22 08:43:45 -0500113 }
akmhoque157b0a42014-05-13 00:26:37 -0500114 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500115 std::cerr << e.what() << std::endl;
116 return;
akmhoque53353462014-04-22 08:43:45 -0500117 }
118 }
119}
120
akmhoque53353462014-04-22 08:43:45 -0500121void
122SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoque31d1d4b2014-05-05 22:08:14 -0500123 const ndn::Name& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500124{
125 sm.writeSeqNoToFile();
126 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
127}
128
akmhoque53353462014-04-22 08:43:45 -0500129void
akmhoque31d1d4b2014-05-05 22:08:14 -0500130SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix,
131 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500132{
133 std::cout << "Publishing Sync Update ......" << std::endl;
134 std::cout << "Update in prefix: " << updatePrefix << std::endl;
135 std::cout << "Seq No: " << seqNo << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500136 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo);
akmhoque53353462014-04-22 08:43:45 -0500137 ndn::Name updateName(updatePrefix);
138 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500139 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
140 1000,
akmhoque53353462014-04-22 08:43:45 -0500141 seqNo);
142}
143
144}//namespace nlsr