44 :
StateModel(), buffer_(), data_ptr_(0), error_message_(), open_scopes_(0),
70 }
catch (
const std::exception& ex) {
91 " JSON feed while parsing hasn't finished");
96 }
catch (
const std::exception& ex) {
111 buffer_.assign(
static_cast<const char*
>(buf),
112 static_cast<const char*
>(buf) + buf_size);
118JSONFeed::defineEvents() {
130JSONFeed::verifyEvents() {
141JSONFeed::defineStates() {
146 std::bind(&JSONFeed::receiveStartHandler,
this));
148 std::bind(&JSONFeed::whiteSpaceBeforeJSONHandler,
this));
150 std::bind(&JSONFeed::eolCommentBeforeJSONHandler,
this));
152 std::bind(&JSONFeed::startCommentBeforeJSONHandler,
this));
154 std::bind(&JSONFeed::cCommentBeforeJSONHandler,
this));
156 std::bind(&JSONFeed::stopCommentBeforeJSONHandler,
this));
158 std::bind(&JSONFeed::innerJSONHandler,
this));
160 std::bind(&JSONFeed::stringJSONHandler,
this));
162 std::bind(&JSONFeed::escapeJSONHandler,
this));
164 std::bind(&JSONFeed::eolCommentHandler,
this));
166 std::bind(&JSONFeed::startCommentHandler,
this));
168 std::bind(&JSONFeed::cCommentHandler,
this));
170 std::bind(&JSONFeed::stopCommentHandler,
this));
172 std::bind(&JSONFeed::endJSONHandler,
this));
176JSONFeed::feedFailure(
const std::string& error_msg) {
177 error_message_ = error_msg;
182JSONFeed::onModelFailure(
const std::string& explanation) {
183 if (error_message_.empty()) {
184 error_message_ = explanation;
189JSONFeed::popNextFromBuffer(
char& next) {
191 if (!buffer_.empty() && (data_ptr_ < buffer_.size())) {
192 next = buffer_[data_ptr_++];
199JSONFeed::getNextFromBuffer() {
208 "JSONFeed requires new data to progress, but no data"
209 " have been provided. The transaction is aborted to avoid"
214 const bool data_exist = popNextFromBuffer(c);
220 "JSONFeed state indicates that new data have been"
221 " provided to be parsed, but the transaction buffer"
222 " contains no new data.");
235JSONFeed::invalidEventError(
const std::string& handler_name,
236 const unsigned int event) {
237 isc_throw(JSONFeedError, handler_name <<
": invalid event "
242JSONFeed::receiveStartHandler() {
243 char c = getNextFromBuffer();
265 output_.push_back(c);
273 feedFailure(
"invalid first character " + std::string(1, c));
279 invalidEventError(
"receiveStartHandler",
getNextEvent());
285JSONFeed::whiteSpaceBeforeJSONHandler() {
286 char c = getNextFromBuffer();
306 output_.push_back(c);
314 feedFailure(
"invalid character " + std::string(1, c));
320JSONFeed::eolCommentBeforeJSONHandler() {
321 char c = getNextFromBuffer();
336JSONFeed::startCommentBeforeJSONHandler() {
337 char c = getNextFromBuffer();
349 feedFailure(
"invalid characters /" + std::string(1, c));
355JSONFeed::cCommentBeforeJSONHandler() {
356 char c = getNextFromBuffer();
371JSONFeed::stopCommentBeforeJSONHandler() {
372 char c = getNextFromBuffer();
391JSONFeed::innerJSONHandler() {
392 char c = getNextFromBuffer();
397 output_.push_back(c);
404 output_.push_back(c);
405 if (--open_scopes_ == 0) {
414 output_.push_back(c);
427 output_.push_back(c);
434JSONFeed::stringJSONHandler() {
435 char c = getNextFromBuffer();
437 output_.push_back(c);
455JSONFeed::escapeJSONHandler() {
456 char c = getNextFromBuffer();
458 output_.push_back(c);
465JSONFeed::eolCommentHandler() {
466 char c = getNextFromBuffer();
470 output_.push_back(c);
482JSONFeed::startCommentHandler() {
483 char c = getNextFromBuffer();
495 feedFailure(
"invalid characters /" + std::string(1, c));
501JSONFeed::cCommentHandler() {
502 char c = getNextFromBuffer();
510 output_.push_back(c);
522JSONFeed::stopCommentHandler() {
523 char c = getNextFromBuffer();
535 output_.push_back(c);
547JSONFeed::endJSONHandler() {
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.
static const int ESCAPE_JSON_ST
JSON escape next character.
static const int FEED_FAILED_ST
Invalid syntax detected.
static const int EOL_COMMENT_BEFORE_JSON_ST
Skipping an end-of-line comment before actual JSON.
static const int START_COMMENT_ST
Starting one of the comments beginning with a slash.
void postBuffer(const void *buf, const size_t buf_size)
Receives additional data read from a data stream.
static const int EOL_COMMENT_ST
Skipping an end-of-line comment.
static const int WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces before actual JSON.
static const int STOP_COMMENT_BEFORE_JSON_ST
Stopping a C style comment before actual JSON.
static const int START_COMMENT_BEFORE_JSON_ST
Starting one of the comments beginning with a slash before actual JSON.
bool feedOk() const
Checks if the data have been successfully processed.
bool needData() const
Checks if the model needs additional data to continue.
static const int STRING_JSON_ST
Parsing JSON string.
static const int INNER_JSON_ST
Parsing JSON.
static const int JSON_START_ST
Found first opening brace or square bracket.
void poll()
Runs the model as long as data is available.
static const int FEED_FAILED_EVT
Invalid syntax detected.
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
static const int STOP_COMMENT_ST
Stopping a C style comment.
void initModel()
Initializes state model.
static const int FEED_OK_EVT
Found opening brace and the matching closing brace.
data::ElementPtr toElement() const
Returns processed data as a structure of isc::data::Element objects.
static const int RECEIVE_START_ST
State indicating a beginning of a feed.
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
static const int FEED_OK_ST
Found opening and closing brace or square bracket.
static const int C_COMMENT_ST
Skipping a C style comment.
static const int C_COMMENT_BEFORE_JSON_ST
Skipping a C style comment before actual JSON.
static const int JSON_END_ST
Found last closing brace or square bracket.
static ElementPtr fromWire(std::stringstream &in, int length)
These function pparse the wireformat at the given stringstream (of the given length).
Implements a finite state machine.
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
void initDictionaries()
Initializes the event and state dictionaries.
bool isModelDone() const
Returns whether or not the model has finished execution.
virtual void defineEvents()
Populates the set of events.
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
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.
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.
unsigned int getNextEvent() const
Fetches the model's next event.
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
virtual void verifyEvents()
Validates the contents of the set of events.
static const int END_EVT
Event issued to end the model execution.
static const int NOP_EVT
Signifies that no event has occurred.
static const int START_EVT
Event issued to start the model execution.
void abortModel(const std::string &explanation)
Aborts model execution.
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
virtual void defineStates()
Populates the set of states.
void setState(unsigned int state)
Sets the current state to the given state value.
static const int END_ST
Final state, all the state model has reached its conclusion.
unsigned int getLastEvent() const
Fetches the model's last event.
unsigned int getCurrState() const
Fetches the model's current state.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Defines the logger used by the top-level component of kea-lfc.