akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame^] | 1 | /* -*- 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 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #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" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | |
| 33 | namespace nlsr { |
| 34 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 35 | INIT_LOGGER("SequencingManager"); |
| 36 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | using namespace std; |
| 38 | |
| 39 | void |
| 40 | SequencingManager::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 | |
| 48 | void |
| 49 | SequencingManager::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 | |
| 57 | void |
| 58 | SequencingManager::writeSeqNoToFile() |
| 59 | { |
| 60 | std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary); |
| 61 | outputFile << m_combinedSeqNo; |
| 62 | outputFile.close(); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | SequencingManager::initiateSeqNoFromFile() |
| 67 | { |
| 68 | cout << "Seq File Name: " << m_seqFileNameWithPath << endl; |
| 69 | std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 70 | if (inputFile.good()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | 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 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | splittSequenceNo(0); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | SequencingManager::setSeqFileName(string filePath) |
| 86 | { |
| 87 | m_seqFileNameWithPath = filePath; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 88 | if (m_seqFileNameWithPath.empty()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | m_seqFileNameWithPath = getUserHomeDirectory(); |
| 90 | } |
| 91 | m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt"; |
| 92 | } |
| 93 | |
| 94 | string |
| 95 | SequencingManager::getUserHomeDirectory() |
| 96 | { |
| 97 | string homeDirPath(getpwuid(getuid())->pw_dir); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 98 | if (homeDirPath.empty()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | homeDirPath = getenv("HOME"); |
| 100 | } |
| 101 | return homeDirPath; |
| 102 | } |
| 103 | |
| 104 | ostream& |
| 105 | operator<<(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 | |