blob: c7c6c7e4e5378894f695e08b70736f7f83777db6 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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>
26#include <iostream>
27#include <fstream>
28#include <pwd.h>
29#include <cstdlib>
30#include <unistd.h>
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050031#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
dmcoomescf8d0ed2017-02-21 11:39:01 -060035INIT_LOGGER(SequencingManager);
akmhoque674b0b12014-05-20 14:33:28 -050036
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060037SequencingManager::SequencingManager(const std::string& filePath, int hypState)
38 : m_hyperbolicState(hypState)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060039{
40 setSeqFileDirectory(filePath);
41 initiateSeqNoFromFile();
42}
43
akmhoque53353462014-04-22 08:43:45 -050044void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060045SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050046{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050047 writeLog();
48 std::ofstream outputFile(m_seqFileNameWithPath.c_str());
49 std::ostringstream os;
50 os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
51 << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
52 << "CorLsaSeq " << std::to_string(m_corLsaSeq);
53 outputFile << os.str();
akmhoque53353462014-04-22 08:43:45 -050054 outputFile.close();
55}
56
57void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060058SequencingManager::initiateSeqNoFromFile()
akmhoque53353462014-04-22 08:43:45 -050059{
dmcoomes5bcb39e2017-10-31 15:07:55 -050060 NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050061 std::ifstream inputFile(m_seqFileNameWithPath.c_str());
62
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060063 std::string seqType;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050064 // Good checks that file is not (bad or eof or fail)
akmhoque157b0a42014-05-13 00:26:37 -050065 if (inputFile.good()) {
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060066 inputFile >> seqType >> m_nameLsaSeq;
67 inputFile >> seqType >> m_adjLsaSeq;
68 inputFile >> seqType >> m_corLsaSeq;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050069
70 inputFile.close();
71
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060072 // Increment by 10 in case last run of NLSR was not able to write to file
73 // before crashing
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050074 m_nameLsaSeq += 10;
Nick Gordon5c467f02016-07-13 13:40:10 -050075
76 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -060077 if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -050078 if (m_corLsaSeq != 0) {
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060079 NLSR_LOG_WARN("This router was previously configured for hyperbolic " <<
80 "routing without clearing the seq. no. file.");
Nick Gordon5c467f02016-07-13 13:40:10 -050081 m_corLsaSeq = 0;
82 }
83 m_adjLsaSeq += 10;
84 }
85
86 // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087 if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050088 if (m_adjLsaSeq != 0) {
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060089 NLSR_LOG_WARN("This router was previously configured for link-state " <<
90 "routing without clearing the seq. no. file.");
Nick Gordon5c467f02016-07-13 13:40:10 -050091 m_adjLsaSeq = 0;
92 }
93 m_corLsaSeq += 10;
94 }
akmhoque53353462014-04-22 08:43:45 -050095 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050096 writeLog();
akmhoque53353462014-04-22 08:43:45 -050097}
98
99void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100SequencingManager::setSeqFileDirectory(const std::string& filePath)
akmhoque53353462014-04-22 08:43:45 -0500101{
102 m_seqFileNameWithPath = filePath;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500103
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (m_seqFileNameWithPath.empty()) {
Nick Gordone98480b2017-05-24 11:23:03 -0500105 std::string homeDirPath(getpwuid(getuid())->pw_dir);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500106 if (homeDirPath.empty()) {
107 homeDirPath = getenv("HOME");
108 }
109 m_seqFileNameWithPath = homeDirPath;
akmhoque53353462014-04-22 08:43:45 -0500110 }
111 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
112}
113
akmhoque2f423352014-06-03 11:49:35 -0500114void
Vince Lehman904c2412014-09-23 19:36:11 -0500115SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500116{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500117 if (m_hyperbolicState == HYPERBOLIC_STATE_OFF ||
118 m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
119 NLSR_LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
120 }
121 if (m_hyperbolicState == HYPERBOLIC_STATE_ON ||
122 m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
123 NLSR_LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
124 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500125 NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
akmhoque53353462014-04-22 08:43:45 -0500126}
127
Nick Gordonfad8e252016-08-11 14:21:38 -0500128} // namespace nlsr