blob: cdcbad94db8b59c39b9dccc5c24472db28588d59 [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,
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
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("SequencingManager");
36
akmhoque53353462014-04-22 08:43:45 -050037void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060038SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050039{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050040 writeLog();
41 std::ofstream outputFile(m_seqFileNameWithPath.c_str());
42 std::ostringstream os;
43 os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
44 << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
45 << "CorLsaSeq " << std::to_string(m_corLsaSeq);
46 outputFile << os.str();
akmhoque53353462014-04-22 08:43:45 -050047 outputFile.close();
48}
49
50void
Nick Gordon5c467f02016-07-13 13:40:10 -050051SequencingManager::initiateSeqNoFromFile(int hypState)
akmhoque53353462014-04-22 08:43:45 -050052{
dmcoomes5bcb39e2017-10-31 15:07:55 -050053 NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050054 std::ifstream inputFile(m_seqFileNameWithPath.c_str());
55
56 // Good checks that file is not (bad or eof or fail)
akmhoque157b0a42014-05-13 00:26:37 -050057 if (inputFile.good()) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050058 std::string lsaOrCombinedSeqNo;
59 uint64_t seqNo = 0;
60
61 // If file has a combined seq number, lsaOrCombinedSeqNo would hold it
62 // and seqNo will be zero everytime
63 inputFile >> lsaOrCombinedSeqNo >> seqNo;
64 m_nameLsaSeq = seqNo;
65
66 inputFile >> lsaOrCombinedSeqNo >> seqNo;
67 m_adjLsaSeq = seqNo;
68
69 inputFile >> lsaOrCombinedSeqNo >> seqNo;;
70 m_corLsaSeq = seqNo;
71
72 // File was in old format and had a combined sequence number
73 // if all of the seqNo should are still zero and
74 // lsaOrCombinedSeqNo != CorLsaSeq
75 if (m_nameLsaSeq == 0 && m_adjLsaSeq == 0 && m_corLsaSeq == 0 &&
76 lsaOrCombinedSeqNo != "CorLsaSeq") {
dmcoomes5bcb39e2017-10-31 15:07:55 -050077 NLSR_LOG_DEBUG("Old file had combined sequence number: " << lsaOrCombinedSeqNo);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050078 std::istringstream iss(lsaOrCombinedSeqNo);
79 iss >> seqNo;
80 m_adjLsaSeq = (seqNo & 0xFFFFF);
81 m_corLsaSeq = ((seqNo >> 20) & 0xFFFFF);
82 m_nameLsaSeq = ((seqNo >> 40) & 0xFFFFFF);
83 }
84
85 inputFile.close();
86
87 m_nameLsaSeq += 10;
Nick Gordon5c467f02016-07-13 13:40:10 -050088
89 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
90 if (hypState != HYPERBOLIC_STATE_ON) {
91 if (m_corLsaSeq != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050092 NLSR_LOG_WARN("This router was previously configured for hyperbolic"
Nick Gordon5c467f02016-07-13 13:40:10 -050093 << " routing without clearing the seq. no. file.");
94 m_corLsaSeq = 0;
95 }
96 m_adjLsaSeq += 10;
97 }
98
99 // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
100 if (hypState != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500101 if (m_adjLsaSeq != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500102 NLSR_LOG_WARN("This router was previously configured for link-state"
Nick Gordon5c467f02016-07-13 13:40:10 -0500103 << " routing without clearing the seq. no. file.");
104 m_adjLsaSeq = 0;
105 }
106 m_corLsaSeq += 10;
107 }
akmhoque53353462014-04-22 08:43:45 -0500108 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500109 writeLog();
akmhoque53353462014-04-22 08:43:45 -0500110}
111
112void
Nick Gordone98480b2017-05-24 11:23:03 -0500113SequencingManager::setSeqFileDirectory(std::string filePath)
akmhoque53353462014-04-22 08:43:45 -0500114{
115 m_seqFileNameWithPath = filePath;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500116
akmhoque157b0a42014-05-13 00:26:37 -0500117 if (m_seqFileNameWithPath.empty()) {
Nick Gordone98480b2017-05-24 11:23:03 -0500118 std::string homeDirPath(getpwuid(getuid())->pw_dir);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500119 if (homeDirPath.empty()) {
120 homeDirPath = getenv("HOME");
121 }
122 m_seqFileNameWithPath = homeDirPath;
akmhoque53353462014-04-22 08:43:45 -0500123 }
124 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
125}
126
akmhoque2f423352014-06-03 11:49:35 -0500127void
Vince Lehman904c2412014-09-23 19:36:11 -0500128SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500129{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500130 NLSR_LOG_DEBUG("----SequencingManager----");
131 NLSR_LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
132 NLSR_LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
133 NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
akmhoque53353462014-04-22 08:43:45 -0500134}
135
Nick Gordonfad8e252016-08-11 14:21:38 -0500136} // namespace nlsr