blob: 65d004fe1485573d11b6124a639e2426660a6d1e [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- 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 **/
akmhoque53353462014-04-22 08:43:45 -050023#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
32namespace nlsr {
33
34using namespace std;
35
akmhoque53353462014-04-22 08:43:45 -050036static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050037mapEntryCompareByRouter(MapEntry& mpe1, const ndn::Name& rtrName)
akmhoque53353462014-04-22 08:43:45 -050038{
39 return mpe1.getRouter() == rtrName;
40}
41
42static bool
akmhoquefdbddb12014-05-02 18:35:19 -050043mapEntryCompareByMappingNo(MapEntry& mpe1, int32_t mappingNo)
akmhoque53353462014-04-22 08:43:45 -050044{
45 return mpe1.getMappingNumber() == mappingNo;
46}
47
48void
akmhoque31d1d4b2014-05-05 22:08:14 -050049Map::addEntry(const ndn::Name& rtrName)
akmhoque53353462014-04-22 08:43:45 -050050{
51 MapEntry me(rtrName, m_mappingIndex);
akmhoque157b0a42014-05-13 00:26:37 -050052 if (addEntry(me)) {
akmhoque53353462014-04-22 08:43:45 -050053 m_mappingIndex++;
54 }
55}
56
57bool
akmhoquefdbddb12014-05-02 18:35:19 -050058Map::addEntry(MapEntry& mpe)
akmhoque53353462014-04-22 08:43:45 -050059{
60 //cout << mpe;
61 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
62 m_table.end(),
akmhoque157b0a42014-05-13 00:26:37 -050063 ndn::bind(&mapEntryCompareByRouter,
64 _1, mpe.getRouter()));
65 if (it == m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050066 m_table.push_back(mpe);
67 return true;
68 }
69 return false;
70}
71
akmhoque31d1d4b2014-05-05 22:08:14 -050072const ndn::Name
akmhoquefdbddb12014-05-02 18:35:19 -050073Map::getRouterNameByMappingNo(int32_t mn)
akmhoque53353462014-04-22 08:43:45 -050074{
75 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
76 m_table.end(),
akmhoque157b0a42014-05-13 00:26:37 -050077 ndn::bind(&mapEntryCompareByMappingNo,
78 _1, mn));
79 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050080 return (*it).getRouter();
81 }
akmhoque31d1d4b2014-05-05 22:08:14 -050082 return ndn::Name();
akmhoque53353462014-04-22 08:43:45 -050083}
84
akmhoquefdbddb12014-05-02 18:35:19 -050085int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050086Map::getMappingNoByRouterName(const ndn::Name& rName)
akmhoque53353462014-04-22 08:43:45 -050087{
88 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
89 m_table.end(),
akmhoque157b0a42014-05-13 00:26:37 -050090 ndn::bind(&mapEntryCompareByRouter,
91 _1, rName));
92 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050093 return (*it).getMappingNumber();
94 }
95 return -1;
96}
97
98void
99Map::createFromAdjLsdb(Nlsr& pnlsr)
100{
101 std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb();
102 for (std::list<AdjLsa>::iterator it = adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500103 it != adjLsdb.end() ; it++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 addEntry((*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500105 std::list<Adjacent> adl = (*it).getAdl().getAdjList();
106 for (std::list<Adjacent>::iterator itAdl = adl.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500107 itAdl != adl.end() ; itAdl++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 addEntry((*itAdl).getName());
akmhoque53353462014-04-22 08:43:45 -0500109 }
110 }
111}
112
113void
114Map::reset()
115{
116 m_table.clear();
117 m_mappingIndex = 0;
118}
119
120ostream&
121operator<<(ostream& os, Map& map)
122{
123 os << "---------------Map----------------------" << endl;
124 std::list<MapEntry> ml = map.getMapList();
akmhoque157b0a42014-05-13 00:26:37 -0500125 for (std::list<MapEntry>::iterator it = ml.begin(); it != ml.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500126 os << (*it);
127 }
128 return os;
129}
130
131} //namespace nlsr