blob: 2570d2d34e1fbd74a82448eda958b739f26984dc [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());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700100 pnlsr.getLsdb().expressInterest(interestName, 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 }
akmhoque2f423352014-06-03 11:49:35 -0500102 if (pnlsr.getLsdb().isAdjLsaNew(rName.append("adjacency"), sm.getAdjLsaSeq())) {
akmhoque674b0b12014-05-20 14:33:28 -0500103 _LOG_DEBUG("Updated Adj LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500104 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500105 interestName.append(routerName);
106 interestName.append("adjacency");
107 interestName.appendNumber(sm.getAdjLsaSeq());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700108 pnlsr.getLsdb().expressInterest(interestName, 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 }
110 if (pnlsr.getLsdb().isCoordinateLsaNew(rName.append("coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -0500111 sm.getCorLsaSeq())) {
akmhoque674b0b12014-05-20 14:33:28 -0500112 _LOG_DEBUG("Updated Cor LSA. Need to fetch it");
akmhoque157b0a42014-05-13 00:26:37 -0500113 ndn::Name interestName(pnlsr.getConfParameter().getLsaPrefix());
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 interestName.append(routerName);
115 interestName.append("coordinate");
116 interestName.appendNumber(sm.getCorLsaSeq());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700117 pnlsr.getLsdb().expressInterest(interestName, 0);
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 }
akmhoque53353462014-04-22 08:43:45 -0500119 }
akmhoque157b0a42014-05-13 00:26:37 -0500120 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 std::cerr << e.what() << std::endl;
122 return;
akmhoque53353462014-04-22 08:43:45 -0500123 }
124 }
125}
126
akmhoque53353462014-04-22 08:43:45 -0500127void
128SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 const ndn::Name& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500130{
131 sm.writeSeqNoToFile();
132 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
133}
134
akmhoque53353462014-04-22 08:43:45 -0500135void
akmhoque31d1d4b2014-05-05 22:08:14 -0500136SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix,
137 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500138{
akmhoque674b0b12014-05-20 14:33:28 -0500139 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << "Seq no: " << seqNo);
akmhoque53353462014-04-22 08:43:45 -0500140 ndn::Name updateName(updatePrefix);
141 string data("NoData");
akmhoque31d1d4b2014-05-05 22:08:14 -0500142 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(),
143 1000,
akmhoque53353462014-04-22 08:43:45 -0500144 seqNo);
145}
146
147}//namespace nlsr