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 | |
| 259 | ndn::util::SegmentFetcher::fetch(m_face, |
| 260 | interest, |
Muktadir R Chowdhury | 4c7caad | 2015-09-03 15:49:22 -0500 | [diff] [blame] | 261 | m_validator, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 262 | std::bind(&Nlsrc::onFetchSuccess<T>, |
| 263 | this, _1, recordLsa), |
| 264 | std::bind(&Nlsrc::onTimeout, this, _1, _2)); |
| 265 | } |
| 266 | |
| 267 | template <class T> |
| 268 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 269 | Nlsrc::fetchFromRt(const std::function<void(const T&)>& recordDataset) |
| 270 | { |
| 271 | ndn::Name command = RT_PREFIX; |
| 272 | |
| 273 | ndn::Interest interest(command); |
| 274 | |
| 275 | ndn::util::SegmentFetcher::fetch(m_face, |
| 276 | interest, |
| 277 | m_validator, |
| 278 | std::bind(&Nlsrc::onFetchSuccess<T>, |
| 279 | this, _1, recordDataset), |
| 280 | std::bind(&Nlsrc::onTimeout, this, _1, _2)); |
| 281 | } |
| 282 | |
| 283 | template <class T> |
| 284 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 285 | Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 286 | const std::function<void(const T&)>& recordDataset) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 287 | { |
| 288 | ndn::Block block; |
| 289 | size_t offset = 0; |
| 290 | |
| 291 | while (offset < data->size()) { |
| 292 | bool isOk = false; |
| 293 | std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset); |
| 294 | |
| 295 | if (!isOk) { |
| 296 | std::cerr << "ERROR: cannot decode LSA TLV" << std::endl; |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | offset += block.size(); |
| 301 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 302 | T data(block); |
| 303 | recordDataset(data); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | runNextStep(); |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | Nlsrc::onTimeout(uint32_t errorCode, const std::string& error) |
| 311 | { |
| 312 | std::cerr << "Request timed out (code: " << errorCode |
| 313 | << ", error: " << error << ")" << std::endl; |
| 314 | } |
| 315 | |
| 316 | std::string |
| 317 | Nlsrc::getLsaInfoString(const nlsr::tlv::LsaInfo& info) |
| 318 | { |
| 319 | std::ostringstream os; |
| 320 | os << " info=" << info; |
| 321 | |
| 322 | return os.str(); |
| 323 | } |
| 324 | |
| 325 | void |
| 326 | Nlsrc::recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa) |
| 327 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 328 | Router& router = getRouterLsdb(lsa.getLsaInfo()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 329 | |
| 330 | std::ostringstream os; |
| 331 | os << " AdjacencyLsa:" << std::endl; |
| 332 | |
| 333 | os << getLsaInfoString(lsa.getLsaInfo()) << std::endl; |
| 334 | |
| 335 | for (const auto& adjacency : lsa.getAdjacencies()) { |
| 336 | os << " adjacency=" << adjacency << std::endl; |
| 337 | } |
| 338 | |
| 339 | router.adjacencyLsaString = os.str(); |
| 340 | } |
| 341 | |
| 342 | void |
| 343 | Nlsrc::recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa) |
| 344 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 345 | Router& router = getRouterLsdb(lsa.getLsaInfo()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 346 | |
| 347 | std::ostringstream os; |
| 348 | os << " Coordinate LSA:" << std::endl; |
| 349 | |
| 350 | os << getLsaInfoString(lsa.getLsaInfo()) << std::endl; |
| 351 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 352 | int i = 0; |
| 353 | for (auto const& value: lsa.getHyperbolicAngle()) { |
| 354 | os << " Hyp Angle " << i++ << ": "<< value << " "; |
| 355 | } |
| 356 | os << "\n radius=" << lsa.getHyperbolicRadius() << std::endl; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 357 | |
| 358 | router.coordinateLsaString = os.str(); |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | Nlsrc::recordNameLsa(const nlsr::tlv::NameLsa& lsa) |
| 363 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 364 | Router& router = getRouterLsdb(lsa.getLsaInfo()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 365 | |
| 366 | std::ostringstream os; |
| 367 | os << " Name LSA:" << std::endl; |
| 368 | |
| 369 | os << getLsaInfoString(lsa.getLsaInfo()) << std::endl; |
| 370 | |
| 371 | for (const auto& name : lsa.getNames()) { |
| 372 | os << " name=" << name << std::endl; |
| 373 | } |
| 374 | |
| 375 | router.nameLsaString = os.str(); |
| 376 | } |
| 377 | |
| 378 | void |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 379 | Nlsrc::recordRtable(const nlsr::tlv::RoutingTableStatus& rts) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 380 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 381 | std::ostringstream os; |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 382 | for (const auto& rt : rts.getRoutingtable()) { |
| 383 | os << rt << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 384 | } |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 385 | m_rtString = os.str(); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 389 | Nlsrc::printLsdb() |
| 390 | { |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 391 | std::cout << "LSDB:" << std::endl; |
| 392 | |
| 393 | for (const auto& item : m_routers) { |
| 394 | std::cout << " OriginRouter: " << item.first << std::endl; |
| 395 | std::cout << std::endl; |
| 396 | |
| 397 | const Router& router = item.second; |
| 398 | |
| 399 | if (!router.adjacencyLsaString.empty()) { |
| 400 | std::cout << router.adjacencyLsaString << std::endl; |
| 401 | } |
| 402 | |
| 403 | if (!router.coordinateLsaString.empty()) { |
| 404 | std::cout << router.coordinateLsaString << std::endl; |
| 405 | } |
| 406 | |
| 407 | if (!router.nameLsaString.empty()) { |
| 408 | std::cout << router.nameLsaString << std::endl; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 413 | void |
| 414 | Nlsrc::printRT() |
| 415 | { |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 416 | if (!m_rtString.empty()) { |
| 417 | std::cout << "Routing Table" << std::endl; |
| 418 | std::cout << m_rtString << std::endl; |
| 419 | } |
| 420 | else { |
| 421 | std::cout << "Routing Table is not calculated yet" << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | Nlsrc::printAll() |
| 427 | { |
| 428 | std::cout << "NLSR Status" << std::endl; |
| 429 | printLsdb(); |
| 430 | printRT(); |
| 431 | } |
| 432 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 433 | Nlsrc::Router& |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 434 | Nlsrc::getRouterLsdb(const nlsr::tlv::LsaInfo& info) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 435 | { |
| 436 | const ndn::Name& originRouterName = info.getOriginRouter(); |
| 437 | |
| 438 | const auto& pair = |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 439 | m_routers.insert(std::make_pair(originRouterName, Router())); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 440 | |
| 441 | return pair.first->second; |
| 442 | } |
| 443 | |
| 444 | } // namespace nlsrc |
| 445 | |
| 446 | //////////////////////////////////////////////////////////////////////////////// |
| 447 | //////////////////////////////////////////////////////////////////////////////// |
| 448 | |
| 449 | int |
| 450 | main(int argc, char** argv) |
| 451 | { |
| 452 | ndn::Face face; |
| 453 | nlsrc::Nlsrc nlsrc(face); |
| 454 | |
| 455 | nlsrc.programName = argv[0]; |
| 456 | |
| 457 | if (argc < 2) { |
| 458 | nlsrc.printUsage(); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | int opt; |
| 463 | while ((opt = ::getopt(argc, argv, "hV")) != -1) { |
| 464 | switch (opt) { |
| 465 | case 'h': |
| 466 | nlsrc.printUsage(); |
| 467 | return 0; |
| 468 | case 'V': |
| 469 | std::cout << NLSR_VERSION_BUILD_STRING << std::endl; |
| 470 | return 0; |
| 471 | default: |
| 472 | nlsrc.printUsage(); |
| 473 | return 1; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (argc == ::optind) { |
| 478 | nlsrc.printUsage(); |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | try { |
| 483 | ::optind = 2; // Set ::optind to the command's index |
| 484 | |
| 485 | nlsrc.commandLineArguments = argv + ::optind; |
| 486 | nlsrc.nOptions = argc - ::optind; |
| 487 | |
| 488 | // argv[1] points to the command, so pass it to the dispatch |
| 489 | bool isOk = nlsrc.dispatch(argv[1]); |
| 490 | if (!isOk) { |
| 491 | nlsrc.printUsage(); |
| 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | face.processEvents(); |
| 496 | } |
| 497 | catch (const std::exception& e) { |
| 498 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 499 | return 2; |
| 500 | } |
| 501 | return 0; |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 502 | } |