akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <list> |
| 25 | |
| 26 | #include "nlsr.hpp" |
| 27 | #include "adjacent.hpp" |
| 28 | #include "lsa.hpp" |
| 29 | #include "lsdb.hpp" |
| 30 | #include "map.hpp" |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 31 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 34 | INIT_LOGGER("Map"); |
| 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 39 | mapEntryCompareByRouter(MapEntry& mpe1, const ndn::Name& rtrName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | { |
| 41 | return mpe1.getRouter() == rtrName; |
| 42 | } |
| 43 | |
| 44 | static bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 45 | mapEntryCompareByMappingNo(MapEntry& mpe1, int32_t mappingNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | { |
| 47 | return mpe1.getMappingNumber() == mappingNo; |
| 48 | } |
| 49 | |
| 50 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 51 | Map::addEntry(const ndn::Name& rtrName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | { |
| 53 | MapEntry me(rtrName, m_mappingIndex); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 54 | if (addEntry(me)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | m_mappingIndex++; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 60 | Map::addEntry(MapEntry& mpe) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | { |
| 62 | //cout << mpe; |
| 63 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 64 | m_table.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 65 | ndn::bind(&mapEntryCompareByRouter, |
| 66 | _1, mpe.getRouter())); |
| 67 | if (it == m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | m_table.push_back(mpe); |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 74 | const ndn::Name |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 75 | Map::getRouterNameByMappingNo(int32_t mn) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | { |
| 77 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 78 | m_table.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | ndn::bind(&mapEntryCompareByMappingNo, |
| 80 | _1, mn)); |
| 81 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 82 | return (*it).getRouter(); |
| 83 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 84 | return ndn::Name(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | } |
| 86 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 87 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 88 | Map::getMappingNoByRouterName(const ndn::Name& rName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | { |
| 90 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 91 | m_table.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 92 | ndn::bind(&mapEntryCompareByRouter, |
| 93 | _1, rName)); |
| 94 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | return (*it).getMappingNumber(); |
| 96 | } |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | Map::createFromAdjLsdb(Nlsr& pnlsr) |
| 102 | { |
| 103 | std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb(); |
| 104 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 105 | it != adjLsdb.end() ; it++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 106 | addEntry((*it).getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
| 108 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 109 | itAdl != adl.end() ; itAdl++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 110 | addEntry((*itAdl).getName()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 116 | Map::createFromCoordinateLsdb(Nlsr& nlsr) |
| 117 | { |
| 118 | for (CoordinateLsa lsa : nlsr.getLsdb().getCoordinateLsdb()) { |
| 119 | addEntry(lsa.getOrigRouter()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | Map::reset() |
| 125 | { |
| 126 | m_table.clear(); |
| 127 | m_mappingIndex = 0; |
| 128 | } |
| 129 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 130 | void |
| 131 | Map::writeLog() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 133 | _LOG_DEBUG("---------------Map----------------------"); |
| 134 | for (std::list<MapEntry>::iterator it = m_table.begin(); it != m_table.end() ; it++) { |
| 135 | _LOG_DEBUG("MapEntry: ( Router: " << (*it).getRouter() << " Mapping No: " |
| 136 | << (*it).getMappingNumber() << " )"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 140 | } // namespace nlsr |