blob: 7749ae0b05c51b5ea8abf3db36760fd56925d1fa [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
65SequencingManager::initiateSeqNoFromFile()
66{
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);
akmhoque53353462014-04-22 08:43:45 -050072 m_adjLsaSeq += 10;
73 m_corLsaSeq += 10;
74 m_nameLsaSeq += 10;
75 combineSequenceNo();
76 inputFile.close();
77 }
akmhoque157b0a42014-05-13 00:26:37 -050078 else {
Vince Lehman6151e952015-02-16 12:36:00 -060079 splitSequenceNo(0);
akmhoque53353462014-04-22 08:43:45 -050080 }
81}
82
83void
84SequencingManager::setSeqFileName(string filePath)
85{
86 m_seqFileNameWithPath = filePath;
akmhoque157b0a42014-05-13 00:26:37 -050087 if (m_seqFileNameWithPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -050088 m_seqFileNameWithPath = getUserHomeDirectory();
89 }
90 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
91}
92
93string
94SequencingManager::getUserHomeDirectory()
95{
96 string homeDirPath(getpwuid(getuid())->pw_dir);
akmhoque157b0a42014-05-13 00:26:37 -050097 if (homeDirPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -050098 homeDirPath = getenv("HOME");
99 }
100 return homeDirPath;
101}
102
akmhoque2f423352014-06-03 11:49:35 -0500103void
Vince Lehman904c2412014-09-23 19:36:11 -0500104SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500105{
akmhoque2f423352014-06-03 11:49:35 -0500106 _LOG_DEBUG("----SequencingManager----");
107 _LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
108 _LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
109 _LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
110 _LOG_DEBUG("Combined LSDB Seq no: " << m_combinedSeqNo);
akmhoque53353462014-04-22 08:43:45 -0500111}
112
113}//namespace nlsr
114
115