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