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
|
// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <cc/data.h>
#include <cc/json_feed.h>
#include <boost/bind.hpp>
using namespace isc::data;
using namespace isc::util;
namespace isc {
namespace config {
const int JSONFeed::RECEIVE_START_ST;
const int JSONFeed::WHITESPACE_BEFORE_JSON_ST;
const int JSONFeed::JSON_START_ST;
const int JSONFeed::INNER_JSON_ST;
const int JSONFeed::STRING_JSON_ST;
const int JSONFeed::ESCAPE_JSON_ST;
const int JSONFeed::JSON_END_ST;
const int JSONFeed::FEED_OK_ST;
const int JSONFeed::FEED_FAILED_ST;
const int JSONFeed::DATA_READ_OK_EVT;
const int JSONFeed::NEED_MORE_DATA_EVT;
const int JSONFeed::MORE_DATA_PROVIDED_EVT;
const int JSONFeed::FEED_OK_EVT;
const int JSONFeed::FEED_FAILED_EVT;
JSONFeed::JSONFeed()
: StateModel(), buffer_(), error_message_(), open_scopes_(0),
output_() {
}
void
JSONFeed::initModel() {
// Initialize dictionaries of events and states.
initDictionaries();
// Set the current state to starting state and enter the run loop.
setState(RECEIVE_START_ST);
// Parsing starts from here.
postNextEvent(START_EVT);
}
void
JSONFeed::poll() {
try {
// Process the input data until no more data is available or until
// JSON feed ends with matching closing brace.
do {
getState(getCurrState())->run();
} while (!isModelDone() && (getNextEvent() != NOP_EVT) &&
(getNextEvent() != NEED_MORE_DATA_EVT));
} catch (const std::exception& ex) {
abortModel(ex.what());
}
}
bool
JSONFeed::needData() const {
return ((getNextEvent() == NEED_MORE_DATA_EVT) ||
(getNextEvent() == START_EVT));
}
bool
JSONFeed::feedOk() const {
return ((getNextEvent() == END_EVT) &&
(getLastEvent() == FEED_OK_EVT));
}
ElementPtr
JSONFeed::toElement() const {
if (needData()) {
isc_throw(JSONFeedError, "unable to retrieve the data form the"
" JSON feed while parsing hasn't finished");
}
try {
return (Element::fromWire(output_));
} catch (const std::exception& ex) {
isc_throw(JSONFeedError, ex.what());
}
}
void
JSONFeed::postBuffer(const void* buf, const size_t buf_size) {
if (buf_size > 0) {
// The next event is NEED_MORE_DATA_EVT when the parser wants to
// signal that more data is needed. This method is called to supply
// more data and thus it should change the next event to
// MORE_DATA_PROVIDED_EVT.
if (getNextEvent() == NEED_MORE_DATA_EVT) {
transition(getCurrState(), MORE_DATA_PROVIDED_EVT);
}
buffer_.insert(buffer_.end(), static_cast<const char*>(buf),
static_cast<const char*>(buf) + buf_size);
}
}
void
JSONFeed::defineEvents() {
StateModel::defineEvents();
// Define JSONFeed specific events.
defineEvent(DATA_READ_OK_EVT, "DATA_READ_OK_EVT");
defineEvent(NEED_MORE_DATA_EVT, "NEED_MORE_DATA_EVT");
defineEvent(MORE_DATA_PROVIDED_EVT, "MORE_DATA_PROVIDED_EVT");
defineEvent(FEED_OK_EVT, "FEED_OK_EVT");
defineEvent(FEED_FAILED_EVT, "FEED_FAILED_EVT");
}
void
JSONFeed::verifyEvents() {
StateModel::verifyEvents();
getEvent(DATA_READ_OK_EVT);
getEvent(NEED_MORE_DATA_EVT);
getEvent(MORE_DATA_PROVIDED_EVT);
getEvent(FEED_OK_EVT);
getEvent(FEED_FAILED_EVT);
}
void
JSONFeed::defineStates() {
// Call parent class implementation first.
StateModel::defineStates();
defineState(RECEIVE_START_ST, "RECEIVE_START_ST",
boost::bind(&JSONFeed::receiveStartHandler, this));
defineState(WHITESPACE_BEFORE_JSON_ST, "WHITESPACE_BEFORE_JSON_ST",
boost::bind(&JSONFeed::whiteSpaceBeforeJSONHandler, this));
defineState(INNER_JSON_ST, "INNER_JSON_ST",
boost::bind(&JSONFeed::innerJSONHandler, this));
defineState(STRING_JSON_ST, "STRING_JSON_ST",
boost::bind(&JSONFeed::stringJSONHandler, this));
defineState(ESCAPE_JSON_ST, "ESCAPE_JSON_ST",
boost::bind(&JSONFeed::escapeJSONHandler, this));
defineState(JSON_END_ST, "JSON_END_ST",
boost::bind(&JSONFeed::endJSONHandler, this));
}
void
JSONFeed::feedFailure(const std::string& error_msg) {
error_message_ = error_msg;
transition(FEED_FAILED_ST, FEED_FAILED_EVT);
}
void
JSONFeed::onModelFailure(const std::string& explanation) {
if (error_message_.empty()) {
error_message_ = explanation;
}
}
bool
JSONFeed::popNextFromBuffer(char& next) {
// If there are any characters in the buffer, pop next.
if (!buffer_.empty()) {
next = buffer_.front();
buffer_.pop_front();
return (true);
}
return (false);
}
char
JSONFeed::getNextFromBuffer() {
unsigned int ev = getNextEvent();
char c = '\0';
// The caller should always provide additional data when the
// NEED_MORE_DATA_EVT occurs. If the next event is still
// NEED_MORE_DATA_EVT it indicates that the caller hasn't provided
// the data.
if (ev == NEED_MORE_DATA_EVT) {
isc_throw(JSONFeedError,
"JSONFeed requires new data to progress, but no data"
" have been provided. The transaction is aborted to avoid"
" a deadlock.");
} else {
// Try to pop next character from the buffer.
const bool data_exist = popNextFromBuffer(c);
if (!data_exist) {
// There is no more data so it is really not possible that we're
// at MORE_DATA_PROVIDED_EVT.
if (ev == MORE_DATA_PROVIDED_EVT) {
isc_throw(JSONFeedError,
"JSONFeed state indicates that new data have been"
" provided to be parsed, but the transaction buffer"
" contains no new data.");
} else {
// If there is no more data we should set NEED_MORE_DATA_EVT
// event to indicate that new data should be provided.
transition(getCurrState(), NEED_MORE_DATA_EVT);
}
}
}
return (c);
}
void
JSONFeed::invalidEventError(const std::string& handler_name,
const unsigned int event) {
isc_throw(JSONFeedError, handler_name << ": "
<< " invalid event " << getEventLabel(static_cast<int>(event)));
}
void
JSONFeed::receiveStartHandler() {
char c = getNextFromBuffer();
if (getNextEvent() != NEED_MORE_DATA_EVT) {
switch (getNextEvent()) {
case START_EVT:
switch (c) {
case '\t':
case '\n':
case '\r':
case ' ':
transition(WHITESPACE_BEFORE_JSON_ST, DATA_READ_OK_EVT);
return;
case '{':
case '[':
output_.push_back(c);
++open_scopes_;
transition(INNER_JSON_ST, DATA_READ_OK_EVT);
return;
// Cannot start by a string
case '"':
default:
feedFailure("invalid first character " + std::string(1, c));
break;
}
default:
invalidEventError("receiveStartHandler", getNextEvent());
}
}
}
void
JSONFeed::whiteSpaceBeforeJSONHandler() {
char c = getNextFromBuffer();
if (getNextEvent() != NEED_MORE_DATA_EVT) {
switch (c) {
case '\t':
case '\n':
case '\r':
case ' ':
transition(getCurrState(), DATA_READ_OK_EVT);
break;
case '{':
case '[':
output_.push_back(c);
++open_scopes_;
transition(INNER_JSON_ST, DATA_READ_OK_EVT);
break;
// Cannot start by a string
case '"':
default:
feedFailure("invalid character " + std::string(1, c));
}
}
}
void
JSONFeed::innerJSONHandler() {
char c = getNextFromBuffer();
if (getNextEvent() != NEED_MORE_DATA_EVT) {
output_.push_back(c);
switch(c) {
case '{':
case '[':
transition(getCurrState(), DATA_READ_OK_EVT);
++open_scopes_;
break;
case '}':
case ']':
if (--open_scopes_ == 0) {
transition(JSON_END_ST, FEED_OK_EVT);
} else {
transition(getCurrState(), DATA_READ_OK_EVT);
}
break;
case '"':
transition(STRING_JSON_ST, DATA_READ_OK_EVT);
break;
default:
transition(getCurrState(), DATA_READ_OK_EVT);
}
}
}
void
JSONFeed::stringJSONHandler() {
char c = getNextFromBuffer();
if (getNextEvent() != NEED_MORE_DATA_EVT) {
output_.push_back(c);
switch(c) {
case '"':
transition(INNER_JSON_ST, DATA_READ_OK_EVT);
break;
case '\\':
transition(ESCAPE_JSON_ST, DATA_READ_OK_EVT);
break;
default:
transition(getCurrState(), DATA_READ_OK_EVT);
}
}
}
void
JSONFeed::escapeJSONHandler() {
char c = getNextFromBuffer();
if (getNextEvent() != NEED_MORE_DATA_EVT) {
output_.push_back(c);
transition(STRING_JSON_ST, DATA_READ_OK_EVT);
}
}
void
JSONFeed::endJSONHandler() {
switch (getNextEvent()) {
case FEED_OK_EVT:
transition(END_ST, END_EVT);
break;
case FEED_FAILED_EVT:
abortModel("reading into JSON feed failed");
break;
default:
invalidEventError("endJSONHandler", getNextEvent());
}
}
} // end of namespace config
} // end of namespace isc
|