blob: f4a42a26726c1129c06bd982f4fcb0beab7bbb74 [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,
86 pnlsr.getConfParameter().getInterestResendTime());
87 }
88 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"),
akmhoque157b0a42014-05-13 00:26:37 -050089 sm.getAdjLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050090 std::cout << "Updated Adj LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050091 _LOG_DEBUG("Updated Adj LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -050092 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050093 interestName.append(routerName);
94 interestName.append("adjacency");
95 interestName.appendNumber(sm.getAdjLsaSeq());
96 pnlsr.getLsdb().expressInterest(interestName,
97 pnlsr.getConfParameter().getInterestResendTime());
98 }
99 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -0500100 sm.getCorLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 std::cout << "Updated Cor LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500102 _LOG_DEBUG("Updated Cor LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500103 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 interestName.append(routerName);
105 interestName.append("coordinate");
106 interestName.appendNumber(sm.getCorLsaSeq());
107 pnlsr.getLsdb().expressInterest(interestName,
108 pnlsr.getConfParameter().getInterestResendTime());
109 }
akmhoque53353462014-04-22 08:43:45 -0500110 }
akmhoque157b0a42014-05-13 00:26:37 -0500111 catch (std::exception& e) {
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;
akmhoque674b0b12014-05-20 14:33:28 -0500133 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo);
akmhoque53353462014-04-22 08:43:45 -0500134 ndn::Name updateName(updatePrefix);
135 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500136 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
137 1000,
akmhoque53353462014-04-22 08:43:45 -0500138 seqNo);
139}
140
141}//namespace nlsr