Loading...
Searching...
No Matches
sequencing-manager.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2025, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
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/>.
20 */
21
23#include "logger.hpp"
24
25#include <string>
26#include <fstream>
27#include <pwd.h>
28#include <cstdlib>
29#include <unistd.h>
30
31namespace nlsr {
32
33INIT_LOGGER(SequencingManager);
34
35SequencingManager::SequencingManager(const std::string& filePath, int hypState)
36 : m_hyperbolicState(hypState)
37{
38 setSeqFileDirectory(filePath);
39 initiateSeqNoFromFile();
40}
41
42void
44{
45 writeLog();
46 std::string tempPath = m_seqFileNameWithPath + ".tmp";
47 std::ofstream outputFile(tempPath.c_str());
48 outputFile << "NameLsaSeq " << m_nameLsaSeq << "\n"
49 << "AdjLsaSeq " << m_adjLsaSeq << "\n"
50 << "CorLsaSeq " << m_corLsaSeq;
51 outputFile.close();
52 std::filesystem::rename(tempPath, m_seqFileNameWithPath);
53}
54
55void
56SequencingManager::initiateSeqNoFromFile()
57{
58 NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
59 std::ifstream inputFile(m_seqFileNameWithPath.c_str());
60
61 std::string seqType;
62 // Good checks that file is not (bad or eof or fail)
63 if (inputFile.good()) {
64 inputFile >> seqType >> m_nameLsaSeq;
65 inputFile >> seqType >> m_adjLsaSeq;
66 inputFile >> seqType >> m_corLsaSeq;
67
68 inputFile.close();
69
70 // Increment by 10 in case last run of NLSR was not able to write to file
71 // before crashing
72 m_nameLsaSeq += 10;
73
74 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
75 if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
76 if (m_corLsaSeq != 0) {
77 NLSR_LOG_WARN("This router was previously configured for hyperbolic " <<
78 "routing without clearing the seq. no. file.");
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.
85 if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
86 if (m_adjLsaSeq != 0) {
87 NLSR_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 }
93 }
94 writeLog();
95}
96
97void
98SequencingManager::setSeqFileDirectory(const std::string& filePath)
99{
100 m_seqFileNameWithPath = filePath;
101
102 if (m_seqFileNameWithPath.empty()) {
103 std::string homeDirPath(getpwuid(getuid())->pw_dir);
104 if (homeDirPath.empty()) {
105 homeDirPath = getenv("HOME");
106 }
107 m_seqFileNameWithPath = homeDirPath;
108 }
109 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
110}
111
112void
113SequencingManager::writeLog() const
114{
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 }
123 NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
124}
125
126} // namespace nlsr
SequencingManager(const std::string &filePath, int hypState)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition logger.hpp:38
#define INIT_LOGGER(name)
Definition logger.hpp:35
#define NLSR_LOG_WARN(x)
Definition logger.hpp:40
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
@ HYPERBOLIC_STATE_ON
@ HYPERBOLIC_STATE_DRY_RUN
@ HYPERBOLIC_STATE_OFF