blob: 72e0c478d39dc381b52a8b76c026923a14c72195 [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
58SequencingManager::writeSeqNoToFile()
59{
60 std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary);
61 outputFile << m_combinedSeqNo;
62 outputFile.close();
63}
64
65void
66SequencingManager::initiateSeqNoFromFile()
67{
68 cout << "Seq File Name: " << m_seqFileNameWithPath << endl;
69 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
104ostream&
105operator<<(ostream& os, const SequencingManager& sm)
106{
107 std::cout << "----SequencingManager----" << std::endl;
108 std::cout << "Adj LSA seq no: " << sm.getAdjLsaSeq() << endl;
109 std::cout << "Cor LSA Seq no: " << sm.getCorLsaSeq() << endl;
110 std::cout << "Name LSA Seq no: " << sm.getNameLsaSeq() << endl;
111 std::cout << "Combined LSDB Seq no: " << sm.getCombinedSeqNo() << endl;
112 return os;
113}
114
115}//namespace nlsr
116
117