Kea  1.9.9-git
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>
11 #include <exceptions/exceptions.h>
12 #include <util/state_model.h>
13 #include <boost/shared_ptr.hpp>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 
18 namespace isc {
19 namespace config {
20 
21 class JSONFeed;
22 
24 typedef boost::shared_ptr<JSONFeed> JSONFeedPtr;
25 
27 typedef boost::shared_ptr<const JSONFeed> ConstJSONFeedPtr;
28 
30 class JSONFeedError : public Exception {
31 public:
32  JSONFeedError(const char* file, size_t line, const char* what) :
33  isc::Exception(file, line, what) { };
34 };
35 
65 
71 class JSONFeed : public util::StateModel {
72 public:
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 
143  static const int DATA_READ_OK_EVT = SM_DERIVED_EVENT_MIN + 1;
144 
146  static const int NEED_MORE_DATA_EVT = SM_DERIVED_EVENT_MIN + 2;
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 
203  data::ElementPtr toElement() const;
204 
212  void postBuffer(const void* buf, const size_t buf_size);
213 
214 
215 private:
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
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
Definition: json_feed.h:143
JSONFeedError(const char *file, size_t line, const char *what)
Definition: json_feed.h:32
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 START_COMMENT_BEFORE_JSON_ST
Starting one of the comments beginning with a slash before actual JSON.
Definition: json_feed.h:88
Implements a finite state machine.
Definition: state_model.h:274
static const int FEED_FAILED_EVT
Invalid syntax detected.
Definition: json_feed.h:155
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 WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces before actual JSON.
Definition: json_feed.h:82
static const int FEED_OK_ST
Found opening and closing brace or square bracket.
Definition: json_feed.h:128
static const int RECEIVE_START_ST
State indicating a beginning of a feed.
Definition: json_feed.h:79
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
Definition: json_feed.h:149
A generic exception thrown upon an error in the JSONFeed.
Definition: json_feed.h:30
std::string getProcessedText() const
Returns the text parsed into the buffer.
Definition: json_feed.h:195
boost::shared_ptr< JSONFeed > JSONFeedPtr
Pointer to the JSONFeed.
Definition: json_feed.h:21
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 NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
Definition: json_feed.h:146
static const int ESCAPE_JSON_ST
JSON escape next character.
Definition: json_feed.h:106
bool needData() const
Checks if the model needs additional data to continue.
Definition: json_feed.cc:76
static const int FEED_OK_EVT
Found opening brace and the matching closing brace.
Definition: json_feed.h:152
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:304
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:285
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static const int C_COMMENT_BEFORE_JSON_ST
Skipping a C style comment before actual JSON.
Definition: json_feed.h:91
static const int START_COMMENT_ST
Starting one of the comments beginning with a slash.
Definition: json_feed.h:112
State model for asynchronous read of data in JSON format.
Definition: json_feed.h:71
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::string getErrorMessage() const
Returns error string when data processing has failed.
Definition: json_feed.h:190
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition: json_feed.h:27
static const int STOP_COMMENT_BEFORE_JSON_ST
Stopping a C style comment before actual JSON.
Definition: json_feed.h:94
static const int C_COMMENT_ST
Skipping a C style comment.
Definition: json_feed.h:115
static const int JSON_END_ST
Found last closing brace or square bracket.
Definition: json_feed.h:121
bool feedOk() const
Checks if the data have been successfully processed.
Definition: json_feed.cc:82
static const int STOP_COMMENT_ST
Stopping a C style comment.
Definition: json_feed.h:118
data::ElementPtr toElement() const
Returns processed data as a structure of isc::data::Element objects.
Definition: json_feed.cc:88
This file defines the class StateModel.
static const int EOL_COMMENT_ST
Skipping an end-of-line comment.
Definition: json_feed.h:109
static const int JSON_START_ST
Found first opening brace or square bracket.
Definition: json_feed.h:97
JSONFeed()
Constructor.
Definition: json_feed.cc:43
void poll()
Runs the model as long as data is available.
Definition: json_feed.cc:61
void initModel()
Initializes state model.
Definition: json_feed.cc:49
static const int FEED_FAILED_ST
Invalid syntax detected.
Definition: json_feed.h:133