blob: 4725c9b10d109291b4f8ee520ca8e2ad5b621f60 [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 "sync-logic-handler.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050024
Vince Lehman0a7da612014-10-29 14:39:29 -050025#include "common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050026#include "conf-parameter.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050028#include "lsa.hpp"
29#include "lsdb.hpp"
30#include "sequencing-manager.hpp"
31#include "utility/name-helper.hpp"
32
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("SyncLogicHandler");
36
akmhoquefdbddb12014-05-02 18:35:19 -050037using namespace ndn;
38using namespace std;
39
Vince Lehman904c2412014-09-23 19:36:11 -050040class SyncUpdate
41{
42public:
43 class Error : public std::runtime_error
44 {
45 public:
46 explicit
47 Error(const std::string& what)
48 : std::runtime_error(what)
49 {
50 }
51 };
52
53public:
54 SyncUpdate(const ndn::Name& name, uint64_t seqNo)
55 : m_name(name)
56 , m_seqManager(seqNo)
57 {
58 }
59
60 const ndn::Name&
61 getName() const
62 {
63 return m_name;
64 }
65
66 const ndn::Name
67 getOriginRouter() const
68 {
69 int32_t nlsrPosition = util::getNameComponentPosition(m_name, NLSR_COMPONENT);
70 int32_t lsaPosition = util::getNameComponentPosition(m_name, LSA_COMPONENT);
71
72 if (nlsrPosition < 0 || lsaPosition < 0) {
73 throw Error("Cannot parse update name because expected components are missing");
74 }
75
76 ndn::Name networkName = m_name.getSubName(0, nlsrPosition);
77 ndn::Name routerName = m_name.getSubName(lsaPosition + 1);
78
79 ndn::Name originRouter = networkName;
80 originRouter.append(routerName);
81
82 return originRouter;
83 }
84
85 uint64_t
86 getNameLsaSeqNo() const
87 {
88 return m_seqManager.getNameLsaSeq();
89 }
90
91 uint64_t
92 getAdjLsaSeqNo() const
93 {
94 return m_seqManager.getAdjLsaSeq();
95 }
96
97 uint64_t
98 getCorLsaSeqNo() const
99 {
100 return m_seqManager.getCorLsaSeq();
101 }
102
103 const SequencingManager&
104 getSequencingManager() const
105 {
106 return m_seqManager;
107 }
108
109private:
110 const ndn::Name m_name;
111 SequencingManager m_seqManager;
112
113 static const std::string NLSR_COMPONENT;
114 static const std::string LSA_COMPONENT;
115};
116
117const std::string SyncUpdate::NLSR_COMPONENT = "NLSR";
118const std::string SyncUpdate::LSA_COMPONENT = "LSA";
119
Alexander Afanasyev7c8882f2014-10-28 12:12:15 -0700120template<class T>
121class NullDeleter
122{
123public:
124 void
125 operator()(T*)
126 {
127 }
128};
129
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600130SyncLogicHandler::SyncLogicHandler(ndn::Face& face,
131 Lsdb& lsdb, ConfParameter& conf,
132 SequencingManager& seqManager)
133 : m_validator(new ndn::ValidatorNull())
134 , m_syncFace(face)
135 , m_lsdb(lsdb)
136 , m_confParam(conf)
137 , m_sequencingManager(seqManager)
138{
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600139}
140
akmhoque53353462014-04-22 08:43:45 -0500141void
Vince Lehman904c2412014-09-23 19:36:11 -0500142SyncLogicHandler::createSyncSocket()
akmhoque53353462014-04-22 08:43:45 -0500143{
akmhoque674b0b12014-05-20 14:33:28 -0500144 _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
Vince Lehman904c2412014-09-23 19:36:11 -0500145
146 // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory
147 // of the object
Alexander Afanasyev7c8882f2014-10-28 12:12:15 -0700148 ndn::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
Vince Lehman904c2412014-09-23 19:36:11 -0500149
150 m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, facePtr,
151 ndn::bind(&SyncLogicHandler::onNsyncUpdate,
152 this, _1, _2),
153 ndn::bind(&SyncLogicHandler::onNsyncRemoval,
154 this, _1));
akmhoque53353462014-04-22 08:43:45 -0500155}
156
157void
Vince Lehman904c2412014-09-23 19:36:11 -0500158SyncLogicHandler::onNsyncUpdate(const vector<Sync::MissingDataInfo>& v, Sync::SyncSocket* socket)
akmhoque53353462014-04-22 08:43:45 -0500159{
Vince Lehman904c2412014-09-23 19:36:11 -0500160 _LOG_DEBUG("Received Nsync update event");
161
162 for (size_t i = 0; i < v.size(); i++){
163 _LOG_DEBUG("Update Name: " << v[i].prefix << " Seq no: " << v[i].high.getSeq());
164
165 SyncUpdate update(v[i].prefix, v[i].high.getSeq());
166
167 processUpdateFromSync(update);
akmhoque53353462014-04-22 08:43:45 -0500168 }
169}
170
171void
Vince Lehman904c2412014-09-23 19:36:11 -0500172SyncLogicHandler::onNsyncRemoval(const string& prefix)
akmhoque53353462014-04-22 08:43:45 -0500173{
Vince Lehman904c2412014-09-23 19:36:11 -0500174 _LOG_DEBUG("Received Nsync removal event");
akmhoque53353462014-04-22 08:43:45 -0500175}
176
177void
Vince Lehman904c2412014-09-23 19:36:11 -0500178SyncLogicHandler::processUpdateFromSync(const SyncUpdate& update)
akmhoque53353462014-04-22 08:43:45 -0500179{
Vince Lehman904c2412014-09-23 19:36:11 -0500180 ndn::Name originRouter;
akmhoque53353462014-04-22 08:43:45 -0500181
Vince Lehman904c2412014-09-23 19:36:11 -0500182 try {
183 originRouter = update.getOriginRouter();
184 }
185 catch (std::exception& e) {
186 _LOG_WARN("Received malformed sync update");
akmhoque31d1d4b2014-05-05 22:08:14 -0500187 return;
akmhoque53353462014-04-22 08:43:45 -0500188 }
akmhoque53353462014-04-22 08:43:45 -0500189
Vince Lehman904c2412014-09-23 19:36:11 -0500190 // A router should not try to fetch its own LSA
191 if (originRouter != m_confParam.getRouterPrefix()) {
192
193 update.getSequencingManager().writeLog();
194
akmhoque157b0a42014-05-13 00:26:37 -0500195 try {
alvy49b1c0c2014-12-19 13:57:46 -0600196 if (isLsaNew(originRouter, NameLsa::TYPE_STRING, update.getNameLsaSeqNo())) {
Vince Lehman904c2412014-09-23 19:36:11 -0500197 _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB");
198
alvy49b1c0c2014-12-19 13:57:46 -0600199 expressInterestForLsa(update, NameLsa::TYPE_STRING, update.getNameLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500200 }
Vince Lehman904c2412014-09-23 19:36:11 -0500201
alvy49b1c0c2014-12-19 13:57:46 -0600202 if (isLsaNew(originRouter, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo())) {
Vince Lehman904c2412014-09-23 19:36:11 -0500203 _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB");
204
alvy49b1c0c2014-12-19 13:57:46 -0600205 expressInterestForLsa(update, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500206 }
Vince Lehman904c2412014-09-23 19:36:11 -0500207
alvy49b1c0c2014-12-19 13:57:46 -0600208 if (isLsaNew(originRouter, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo())) {
Vince Lehman904c2412014-09-23 19:36:11 -0500209 _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB");
210
alvy49b1c0c2014-12-19 13:57:46 -0600211 expressInterestForLsa(update, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500212 }
akmhoque53353462014-04-22 08:43:45 -0500213 }
akmhoque157b0a42014-05-13 00:26:37 -0500214 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500215 std::cerr << e.what() << std::endl;
216 return;
akmhoque53353462014-04-22 08:43:45 -0500217 }
218 }
219}
220
Vince Lehman904c2412014-09-23 19:36:11 -0500221bool
222SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType,
223 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500224{
Vince Lehman904c2412014-09-23 19:36:11 -0500225 ndn::Name lsaKey = originRouter;
226 lsaKey.append(lsaType);
227
alvy49b1c0c2014-12-19 13:57:46 -0600228 if (lsaType == NameLsa::TYPE_STRING)
Vince Lehman904c2412014-09-23 19:36:11 -0500229 {
230 return m_lsdb.isNameLsaNew(lsaKey, seqNo);
231 }
alvy49b1c0c2014-12-19 13:57:46 -0600232 else if (lsaType == AdjLsa::TYPE_STRING)
Vince Lehman904c2412014-09-23 19:36:11 -0500233 {
234 return m_lsdb.isAdjLsaNew(lsaKey, seqNo);
235 }
alvy49b1c0c2014-12-19 13:57:46 -0600236 else if (lsaType == CoordinateLsa::TYPE_STRING)
Vince Lehman904c2412014-09-23 19:36:11 -0500237 {
238 return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo);
239 }
240
241 return false;
akmhoque53353462014-04-22 08:43:45 -0500242}
243
akmhoque53353462014-04-22 08:43:45 -0500244void
Vince Lehman904c2412014-09-23 19:36:11 -0500245SyncLogicHandler::expressInterestForLsa(const SyncUpdate& update, std::string lsaType,
246 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500247{
Vince Lehman904c2412014-09-23 19:36:11 -0500248 ndn::Name interest(update.getName());
249 interest.append(lsaType);
250 interest.appendNumber(seqNo);
251
252 m_lsdb.expressInterest(interest, 0);
253}
254
255void
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600256SyncLogicHandler::publishRoutingUpdate()
Vince Lehman904c2412014-09-23 19:36:11 -0500257{
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600258 m_sequencingManager.writeSeqNoToFile();
259
260 publishSyncUpdate(m_updatePrefix, m_sequencingManager.getCombinedSeqNo());
Vince Lehman904c2412014-09-23 19:36:11 -0500261}
262
263void
Vince Lehmanc11cc202015-01-20 11:41:33 -0600264SyncLogicHandler::buildUpdatePrefix()
265{
266 m_updatePrefix = m_confParam.getLsaPrefix();
267 m_updatePrefix.append(m_confParam.getSiteName());
268 m_updatePrefix.append(m_confParam.getRouterName());
269}
270
271void
Vince Lehman904c2412014-09-23 19:36:11 -0500272SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo)
273{
274 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo);
275
akmhoque53353462014-04-22 08:43:45 -0500276 ndn::Name updateName(updatePrefix);
277 string data("NoData");
Vince Lehman904c2412014-09-23 19:36:11 -0500278
279 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), 1000, seqNo);
akmhoque53353462014-04-22 08:43:45 -0500280}
281
282}//namespace nlsr