11#include <boost/bind.hpp>
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");
88 }
catch (
const std::exception& ex) {
103 buffer_.assign(
static_cast<const char*
>(buf),
104 static_cast<const char*
>(buf) + buf_size);
110JSONFeed::defineEvents() {
122JSONFeed::verifyEvents() {
133JSONFeed::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));
152JSONFeed::feedFailure(
const std::string& error_msg) {
153 error_message_ = error_msg;
158JSONFeed::onModelFailure(
const std::string& explanation) {
159 if (error_message_.empty()) {
160 error_message_ = explanation;
165JSONFeed::popNextFromBuffer(
char& next) {
167 if (!buffer_.empty() && (data_ptr_ < buffer_.size())) {
168 next = buffer_[data_ptr_++];
175JSONFeed::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.");
211JSONFeed::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)));
218JSONFeed::receiveStartHandler() {
219 char c = getNextFromBuffer();
233 output_.push_back(c);
241 feedFailure(
"invalid first character " + std::string(1, c));
247 invalidEventError(
"receiveStartHandler",
getNextEvent());
253JSONFeed::whiteSpaceBeforeJSONHandler() {
254 char c = getNextFromBuffer();
266 output_.push_back(c);
274 feedFailure(
"invalid character " + std::string(1, c));
280JSONFeed::innerJSONHandler() {
281 char c = getNextFromBuffer();
283 output_.push_back(c);
294 if (--open_scopes_ == 0) {
313JSONFeed::stringJSONHandler() {
314 char c = getNextFromBuffer();
316 output_.push_back(c);
334JSONFeed::escapeJSONHandler() {
335 char c = getNextFromBuffer();
337 output_.push_back(c);
344JSONFeed::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.
void postBuffer(const void *buf, const size_t buf_size)
Receives additional data read from a data stream.
static const int WHITESPACE_BEFORE_JSON_ST
Skipping whitespaces 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.
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 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-dhcp-ddns.