-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconf.cc
More file actions
120 lines (103 loc) · 2.65 KB
/
conf.cc
File metadata and controls
120 lines (103 loc) · 2.65 KB
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
#include "./include/conf.hpp"
#include "./include/core.hpp"
//���ļ��ж�ȡ����
std::string GetOptLong(int argc, char *argv[])
{
std::string rc = "";
int opt, option_index = 0;
const char *optstring = ":c:";
static struct option long_options[] = {
{"config_path", required_argument, NULL, 'c'}, {0, 0, 0, 0}};
while (true)
{
if (-1 == (opt = getopt_long(argc, argv, optstring, long_options,
&option_index)))
{
break;
}
if ('c' == opt)
{
rc.assign(argv[optind - 1]);
}
}
return rc;
}
bool readConf(std::string conf,
std::vector<NodeInfo> & p,
NodeInfo & c,
Mode & m)
{
std::fstream conf_file;
conf_file.open(conf);
if (!conf_file.is_open())
{
return false;
}
// read
while (!conf_file.eof())
{
std::string buf;
getline(conf_file, buf);
// Annotate
if ('!' == buf[0])
{
continue;
}
// get the mode
if(buf.find("mode") != std::string::npos) {
if(buf.find("coordinator") != std::string::npos) {
m = MODE_C;
} else if(buf.find("participant") != std::string::npos) {
m = MODE_P;
} else {
m = MODE_INVALID;
}
}
else if(buf.find("coordinator_info") != std::string::npos) {
size_t pos = buf.find("coordinator_info");
buf = buf.substr(pos + 16);
buf.erase(remove(buf.begin(), buf.end(), ' '), buf.end());
pos = buf.find(":");
if(pos == std::string::npos) {
return false;
}
else {
c.add = buf.substr(0, pos);
c.port = strtol((buf.substr(pos + 1)).c_str(), nullptr, 10);
}
}
else if(buf.find("participant_info") != std::string::npos) {
NodeInfo tmp;
size_t pos = buf.find("participant_info");
buf = buf.substr(pos + 16);
buf.erase(remove(buf.begin(), buf.end(), ' '), buf.end());
pos = buf.find(":");
if(pos == std::string::npos) {
return false;
}
else {
tmp.add = buf.substr(0, pos);
tmp.port = strtol((buf.substr(pos + 1)).c_str(), nullptr, 10);
p.push_back(tmp);
}
}
}
return true;
}
void showInfo(std::vector<NodeInfo> p,
NodeInfo c,
Mode m)
{
if(m == MODE_C) {
std::cout << "COORDINATOR" << std::endl;
} else if ( m == MODE_P ) {
std::cout << "PARTICIPANT" << std::endl;
} else {
std::cout << "INVALID" << std::endl;
}
std::cout << "C: " << c.add << ":" << c.port << std::endl;
for(auto entry : p)
{
std::cout << "P: " << entry.add << ":" << entry.port << std::endl;
}
}