blob: 2bffc285a14297f2da5b7dfa15e7e49ef6a28785 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2e51f62015-01-20 15:03:11 -06003 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include <string>
23#include <iostream>
24#include <fstream>
25#include <pwd.h>
26#include <cstdlib>
27#include <unistd.h>
28
29#include "sequencing-manager.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050031
32namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("SequencingManager");
35
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37
38void
Vince Lehman6151e952015-02-16 12:36:00 -060039SequencingManager::splitSequenceNo(uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050040{
41 m_combinedSeqNo = seqNo;
42 m_adjLsaSeq = (m_combinedSeqNo & 0xFFFFF);
43 m_corLsaSeq = ((m_combinedSeqNo >> 20) & 0xFFFFF);
Vince Lehman6151e952015-02-16 12:36:00 -060044 m_nameLsaSeq = ((m_combinedSeqNo >> 40) & 0xFFFFFF);
akmhoque53353462014-04-22 08:43:45 -050045}
46
47void
48SequencingManager::combineSequenceNo()
49{
50 m_combinedSeqNo = 0;
51 m_combinedSeqNo = m_combinedSeqNo | m_adjLsaSeq;
52 m_combinedSeqNo = m_combinedSeqNo | (m_corLsaSeq << 20);
53 m_combinedSeqNo = m_combinedSeqNo | (m_nameLsaSeq << 40);
54}
55
56void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060057SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050058{
59 std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary);
60 outputFile << m_combinedSeqNo;
61 outputFile.close();
62}
63
64void
Nick Gordon5c467f02016-07-13 13:40:10 -050065SequencingManager::initiateSeqNoFromFile(int hypState)
akmhoque53353462014-04-22 08:43:45 -050066{
akmhoque2f423352014-06-03 11:49:35 -050067 _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
akmhoque53353462014-04-22 08:43:45 -050068 std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary);
akmhoque157b0a42014-05-13 00:26:37 -050069 if (inputFile.good()) {
akmhoque53353462014-04-22 08:43:45 -050070 inputFile >> m_combinedSeqNo;
Vince Lehman6151e952015-02-16 12:36:00 -060071 splitSequenceNo(m_combinedSeqNo);
Nick Gordon5c467f02016-07-13 13:40:10 -050072
73 // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
74 if (hypState != HYPERBOLIC_STATE_ON) {
75 if (m_corLsaSeq != 0) {
76 _LOG_WARN("This router was previously configured for hyperbolic"
77 << " routing without clearing the seq. no. file.");
78 m_corLsaSeq = 0;
79 }
80 m_adjLsaSeq += 10;
81 }
82
83 // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
84 if (hypState != HYPERBOLIC_STATE_OFF) {
85 if (m_adjLsaSeq != 0 &&
86 hypState == HYPERBOLIC_STATE_ON) {
87 _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 }
akmhoque53353462014-04-22 08:43:45 -050093 m_nameLsaSeq += 10;
94 combineSequenceNo();
95 inputFile.close();
96 }
akmhoque157b0a42014-05-13 00:26:37 -050097 else {
Vince Lehman6151e952015-02-16 12:36:00 -060098 splitSequenceNo(0);
akmhoque53353462014-04-22 08:43:45 -050099 }
100}
101
102void
103SequencingManager::setSeqFileName(string filePath)
104{
105 m_seqFileNameWithPath = filePath;
akmhoque157b0a42014-05-13 00:26:37 -0500106 if (m_seqFileNameWithPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -0500107 m_seqFileNameWithPath = getUserHomeDirectory();
108 }
109 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
110}
111
112string
113SequencingManager::getUserHomeDirectory()
114{
115 string homeDirPath(getpwuid(getuid())->pw_dir);
akmhoque157b0a42014-05-13 00:26:37 -0500116 if (homeDirPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -0500117 homeDirPath = getenv("HOME");
118 }
119 return homeDirPath;
120}
121
akmhoque2f423352014-06-03 11:49:35 -0500122void
Vince Lehman904c2412014-09-23 19:36:11 -0500123SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500124{
akmhoque2f423352014-06-03 11:49:35 -0500125 _LOG_DEBUG("----SequencingManager----");
126 _LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
127 _LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
128 _LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
129 _LOG_DEBUG("Combined LSDB Seq no: " << m_combinedSeqNo);
akmhoque53353462014-04-22 08:43:45 -0500130}
131
132}//namespace nlsr