sequencing-manager.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "sequencing-manager.hpp"
23 #include "logger.hpp"
24 
25 #include <string>
26 #include <fstream>
27 #include <pwd.h>
28 #include <cstdlib>
29 #include <unistd.h>
30 
31 namespace nlsr {
32 
33 INIT_LOGGER(SequencingManager);
34 
35 SequencingManager::SequencingManager(const std::string& filePath, int hypState)
36  : m_hyperbolicState(hypState)
37 {
38  setSeqFileDirectory(filePath);
39  initiateSeqNoFromFile();
40 }
41 
42 void
44 {
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();
52  outputFile.close();
53 }
54 
55 void
56 SequencingManager::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 
97 void
98 SequencingManager::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 
112 void
113 SequencingManager::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