akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #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" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 30 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
| 32 | namespace nlsr { |
| 33 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | INIT_LOGGER("SequencingManager"); |
| 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
| 38 | void |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 39 | SequencingManager::splitSequenceNo(uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | { |
| 41 | m_combinedSeqNo = seqNo; |
| 42 | m_adjLsaSeq = (m_combinedSeqNo & 0xFFFFF); |
| 43 | m_corLsaSeq = ((m_combinedSeqNo >> 20) & 0xFFFFF); |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 44 | m_nameLsaSeq = ((m_combinedSeqNo >> 40) & 0xFFFFFF); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
| 48 | SequencingManager::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 | |
| 56 | void |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 57 | SequencingManager::writeSeqNoToFile() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | { |
| 59 | std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary); |
| 60 | outputFile << m_combinedSeqNo; |
| 61 | outputFile.close(); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | SequencingManager::initiateSeqNoFromFile() |
| 66 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 67 | _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | if (inputFile.good()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | inputFile >> m_combinedSeqNo; |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 71 | splitSequenceNo(m_combinedSeqNo); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | m_adjLsaSeq += 10; |
| 73 | m_corLsaSeq += 10; |
| 74 | m_nameLsaSeq += 10; |
| 75 | combineSequenceNo(); |
| 76 | inputFile.close(); |
| 77 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 78 | else { |
Vince Lehman | 6151e95 | 2015-02-16 12:36:00 -0600 | [diff] [blame] | 79 | splitSequenceNo(0); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | SequencingManager::setSeqFileName(string filePath) |
| 85 | { |
| 86 | m_seqFileNameWithPath = filePath; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 87 | if (m_seqFileNameWithPath.empty()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | m_seqFileNameWithPath = getUserHomeDirectory(); |
| 89 | } |
| 90 | m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt"; |
| 91 | } |
| 92 | |
| 93 | string |
| 94 | SequencingManager::getUserHomeDirectory() |
| 95 | { |
| 96 | string homeDirPath(getpwuid(getuid())->pw_dir); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 97 | if (homeDirPath.empty()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 98 | homeDirPath = getenv("HOME"); |
| 99 | } |
| 100 | return homeDirPath; |
| 101 | } |
| 102 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 103 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 104 | SequencingManager::writeLog() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 105 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 106 | _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); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | }//namespace nlsr |
| 114 | |
| 115 | |