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 <iostream>
27 #include <fstream>
28 #include <pwd.h>
29 #include <cstdlib>
30 #include <unistd.h>
31 #include <boost/algorithm/string.hpp>
32 
33 namespace nlsr {
34 
35 INIT_LOGGER(SequencingManager);
36 
37 SequencingManager::SequencingManager(const std::string& filePath, int hypState)
38  : m_hyperbolicState(hypState)
39 {
40  setSeqFileDirectory(filePath);
41  initiateSeqNoFromFile();
42 }
43 
44 void
46 {
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();
54  outputFile.close();
55 }
56 
57 void
58 SequencingManager::initiateSeqNoFromFile()
59 {
60  NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
61  std::ifstream inputFile(m_seqFileNameWithPath.c_str());
62 
63  std::string seqType;
64  // Good checks that file is not (bad or eof or fail)
65  if (inputFile.good()) {
66  inputFile >> seqType >> m_nameLsaSeq;
67  inputFile >> seqType >> m_adjLsaSeq;
68  inputFile >> seqType >> m_corLsaSeq;
69 
70  inputFile.close();
71 
72  // Increment by 10 in case last run of NLSR was not able to write to file
73  // before crashing
74  m_nameLsaSeq += 10;
75 
76  // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
77  if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
78  if (m_corLsaSeq != 0) {
79  NLSR_LOG_WARN("This router was previously configured for hyperbolic " <<
80  "routing without clearing the seq. no. file.");
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.
87  if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
88  if (m_adjLsaSeq != 0) {
89  NLSR_LOG_WARN("This router was previously configured for link-state " <<
90  "routing without clearing the seq. no. file.");
91  m_adjLsaSeq = 0;
92  }
93  m_corLsaSeq += 10;
94  }
95  }
96  writeLog();
97 }
98 
99 void
100 SequencingManager::setSeqFileDirectory(const std::string& filePath)
101 {
102  m_seqFileNameWithPath = filePath;
103 
104  if (m_seqFileNameWithPath.empty()) {
105  std::string homeDirPath(getpwuid(getuid())->pw_dir);
106  if (homeDirPath.empty()) {
107  homeDirPath = getenv("HOME");
108  }
109  m_seqFileNameWithPath = homeDirPath;
110  }
111  m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
112 }
113 
114 void
115 SequencingManager::writeLog() const
116 {
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  }
125  NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
126 }
127 
128 } // namespace nlsr
#define NLSR_LOG_WARN(x)
Definition: logger.hpp:40
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
SequencingManager(const std::string &filePath, int hypState)