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