Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, The University of Memphis, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "nlsrc.hpp" |
| 23 | |
| 24 | #include "version.hpp" |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 25 | #include "src/publisher/dataset-interest-handler.hpp" |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 26 | |
| 27 | #include <ndn-cxx/face.hpp> |
| 28 | #include <ndn-cxx/data.hpp> |
| 29 | #include <ndn-cxx/interest.hpp> |
| 30 | #include <ndn-cxx/encoding/block.hpp> |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 32 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 33 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/security/key-chain.hpp> |
| 35 | #include <ndn-cxx/security/command-interest-signer.hpp> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 36 | |
| 37 | #include <iostream> |
| 38 | |
| 39 | namespace nlsrc { |
| 40 | |
| 41 | const ndn::Name Nlsrc::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr"); |
| 42 | const ndn::Name Nlsrc::LSDB_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("lsdb"); |
| 43 | const ndn::Name Nlsrc::NAME_UPDATE_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("prefix-update"); |
| 44 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 45 | const ndn::Name Nlsrc::RT_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("routing-table"); |
| 46 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 47 | const uint32_t Nlsrc::ERROR_CODE_TIMEOUT = 10060; |
| 48 | const uint32_t Nlsrc::RESPONSE_CODE_SUCCESS = 200; |
| 49 | |
| 50 | Nlsrc::Nlsrc(ndn::Face& face) |
| 51 | : m_face(face) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | Nlsrc::printUsage() |
| 57 | { |
| 58 | std::cout << "Usage:\n" << programName << " [-h] [-V] COMMAND [<Command Options>]\n" |
| 59 | " -h print usage and exit\n" |
| 60 | " -V print version and exit\n" |
| 61 | "\n" |
| 62 | " COMMAND can be one of the following:\n" |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 63 | " lsdb\n" |
| 64 | " display NLSR lsdb status\n" |
| 65 | " routing\n" |
| 66 | " display routing table status\n" |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 67 | " status\n" |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 68 | " display all NLSR status (lsdb & routingtable)\n" |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 69 | " advertise name\n" |
| 70 | " advertise a name prefix through NLSR\n" |
| 71 | " withdraw name\n" |
| 72 | " remove a name prefix advertised through NLSR" |
| 73 | << std::endl; |
| 74 | } |
| 75 | |
| 76 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 77 | Nlsrc::getStatus(const std::string& command) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 78 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 79 | if (command == "lsdb") { |
| 80 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this)); |
| 81 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this)); |
| 82 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this)); |
| 83 | m_fetchSteps.push_back(std::bind(&Nlsrc::printLsdb, this)); |
| 84 | } |
| 85 | else if (command == "routing") { |
| 86 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchRtables, this)); |
| 87 | m_fetchSteps.push_back(std::bind(&Nlsrc::printRT, this)); |
| 88 | } |
| 89 | else if(command == "status") { |
| 90 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this)); |
| 91 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this)); |
| 92 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this)); |
| 93 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchRtables, this)); |
| 94 | m_fetchSteps.push_back(std::bind(&Nlsrc::printAll, this)); |
| 95 | } |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 96 | runNextStep(); |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | Nlsrc::dispatch(const std::string& command) |
| 101 | { |
| 102 | if (command == "advertise") { |
| 103 | if (nOptions != 1) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | advertiseName(); |
| 108 | return true; |
| 109 | } |
| 110 | else if (command == "withdraw") { |
| 111 | if (nOptions != 1) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | withdrawName(); |
| 116 | return true; |
| 117 | } |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 118 | else if ((command == "lsdb")|| (command == "routing")||(command == "status")) { |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 119 | if (nOptions != 0) { |
| 120 | return false; |
| 121 | } |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 122 | commandString = command; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 123 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 124 | getStatus(command); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | Nlsrc::runNextStep() |
| 133 | { |
| 134 | if (m_fetchSteps.empty()) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | std::function<void()> nextStep = m_fetchSteps.front(); |
| 139 | m_fetchSteps.pop_front(); |
| 140 | |
| 141 | nextStep(); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | Nlsrc::advertiseName() |
| 146 | { |
| 147 | ndn::Name name = commandLineArguments[0]; |
| 148 | ndn::Name::Component verb("advertise"); |
| 149 | std::string info = "(Advertise: " + name.toUri() + ")"; |
| 150 | |
| 151 | sendNamePrefixUpdate(name, verb, info); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | Nlsrc::withdrawName() |
| 156 | { |
| 157 | ndn::Name name = commandLineArguments[0]; |
| 158 | ndn::Name::Component verb("withdraw"); |
| 159 | std::string info = "(Withdraw: " + name.toUri() + ")"; |
| 160 | |
| 161 | sendNamePrefixUpdate(name, verb, info); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | Nlsrc::sendNamePrefixUpdate(const ndn::Name& name, |
| 166 | const ndn::Name::Component& verb, |
| 167 | const std::string& info) |
| 168 | { |
| 169 | ndn::nfd::ControlParameters parameters; |
| 170 | parameters.setName(name); |
| 171 | |
| 172 | ndn::Name commandName = NAME_UPDATE_PREFIX; |
| 173 | commandName.append(verb); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 174 | commandName.append(parameters.wireEncode()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 175 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 176 | ndn::security::CommandInterestSigner cis(m_keyChain); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 177 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 178 | ndn::Interest commandInterest = |
| 179 | cis.makeCommandInterest(commandName, |
| 180 | ndn::security::signingByIdentity(m_keyChain.getPib(). |
| 181 | getDefaultIdentity())); |
| 182 | |
| 183 | commandInterest.setMustBeFresh(true); |
| 184 | |
| 185 | m_face.expressInterest(commandInterest, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 186 | std::bind(&Nlsrc::onControlResponse, this, info, _2), |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 187 | std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Nack"), |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 188 | std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Timeout")); |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | Nlsrc::onControlResponse(const std::string& info, const ndn::Data& data) |
| 193 | { |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 194 | if (data.getMetaInfo().getType() == ndn::tlv::ContentType_Nack) { |
| 195 | std::cerr << "ERROR: Run-time advertise/withdraw disabled" << std::endl; |
| 196 | return; |
| 197 | } |
| 198 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 199 | ndn::nfd::ControlResponse response; |
| 200 | |
| 201 | try { |
| 202 | response.wireDecode(data.getContent().blockFromValue()); |
| 203 | } |
| 204 | catch (const std::exception& e) { |
| 205 | std::cerr << "ERROR: Control response decoding error" << std::endl; |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | uint32_t code = response.getCode(); |
| 210 | |
| 211 | if (code != RESPONSE_CODE_SUCCESS) { |
| 212 | std::cerr << "Name prefix update error (code: " << code << ")" << std::endl; |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | std::cout << "Applied Name prefix update successfully: " << info << std::endl; |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | Nlsrc::fetchAdjacencyLsas() |
| 221 | { |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 222 | fetchFromLsdb<nlsr::tlv::AdjacencyLsa>(nlsr::dataset::ADJACENCY_COMPONENT, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 223 | std::bind(&Nlsrc::recordAdjacencyLsa, this, _1)); |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | Nlsrc::fetchCoordinateLsas() |
| 228 | { |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 229 | fetchFromLsdb<nlsr::tlv::CoordinateLsa>(nlsr::dataset::COORDINATE_COMPONENT, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 230 | std::bind(&Nlsrc::recordCoordinateLsa, this, _1)); |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | Nlsrc::fetchNameLsas() |
| 235 | { |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 236 | fetchFromLsdb<nlsr::tlv::NameLsa>(nlsr::dataset::NAME_COMPONENT, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 237 | std::bind(&Nlsrc::recordNameLsa, this, _1)); |
| 238 | } |
| 239 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 240 | void |
| 241 | Nlsrc::fetchRtables() |
| 242 | { |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 243 | fetchFromRt<nlsr::tlv::RoutingTableStatus>( |
| 244 | [this] (const nlsr::tlv::RoutingTableStatus& rts) { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 245 | recordRtable(rts); |
| 246 | }); |
| 247 | } |
| 248 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 249 | template <class T> |
| 250 | void |
| 251 | Nlsrc::fetchFromLsdb(const ndn::Name::Component& datasetType, |
| 252 | const std::function<void(const T&)>& recordLsa) |
| 253 | { |
| 254 | ndn::Name command = LSDB_PREFIX; |
| 255 | command.append(datasetType); |
| 256 | |
| 257 | ndn::Interest interest(command); |
| 258 | |
Ashlesh Gawande | 05cb728 | 2018-08-30 14:39:41 -0500 | [diff] [blame] | 259 | auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, m_validator); |
| 260 | fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordLsa)); |
| 261 | fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2)); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | template <class T> |
| 265 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 266 | Nlsrc::fetchFromRt(const std::function<void(const T&)>& recordDataset) |
| 267 | { |
| 268 | ndn::Name command = RT_PREFIX; |
| 269 | |
| 270 | ndn::Interest interest(command); |
| 271 | |
Ashlesh Gawande | 05cb728 | 2018-08-30 14:39:41 -0500 | [diff] [blame] | 272 | auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, m_validator); |
| 273 | fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordDataset)); |
| 274 | fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2)); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | template <class T> |
| 278 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 279 | Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 280 | const std::function<void(const T&)>& recordDataset) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 281 | { |
| 282 | ndn::Block block; |
| 283 | size_t offset = 0; |
| 284 | |
| 285 | while (offset < data->size()) { |
| 286 | bool isOk = false; |
| 287 | std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset); |
| 288 | |
| 289 | if (!isOk) { |
| 290 | std::cerr << "ERROR: cannot decode LSA TLV" << std::endl; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | offset += block.size(); |
| 295 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 296 | T data(block); |
| 297 | recordDataset(data); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | runNextStep(); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | Nlsrc::onTimeout(uint32_t errorCode, const std::string& error) |
| 305 | { |
| 306 | std::cerr << "Request timed out (code: " << errorCode |
| 307 | << ", error: " << error << ")" << std::endl; |
| 308 | } |
| 309 | |
| 310 | std::string |
| 311 | Nlsrc::getLsaInfoString(const nlsr::tlv::LsaInfo& info) |
| 312 | { |
| 313 | std::ostringstream os; |
| 314 | os << " info=" << info; |
| 315 | |
| 316 | return os.str(); |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | Nlsrc::recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa) |
| 321 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 322 | Router& router = getRouterLsdb(lsa.getLsaInfo()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 323 | |
| 324 | std::ostringstream os; |
| 325 | os << " AdjacencyLsa:" << std::endl; |
| 326 | |
| 327 | os << getLsaInfoString(lsa.getLsaInfo()) << std::endl; |
| 328 | |
| 329 | for (const auto& adjacency : lsa.getAdjacencies()) { |
| 330 | os << " adjacency=" << adjacency << std::endl; |
| 331 | } |
| 332 | |
| 333 | router.adjacencyLsaString = os.str(); |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | Nlsrc::recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa) |
| 338 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 339 | Router& router = getRouterLsdb(lsa.getLsaInfo()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 340 | |
| 341 | std::ostringstream os; |
| 342 | os << " Coordinate LSA:" << std::endl; |
| 343 | |
| 344 | os << getLsaInfoString(lsa.getLsaInfo()) << std::endl; |
| 345 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 346 | int i = 0; |
| 347 | for (auto const& value: lsa.getHyperbolicAngle()) { |
| 348 | os << " Hyp Angle " << i++ << ": "<< value << " "; |
| 349 | } |
| 350 | os << "\n radius=" << lsa.getHyperbolicRadius() << std::endl; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 351 | |
| 352 | router.coordinateLsaString = os.str(); |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | Nlsrc::recordNameLsa(const nlsr::tlv::NameLsa& lsa) |
| 357 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 358 | Router& router = getRouterLsdb(lsa.getLsaInfo()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 359 | |
| 360 | std::ostringstream os; |
| 361 | os << " Name LSA:" << std::endl; |
| 362 | |
| 363 | os << getLsaInfoString(lsa.getLsaInfo()) << std::endl; |
| 364 | |
| 365 | for (const auto& name : lsa.getNames()) { |
| 366 | os << " name=" << name << std::endl; |
| 367 | } |
| 368 | |
| 369 | router.nameLsaString = os.str(); |
| 370 | } |
| 371 | |
| 372 | void |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 373 | Nlsrc::recordRtable(const nlsr::tlv::RoutingTableStatus& rts) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 374 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 375 | std::ostringstream os; |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 376 | for (const auto& rt : rts.getRoutingtable()) { |
| 377 | os << rt << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 378 | } |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 379 | m_rtString = os.str(); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 383 | Nlsrc::printLsdb() |
| 384 | { |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 385 | std::cout << "LSDB:" << std::endl; |
| 386 | |
| 387 | for (const auto& item : m_routers) { |
| 388 | std::cout << " OriginRouter: " << item.first << std::endl; |
| 389 | std::cout << std::endl; |
| 390 | |
| 391 | const Router& router = item.second; |
| 392 | |
| 393 | if (!router.adjacencyLsaString.empty()) { |
| 394 | std::cout << router.adjacencyLsaString << std::endl; |
| 395 | } |
| 396 | |
| 397 | if (!router.coordinateLsaString.empty()) { |
| 398 | std::cout << router.coordinateLsaString << std::endl; |
| 399 | } |
| 400 | |
| 401 | if (!router.nameLsaString.empty()) { |
| 402 | std::cout << router.nameLsaString << std::endl; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 407 | void |
| 408 | Nlsrc::printRT() |
| 409 | { |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 410 | if (!m_rtString.empty()) { |
| 411 | std::cout << "Routing Table" << std::endl; |
| 412 | std::cout << m_rtString << std::endl; |
| 413 | } |
| 414 | else { |
| 415 | std::cout << "Routing Table is not calculated yet" << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | Nlsrc::printAll() |
| 421 | { |
| 422 | std::cout << "NLSR Status" << std::endl; |
| 423 | printLsdb(); |
| 424 | printRT(); |
| 425 | } |
| 426 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 427 | Nlsrc::Router& |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 428 | Nlsrc::getRouterLsdb(const nlsr::tlv::LsaInfo& info) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 429 | { |
| 430 | const ndn::Name& originRouterName = info.getOriginRouter(); |
| 431 | |
| 432 | const auto& pair = |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 433 | m_routers.insert(std::make_pair(originRouterName, Router())); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 434 | |
| 435 | return pair.first->second; |
| 436 | } |
| 437 | |
| 438 | } // namespace nlsrc |
| 439 | |
| 440 | //////////////////////////////////////////////////////////////////////////////// |
| 441 | //////////////////////////////////////////////////////////////////////////////// |
| 442 | |
| 443 | int |
| 444 | main(int argc, char** argv) |
| 445 | { |
| 446 | ndn::Face face; |
| 447 | nlsrc::Nlsrc nlsrc(face); |
| 448 | |
| 449 | nlsrc.programName = argv[0]; |
| 450 | |
| 451 | if (argc < 2) { |
| 452 | nlsrc.printUsage(); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | int opt; |
| 457 | while ((opt = ::getopt(argc, argv, "hV")) != -1) { |
| 458 | switch (opt) { |
| 459 | case 'h': |
| 460 | nlsrc.printUsage(); |
| 461 | return 0; |
| 462 | case 'V': |
| 463 | std::cout << NLSR_VERSION_BUILD_STRING << std::endl; |
| 464 | return 0; |
| 465 | default: |
| 466 | nlsrc.printUsage(); |
| 467 | return 1; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (argc == ::optind) { |
| 472 | nlsrc.printUsage(); |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | try { |
| 477 | ::optind = 2; // Set ::optind to the command's index |
| 478 | |
| 479 | nlsrc.commandLineArguments = argv + ::optind; |
| 480 | nlsrc.nOptions = argc - ::optind; |
| 481 | |
| 482 | // argv[1] points to the command, so pass it to the dispatch |
| 483 | bool isOk = nlsrc.dispatch(argv[1]); |
| 484 | if (!isOk) { |
| 485 | nlsrc.printUsage(); |
| 486 | return 1; |
| 487 | } |
| 488 | |
| 489 | face.processEvents(); |
| 490 | } |
| 491 | catch (const std::exception& e) { |
| 492 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 493 | return 2; |
| 494 | } |
| 495 | return 0; |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 496 | } |