blob: dea378200b37e70a566e2730da01c393f6bec718 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include "nlsr.hpp"
24#include "sync-logic-handler.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050025#include "utility/name-helper.hpp"
26#include "lsa.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050028
29namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("SyncLogicHandler");
32
akmhoquefdbddb12014-05-02 18:35:19 -050033using namespace ndn;
34using namespace std;
35
akmhoque53353462014-04-22 08:43:45 -050036void
37SyncLogicHandler::createSyncSocket(Nlsr& pnlsr)
38{
akmhoque674b0b12014-05-20 14:33:28 -050039 _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
akmhoque157b0a42014-05-13 00:26:37 -050040 m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator,
41 m_syncFace,
42 ndn::bind(&SyncLogicHandler::nsyncUpdateCallBack,
43 this, _1, _2, ndn::ref(pnlsr)),
44 ndn::bind(&SyncLogicHandler::nsyncRemoveCallBack,
45 this, _1, ndn::ref(pnlsr)));
akmhoque53353462014-04-22 08:43:45 -050046}
47
48void
49SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v,
50 Sync::SyncSocket* socket, Nlsr& pnlsr)
51{
akmhoque674b0b12014-05-20 14:33:28 -050052 _LOG_DEBUG("nsyncUpdateCallBack called ....");
akmhoquefdbddb12014-05-02 18:35:19 -050053 int32_t n = v.size();
akmhoque157b0a42014-05-13 00:26:37 -050054 for (int32_t i = 0; i < n; i++){
akmhoque2f423352014-06-03 11:49:35 -050055 _LOG_DEBUG("Update Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq());
akmhoque53353462014-04-22 08:43:45 -050056 processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr);
57 }
58}
59
60void
61SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr)
62{
akmhoque674b0b12014-05-20 14:33:28 -050063 _LOG_DEBUG("nsyncRemoveCallBack called ....");
akmhoque53353462014-04-22 08:43:45 -050064}
65
66void
akmhoque31d1d4b2014-05-05 22:08:14 -050067SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix)
akmhoque53353462014-04-22 08:43:45 -050068{
69}
70
71void
akmhoque31d1d4b2014-05-05 22:08:14 -050072SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName,
akmhoque53353462014-04-22 08:43:45 -050073 uint64_t seqNo, Nlsr& pnlsr)
74{
akmhoque53353462014-04-22 08:43:45 -050075 string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -050076 int32_t lasPosition = util::getNameComponentPosition(updateName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -050077 if (lasPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050078 ndn::Name routerName = updateName.getSubName(lasPosition + 1);
akmhoque53353462014-04-22 08:43:45 -050079 processRoutingUpdateFromSync(routerName, seqNo, pnlsr);
akmhoque31d1d4b2014-05-05 22:08:14 -050080 return;
akmhoque53353462014-04-22 08:43:45 -050081 }
82}
83
84void
akmhoque31d1d4b2014-05-05 22:08:14 -050085SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName,
akmhoque53353462014-04-22 08:43:45 -050086 uint64_t seqNo, Nlsr& pnlsr)
87{
akmhoque31d1d4b2014-05-05 22:08:14 -050088 ndn::Name rName = routerName;
akmhoque157b0a42014-05-13 00:26:37 -050089 if (routerName != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -050090 SequencingManager sm(seqNo);
akmhoque2f423352014-06-03 11:49:35 -050091 sm.writeLog();
92 _LOG_DEBUG(routerName);
akmhoque157b0a42014-05-13 00:26:37 -050093 try {
94 if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) {
akmhoque674b0b12014-05-20 14:33:28 -050095 _LOG_DEBUG("Updated Name LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -050096 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -050097 interestName.append(routerName);
98 interestName.append("name");
99 interestName.appendNumber(sm.getNameLsaSeq());
100 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500101 pnlsr.getConfParameter().getInterestResendTime(),
102 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 }
akmhoque2f423352014-06-03 11:49:35 -0500104 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"), sm.getAdjLsaSeq())) {
akmhoque674b0b12014-05-20 14:33:28 -0500105 _LOG_DEBUG("Updated Adj LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500106 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 interestName.append(routerName);
108 interestName.append("adjacency");
109 interestName.appendNumber(sm.getAdjLsaSeq());
110 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500111 pnlsr.getConfParameter().getInterestResendTime(),
112 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 }
114 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -0500115 sm.getCorLsaSeq())) {
akmhoque674b0b12014-05-20 14:33:28 -0500116 _LOG_DEBUG("Updated Cor LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500117 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 interestName.append(routerName);
119 interestName.append("coordinate");
120 interestName.appendNumber(sm.getCorLsaSeq());
121 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500122 pnlsr.getConfParameter().getInterestResendTime(),
123 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 }
akmhoque53353462014-04-22 08:43:45 -0500125 }
akmhoque157b0a42014-05-13 00:26:37 -0500126 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 std::cerr << e.what() << std::endl;
128 return;
akmhoque53353462014-04-22 08:43:45 -0500129 }
130 }
131}
132
akmhoque53353462014-04-22 08:43:45 -0500133void
134SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoque31d1d4b2014-05-05 22:08:14 -0500135 const ndn::Name& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500136{
137 sm.writeSeqNoToFile();
138 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
139}
140
akmhoque53353462014-04-22 08:43:45 -0500141void
akmhoque31d1d4b2014-05-05 22:08:14 -0500142SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix,
143 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500144{
akmhoque674b0b12014-05-20 14:33:28 -0500145 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo);
akmhoque53353462014-04-22 08:43:45 -0500146 ndn::Name updateName(updatePrefix);
147 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500148 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
149 1000,
akmhoque53353462014-04-22 08:43:45 -0500150 seqNo);
151}
152
153}//namespace nlsr