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> |
| 28 | |
| 29 | #include "sequencing-manager.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 30 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
| 32 | namespace nlsr { |
| 33 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | INIT_LOGGER("SequencingManager"); |
| 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
| 38 | void |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 39 | SequencingManager::splitSequenceNo(uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | { |
| 41 | m_combinedSeqNo = seqNo; |
| 42 | m_adjLsaSeq = (m_combinedSeqNo & 0xFFFFF); |
| 43 | m_corLsaSeq = ((m_combinedSeqNo >> 20) & 0xFFFFF); |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 44 | m_nameLsaSeq = ((m_combinedSeqNo >> 40) & 0xFFFFFF); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
| 48 | SequencingManager::combineSequenceNo() |
| 49 | { |
| 50 | m_combinedSeqNo = 0; |
| 51 | m_combinedSeqNo = m_combinedSeqNo | m_adjLsaSeq; |
| 52 | m_combinedSeqNo = m_combinedSeqNo | (m_corLsaSeq << 20); |
| 53 | m_combinedSeqNo = m_combinedSeqNo | (m_nameLsaSeq << 40); |
| 54 | } |
| 55 | |
| 56 | void |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 57 | SequencingManager::writeSeqNoToFile() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | { |
| 59 | std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary); |
| 60 | outputFile << m_combinedSeqNo; |
| 61 | outputFile.close(); |
| 62 | } |
| 63 | |
| 64 | void |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 65 | SequencingManager::initiateSeqNoFromFile(int hypState) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 67 | _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | if (inputFile.good()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | inputFile >> m_combinedSeqNo; |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 71 | splitSequenceNo(m_combinedSeqNo); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 72 | |
| 73 | // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled |
| 74 | if (hypState != HYPERBOLIC_STATE_ON) { |
| 75 | if (m_corLsaSeq != 0) { |
| 76 | _LOG_WARN("This router was previously configured for hyperbolic" |
| 77 | << " routing without clearing the seq. no. file."); |
| 78 | m_corLsaSeq = 0; |
| 79 | } |
| 80 | m_adjLsaSeq += 10; |
| 81 | } |
| 82 | |
| 83 | // Similarly, increment the coordinate LSA seq. no only if link-state is disabled. |
| 84 | if (hypState != HYPERBOLIC_STATE_OFF) { |
| 85 | if (m_adjLsaSeq != 0 && |
| 86 | hypState == HYPERBOLIC_STATE_ON) { |
| 87 | _LOG_WARN("This router was previously configured for link-state" |
| 88 | << " routing without clearing the seq. no. file."); |
| 89 | m_adjLsaSeq = 0; |
| 90 | } |
| 91 | m_corLsaSeq += 10; |
| 92 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | m_nameLsaSeq += 10; |
| 94 | combineSequenceNo(); |
| 95 | inputFile.close(); |
| 96 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 97 | else { |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 98 | splitSequenceNo(0); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | SequencingManager::setSeqFileName(string filePath) |
| 104 | { |
| 105 | m_seqFileNameWithPath = filePath; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 106 | if (m_seqFileNameWithPath.empty()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | m_seqFileNameWithPath = getUserHomeDirectory(); |
| 108 | } |
| 109 | m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt"; |
| 110 | } |
| 111 | |
| 112 | string |
| 113 | SequencingManager::getUserHomeDirectory() |
| 114 | { |
| 115 | string homeDirPath(getpwuid(getuid())->pw_dir); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 116 | if (homeDirPath.empty()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | homeDirPath = getenv("HOME"); |
| 118 | } |
| 119 | return homeDirPath; |
| 120 | } |
| 121 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 122 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 123 | SequencingManager::writeLog() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 125 | _LOG_DEBUG("----SequencingManager----"); |
| 126 | _LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq); |
| 127 | _LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq); |
| 128 | _LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq); |
| 129 | _LOG_DEBUG("Combined LSDB Seq no: " << m_combinedSeqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 132 | } // namespace nlsr |