Kea  2.3.5-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 
82  static const int WHITESPACE_BEFORE_JSON_ST = SM_DERIVED_STATE_MIN + 2;
83 
85  static const int EOL_COMMENT_BEFORE_JSON_ST = SM_DERIVED_STATE_MIN + 3;
86 
88  static const int START_COMMENT_BEFORE_JSON_ST = SM_DERIVED_STATE_MIN + 4;
89 
91  static const int C_COMMENT_BEFORE_JSON_ST = SM_DERIVED_STATE_MIN + 5;
92 
94  static const int STOP_COMMENT_BEFORE_JSON_ST = SM_DERIVED_STATE_MIN + 6;
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 
149  static const int MORE_DATA_PROVIDED_EVT = SM_DERIVED_EVENT_MIN + 3;
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
JSONFeedError(const char *file, size_t line, const char *what)
Definition: json_feed.h:32
Implements a finite state machine.
Definition: state_model.h:274
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
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.
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-lfc.
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition: json_feed.h:27
string & output_
Definition: dns/message.cc:877
std::string getProcessedText() const
Returns the text parsed into the buffer.
Definition: json_feed.h:195
This file defines the class StateModel.
std::string getErrorMessage() const
Returns error string when data processing has failed.
Definition: json_feed.h:190