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>
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 
62 
68 class JSONFeed : public util::StateModel {
69 public:
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 
116  static const int DATA_READ_OK_EVT = SM_DERIVED_EVENT_MIN + 1;
117 
119  static const int NEED_MORE_DATA_EVT = SM_DERIVED_EVENT_MIN + 2;
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 
176  data::ElementPtr toElement() const;
177 
185  void postBuffer(const void* buf, const size_t buf_size);
186 
187 
188 private:
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
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
Definition: json_feed.h:116
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:94
Implements a finite state machine.
Definition: state_model.h:271
static const int FEED_FAILED_EVT
Invalid syntax detected.
Definition: json_feed.h:128
static const int WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces before actual JSON.
Definition: json_feed.h:79
static const int FEED_OK_ST
Found opening and closing brace or square bracket.
Definition: json_feed.h:101
static const int RECEIVE_START_ST
State indicating a beginning of a feed.
Definition: json_feed.h:76
bool needData() const
Checks if the model needs additional data to continue.
Definition: json_feed.cc:68
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:122
A generic exception thrown upon an error in the JSONFeed.
Definition: json_feed.h:30
boost::shared_ptr< JSONFeed > JSONFeedPtr
Pointer to the JSONFeed.
Definition: json_feed.h:21
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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 NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
Definition: json_feed.h:119
static const int ESCAPE_JSON_ST
JSON escape next character.
Definition: json_feed.h:91
static const int FEED_OK_EVT
Found opening brace and the matching closing brace.
Definition: json_feed.h:125
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:282
State model for asynchronous read of data in JSON format.
Definition: json_feed.h:68
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.
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition: json_feed.h:27
std::string getProcessedText() const
Returns the text parsed into the buffer.
Definition: json_feed.h:168
static const int JSON_END_ST
Found last closing brace or square bracket.
Definition: json_feed.h:94
data::ElementPtr toElement() const
Returns processed data as a structure of isc::data::Element objects.
Definition: json_feed.cc:80
bool feedOk() const
Checks if the data have been successfully processed.
Definition: json_feed.cc:74
This file defines the class StateModel.
std::string getErrorMessage() const
Returns error string when data processing has failed.
Definition: json_feed.h:163
static const int JSON_START_ST
Found first opening brace or square bracket.
Definition: json_feed.h:82
JSONFeed()
Constructor.
Definition: json_feed.cc:35
void poll()
Runs the model as long as data is available.
Definition: json_feed.cc:53
void initModel()
Initializes state model.
Definition: json_feed.cc:41
static const int FEED_FAILED_ST
Invalid syntax detected.
Definition: json_feed.h:106