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