blob: c0638c76977c1409f97c9942c0422fb2ac0fd985 [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
25#include "conf-parameter.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050026#include "logger.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050027#include "lsa.hpp"
28#include "lsdb.hpp"
29#include "sequencing-manager.hpp"
30#include "utility/name-helper.hpp"
31
32#include <boost/serialization/shared_ptr.hpp>
33
34using namespace boost::serialization;
akmhoque53353462014-04-22 08:43:45 -050035
36namespace nlsr {
37
akmhoque674b0b12014-05-20 14:33:28 -050038INIT_LOGGER("SyncLogicHandler");
39
akmhoquefdbddb12014-05-02 18:35:19 -050040using namespace ndn;
41using namespace std;
42
Vince Lehman904c2412014-09-23 19:36:11 -050043class SyncUpdate
44{
45public:
46 class Error : public std::runtime_error
47 {
48 public:
49 explicit
50 Error(const std::string& what)
51 : std::runtime_error(what)
52 {
53 }
54 };
55
56public:
57 SyncUpdate(const ndn::Name& name, uint64_t seqNo)
58 : m_name(name)
59 , m_seqManager(seqNo)
60 {
61 }
62
63 const ndn::Name&
64 getName() const
65 {
66 return m_name;
67 }
68
69 const ndn::Name
70 getOriginRouter() const
71 {
72 int32_t nlsrPosition = util::getNameComponentPosition(m_name, NLSR_COMPONENT);
73 int32_t lsaPosition = util::getNameComponentPosition(m_name, LSA_COMPONENT);
74
75 if (nlsrPosition < 0 || lsaPosition < 0) {
76 throw Error("Cannot parse update name because expected components are missing");
77 }
78
79 ndn::Name networkName = m_name.getSubName(0, nlsrPosition);
80 ndn::Name routerName = m_name.getSubName(lsaPosition + 1);
81
82 ndn::Name originRouter = networkName;
83 originRouter.append(routerName);
84
85 return originRouter;
86 }
87
88 uint64_t
89 getNameLsaSeqNo() const
90 {
91 return m_seqManager.getNameLsaSeq();
92 }
93
94 uint64_t
95 getAdjLsaSeqNo() const
96 {
97 return m_seqManager.getAdjLsaSeq();
98 }
99
100 uint64_t
101 getCorLsaSeqNo() const
102 {
103 return m_seqManager.getCorLsaSeq();
104 }
105
106 const SequencingManager&
107 getSequencingManager() const
108 {
109 return m_seqManager;
110 }
111
112private:
113 const ndn::Name m_name;
114 SequencingManager m_seqManager;
115
116 static const std::string NLSR_COMPONENT;
117 static const std::string LSA_COMPONENT;
118};
119
120const std::string SyncUpdate::NLSR_COMPONENT = "NLSR";
121const std::string SyncUpdate::LSA_COMPONENT = "LSA";
122
123const std::string SyncLogicHandler::NAME_COMPONENT = "name";
124const std::string SyncLogicHandler::ADJACENCY_COMPONENT = "adjacency";
125const std::string SyncLogicHandler::COORDINATE_COMPONENT = "coordinate";
126
akmhoque53353462014-04-22 08:43:45 -0500127void
Vince Lehman904c2412014-09-23 19:36:11 -0500128SyncLogicHandler::createSyncSocket()
akmhoque53353462014-04-22 08:43:45 -0500129{
akmhoque674b0b12014-05-20 14:33:28 -0500130 _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
Vince Lehman904c2412014-09-23 19:36:11 -0500131
132 // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory
133 // of the object
134 ndn::shared_ptr<ndn::Face> facePtr(&m_syncFace, null_deleter());
135
136 m_syncSocket = ndn::make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator, facePtr,
137 ndn::bind(&SyncLogicHandler::onNsyncUpdate,
138 this, _1, _2),
139 ndn::bind(&SyncLogicHandler::onNsyncRemoval,
140 this, _1));
akmhoque53353462014-04-22 08:43:45 -0500141}
142
143void
Vince Lehman904c2412014-09-23 19:36:11 -0500144SyncLogicHandler::onNsyncUpdate(const vector<Sync::MissingDataInfo>& v, Sync::SyncSocket* socket)
akmhoque53353462014-04-22 08:43:45 -0500145{
Vince Lehman904c2412014-09-23 19:36:11 -0500146 _LOG_DEBUG("Received Nsync update event");
147
148 for (size_t i = 0; i < v.size(); i++){
149 _LOG_DEBUG("Update Name: " << v[i].prefix << " Seq no: " << v[i].high.getSeq());
150
151 SyncUpdate update(v[i].prefix, v[i].high.getSeq());
152
153 processUpdateFromSync(update);
akmhoque53353462014-04-22 08:43:45 -0500154 }
155}
156
157void
Vince Lehman904c2412014-09-23 19:36:11 -0500158SyncLogicHandler::onNsyncRemoval(const string& prefix)
akmhoque53353462014-04-22 08:43:45 -0500159{
Vince Lehman904c2412014-09-23 19:36:11 -0500160 _LOG_DEBUG("Received Nsync removal event");
akmhoque53353462014-04-22 08:43:45 -0500161}
162
163void
Vince Lehman904c2412014-09-23 19:36:11 -0500164SyncLogicHandler::processUpdateFromSync(const SyncUpdate& update)
akmhoque53353462014-04-22 08:43:45 -0500165{
Vince Lehman904c2412014-09-23 19:36:11 -0500166 ndn::Name originRouter;
akmhoque53353462014-04-22 08:43:45 -0500167
Vince Lehman904c2412014-09-23 19:36:11 -0500168 try {
169 originRouter = update.getOriginRouter();
170 }
171 catch (std::exception& e) {
172 _LOG_WARN("Received malformed sync update");
akmhoque31d1d4b2014-05-05 22:08:14 -0500173 return;
akmhoque53353462014-04-22 08:43:45 -0500174 }
akmhoque53353462014-04-22 08:43:45 -0500175
Vince Lehman904c2412014-09-23 19:36:11 -0500176 // A router should not try to fetch its own LSA
177 if (originRouter != m_confParam.getRouterPrefix()) {
178
179 update.getSequencingManager().writeLog();
180
akmhoque157b0a42014-05-13 00:26:37 -0500181 try {
Vince Lehman904c2412014-09-23 19:36:11 -0500182 if (isLsaNew(originRouter, NAME_COMPONENT, update.getNameLsaSeqNo())) {
183 _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB");
184
185 expressInterestForLsa(update, NAME_COMPONENT, update.getNameLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500186 }
Vince Lehman904c2412014-09-23 19:36:11 -0500187
188 if (isLsaNew(originRouter, ADJACENCY_COMPONENT, update.getAdjLsaSeqNo())) {
189 _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB");
190
191 expressInterestForLsa(update, ADJACENCY_COMPONENT, update.getAdjLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500192 }
Vince Lehman904c2412014-09-23 19:36:11 -0500193
194 if (isLsaNew(originRouter, COORDINATE_COMPONENT, update.getCorLsaSeqNo())) {
195 _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB");
196
197 expressInterestForLsa(update, COORDINATE_COMPONENT, update.getCorLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500198 }
akmhoque53353462014-04-22 08:43:45 -0500199 }
akmhoque157b0a42014-05-13 00:26:37 -0500200 catch (std::exception& e) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500201 std::cerr << e.what() << std::endl;
202 return;
akmhoque53353462014-04-22 08:43:45 -0500203 }
204 }
205}
206
Vince Lehman904c2412014-09-23 19:36:11 -0500207bool
208SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType,
209 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500210{
Vince Lehman904c2412014-09-23 19:36:11 -0500211 ndn::Name lsaKey = originRouter;
212 lsaKey.append(lsaType);
213
214 if (lsaType == NAME_COMPONENT)
215 {
216 return m_lsdb.isNameLsaNew(lsaKey, seqNo);
217 }
218 else if (lsaType == ADJACENCY_COMPONENT)
219 {
220 return m_lsdb.isAdjLsaNew(lsaKey, seqNo);
221 }
222 else if (lsaType == COORDINATE_COMPONENT)
223 {
224 return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo);
225 }
226
227 return false;
akmhoque53353462014-04-22 08:43:45 -0500228}
229
akmhoque53353462014-04-22 08:43:45 -0500230void
Vince Lehman904c2412014-09-23 19:36:11 -0500231SyncLogicHandler::expressInterestForLsa(const SyncUpdate& update, std::string lsaType,
232 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500233{
Vince Lehman904c2412014-09-23 19:36:11 -0500234 ndn::Name interest(update.getName());
235 interest.append(lsaType);
236 interest.appendNumber(seqNo);
237
238 m_lsdb.expressInterest(interest, 0);
239}
240
241void
242SyncLogicHandler::publishRoutingUpdate(SequencingManager& manager, const ndn::Name& updatePrefix)
243{
244 manager.writeSeqNoToFile();
245 publishSyncUpdate(updatePrefix, manager.getCombinedSeqNo());
246}
247
248void
249SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo)
250{
251 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo);
252
akmhoque53353462014-04-22 08:43:45 -0500253 ndn::Name updateName(updatePrefix);
254 string data("NoData");
Vince Lehman904c2412014-09-23 19:36:11 -0500255
256 m_syncSocket->publishData(updateName.toUri(), 0, data.c_str(), data.size(), 1000, seqNo);
akmhoque53353462014-04-22 08:43:45 -0500257}
258
259}//namespace nlsr