blob: 65039bff16441978a8c367c4b3b5d2434d6beaf8 [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);
akmhoque53353462014-04-22 08:43:45 -050040 std::cout << "Creating Sync socket ......" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050041 std::cout << "Sync prefix: " << m_syncPrefix << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050042 m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator,
43 m_syncFace,
44 ndn::bind(&SyncLogicHandler::nsyncUpdateCallBack,
45 this, _1, _2, ndn::ref(pnlsr)),
46 ndn::bind(&SyncLogicHandler::nsyncRemoveCallBack,
47 this, _1, ndn::ref(pnlsr)));
akmhoque53353462014-04-22 08:43:45 -050048}
49
50void
51SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v,
52 Sync::SyncSocket* socket, Nlsr& pnlsr)
53{
akmhoque674b0b12014-05-20 14:33:28 -050054 _LOG_DEBUG("nsyncUpdateCallBack called ....");
akmhoque53353462014-04-22 08:43:45 -050055 std::cout << "nsyncUpdateCallBack called ...." << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -050056 int32_t n = v.size();
akmhoque157b0a42014-05-13 00:26:37 -050057 for (int32_t i = 0; i < n; i++){
akmhoque674b0b12014-05-20 14:33:28 -050058 _LOG_DEBUG("Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq());
akmhoque53353462014-04-22 08:43:45 -050059 std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() <<
60 endl;
61 processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr);
62 }
63}
64
65void
66SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr)
67{
akmhoque674b0b12014-05-20 14:33:28 -050068 _LOG_DEBUG("nsyncRemoveCallBack called ....");
akmhoque53353462014-04-22 08:43:45 -050069 std::cout << "nsyncRemoveCallBack called ...." << std::endl;
70}
71
72void
akmhoque31d1d4b2014-05-05 22:08:14 -050073SyncLogicHandler::removeRouterFromSyncing(const ndn::Name& routerPrefix)
akmhoque53353462014-04-22 08:43:45 -050074{
75}
76
77void
akmhoque31d1d4b2014-05-05 22:08:14 -050078SyncLogicHandler::processUpdateFromSync(const ndn::Name& updateName,
akmhoque53353462014-04-22 08:43:45 -050079 uint64_t seqNo, Nlsr& pnlsr)
80{
akmhoque53353462014-04-22 08:43:45 -050081 string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -050082 int32_t lasPosition = util::getNameComponentPosition(updateName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -050083 if (lasPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050084 ndn::Name routerName = updateName.getSubName(lasPosition + 1);
akmhoque53353462014-04-22 08:43:45 -050085 processRoutingUpdateFromSync(routerName, seqNo, pnlsr);
akmhoque31d1d4b2014-05-05 22:08:14 -050086 return;
akmhoque53353462014-04-22 08:43:45 -050087 }
88}
89
90void
akmhoque31d1d4b2014-05-05 22:08:14 -050091SyncLogicHandler::processRoutingUpdateFromSync(const ndn::Name& routerName,
akmhoque53353462014-04-22 08:43:45 -050092 uint64_t seqNo, Nlsr& pnlsr)
93{
akmhoque31d1d4b2014-05-05 22:08:14 -050094 ndn::Name rName = routerName;
akmhoque157b0a42014-05-13 00:26:37 -050095 if (routerName != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -050096 SequencingManager sm(seqNo);
97 std::cout << sm;
akmhoque674b0b12014-05-20 14:33:28 -050098 //std::cout << "Router Name: " << routerName << endl;
akmhoque157b0a42014-05-13 00:26:37 -050099 try {
100 if (pnlsr.getLsdb().isNameLsaNew(rName.append("name"), sm.getNameLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 std::cout << "Updated Name LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500102 _LOG_DEBUG("Updated Name 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("name");
106 interestName.appendNumber(sm.getNameLsaSeq());
107 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500108 pnlsr.getConfParameter().getInterestResendTime(),
109 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500110 }
111 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"),
akmhoque157b0a42014-05-13 00:26:37 -0500112 sm.getAdjLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 std::cout << "Updated Adj LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500114 _LOG_DEBUG("Updated Adj LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500115 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500116 interestName.append(routerName);
117 interestName.append("adjacency");
118 interestName.appendNumber(sm.getAdjLsaSeq());
119 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500120 pnlsr.getConfParameter().getInterestResendTime(),
121 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 }
123 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -0500124 sm.getCorLsaSeq())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 std::cout << "Updated Cor LSA. Need to fetch it" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500126 _LOG_DEBUG("Updated Cor LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500127 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 interestName.append(routerName);
129 interestName.append("coordinate");
130 interestName.appendNumber(sm.getCorLsaSeq());
131 pnlsr.getLsdb().expressInterest(interestName,
akmhoque06986672014-05-27 13:55:53 -0500132 pnlsr.getConfParameter().getInterestResendTime(),
133 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500134 }
akmhoque53353462014-04-22 08:43:45 -0500135 }
akmhoque157b0a42014-05-13 00:26:37 -0500136 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500137 std::cerr << e.what() << std::endl;
138 return;
akmhoque53353462014-04-22 08:43:45 -0500139 }
140 }
141}
142
akmhoque53353462014-04-22 08:43:45 -0500143void
144SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 const ndn::Name& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500146{
147 sm.writeSeqNoToFile();
148 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
149}
150
akmhoque53353462014-04-22 08:43:45 -0500151void
akmhoque31d1d4b2014-05-05 22:08:14 -0500152SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix,
153 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500154{
155 std::cout << "Publishing Sync Update ......" << std::endl;
156 std::cout << "Update in prefix: " << updatePrefix << std::endl;
157 std::cout << "Seq No: " << seqNo << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500158 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo);
akmhoque53353462014-04-22 08:43:45 -0500159 ndn::Name updateName(updatePrefix);
160 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500161 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
162 1000,
akmhoque53353462014-04-22 08:43:45 -0500163 seqNo);
164}
165
166}//namespace nlsr