Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 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 "tlv/adjacency-lsa.hpp" |
| 23 | #include "tlv/coordinate-lsa.hpp" |
| 24 | #include "tlv/name-lsa.hpp" |
| 25 | |
| 26 | #include <boost/noncopyable.hpp> |
| 27 | #include <ndn-cxx/face.hpp> |
| 28 | #include <ndn-cxx/security/key-chain.hpp> |
| 29 | |
| 30 | #include <deque> |
| 31 | #include <map> |
| 32 | #include <stdexcept> |
| 33 | |
| 34 | #ifndef NLSR_TOOLS_NLSRC_HPP |
| 35 | #define NLSR_TOOLS_NLSRC_HPP |
| 36 | |
| 37 | namespace nlsrc { |
| 38 | |
| 39 | class Nlsrc : boost::noncopyable |
| 40 | { |
| 41 | public: |
| 42 | explicit |
| 43 | Nlsrc(ndn::Face& face); |
| 44 | |
| 45 | void |
| 46 | printUsage(); |
| 47 | |
| 48 | void |
| 49 | getStatus(); |
| 50 | |
| 51 | bool |
| 52 | dispatch(const std::string& cmd); |
| 53 | |
| 54 | private: |
| 55 | void |
| 56 | runNextStep(); |
| 57 | |
| 58 | /** |
| 59 | * \brief Adds a name prefix to be advertised in NLSR's Name LSA |
| 60 | * |
| 61 | * cmd format: |
| 62 | * name |
| 63 | * |
| 64 | */ |
| 65 | void |
| 66 | advertiseName(); |
| 67 | |
| 68 | /** |
| 69 | * \brief Removes a name prefix from NLSR's Name LSA |
| 70 | * |
| 71 | * cmd format: |
| 72 | * name |
| 73 | * |
| 74 | */ |
| 75 | void |
| 76 | withdrawName(); |
| 77 | |
| 78 | void |
| 79 | sendNamePrefixUpdate(const ndn::Name& name, |
| 80 | const ndn::Name::Component& verb, |
| 81 | const std::string& info); |
| 82 | |
| 83 | void |
| 84 | onControlResponse(const std::string& info, const ndn::Data& data); |
| 85 | |
| 86 | private: |
| 87 | void |
| 88 | fetchAdjacencyLsas(); |
| 89 | |
| 90 | void |
| 91 | fetchCoordinateLsas(); |
| 92 | |
| 93 | void |
| 94 | fetchNameLsas(); |
| 95 | |
| 96 | template <class T> |
| 97 | void |
| 98 | fetchFromLsdb(const ndn::Name::Component& datasetType, |
| 99 | const std::function<void(const T&)>& recordLsa); |
| 100 | |
| 101 | template <class T> |
| 102 | void |
| 103 | onFetchSuccess(const ndn::ConstBufferPtr& data, |
| 104 | const std::function<void(const T&)>& recordLsa); |
| 105 | |
| 106 | void |
| 107 | onTimeout(uint32_t errorCode, const std::string& error); |
| 108 | |
| 109 | private: |
| 110 | std::string |
| 111 | getLsaInfoString(const nlsr::tlv::LsaInfo& info); |
| 112 | |
| 113 | void |
| 114 | recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa); |
| 115 | |
| 116 | void |
| 117 | recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa); |
| 118 | |
| 119 | void |
| 120 | recordNameLsa(const nlsr::tlv::NameLsa& lsa); |
| 121 | |
| 122 | void |
| 123 | printLsdb(); |
| 124 | |
| 125 | public: |
| 126 | const char* programName; |
| 127 | |
| 128 | // command parameters without leading 'cmd' component |
| 129 | const char* const* commandLineArguments; |
| 130 | int nOptions; |
| 131 | |
| 132 | private: |
| 133 | struct Router |
| 134 | { |
| 135 | std::string adjacencyLsaString; |
| 136 | std::string coordinateLsaString; |
| 137 | std::string nameLsaString; |
| 138 | }; |
| 139 | |
| 140 | Router& |
| 141 | getRouter(const nlsr::tlv::LsaInfo& info); |
| 142 | |
| 143 | typedef std::map<const ndn::Name, Router> RouterMap; |
| 144 | RouterMap m_routers; |
| 145 | |
| 146 | private: |
| 147 | ndn::KeyChain m_keyChain; |
| 148 | ndn::Face& m_face; |
| 149 | |
| 150 | std::deque<std::function<void()>> m_fetchSteps; |
| 151 | |
| 152 | static const ndn::Name LOCALHOST_PREFIX; |
| 153 | static const ndn::Name LSDB_PREFIX; |
| 154 | static const ndn::Name NAME_UPDATE_PREFIX; |
| 155 | |
| 156 | static const uint32_t ERROR_CODE_TIMEOUT; |
| 157 | static const uint32_t RESPONSE_CODE_SUCCESS; |
| 158 | }; |
| 159 | |
| 160 | } // namespace nlsrc |
| 161 | |
| 162 | #endif // NLSR_TOOLS_NLSRC_HPP |