Kea 1.5.0
json_feed.h
Go to the documentation of this file.
1// Copyright (C) 2017-2018 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
62
68class JSONFeed : public util::StateModel {
69public:
70
73
74
76 static const int RECEIVE_START_ST = SM_DERIVED_STATE_MIN + 1;
77
80
82 static const int JSON_START_ST = SM_DERIVED_STATE_MIN + 3;
83
85 static const int INNER_JSON_ST = SM_DERIVED_STATE_MIN + 4;
86
88 static const int STRING_JSON_ST = SM_DERIVED_STATE_MIN + 5;
89
91 static const int ESCAPE_JSON_ST = SM_DERIVED_STATE_MIN + 6;
92
94 static const int JSON_END_ST = SM_DERIVED_STATE_MIN + 7;
95
101 static const int FEED_OK_ST = SM_DERIVED_STATE_MIN + 100;
102
106 static const int FEED_FAILED_ST = SM_DERIVED_STATE_MIN + 101;
107
109
110
113
114
117
120
123
125 static const int FEED_OK_EVT = SM_DERIVED_EVENT_MIN + 100;
126
128 static const int FEED_FAILED_EVT = SM_DERIVED_EVENT_MIN + 101;
129
131
133 JSONFeed();
134
139 void initModel();
140
149 void poll();
150
157 bool needData() const;
158
160 bool feedOk() const;
161
163 std::string getErrorMessage() const {
164 return (error_message_);
165 }
166
168 std::string getProcessedText() const {
169 return (output_);
170 }
171
177
185 void postBuffer(const void* buf, const size_t buf_size);
186
187
188private:
189
192 using StateModel::runModel;
193
195 virtual void defineEvents();
196
198 virtual void verifyEvents();
199
201 virtual void defineStates();
202
209 void feedFailure(const std::string& error_msg);
210
214 virtual void onModelFailure(const std::string& explanation);
215
230 char getNextFromBuffer();
231
245 void invalidEventError(const std::string& handler_name,
246 const unsigned int event);
247
254 bool popNextFromBuffer(char& next);
255
258
259
261 void receiveStartHandler();
262
264 void whiteSpaceBeforeJSONHandler();
265
267 void innerJSONHandler();
268
270 void stringJSONHandler();
271
273 void escapeJSONHandler();
274
276 void endJSONHandler();
277
279
281 std::vector<char> buffer_;
282
284 size_t data_ptr_;
285
287 std::string error_message_;
288
291 uint64_t open_scopes_;
292
294 std::string output_;
295};
296
297} // end of namespace config
298} // end of namespace isc
299
300#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:68
static const int ESCAPE_JSON_ST
JSON escape next character.
Definition: json_feed.h:91
static const int FEED_FAILED_ST
Invalid syntax detected.
Definition: json_feed.h:106
void postBuffer(const void *buf, const size_t buf_size)
Receives additional data read from a data stream.
Definition: json_feed.cc:94
static const int WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces before actual JSON.
Definition: json_feed.h:79
bool feedOk() const
Checks if the data have been successfully processed.
Definition: json_feed.cc:74
bool needData() const
Checks if the model needs additional data to continue.
Definition: json_feed.cc:68
static const int STRING_JSON_ST
Parsing JSON string.
Definition: json_feed.h:88
static const int INNER_JSON_ST
Parsing JSON.
Definition: json_feed.h:85
static const int JSON_START_ST
Found first opening brace or square bracket.
Definition: json_feed.h:82
void poll()
Runs the model as long as data is available.
Definition: json_feed.cc:53
static const int FEED_FAILED_EVT
Invalid syntax detected.
Definition: json_feed.h:128
std::string getErrorMessage() const
Returns error string when data processing has failed.
Definition: json_feed.h:163
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
Definition: json_feed.h:122
void initModel()
Initializes state model.
Definition: json_feed.cc:41
static const int FEED_OK_EVT
Found opening brace and the matching closing brace.
Definition: json_feed.h:125
data::ElementPtr toElement() const
Returns processed data as a structure of isc::data::Element objects.
Definition: json_feed.cc:80
std::string getProcessedText() const
Returns the text parsed into the buffer.
Definition: json_feed.h:168
static const int RECEIVE_START_ST
State indicating a beginning of a feed.
Definition: json_feed.h:76
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
Definition: json_feed.h:119
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
Definition: json_feed.h:116
JSONFeed()
Constructor.
Definition: json_feed.cc:35
static const int FEED_OK_ST
Found opening and closing brace or square bracket.
Definition: json_feed.h:101
static const int JSON_END_ST
Found last closing brace or square bracket.
Definition: json_feed.h:94
Implements a finite state machine.
Definition: state_model.h:271
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:282
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
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:22
Defines the logger used by the top-level component of kea-dhcp-ddns.
This file defines the class StateModel.