akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 22 | #include "sequencing-manager.hpp" |
| 23 | #include "logger.hpp" |
| 24 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <iostream> |
| 27 | #include <fstream> |
| 28 | #include <pwd.h> |
| 29 | #include <cstdlib> |
| 30 | #include <unistd.h> |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 31 | #include <boost/algorithm/string.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | namespace nlsr { |
| 34 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 35 | INIT_LOGGER(SequencingManager); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 36 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 37 | SequencingManager::SequencingManager(std::string filePath, int hypState) |
| 38 | : m_nameLsaSeq(0) |
| 39 | , m_adjLsaSeq(0) |
| 40 | , m_corLsaSeq(0) |
| 41 | , m_hyperbolicState(hypState) |
| 42 | { |
| 43 | setSeqFileDirectory(filePath); |
| 44 | initiateSeqNoFromFile(); |
| 45 | } |
| 46 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | void |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 48 | SequencingManager::writeSeqNoToFile() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 49 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 50 | writeLog(); |
| 51 | std::ofstream outputFile(m_seqFileNameWithPath.c_str()); |
| 52 | std::ostringstream os; |
| 53 | os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n" |
| 54 | << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n" |
| 55 | << "CorLsaSeq " << std::to_string(m_corLsaSeq); |
| 56 | outputFile << os.str(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | outputFile.close(); |
| 58 | } |
| 59 | |
| 60 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 61 | SequencingManager::initiateSeqNoFromFile() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 63 | NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 64 | std::ifstream inputFile(m_seqFileNameWithPath.c_str()); |
| 65 | |
| 66 | // Good checks that file is not (bad or eof or fail) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 67 | if (inputFile.good()) { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 68 | std::string lsaOrCombinedSeqNo; |
| 69 | uint64_t seqNo = 0; |
| 70 | |
| 71 | // If file has a combined seq number, lsaOrCombinedSeqNo would hold it |
| 72 | // and seqNo will be zero everytime |
| 73 | inputFile >> lsaOrCombinedSeqNo >> seqNo; |
| 74 | m_nameLsaSeq = seqNo; |
| 75 | |
| 76 | inputFile >> lsaOrCombinedSeqNo >> seqNo; |
| 77 | m_adjLsaSeq = seqNo; |
| 78 | |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 79 | inputFile >> lsaOrCombinedSeqNo >> seqNo; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 80 | m_corLsaSeq = seqNo; |
| 81 | |
| 82 | // File was in old format and had a combined sequence number |
| 83 | // if all of the seqNo should are still zero and |
| 84 | // lsaOrCombinedSeqNo != CorLsaSeq |
| 85 | if (m_nameLsaSeq == 0 && m_adjLsaSeq == 0 && m_corLsaSeq == 0 && |
| 86 | lsaOrCombinedSeqNo != "CorLsaSeq") { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 87 | NLSR_LOG_DEBUG("Old file had combined sequence number: " << lsaOrCombinedSeqNo); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 88 | std::istringstream iss(lsaOrCombinedSeqNo); |
| 89 | iss >> seqNo; |
| 90 | m_adjLsaSeq = (seqNo & 0xFFFFF); |
| 91 | m_corLsaSeq = ((seqNo >> 20) & 0xFFFFF); |
| 92 | m_nameLsaSeq = ((seqNo >> 40) & 0xFFFFFF); |
| 93 | } |
| 94 | |
| 95 | inputFile.close(); |
| 96 | |
| 97 | m_nameLsaSeq += 10; |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 98 | |
| 99 | // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 100 | if (m_hyperbolicState != HYPERBOLIC_STATE_ON) { |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 101 | if (m_corLsaSeq != 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 102 | NLSR_LOG_WARN("This router was previously configured for hyperbolic" |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 103 | << " routing without clearing the seq. no. file."); |
| 104 | m_corLsaSeq = 0; |
| 105 | } |
| 106 | m_adjLsaSeq += 10; |
| 107 | } |
| 108 | |
| 109 | // Similarly, increment the coordinate LSA seq. no only if link-state is disabled. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 110 | if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 111 | if (m_adjLsaSeq != 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 112 | NLSR_LOG_WARN("This router was previously configured for link-state" |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 113 | << " routing without clearing the seq. no. file."); |
| 114 | m_adjLsaSeq = 0; |
| 115 | } |
| 116 | m_corLsaSeq += 10; |
| 117 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | } |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 119 | writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 123 | SequencingManager::setSeqFileDirectory(const std::string& filePath) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | { |
| 125 | m_seqFileNameWithPath = filePath; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 126 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 127 | if (m_seqFileNameWithPath.empty()) { |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 128 | std::string homeDirPath(getpwuid(getuid())->pw_dir); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 129 | if (homeDirPath.empty()) { |
| 130 | homeDirPath = getenv("HOME"); |
| 131 | } |
| 132 | m_seqFileNameWithPath = homeDirPath; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | } |
| 134 | m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt"; |
| 135 | } |
| 136 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 137 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 138 | SequencingManager::writeLog() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 140 | NLSR_LOG_DEBUG("----SequencingManager----"); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 141 | if (m_hyperbolicState == HYPERBOLIC_STATE_OFF || |
| 142 | m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) { |
| 143 | NLSR_LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq); |
| 144 | } |
| 145 | if (m_hyperbolicState == HYPERBOLIC_STATE_ON || |
| 146 | m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) { |
| 147 | NLSR_LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq); |
| 148 | } |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 149 | NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 152 | } // namespace nlsr |