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 | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, 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> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | #include <fstream> |
| 27 | #include <pwd.h> |
| 28 | #include <cstdlib> |
| 29 | #include <unistd.h> |
| 30 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | namespace nlsr { |
| 32 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 33 | INIT_LOGGER(SequencingManager); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | |
Ashlesh Gawande | 55d1b5c | 2019-12-19 16:21:58 -0600 | [diff] [blame] | 35 | SequencingManager::SequencingManager(const std::string& filePath, int hypState) |
| 36 | : m_hyperbolicState(hypState) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 37 | { |
| 38 | setSeqFileDirectory(filePath); |
| 39 | initiateSeqNoFromFile(); |
| 40 | } |
| 41 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 42 | void |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 43 | SequencingManager::writeSeqNoToFile() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 45 | 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(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | outputFile.close(); |
| 53 | } |
| 54 | |
| 55 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 56 | SequencingManager::initiateSeqNoFromFile() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 58 | NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 59 | std::ifstream inputFile(m_seqFileNameWithPath.c_str()); |
| 60 | |
Ashlesh Gawande | 55d1b5c | 2019-12-19 16:21:58 -0600 | [diff] [blame] | 61 | std::string seqType; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 62 | // Good checks that file is not (bad or eof or fail) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 63 | if (inputFile.good()) { |
Ashlesh Gawande | 55d1b5c | 2019-12-19 16:21:58 -0600 | [diff] [blame] | 64 | inputFile >> seqType >> m_nameLsaSeq; |
| 65 | inputFile >> seqType >> m_adjLsaSeq; |
| 66 | inputFile >> seqType >> m_corLsaSeq; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 67 | |
| 68 | inputFile.close(); |
| 69 | |
Ashlesh Gawande | 55d1b5c | 2019-12-19 16:21:58 -0600 | [diff] [blame] | 70 | // Increment by 10 in case last run of NLSR was not able to write to file |
| 71 | // before crashing |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 72 | m_nameLsaSeq += 10; |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 73 | |
| 74 | // 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] | 75 | if (m_hyperbolicState != HYPERBOLIC_STATE_ON) { |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 76 | if (m_corLsaSeq != 0) { |
Ashlesh Gawande | 55d1b5c | 2019-12-19 16:21:58 -0600 | [diff] [blame] | 77 | NLSR_LOG_WARN("This router was previously configured for hyperbolic " << |
| 78 | "routing without clearing the seq. no. file."); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 79 | 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 Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 85 | if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 86 | if (m_adjLsaSeq != 0) { |
Ashlesh Gawande | 55d1b5c | 2019-12-19 16:21:58 -0600 | [diff] [blame] | 87 | NLSR_LOG_WARN("This router was previously configured for link-state " << |
| 88 | "routing without clearing the seq. no. file."); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 89 | m_adjLsaSeq = 0; |
| 90 | } |
| 91 | m_corLsaSeq += 10; |
| 92 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | } |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 94 | writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 98 | SequencingManager::setSeqFileDirectory(const std::string& filePath) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | { |
| 100 | m_seqFileNameWithPath = filePath; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 101 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 102 | if (m_seqFileNameWithPath.empty()) { |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 103 | std::string homeDirPath(getpwuid(getuid())->pw_dir); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 104 | if (homeDirPath.empty()) { |
| 105 | homeDirPath = getenv("HOME"); |
| 106 | } |
| 107 | m_seqFileNameWithPath = homeDirPath; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 108 | } |
| 109 | m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt"; |
| 110 | } |
| 111 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 112 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 113 | SequencingManager::writeLog() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 115 | 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 | } |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 123 | NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 126 | } // namespace nlsr |