11 #include <boost/bind.hpp> 19 const int JSONFeed::RECEIVE_START_ST;
20 const int JSONFeed::WHITESPACE_BEFORE_JSON_ST;
21 const int JSONFeed::JSON_START_ST;
22 const int JSONFeed::INNER_JSON_ST;
23 const int JSONFeed::STRING_JSON_ST;
24 const int JSONFeed::ESCAPE_JSON_ST;
25 const int JSONFeed::JSON_END_ST;
26 const int JSONFeed::FEED_OK_ST;
27 const int JSONFeed::FEED_FAILED_ST;
29 const int JSONFeed::DATA_READ_OK_EVT;
30 const int JSONFeed::NEED_MORE_DATA_EVT;
31 const int JSONFeed::MORE_DATA_PROVIDED_EVT;
32 const int JSONFeed::FEED_OK_EVT;
33 const int JSONFeed::FEED_FAILED_EVT;
36 :
StateModel(), buffer_(), data_ptr_(0), error_message_(), open_scopes_(0),
62 }
catch (
const std::exception& ex) {
83 " JSON feed while parsing hasn't finished");
86 return (Element::fromWire(output_));
88 }
catch (
const std::exception& ex) {
103 buffer_.assign(static_cast<const char*>(buf),
104 static_cast<const char*>(buf) + buf_size);
110 JSONFeed::defineEvents() {
111 StateModel::defineEvents();
122 JSONFeed::verifyEvents() {
123 StateModel::verifyEvents();
133 JSONFeed::defineStates() {
135 StateModel::defineStates();
138 boost::bind(&JSONFeed::receiveStartHandler,
this));
140 boost::bind(&JSONFeed::whiteSpaceBeforeJSONHandler,
this));
142 boost::bind(&JSONFeed::innerJSONHandler,
this));
144 boost::bind(&JSONFeed::stringJSONHandler,
this));
146 boost::bind(&JSONFeed::escapeJSONHandler,
this));
148 boost::bind(&JSONFeed::endJSONHandler,
this));
152 JSONFeed::feedFailure(
const std::string& error_msg) {
153 error_message_ = error_msg;
158 JSONFeed::onModelFailure(
const std::string& explanation) {
159 if (error_message_.empty()) {
160 error_message_ = explanation;
165 JSONFeed::popNextFromBuffer(
char& next) {
167 if (!buffer_.empty() && (data_ptr_ < buffer_.size())) {
168 next = buffer_[data_ptr_++];
175 JSONFeed::getNextFromBuffer() {
184 "JSONFeed requires new data to progress, but no data" 185 " have been provided. The transaction is aborted to avoid" 190 const bool data_exist = popNextFromBuffer(c);
196 "JSONFeed state indicates that new data have been" 197 " provided to be parsed, but the transaction buffer" 198 " contains no new data.");
211 JSONFeed::invalidEventError(
const std::string& handler_name,
212 const unsigned int event) {
213 isc_throw(JSONFeedError, handler_name <<
": " 214 <<
" invalid event " <<
getEventLabel(static_cast<int>(event)));
218 JSONFeed::receiveStartHandler() {
219 char c = getNextFromBuffer();
233 output_.push_back(c);
241 feedFailure(
"invalid first character " + std::string(1, c));
247 invalidEventError(
"receiveStartHandler",
getNextEvent());
253 JSONFeed::whiteSpaceBeforeJSONHandler() {
254 char c = getNextFromBuffer();
266 output_.push_back(c);
274 feedFailure(
"invalid character " + std::string(1, c));
280 JSONFeed::innerJSONHandler() {
281 char c = getNextFromBuffer();
283 output_.push_back(c);
294 if (--open_scopes_ == 0) {
313 JSONFeed::stringJSONHandler() {
314 char c = getNextFromBuffer();
316 output_.push_back(c);
334 JSONFeed::escapeJSONHandler() {
335 char c = getNextFromBuffer();
337 output_.push_back(c);
344 JSONFeed::endJSONHandler() {
void defineState(unsigned int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
Adds an state value and associated label to the set of states.
static const int NOP_EVT
Signifies that no event has occurred.
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
bool isModelDone() const
Returns whether or not the model has finished execution.
void postBuffer(const void *buf, const size_t buf_size)
Receives additional data read from a data stream.
Implements a finite state machine.
static const int FEED_FAILED_EVT
Invalid syntax detected.
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.
static const int WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces before actual JSON.
static const int RECEIVE_START_ST
State indicating a beginning of a feed.
bool needData() const
Checks if the model needs additional data to continue.
void abortModel(const std::string &explanation)
Aborts model execution.
static const int START_EVT
Event issued to start the model execution.
boost::shared_ptr< Element > ElementPtr
void setState(unsigned int state)
Sets the current state to the given state value.
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
A generic exception thrown upon an error in the JSONFeed.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
unsigned int getLastEvent() const
Fetches the model's last event.
static const int STRING_JSON_ST
Parsing JSON string.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static const int INNER_JSON_ST
Parsing JSON.
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
static const int ESCAPE_JSON_ST
JSON escape next character.
static const int FEED_OK_EVT
Found opening brace and the matching closing brace.
unsigned int getNextEvent() const
Fetches the model's next event.
unsigned int getCurrState() const
Fetches the model's current state.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
static const int JSON_END_ST
Found last closing brace or square bracket.
void initDictionaries()
Initializes the event and state dictionaries.
data::ElementPtr toElement() const
Returns processed data as a structure of isc::data::Element objects.
static const int END_ST
Final state, all the state model has reached its conclusion.
bool feedOk() const
Checks if the data have been successfully processed.
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
void poll()
Runs the model as long as data is available.
static const int END_EVT
Event issued to end the model execution.
void initModel()
Initializes state model.
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
static const int FEED_FAILED_ST
Invalid syntax detected.