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