-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodelist.cxx
236 lines (196 loc) · 4.51 KB
/
nodelist.cxx
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
*
*
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
const char *SEPARATOR = " ";
struct nodeprop {
int id;
std::string host;
int port;
bool is_dummy;
};
struct rbcp_com {
std::string host;
unsigned int port;
unsigned int address;
std::vector<char> data;
};
bool isNumber(const std::string &s)
{
bool val;
if (s.find_first_not_of(".0123456789") == std::string::npos
&& s.front() != '.'
&& s.back() != '.') {
val = true;
} else {
val = false;
}
return val;
}
bool isHexNumber(const std::string &s)
{
bool val;
if (s.find_first_not_of("0123456789abcdefABCDEF")
== std::string::npos) {
val = true;
} else {
val = false;
}
return val;
}
int process_node(std::vector<struct nodeprop> &nodes)
{
return 0;
}
int process_rbcp(std::vector <std::string> &words, std::vector<struct rbcp_com> &com)
{
static std::string host("bc");
static unsigned int port(4660);
#if 0
std::cout << "#DR ";
for (auto &i : words) std::cout << " " << i;
std::cout << std::endl;
#endif
if ((words[0] == "rbcp") || (words[0] == "RBCP")) {
if ((words[1] == "host") && (words.size() > 2)) {
host = words[2];
}
if ((words[1] == "port") && (words.size() > 2)) {
port = std::stoi(words[2], nullptr, 10);
}
if (words.size() > 3) {
if (isHexNumber(words[1]) && isHexNumber(words[2])) {
struct rbcp_com lcom;
lcom.host = host;
lcom.port = port;
lcom.address = std::stoi(words[1], nullptr, 16);
for (unsigned int i = 2 ; i < words.size() ; i++) {
unsigned int val = std::stoi(words[i], nullptr, 16);
char cval = static_cast<char>(val & 0xff);
lcom.data.push_back(cval);
}
com.push_back(lcom);
} else {
std::cerr << "wrong line :";
for (unsigned int i = 0 ; i < words.size() ; i++) {
std::cerr << " " << words[i];
}
std::cerr << std::endl;
}
}
} else {
return -1;
}
return 0;
}
int nodelist(char *filename,
std::vector<struct nodeprop> &nodes,
std::vector<struct rbcp_com> &rcoms)
{
std::ifstream ifs(filename);
if (! ifs.is_open()) {
std::cerr << filename << " : file open error" <<std::endl;
return -1;
}
std::string line;
while (getline(ifs, line)) {
std::vector <std::string> words;
words.clear();
words.resize(0);
int offset = 0;
while (true) {
std::string::size_type pos = line.find(SEPARATOR, offset);
if (pos == std::string::npos) {
if (line.substr(offset) != "") {
words.push_back(line.substr(offset));
}
break;
}
std::string oneword(line.substr(offset, pos - offset));
if (oneword != "") {
words.push_back(oneword);
}
offset = pos + 1;
}
if (words.size() < 1) continue;
#if 0
std::cout << "#D " << words.size() << " ; " << line << std::endl;
#endif
if (words[0].front() == '#') continue;
if ((words[0] == "rbcp") || (words[0] == "RBCP")) {
process_rbcp(words, rcoms);
continue;
}
if (words.size() >= 3) {
struct nodeprop nprop;
nprop.is_dummy = false;
if (isNumber(words[0])) {
std::istringstream iss(words[0]);
iss >> nprop.id;
} else {
std::cerr << "Err. : " << words[0]
<< " in " << line << std::endl;
continue;
}
nprop.host = words[1];
if (isNumber(words[2])) {
std::istringstream iss(words[2]);
iss >> nprop.port;
} else {
std::cerr << "Err. : " << words[2]
<< " in " << line << std::endl;
continue;
}
if (words.size() > 3) {
if ((words[3] == "dummy")
|| (words[3] == "DUMMY")) {
nprop.is_dummy = true;
}
}
#if 0
std::cout << "id: " << nprop.id
<< " host: " << nprop.host
<< " port: " << nprop.port <<std::endl;
#endif
nodes.push_back(nprop);
} else {
std::cerr << "Err. : " << line << std::endl;
continue;
}
}
ifs.close();
return 0;
}
#ifdef TEST_MAIN
int main(int argc, char* argv[])
{
std::vector<struct nodeprop> nodes;
std::vector<struct rbcp_com> rcommands;
nodelist(argv[1], nodes, rcommands);
for (auto i : nodes) {
std::cout << "id: " << std::dec << i.id
<< " host: " <<i.host
<< " port: " << i.port
<< " is_dummy: " << i.is_dummy <<std::endl;
}
for (auto i : rcommands) {
std::cout << "host: " << i.host
<< " port: " << std::dec << i.port
<< " address: 0x" << std::hex << std::setfill('0') << std::setw(4)
<< i.address
<< " :";
for (auto j : i.data) {
std::cout << " " << std::hex << std::setfill('0') << std::setw(2)
<< (static_cast<unsigned int>(j) & 0xff);
}
std::cout << std::endl;
}
return 0;
}
#endif