blob: b988d9cc2ec935241be8d626e3e584f883564a91 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <string>
24#include <iostream>
25#include <fstream>
26#include <pwd.h>
27#include <cstdlib>
28#include <unistd.h>
29
30#include "sequencing-manager.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050032
33namespace nlsr {
34
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("SequencingManager");
36
akmhoque53353462014-04-22 08:43:45 -050037using namespace std;
38
39void
40SequencingManager::splittSequenceNo(uint64_t seqNo)
41{
42 m_combinedSeqNo = seqNo;
43 m_adjLsaSeq = (m_combinedSeqNo & 0xFFFFF);
44 m_corLsaSeq = ((m_combinedSeqNo >> 20) & 0xFFFFF);
45 m_nameLsaSeq = ((m_combinedSeqNo >> 40) & 0xFFFFF);
46}
47
48void
49SequencingManager::combineSequenceNo()
50{
51 m_combinedSeqNo = 0;
52 m_combinedSeqNo = m_combinedSeqNo | m_adjLsaSeq;
53 m_combinedSeqNo = m_combinedSeqNo | (m_corLsaSeq << 20);
54 m_combinedSeqNo = m_combinedSeqNo | (m_nameLsaSeq << 40);
55}
56
57void
Vince Lehman0bcf9a32014-12-10 11:24:45 -060058SequencingManager::writeSeqNoToFile() const
akmhoque53353462014-04-22 08:43:45 -050059{
60 std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary);
61 outputFile << m_combinedSeqNo;
62 outputFile.close();
63}
64
65void
66SequencingManager::initiateSeqNoFromFile()
67{
akmhoque2f423352014-06-03 11:49:35 -050068 _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
akmhoque53353462014-04-22 08:43:45 -050069 std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary);
akmhoque157b0a42014-05-13 00:26:37 -050070 if (inputFile.good()) {
akmhoque53353462014-04-22 08:43:45 -050071 inputFile >> m_combinedSeqNo;
72 splittSequenceNo(m_combinedSeqNo);
73 m_adjLsaSeq += 10;
74 m_corLsaSeq += 10;
75 m_nameLsaSeq += 10;
76 combineSequenceNo();
77 inputFile.close();
78 }
akmhoque157b0a42014-05-13 00:26:37 -050079 else {
akmhoque53353462014-04-22 08:43:45 -050080 splittSequenceNo(0);
81 }
82}
83
84void
85SequencingManager::setSeqFileName(string filePath)
86{
87 m_seqFileNameWithPath = filePath;
akmhoque157b0a42014-05-13 00:26:37 -050088 if (m_seqFileNameWithPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -050089 m_seqFileNameWithPath = getUserHomeDirectory();
90 }
91 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
92}
93
94string
95SequencingManager::getUserHomeDirectory()
96{
97 string homeDirPath(getpwuid(getuid())->pw_dir);
akmhoque157b0a42014-05-13 00:26:37 -050098 if (homeDirPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -050099 homeDirPath = getenv("HOME");
100 }
101 return homeDirPath;
102}
103
akmhoque2f423352014-06-03 11:49:35 -0500104void
Vince Lehman904c2412014-09-23 19:36:11 -0500105SequencingManager::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500106{
akmhoque2f423352014-06-03 11:49:35 -0500107 _LOG_DEBUG("----SequencingManager----");
108 _LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
109 _LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
110 _LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
111 _LOG_DEBUG("Combined LSDB Seq no: " << m_combinedSeqNo);
akmhoque53353462014-04-22 08:43:45 -0500112}
113
114}//namespace nlsr
115
116