blob: 047b5d2950cc3df77fe907480a41ad9cc007d26e [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include "sync-logic-handler.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050023#include "common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050024#include "conf-parameter.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050025#include "lsa.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050026#include "utility/name-helper.hpp"
Nick Gordon9eac4d92017-08-29 17:31:29 -050027#include "logger.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050028
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("SyncLogicHandler");
32
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050033const std::string NLSR_COMPONENT = "NLSR";
34const std::string LSA_COMPONENT = "LSA";
Vince Lehman904c2412014-09-23 19:36:11 -050035
Alexander Afanasyev7c8882f2014-10-28 12:12:15 -070036template<class T>
37class NullDeleter
38{
39public:
40 void
41 operator()(T*)
42 {
43 }
44};
45
Nick Gordon9eac4d92017-08-29 17:31:29 -050046SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, ConfParameter& conf)
47 : onNewLsa(ndn::make_unique<OnNewLsa>())
48 , m_syncFace(face)
49 , m_isLsaNew(isLsaNew)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060050 , m_confParam(conf)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060051{
Vince Lehman0bcf9a32014-12-10 11:24:45 -060052}
53
akmhoque53353462014-04-22 08:43:45 -050054void
Vince Lehman9d097802015-03-16 17:55:59 -050055SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix)
akmhoque53353462014-04-22 08:43:45 -050056{
Vince Lehman9d097802015-03-16 17:55:59 -050057 if (m_syncSocket != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050058 NLSR_LOG_WARN("Trying to create Sync socket, but Sync socket already exists");
Vince Lehman9d097802015-03-16 17:55:59 -050059 return;
60 }
61
62 m_syncPrefix = syncPrefix;
63
64 // Build LSA sync update prefix
65 buildUpdatePrefix();
66
dmcoomes5bcb39e2017-10-31 15:07:55 -050067 NLSR_LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
Vince Lehman904c2412014-09-23 19:36:11 -050068
69 // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory
70 // of the object
dmcoomes9f936662017-03-02 10:33:09 -060071 std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
Vince Lehman904c2412014-09-23 19:36:11 -050072
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050073 m_syncSocket = std::make_shared<chronosync::Socket>(m_syncPrefix, m_nameLsaUserPrefix, *facePtr,
Nick Gordone98480b2017-05-24 11:23:03 -050074 std::bind(&SyncLogicHandler::onChronoSyncUpdate, this, _1));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050075
76 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
77 m_syncSocket->addSyncNode(m_adjLsaUserPrefix);
78 }
laqinfanac4b6562017-12-08 11:24:21 +000079 else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
80 m_syncSocket->addSyncNode(m_coorLsaUserPrefix);
81 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050082 else {
laqinfanac4b6562017-12-08 11:24:21 +000083 m_syncSocket->addSyncNode(m_adjLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050084 m_syncSocket->addSyncNode(m_coorLsaUserPrefix);
85 }
akmhoque53353462014-04-22 08:43:45 -050086}
87
88void
Nick Gordone98480b2017-05-24 11:23:03 -050089SyncLogicHandler::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& v)
akmhoque53353462014-04-22 08:43:45 -050090{
dmcoomes5bcb39e2017-10-31 15:07:55 -050091 NLSR_LOG_DEBUG("Received ChronoSync update event");
Vince Lehman904c2412014-09-23 19:36:11 -050092
93 for (size_t i = 0; i < v.size(); i++){
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050094 ndn::Name updateName = v[i].session.getPrefix(-1);
Vince Lehman904c2412014-09-23 19:36:11 -050095
dmcoomes5bcb39e2017-10-31 15:07:55 -050096 NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << v[i].high);
Vince Lehman904c2412014-09-23 19:36:11 -050097
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050098 int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT);
99 int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT);
100
101 if (nlsrPosition < 0 || lsaPosition < 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500102 NLSR_LOG_WARN("Received malformed sync update");
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500103 return;
104 }
105
106 ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
107 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
108
109 ndn::Name originRouter = networkName;
110 originRouter.append(routerName);
111
112 processUpdateFromSync(originRouter, updateName, v[i].high);
akmhoque53353462014-04-22 08:43:45 -0500113 }
114}
115
116void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500117SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
118 const ndn::Name& updateName, const uint64_t& seqNo)
akmhoque53353462014-04-22 08:43:45 -0500119{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500120 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -0500121
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500122 // A router should not try to fetch its own LSA
123 if (originRouter != m_confParam.getRouterPrefix()) {
Vince Lehman904c2412014-09-23 19:36:11 -0500124
Nick Gordon727d4832017-10-13 18:04:25 -0500125 Lsa::Type lsaType;
126 std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
Vince Lehman904c2412014-09-23 19:36:11 -0500127
dmcoomes5bcb39e2017-10-31 15:07:55 -0500128 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500129 << " sequence number than entry in LSDB");
130
Nick Gordon9eac4d92017-08-29 17:31:29 -0500131 if (m_isLsaNew(originRouter, lsaType, seqNo)) {
Nick Gordon727d4832017-10-13 18:04:25 -0500132 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500133 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500134 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing"
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500135 << " is enabled. Not going to fetch.");
136 return;
137 }
138
Nick Gordon727d4832017-10-13 18:04:25 -0500139 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500140 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500141 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state"
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500142 << " is enabled. Not going to fetch.");
143 return;
144 }
Nick Gordon9eac4d92017-08-29 17:31:29 -0500145 (*onNewLsa)(updateName, seqNo);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500146 }
akmhoque53353462014-04-22 08:43:45 -0500147 }
148}
149
Vince Lehman904c2412014-09-23 19:36:11 -0500150void
Nick Gordon727d4832017-10-13 18:04:25 -0500151SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500152{
Vince Lehman9d097802015-03-16 17:55:59 -0500153 if (m_syncSocket == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500154 NLSR_LOG_FATAL("Cannot publish routing update; SyncSocket does not exist");
Vince Lehman9d097802015-03-16 17:55:59 -0500155
dmcoomes9f936662017-03-02 10:33:09 -0600156 BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncSocket does not exist"));
Vince Lehman9d097802015-03-16 17:55:59 -0500157 }
158
Nick Gordon727d4832017-10-13 18:04:25 -0500159
160 switch (type) {
161 case Lsa::Type::ADJACENCY:
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500162 publishSyncUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500163 break;
164 case Lsa::Type::COORDINATE:
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500165 publishSyncUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500166 break;
167 case Lsa::Type::NAME:
168 publishSyncUpdate(m_nameLsaUserPrefix, seqNo);
169 break;
170 default:
171 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500172 }
Vince Lehman904c2412014-09-23 19:36:11 -0500173}
174
175void
Vince Lehmanc11cc202015-01-20 11:41:33 -0600176SyncLogicHandler::buildUpdatePrefix()
177{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500178 ndn::Name updatePrefix = m_confParam.getLsaPrefix();
179 updatePrefix.append(m_confParam.getSiteName());
180 updatePrefix.append(m_confParam.getRouterName());
181
182 m_nameLsaUserPrefix = updatePrefix;
Nick Gordon727d4832017-10-13 18:04:25 -0500183 m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500184
185 m_adjLsaUserPrefix = updatePrefix;
Nick Gordon727d4832017-10-13 18:04:25 -0500186 m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500187
188 m_coorLsaUserPrefix = updatePrefix;
Nick Gordon727d4832017-10-13 18:04:25 -0500189 m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehmanc11cc202015-01-20 11:41:33 -0600190}
191
192void
Vince Lehman904c2412014-09-23 19:36:11 -0500193SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo)
194{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500195 NLSR_LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo);
Vince Lehman904c2412014-09-23 19:36:11 -0500196
akmhoque53353462014-04-22 08:43:45 -0500197 ndn::Name updateName(updatePrefix);
Nick Gordone98480b2017-05-24 11:23:03 -0500198 std::string data("NoData");
Vince Lehman904c2412014-09-23 19:36:11 -0500199
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600200 m_syncSocket->publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
201 ndn::time::milliseconds(1000), seqNo, updateName);
akmhoque53353462014-04-22 08:43:45 -0500202}
203
Nick Gordonfad8e252016-08-11 14:21:38 -0500204} // namespace nlsr