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(std::string filePath, int hypState)
38  : m_nameLsaSeq(0)
39  , m_adjLsaSeq(0)
40  , m_corLsaSeq(0)
41  , m_hyperbolicState(hypState)
42 {
43  setSeqFileDirectory(filePath);
44  initiateSeqNoFromFile();
45 }
46 
47 void
49 {
50  writeLog();
51  std::ofstream outputFile(m_seqFileNameWithPath.c_str());
52  std::ostringstream os;
53  os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
54  << "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
55  << "CorLsaSeq " << std::to_string(m_corLsaSeq);
56  outputFile << os.str();
57  outputFile.close();
58 }
59 
60 void
61 SequencingManager::initiateSeqNoFromFile()
62 {
63  NLSR_LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
64  std::ifstream inputFile(m_seqFileNameWithPath.c_str());
65 
66  // Good checks that file is not (bad or eof or fail)
67  if (inputFile.good()) {
68  std::string lsaOrCombinedSeqNo;
69  uint64_t seqNo = 0;
70 
71  // If file has a combined seq number, lsaOrCombinedSeqNo would hold it
72  // and seqNo will be zero everytime
73  inputFile >> lsaOrCombinedSeqNo >> seqNo;
74  m_nameLsaSeq = seqNo;
75 
76  inputFile >> lsaOrCombinedSeqNo >> seqNo;
77  m_adjLsaSeq = seqNo;
78 
79  inputFile >> lsaOrCombinedSeqNo >> seqNo;
80  m_corLsaSeq = seqNo;
81 
82  // File was in old format and had a combined sequence number
83  // if all of the seqNo should are still zero and
84  // lsaOrCombinedSeqNo != CorLsaSeq
85  if (m_nameLsaSeq == 0 && m_adjLsaSeq == 0 && m_corLsaSeq == 0 &&
86  lsaOrCombinedSeqNo != "CorLsaSeq") {
87  NLSR_LOG_DEBUG("Old file had combined sequence number: " << lsaOrCombinedSeqNo);
88  std::istringstream iss(lsaOrCombinedSeqNo);
89  iss >> seqNo;
90  m_adjLsaSeq = (seqNo & 0xFFFFF);
91  m_corLsaSeq = ((seqNo >> 20) & 0xFFFFF);
92  m_nameLsaSeq = ((seqNo >> 40) & 0xFFFFFF);
93  }
94 
95  inputFile.close();
96 
97  m_nameLsaSeq += 10;
98 
99  // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
100  if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
101  if (m_corLsaSeq != 0) {
102  NLSR_LOG_WARN("This router was previously configured for hyperbolic"
103  << " routing without clearing the seq. no. file.");
104  m_corLsaSeq = 0;
105  }
106  m_adjLsaSeq += 10;
107  }
108 
109  // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
110  if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
111  if (m_adjLsaSeq != 0) {
112  NLSR_LOG_WARN("This router was previously configured for link-state"
113  << " routing without clearing the seq. no. file.");
114  m_adjLsaSeq = 0;
115  }
116  m_corLsaSeq += 10;
117  }
118  }
119  writeLog();
120 }
121 
122 void
123 SequencingManager::setSeqFileDirectory(const std::string& filePath)
124 {
125  m_seqFileNameWithPath = filePath;
126 
127  if (m_seqFileNameWithPath.empty()) {
128  std::string homeDirPath(getpwuid(getuid())->pw_dir);
129  if (homeDirPath.empty()) {
130  homeDirPath = getenv("HOME");
131  }
132  m_seqFileNameWithPath = homeDirPath;
133  }
134  m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
135 }
136 
137 void
138 SequencingManager::writeLog() const
139 {
140  NLSR_LOG_DEBUG("----SequencingManager----");
141  if (m_hyperbolicState == HYPERBOLIC_STATE_OFF ||
142  m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
143  NLSR_LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
144  }
145  if (m_hyperbolicState == HYPERBOLIC_STATE_ON ||
146  m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
147  NLSR_LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
148  }
149  NLSR_LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
150 }
151 
152 } // 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(std::string filePath, int hypState)