blob: ae0c8b527c34f8a4db7f5fee30b1a1a50fab4f4d [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
Nick Gordone98480b2017-05-24 11:23:03 -050022#include "sequencing-manager.hpp"
23#include "logger.hpp"
24
akmhoque53353462014-04-22 08:43:45 -050025#include <string>
akmhoque53353462014-04-22 08:43:45 -050026#include <fstream>
27#include <pwd.h>
28#include <cstdlib>
29#include <unistd.h>
30
akmhoque53353462014-04-22 08:43:45 -050031namespace nlsr {
32
dmcoomescf8d0ed2017-02-21 11:39:01 -060033INIT_LOGGER(SequencingManager);
akmhoque674b0b12014-05-20 14:33:28 -050034
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060035SequencingManager::SequencingManager(const std::string& filePath, int hypState)
36 : m_hyperbolicState(hypState)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060037{
38 setSeqFileDirectory(filePath);
39 initiateSeqNoFromFile();
40}
41
akmhoque53353462014-04-22 08:43:45 -050042void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060043SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050044{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050045 writeLog();
46 std::ofstream outputFile(m_seqFileNameWithPath.c_str());
47 std::ostringstream os;
48 os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
49 << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
50 << "CorLsaSeq " << std::to_string(m_corLsaSeq);
51 outputFile << os.str();
akmhoque53353462014-04-22 08:43:45 -050052 outputFile.close();
53}
54
55void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056SequencingManager::initiateSeqNoFromFile()
akmhoque53353462014-04-22 08:43:45 -050057{
dmcoomes5bcb39e2017-10-31 15:07:55 -050058 NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050059 std::ifstream inputFile(m_seqFileNameWithPath.c_str());
60
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060061 std::string seqType;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050062 // Good checks that file is not (bad or eof or fail)
akmhoque157b0a42014-05-13 00:26:37 -050063 if (inputFile.good()) {
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060064 inputFile >> seqType >> m_nameLsaSeq;
65 inputFile >> seqType >> m_adjLsaSeq;
66 inputFile >> seqType >> m_corLsaSeq;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050067
68 inputFile.close();
69
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060070 // Increment by 10 in case last run of NLSR was not able to write to file
71 // before crashing
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050072 m_nameLsaSeq += 10;
Nick Gordon5c467f02016-07-13 13:40:10 -050073
74 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -060075 if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -050076 if (m_corLsaSeq != 0) {
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060077 NLSR_LOG_WARN("This router was previously configured for hyperbolic " <<
78 "routing without clearing the seq. no. file.");
Nick Gordon5c467f02016-07-13 13:40:10 -050079 m_corLsaSeq = 0;
80 }
81 m_adjLsaSeq += 10;
82 }
83
84 // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060085 if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050086 if (m_adjLsaSeq != 0) {
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060087 NLSR_LOG_WARN("This router was previously configured for link-state " <<
88 "routing without clearing the seq. no. file.");
Nick Gordon5c467f02016-07-13 13:40:10 -050089 m_adjLsaSeq = 0;
90 }
91 m_corLsaSeq += 10;
92 }
akmhoque53353462014-04-22 08:43:45 -050093 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050094 writeLog();
akmhoque53353462014-04-22 08:43:45 -050095}
96
97void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098SequencingManager::setSeqFileDirectory(const std::string& filePath)
akmhoque53353462014-04-22 08:43:45 -050099{
100 m_seqFileNameWithPath = filePath;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500101
akmhoque157b0a42014-05-13 00:26:37 -0500102 if (m_seqFileNameWithPath.empty()) {
Nick Gordone98480b2017-05-24 11:23:03 -0500103 std::string homeDirPath(getpwuid(getuid())->pw_dir);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500104 if (homeDirPath.empty()) {
105 homeDirPath = getenv("HOME");
106 }
107 m_seqFileNameWithPath = homeDirPath;
akmhoque53353462014-04-22 08:43:45 -0500108 }
109 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
110}
111
akmhoque2f423352014-06-03 11:49:35 -0500112void
Vince Lehman904c2412014-09-23 19:36:11 -0500113SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500114{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500115 if (m_hyperbolicState == HYPERBOLIC_STATE_OFF ||
116 m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
117 NLSR_LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
118 }
119 if (m_hyperbolicState == HYPERBOLIC_STATE_ON ||
120 m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
121 NLSR_LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
122 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500123 NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
akmhoque53353462014-04-22 08:43:45 -0500124}
125
Nick Gordonfad8e252016-08-11 14:21:38 -0500126} // namespace nlsr