forked from VestigeJ/dataminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataminer.cpp
154 lines (113 loc) · 3.57 KB
/
dataminer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <boost/thread.hpp>
#include <boost/tokenizer.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
#include <fstream>
#include "worker.h"
using namespace boost;
static Settings settings;
static string settingsName;
static Worker *createWorker(string outputLocation, string uri, string poll)
{
Worker *worker = new Worker();
if (!worker->setup(&settings, outputLocation, uri, poll))
return nullptr;
return worker;
}
static string getSettingsName(string progName)
{
const char* homeDrive = getenv("HOMEDRIVE");
const char* homePath = getenv("HOMEPATH");
string homeDir = "";
if (homeDrive != nullptr && homePath != nullptr)
{
// Windows
homeDir = homeDrive;
homeDir += homePath;
}
else {
// unix
const char *home = getenv("HOME");
if (home != nullptr)
homeDir = home;
// homeDir is "" (current directory) if no environment
}
boost::filesystem::path dir (homeDir);
boost::filesystem::path file (progName + ".settings");
boost::filesystem::path fullFilename = dir / file;
return fullFilename.string();
}
int main(int argc, char** argv)
{
// Check command line arguments.
if (argc < 2 || argc > 4)
{
std::cerr <<
"Usage: " << endl <<
" dataminer <output location> <uri> [poll cycle in seconds - defaults is 60]" << endl <<
"Example:" << endl <<
" dataminer /tmp https://smstestbed.nist.gov/vds" << endl <<
"or" << endl <<
" dataminer <config file>" << endl;
return -1;
}
settingsName = getSettingsName("dataminer");
settings.restore(settingsName);
std::vector<Worker*> worker_pool;
boost::thread *bthread = nullptr;
if (argc != 2)
{
string outputLocation = argv[1];
string uri = argv[2];
string poll = argc > 3 ? argv[3] : "60";
Worker *worker = createWorker(outputLocation, uri, poll);
if (worker == nullptr)
return -1;
worker_pool.push_back(worker);
bthread = new boost::thread(boost::bind(&Worker::run, worker));
}
else {
ifstream in(argv[1]);
if (!in.is_open()) {
std::cerr << "Cannot open config file " << argv[1] << "!" << endl;
return -1;
}
typedef tokenizer< escaped_list_separator<char> > Tokenizer;
vector< string > vec;
string line;
while (getline(in, line))
{
Tokenizer tok(line);
vec.assign(tok.begin(), tok.end());
unsigned long argc = vec.size();
// empty line
if (argc == 0)
continue;
if (argc != 2 && argc != 3)
{
std::cerr << "Invalid input [" << line << "]" << endl;
return -1;
}
string outputLocation = vec[0];
if (outputLocation[0] == '#')
continue;
string uri = vec[1];
string poll = argc > 2 ? vec[2] : "60";
Worker *worker = createWorker(outputLocation, uri, poll);
if (worker == nullptr)
continue;
worker_pool.push_back(worker);
bthread = new boost::thread(boost::bind(&Worker::run, worker));
if (bthread == nullptr)
return -1;
}
}
if (bthread == nullptr)
return -1;
while (true)
{
settings.save(settingsName);
boost::this_thread::sleep_for( boost::chrono::seconds(5) );
}
return 0;
}