Kea 2.5.8
json_feed.h
Go to the documentation of this file.
1// Copyright (C) 2017-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef JSON_FEED_H
8#define JSON_FEED_H
9
10#include <cc/data.h>
12#include <util/state_model.h>
13#include <boost/shared_ptr.hpp>
14#include <stdint.h>
15#include <string>
16#include <vector>
17
18namespace isc {
19namespace config {
20
21class JSONFeed;
22
24typedef boost::shared_ptr<JSONFeed> JSONFeedPtr;
25
27typedef boost::shared_ptr<const JSONFeed> ConstJSONFeedPtr;
28
30class JSONFeedError : public Exception {
31public:
32 JSONFeedError(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) { };
34};
35
65
71class JSONFeed : public util::StateModel {
72public:
73
76
77
79 static const int RECEIVE_START_ST = SM_DERIVED_STATE_MIN + 1;
80
83
86
89
92
95
97 static const int JSON_START_ST = SM_DERIVED_STATE_MIN + 7;
98
100 static const int INNER_JSON_ST = SM_DERIVED_STATE_MIN + 8;
101
103 static const int STRING_JSON_ST = SM_DERIVED_STATE_MIN + 9;
104
106 static const int ESCAPE_JSON_ST = SM_DERIVED_STATE_MIN + 10;
107
109 static const int EOL_COMMENT_ST = SM_DERIVED_STATE_MIN + 11;
110
112 static const int START_COMMENT_ST = SM_DERIVED_STATE_MIN + 12;
113
115 static const int C_COMMENT_ST = SM_DERIVED_STATE_MIN + 13;
116
118 static const int STOP_COMMENT_ST = SM_DERIVED_STATE_MIN + 14;
119
121 static const int JSON_END_ST = SM_DERIVED_STATE_MIN + 15;
122
128 static const int FEED_OK_ST = SM_DERIVED_STATE_MIN + 100;
129
133 static const int FEED_FAILED_ST = SM_DERIVED_STATE_MIN + 101;
134
136
137
140
141
144
147
150
152 static const int FEED_OK_EVT = SM_DERIVED_EVENT_MIN + 100;
153
155 static const int FEED_FAILED_EVT = SM_DERIVED_EVENT_MIN + 101;
156
158
160 JSONFeed();
161
166 void initModel();
167
176 void poll();
177
184 bool needData() const;
185
187 bool feedOk() const;
188
190 std::string getErrorMessage() const {
191 return (error_message_);
192 }
193
195 std::string getProcessedText() const {
196 return (output_);
197 }
198
204
212 void postBuffer(const void* buf, const size_t buf_size);
213
214
215private:
216
219 using StateModel::runModel;
220
222 virtual void defineEvents();
223
225 virtual void verifyEvents();
226
228 virtual void defineStates();
229
236 void feedFailure(const std::string& error_msg);
237
241 virtual void onModelFailure(const std::string& explanation);
242
257 char getNextFromBuffer();
258
272 void invalidEventError(const std::string& handler_name,
273 const unsigned int event);
274
281 bool popNextFromBuffer(char& next);
282
285
286
288 void receiveStartHandler();
289
291 void whiteSpaceBeforeJSONHandler();
292
294 void eolCommentBeforeJSONHandler();
295
297 void startCommentBeforeJSONHandler();
298
300 void cCommentBeforeJSONHandler();
301
303 void stopCommentBeforeJSONHandler();
304
306 void innerJSONHandler();
307
309 void stringJSONHandler();
310
312 void escapeJSONHandler();
313
315 void eolCommentHandler();
316
318 void startCommentHandler();
319
321 void cCommentHandler();
322
324 void stopCommentHandler();
325
327 void endJSONHandler();
328
330
332 std::vector<char> buffer_;
333
335 size_t data_ptr_;
336
338 std::string error_message_;
339
342 uint64_t open_scopes_;
343
345 std::string output_;
346};
347
348} // end of namespace config
349} // end of namespace isc
350
351#endif // JSON_FEED_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception thrown upon an error in the JSONFeed.
Definition: json_feed.h:30
JSONFeedError(const char *file, size_t line, const char *what)
Definition: json_feed.h:32
State model for asynchronous read of data in JSON format.
Definition: json_feed.h:71
static const int ESCAPE_JSON_ST
JSON escape next character.
Definition: json_feed.h:106
static const int FEED_FAILED_ST
Invalid syntax detected.
Definition: json_feed.h:133
static const int EOL_COMMENT_BEFORE_JSON_ST
Skipping an end-of-line comment before actual JSON.
Definition: json_feed.h:85
static const int START_COMMENT_ST
Starting one of the comments beginning with a slash.
Definition: json_feed.h:112
void postBuffer(const void *buf, const size_t buf_size)
Receives additional data read from a data stream.
Definition: json_feed.cc:102
static const int EOL_COMMENT_ST
Skipping an end-of-line comment.
Definition: json_feed.h:109
static const int WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces before actual JSON.
Definition: json_feed.h:82
static const int STOP_COMMENT_BEFORE_JSON_ST
Stopping a C style comment before actual JSON.
Definition: json_feed.h:94
static const int START_COMMENT_BEFORE_JSON_ST
Starting one of the comments beginning with a slash before actual JSON.
Definition: json_feed.h:88
bool feedOk() const
Checks if the data have been successfully processed.
Definition: json_feed.cc:82
bool needData() const
Checks if the model needs additional data to continue.
Definition: json_feed.cc:76
static const int STRING_JSON_ST
Parsing JSON string.
Definition: json_feed.h:103
static const int INNER_JSON_ST
Parsing JSON.
Definition: json_feed.h:100
static const int JSON_START_ST
Found first opening brace or square bracket.
Definition: json_feed.h:97
void poll()
Runs the model as long as data is available.
Definition: json_feed.cc:61
static const int FEED_FAILED_EVT
Invalid syntax detected.
Definition: json_feed.h:155
std::string getErrorMessage() const
Returns error string when data processing has failed.
Definition: json_feed.h:190
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
Definition: json_feed.h:149
static const int STOP_COMMENT_ST
Stopping a C style comment.
Definition: json_feed.h:118
void initModel()
Initializes state model.
Definition: json_feed.cc:49
static const int FEED_OK_EVT
Found opening brace and the matching closing brace.
Definition: json_feed.h:152
data::ElementPtr toElement() const
Returns processed data as a structure of isc::data::Element objects.
Definition: json_feed.cc:88
std::string getProcessedText() const
Returns the text parsed into the buffer.
Definition: json_feed.h:195
static const int RECEIVE_START_ST
State indicating a beginning of a feed.
Definition: json_feed.h:79
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
Definition: json_feed.h:146
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
Definition: json_feed.h:143
JSONFeed()
Constructor.
Definition: json_feed.cc:43
static const int FEED_OK_ST
Found opening and closing brace or square bracket.
Definition: json_feed.h:128
static const int C_COMMENT_ST
Skipping a C style comment.
Definition: json_feed.h:115
static const int C_COMMENT_BEFORE_JSON_ST
Skipping a C style comment before actual JSON.
Definition: json_feed.h:91
static const int JSON_END_ST
Found last closing brace or square bracket.
Definition: json_feed.h:121
Implements a finite state machine.
Definition: state_model.h:274
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:285
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:304
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition: json_feed.h:27
boost::shared_ptr< JSONFeed > JSONFeedPtr
Pointer to the JSONFeed.
Definition: json_feed.h:24
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
Defines the logger used by the top-level component of kea-lfc.
This file defines the class StateModel.