blob: dbf943f8ba6a0d1fcdb3bfc3b8628a3d80395e79 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <iostream>
3#include <fstream>
4#include <pwd.h>
5#include <cstdlib>
6#include <unistd.h>
7
8#include "sequencing-manager.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05009#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050010
11namespace nlsr {
12
akmhoque674b0b12014-05-20 14:33:28 -050013INIT_LOGGER("SequencingManager");
14
akmhoque53353462014-04-22 08:43:45 -050015using namespace std;
16
17void
18SequencingManager::splittSequenceNo(uint64_t seqNo)
19{
20 m_combinedSeqNo = seqNo;
21 m_adjLsaSeq = (m_combinedSeqNo & 0xFFFFF);
22 m_corLsaSeq = ((m_combinedSeqNo >> 20) & 0xFFFFF);
23 m_nameLsaSeq = ((m_combinedSeqNo >> 40) & 0xFFFFF);
24}
25
26void
27SequencingManager::combineSequenceNo()
28{
29 m_combinedSeqNo = 0;
30 m_combinedSeqNo = m_combinedSeqNo | m_adjLsaSeq;
31 m_combinedSeqNo = m_combinedSeqNo | (m_corLsaSeq << 20);
32 m_combinedSeqNo = m_combinedSeqNo | (m_nameLsaSeq << 40);
33}
34
35void
36SequencingManager::writeSeqNoToFile()
37{
38 std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary);
39 outputFile << m_combinedSeqNo;
40 outputFile.close();
41}
42
43void
44SequencingManager::initiateSeqNoFromFile()
45{
46 cout << "Seq File Name: " << m_seqFileNameWithPath << endl;
47 std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary);
akmhoque157b0a42014-05-13 00:26:37 -050048 if (inputFile.good()) {
akmhoque53353462014-04-22 08:43:45 -050049 inputFile >> m_combinedSeqNo;
50 splittSequenceNo(m_combinedSeqNo);
51 m_adjLsaSeq += 10;
52 m_corLsaSeq += 10;
53 m_nameLsaSeq += 10;
54 combineSequenceNo();
55 inputFile.close();
56 }
akmhoque157b0a42014-05-13 00:26:37 -050057 else {
akmhoque53353462014-04-22 08:43:45 -050058 splittSequenceNo(0);
59 }
60}
61
62void
63SequencingManager::setSeqFileName(string filePath)
64{
65 m_seqFileNameWithPath = filePath;
akmhoque157b0a42014-05-13 00:26:37 -050066 if (m_seqFileNameWithPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -050067 m_seqFileNameWithPath = getUserHomeDirectory();
68 }
69 m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
70}
71
72string
73SequencingManager::getUserHomeDirectory()
74{
75 string homeDirPath(getpwuid(getuid())->pw_dir);
akmhoque157b0a42014-05-13 00:26:37 -050076 if (homeDirPath.empty()) {
akmhoque53353462014-04-22 08:43:45 -050077 homeDirPath = getenv("HOME");
78 }
79 return homeDirPath;
80}
81
82ostream&
83operator<<(ostream& os, const SequencingManager& sm)
84{
85 std::cout << "----SequencingManager----" << std::endl;
86 std::cout << "Adj LSA seq no: " << sm.getAdjLsaSeq() << endl;
87 std::cout << "Cor LSA Seq no: " << sm.getCorLsaSeq() << endl;
88 std::cout << "Name LSA Seq no: " << sm.getNameLsaSeq() << endl;
89 std::cout << "Combined LSDB Seq no: " << sm.getCombinedSeqNo() << endl;
90 return os;
91}
92
93}//namespace nlsr
94
95