blob: 544976ee0926d8d65cbc75d4c3bfc413a848025f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
akmhoque53353462014-04-22 08:43:45 -050020
Nick Gordon22b5c952017-08-10 17:48:15 -050021#include "map.hpp"
akmhoque53353462014-04-22 08:43:45 -050022#include "nlsr.hpp"
23#include "adjacent.hpp"
24#include "lsa.hpp"
25#include "lsdb.hpp"
Nick Gordon22b5c952017-08-10 17:48:15 -050026
akmhoque2f423352014-06-03 11:49:35 -050027#include "logger.hpp"
Nick Gordon22b5c952017-08-10 17:48:15 -050028
29#include <iostream>
30#include <list>
31
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
akmhoque2f423352014-06-03 11:49:35 -050034INIT_LOGGER("Map");
35
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37
akmhoque53353462014-04-22 08:43:45 -050038static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050039mapEntryCompareByRouter(MapEntry& mpe1, const ndn::Name& rtrName)
akmhoque53353462014-04-22 08:43:45 -050040{
41 return mpe1.getRouter() == rtrName;
42}
43
44static bool
akmhoquefdbddb12014-05-02 18:35:19 -050045mapEntryCompareByMappingNo(MapEntry& mpe1, int32_t mappingNo)
akmhoque53353462014-04-22 08:43:45 -050046{
47 return mpe1.getMappingNumber() == mappingNo;
48}
49
50void
akmhoque31d1d4b2014-05-05 22:08:14 -050051Map::addEntry(const ndn::Name& rtrName)
akmhoque53353462014-04-22 08:43:45 -050052{
53 MapEntry me(rtrName, m_mappingIndex);
akmhoque157b0a42014-05-13 00:26:37 -050054 if (addEntry(me)) {
akmhoque53353462014-04-22 08:43:45 -050055 m_mappingIndex++;
56 }
57}
58
59bool
akmhoquefdbddb12014-05-02 18:35:19 -050060Map::addEntry(MapEntry& mpe)
akmhoque53353462014-04-22 08:43:45 -050061{
62 //cout << mpe;
63 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
64 m_table.end(),
dmcoomes9f936662017-03-02 10:33:09 -060065 std::bind(&mapEntryCompareByRouter,
akmhoque157b0a42014-05-13 00:26:37 -050066 _1, mpe.getRouter()));
67 if (it == m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050068 m_table.push_back(mpe);
69 return true;
70 }
71 return false;
72}
73
akmhoque31d1d4b2014-05-05 22:08:14 -050074const ndn::Name
akmhoquefdbddb12014-05-02 18:35:19 -050075Map::getRouterNameByMappingNo(int32_t mn)
akmhoque53353462014-04-22 08:43:45 -050076{
77 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
78 m_table.end(),
dmcoomes9f936662017-03-02 10:33:09 -060079 std::bind(&mapEntryCompareByMappingNo,
akmhoque157b0a42014-05-13 00:26:37 -050080 _1, mn));
81 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050082 return (*it).getRouter();
83 }
akmhoque31d1d4b2014-05-05 22:08:14 -050084 return ndn::Name();
akmhoque53353462014-04-22 08:43:45 -050085}
86
akmhoquefdbddb12014-05-02 18:35:19 -050087int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050088Map::getMappingNoByRouterName(const ndn::Name& rName)
akmhoque53353462014-04-22 08:43:45 -050089{
90 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
91 m_table.end(),
dmcoomes9f936662017-03-02 10:33:09 -060092 std::bind(&mapEntryCompareByRouter,
akmhoque157b0a42014-05-13 00:26:37 -050093 _1, rName));
94 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050095 return (*it).getMappingNumber();
96 }
97 return -1;
98}
99
100void
akmhoque53353462014-04-22 08:43:45 -0500101Map::reset()
102{
103 m_table.clear();
104 m_mappingIndex = 0;
105}
106
akmhoque2f423352014-06-03 11:49:35 -0500107void
108Map::writeLog()
akmhoque53353462014-04-22 08:43:45 -0500109{
akmhoque2f423352014-06-03 11:49:35 -0500110 _LOG_DEBUG("---------------Map----------------------");
111 for (std::list<MapEntry>::iterator it = m_table.begin(); it != m_table.end() ; it++) {
112 _LOG_DEBUG("MapEntry: ( Router: " << (*it).getRouter() << " Mapping No: "
113 << (*it).getMappingNumber() << " )");
akmhoque53353462014-04-22 08:43:45 -0500114 }
akmhoque53353462014-04-22 08:43:45 -0500115}
116
Nick Gordonfad8e252016-08-11 14:21:38 -0500117} // namespace nlsr