/
StringMessageBuilder.cpp
382 lines (309 loc) · 9.26 KB
/
StringMessageBuilder.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* StringMessageBuilder.cpp
* Builds a Message object from a list of strings & a Descriptor.
* Copyright (C) 2011 Simon Newton
*/
#include <ola/Logging.h>
#include <ola/StringUtils.h>
#include <ola/messaging/Descriptor.h>
#include <ola/messaging/Message.h>
#include <ola/network/IPV4Address.h>
#include <ola/network/MACAddress.h>
#include <ola/rdm/StringMessageBuilder.h>
#include <ola/rdm/UID.h>
#include <memory>
#include <string>
#include <vector>
#include "common/rdm/GroupSizeCalculator.h"
namespace ola {
namespace rdm {
using ola::messaging::MessageFieldInterface;
using ola::rdm::UID;
using std::auto_ptr;
using std::string;
using std::vector;
StringMessageBuilder::StringMessageBuilder()
: m_offset(0),
m_input_size(0),
m_group_instance_count(0),
m_error(false) {
}
/**
* @brief Clean up
*/
StringMessageBuilder::~StringMessageBuilder() {
CleanUpVector();
}
/**
* @brief Get the Message object that this Builder created
*
* This method is *not* re-entrant.
* @param inputs the string inputs provided to build the Message
* @param descriptor the descriptor to use to build the Message
* @returns a Message object, or NULL if the inputs failed.
*/
const ola::messaging::Message *StringMessageBuilder::GetMessage(
const vector<string> &inputs,
const ola::messaging::Descriptor *descriptor) {
InitVars(inputs);
// first figure out if the number of inputs provided matches the number
// expected by the descriptor. This accounts for repeating groups.
GroupSizeCalculator calculator;
GroupSizeCalculator::calculator_state state = calculator.CalculateGroupSize(
inputs.size(),
descriptor,
&m_group_instance_count);
switch (state) {
case GroupSizeCalculator::INSUFFICIENT_TOKENS:
SetError("Insufficient tokens");
return NULL;
case GroupSizeCalculator::EXTRA_TOKENS:
SetError("Extra tokens");
return NULL;
case GroupSizeCalculator::MISMATCHED_TOKENS:
SetError("Mismatched tokens");
return NULL;
case GroupSizeCalculator::MULTIPLE_VARIABLE_GROUPS:
SetError("Multiple variable groups");
return NULL;
case GroupSizeCalculator::NESTED_VARIABLE_GROUPS:
SetError("Nested variable groups");
return NULL;
case GroupSizeCalculator::SINGLE_VARIABLE_GROUP:
case GroupSizeCalculator::NO_VARIABLE_GROUPS:
break;
}
// now we now that this list of inputs can be parsed, and we know the number
// of instances of a repeating group if there is one.
descriptor->Accept(this);
if (m_error) {
OLA_WARN << "Error building message, field is: " << m_error_string;
return NULL;
}
if (m_groups.size() != 1) {
OLA_WARN << "Mismatched stack, size was " << m_groups.size();
return NULL;
}
const ola::messaging::Message *message = new ola::messaging::Message(
m_groups.top());
m_groups.top().clear();
return message;
}
/**
* Bool values can be true,false,0,1
*/
void StringMessageBuilder::Visit(
const ola::messaging::BoolFieldDescriptor *descriptor) {
if (StopParsing())
return;
bool value = false;
bool valid = false;
string token = m_inputs[m_offset++];
ola::StringTrim(&token);
ola::ToLower(&token);
if (token == "true") {
valid = value = true;
} else if (token == "false") {
value = false;
valid = true;
}
if (!valid) {
uint8_t int_value;
if (ola::StringToInt(token, &int_value)) {
if (int_value == 1) {
valid = value = true;
} else if (int_value == 0) {
valid = true;
value = false;
}
}
}
if (!valid) {
SetError(descriptor->Name());
return;
}
m_groups.top().push_back(
new ola::messaging::BoolMessageField(descriptor, value));
}
/**
* IPV4 Addresses
*/
void StringMessageBuilder::Visit(
const ola::messaging::IPV4FieldDescriptor *descriptor) {
if (StopParsing())
return;
string token = m_inputs[m_offset++];
ola::network::IPV4Address ip_address;
if (!ola::network::IPV4Address::FromString(token, &ip_address)) {
SetError(descriptor->Name());
return;
}
m_groups.top().push_back(
new ola::messaging::IPV4MessageField(descriptor, ip_address));
}
/**
* MAC Addresses
*/
void StringMessageBuilder::Visit(
const ola::messaging::MACFieldDescriptor *descriptor) {
if (StopParsing())
return;
string token = m_inputs[m_offset++];
ola::network::MACAddress mac_address;
if (!ola::network::MACAddress::FromString(token, &mac_address)) {
SetError(descriptor->Name());
return;
}
m_groups.top().push_back(
new ola::messaging::MACMessageField(descriptor, mac_address));
}
/**
* UIDs.
*/
void StringMessageBuilder::Visit(
const ola::messaging::UIDFieldDescriptor *descriptor) {
if (StopParsing())
return;
string token = m_inputs[m_offset++];
auto_ptr<UID> uid(UID::FromString(token));
if (!uid.get()) {
SetError(descriptor->Name());
return;
}
m_groups.top().push_back(
new ola::messaging::UIDMessageField(descriptor, *uid));
}
/**
* Handle strings
*/
void StringMessageBuilder::Visit(
const ola::messaging::StringFieldDescriptor *descriptor) {
if (StopParsing())
return;
const string &token = m_inputs[m_offset++];
if (descriptor->MaxSize() != 0 &&
token.size() > descriptor->MaxSize()) {
SetError(descriptor->Name());
return;
}
m_groups.top().push_back(
new ola::messaging::StringMessageField(descriptor, token));
}
/**
* uint8
*/
void StringMessageBuilder::Visit(
const ola::messaging::UInt8FieldDescriptor *descriptor) {
VisitInt(descriptor);
}
void StringMessageBuilder::Visit(
const ola::messaging::UInt16FieldDescriptor *descriptor) {
VisitInt(descriptor);
}
void StringMessageBuilder::Visit(
const ola::messaging::UInt32FieldDescriptor *descriptor) {
VisitInt(descriptor);
}
void StringMessageBuilder::Visit(
const ola::messaging::Int8FieldDescriptor *descriptor) {
VisitInt(descriptor);
}
void StringMessageBuilder::Visit(
const ola::messaging::Int16FieldDescriptor *descriptor) {
VisitInt(descriptor);
}
void StringMessageBuilder::Visit(
const ola::messaging::Int32FieldDescriptor *descriptor) {
VisitInt(descriptor);
}
/**
* Visit a group
*/
void StringMessageBuilder::Visit(
const ola::messaging::FieldDescriptorGroup *descriptor) {
unsigned int iterations = descriptor->FixedSize() ? descriptor->MinBlocks() :
m_group_instance_count;
for (unsigned int i = 0; i < iterations; ++i) {
vector<const MessageFieldInterface*> fields;
m_groups.push(fields);
for (unsigned int j = 0; j < descriptor->FieldCount(); ++j) {
descriptor->GetField(j)->Accept(this);
}
const vector<const MessageFieldInterface*> &populated_fields =
m_groups.top();
const ola::messaging::MessageFieldInterface *message =
new ola::messaging::GroupMessageField(descriptor, populated_fields);
m_groups.pop();
m_groups.top().push_back(message);
}
}
/**
* This is a no-op since we handle descending ourselves in Visit()
*/
void StringMessageBuilder::PostVisit(
const ola::messaging::FieldDescriptorGroup *descriptor) {
(void) descriptor;
}
bool StringMessageBuilder::StopParsing() const {
return m_error || m_offset >= m_input_size;
}
void StringMessageBuilder::SetError(const string &error) {
m_error = true;
m_error_string = error;
}
template<typename type>
void StringMessageBuilder::VisitInt(
const ola::messaging::IntegerFieldDescriptor<type> *descriptor) {
if (StopParsing())
return;
type int_value;
string input = m_inputs[m_offset++];
if (descriptor->LookupLabel(input, &int_value) ||
ola::PrefixedHexStringToInt(input, &int_value) ||
ola::StringToInt(input, &int_value)) {
if (descriptor->IsValid(int_value)) {
m_groups.top().push_back(
new ola::messaging::BasicMessageField<type>(descriptor, int_value));
} else {
SetError(descriptor->Name());
}
} else {
SetError(descriptor->Name());
}
}
void StringMessageBuilder::InitVars(const vector<string> &inputs) {
CleanUpVector();
// add the first fields vector to the stack
vector<const MessageFieldInterface*> fields;
m_groups.push(fields);
m_inputs = inputs;
m_input_size = inputs.size();
m_error = false;
m_offset = 0;
}
void StringMessageBuilder::CleanUpVector() {
while (!m_groups.empty()) {
const vector<const MessageFieldInterface*> &fields = m_groups.top();
vector<const MessageFieldInterface*>::const_iterator iter = fields.begin();
for (; iter != fields.end(); ++iter) {
delete *iter;
}
m_groups.pop();
}
}
} // namespace rdm
} // namespace ola