json_feed.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright (C) 2017 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. #include <cc/data.h>
  7. #include <cc/json_feed.h>
  8. #include <boost/bind.hpp>
  9. using namespace isc::data;
  10. using namespace isc::util;
  11. namespace isc {
  12. namespace config {
  13. const int JSONFeed::RECEIVE_START_ST;
  14. const int JSONFeed::WHITESPACE_BEFORE_JSON_ST;
  15. const int JSONFeed::JSON_START_ST;
  16. const int JSONFeed::INNER_JSON_ST;
  17. const int JSONFeed::JSON_END_ST;
  18. const int JSONFeed::FEED_OK_ST;
  19. const int JSONFeed::FEED_FAILED_ST;
  20. const int JSONFeed::DATA_READ_OK_EVT;
  21. const int JSONFeed::NEED_MORE_DATA_EVT;
  22. const int JSONFeed::MORE_DATA_PROVIDED_EVT;
  23. const int JSONFeed::FEED_OK_EVT;
  24. const int JSONFeed::FEED_FAILED_EVT;
  25. JSONFeed::JSONFeed()
  26. : StateModel(), buffer_(), error_message_(), open_scopes_(0),
  27. output_() {
  28. }
  29. void
  30. JSONFeed::initModel() {
  31. // Initialize dictionaries of events and states.
  32. initDictionaries();
  33. // Set the current state to starting state and enter the run loop.
  34. setState(RECEIVE_START_ST);
  35. // Parsing starts from here.
  36. postNextEvent(START_EVT);
  37. }
  38. void
  39. JSONFeed::poll() {
  40. try {
  41. // Process the input data until no more data is available or until
  42. // JSON feed ends with matching closing brace.
  43. do {
  44. getState(getCurrState())->run();
  45. } while (!isModelDone() && (getNextEvent() != NOP_EVT) &&
  46. (getNextEvent() != NEED_MORE_DATA_EVT));
  47. } catch (const std::exception& ex) {
  48. abortModel(ex.what());
  49. }
  50. }
  51. bool
  52. JSONFeed::needData() const {
  53. return ((getNextEvent() == NEED_MORE_DATA_EVT) ||
  54. (getNextEvent() == START_EVT));
  55. }
  56. bool
  57. JSONFeed::feedOk() const {
  58. return ((getNextEvent() == END_EVT) &&
  59. (getLastEvent() == FEED_OK_EVT));
  60. }
  61. ElementPtr
  62. JSONFeed::toElement() const {
  63. if (needData()) {
  64. isc_throw(JSONFeedError, "unable to retrieve the data form the"
  65. " JSON feed while parsing hasn't finished");
  66. }
  67. try {
  68. return (Element::fromWire(output_));
  69. } catch (const std::exception& ex) {
  70. isc_throw(JSONFeedError, ex.what());
  71. }
  72. }
  73. void
  74. JSONFeed::postBuffer(const void* buf, const size_t buf_size) {
  75. if (buf_size > 0) {
  76. // The next event is NEED_MORE_DATA_EVT when the parser wants to
  77. // signal that more data is needed. This method is called to supply
  78. // more data and thus it should change the next event to
  79. // MORE_DATA_PROVIDED_EVT.
  80. if (getNextEvent() == NEED_MORE_DATA_EVT) {
  81. transition(getCurrState(), MORE_DATA_PROVIDED_EVT);
  82. }
  83. buffer_.insert(buffer_.end(), static_cast<const char*>(buf),
  84. static_cast<const char*>(buf) + buf_size);
  85. }
  86. }
  87. void
  88. JSONFeed::defineEvents() {
  89. StateModel::defineEvents();
  90. // Define JSONFeed specific events.
  91. defineEvent(DATA_READ_OK_EVT, "DATA_READ_OK_EVT");
  92. defineEvent(NEED_MORE_DATA_EVT, "NEED_MORE_DATA_EVT");
  93. defineEvent(MORE_DATA_PROVIDED_EVT, "MORE_DATA_PROVIDED_EVT");
  94. defineEvent(FEED_OK_EVT, "FEED_OK_EVT");
  95. defineEvent(FEED_FAILED_EVT, "FEED_FAILED_EVT");
  96. }
  97. void
  98. JSONFeed::verifyEvents() {
  99. StateModel::verifyEvents();
  100. getEvent(DATA_READ_OK_EVT);
  101. getEvent(NEED_MORE_DATA_EVT);
  102. getEvent(MORE_DATA_PROVIDED_EVT);
  103. getEvent(FEED_OK_EVT);
  104. getEvent(FEED_FAILED_EVT);
  105. }
  106. void
  107. JSONFeed::defineStates() {
  108. // Call parent class implementation first.
  109. StateModel::defineStates();
  110. defineState(RECEIVE_START_ST, "RECEIVE_START_ST",
  111. boost::bind(&JSONFeed::receiveStartHandler, this));
  112. defineState(WHITESPACE_BEFORE_JSON_ST, "WHITESPACE_BEFORE_JSON_ST",
  113. boost::bind(&JSONFeed::whiteSpaceBeforeJSONHandler, this));
  114. defineState(INNER_JSON_ST, "INNER_JSON_ST",
  115. boost::bind(&JSONFeed::innerJSONHandler, this));
  116. defineState(JSON_END_ST, "JSON_END_ST",
  117. boost::bind(&JSONFeed::endJSONHandler, this));
  118. }
  119. void
  120. JSONFeed::feedFailure(const std::string& error_msg) {
  121. error_message_ = error_msg;
  122. transition(FEED_FAILED_ST, FEED_FAILED_EVT);
  123. }
  124. void
  125. JSONFeed::onModelFailure(const std::string& explanation) {
  126. if (error_message_.empty()) {
  127. error_message_ = explanation;
  128. }
  129. }
  130. bool
  131. JSONFeed::popNextFromBuffer(char& next) {
  132. // If there are any characters in the buffer, pop next.
  133. if (!buffer_.empty()) {
  134. next = buffer_.front();
  135. buffer_.pop_front();
  136. return (true);
  137. }
  138. return (false);
  139. }
  140. char
  141. JSONFeed::getNextFromBuffer() {
  142. unsigned int ev = getNextEvent();
  143. char c = '\0';
  144. // The caller should always provide additional data when the
  145. // NEED_MORE_DATA_EVT occurrs. If the next event is still
  146. // NEED_MORE_DATA_EVT it indicates that the caller hasn't provided
  147. // the data.
  148. if (ev == NEED_MORE_DATA_EVT) {
  149. isc_throw(JSONFeedError,
  150. "JSONFeed requires new data to progress, but no data"
  151. " have been provided. The transaction is aborted to avoid"
  152. " a deadlock.");
  153. } else {
  154. // Try to pop next character from the buffer.
  155. const bool data_exist = popNextFromBuffer(c);
  156. if (!data_exist) {
  157. // There is no more data so it is really not possible that we're
  158. // at MORE_DATA_PROVIDED_EVT.
  159. if (ev == MORE_DATA_PROVIDED_EVT) {
  160. isc_throw(JSONFeedError,
  161. "JSONFeed state indicates that new data have been"
  162. " provided to be parsed, but the transaction buffer"
  163. " contains no new data.");
  164. } else {
  165. // If there is no more data we should set NEED_MORE_DATA_EVT
  166. // event to indicate that new data should be provided.
  167. transition(getCurrState(), NEED_MORE_DATA_EVT);
  168. }
  169. }
  170. }
  171. return (c);
  172. }
  173. void
  174. JSONFeed::invalidEventError(const std::string& handler_name,
  175. const unsigned int event) {
  176. isc_throw(JSONFeedError, handler_name << ": "
  177. << " invalid event " << getEventLabel(static_cast<int>(event)));
  178. }
  179. void
  180. JSONFeed::receiveStartHandler() {
  181. char c = getNextFromBuffer();
  182. if (getNextEvent() != NEED_MORE_DATA_EVT) {
  183. switch(getNextEvent()) {
  184. case START_EVT:
  185. switch (c) {
  186. case '\t':
  187. case '\n':
  188. case '\r':
  189. case ' ':
  190. transition(WHITESPACE_BEFORE_JSON_ST, DATA_READ_OK_EVT);
  191. return;
  192. case '{':
  193. case '[':
  194. output_.push_back(c);
  195. ++open_scopes_;
  196. transition(INNER_JSON_ST, DATA_READ_OK_EVT);
  197. return;
  198. default:
  199. feedFailure("invalid first character " + std::string(1, c));
  200. }
  201. default:
  202. invalidEventError("receiveStartHandler", getNextEvent());
  203. }
  204. }
  205. }
  206. void
  207. JSONFeed::whiteSpaceBeforeJSONHandler() {
  208. char c = getNextFromBuffer();
  209. if (getNextEvent() != NEED_MORE_DATA_EVT) {
  210. switch (c) {
  211. case '\t':
  212. case '\n':
  213. case '\r':
  214. case ' ':
  215. transition(getCurrState(), DATA_READ_OK_EVT);
  216. break;
  217. case '{':
  218. case '[':
  219. output_.push_back(c);
  220. ++open_scopes_;
  221. transition(INNER_JSON_ST, DATA_READ_OK_EVT);
  222. break;
  223. default:
  224. feedFailure("invalid character " + std::string(1, c));
  225. }
  226. }
  227. }
  228. void
  229. JSONFeed::innerJSONHandler() {
  230. char c = getNextFromBuffer();
  231. if (getNextEvent() != NEED_MORE_DATA_EVT) {
  232. output_.push_back(c);
  233. switch(c) {
  234. case '{':
  235. case '[':
  236. transition(getCurrState(), DATA_READ_OK_EVT);
  237. ++open_scopes_;
  238. break;
  239. case '}':
  240. case ']':
  241. if (--open_scopes_ == 0) {
  242. transition(JSON_END_ST, FEED_OK_EVT);
  243. } else {
  244. transition(getCurrState(), DATA_READ_OK_EVT);
  245. }
  246. break;
  247. default:
  248. transition(getCurrState(), DATA_READ_OK_EVT);
  249. }
  250. }
  251. }
  252. void
  253. JSONFeed::endJSONHandler() {
  254. switch (getNextEvent()) {
  255. case FEED_OK_EVT:
  256. transition(END_ST, END_EVT);
  257. break;
  258. case FEED_FAILED_EVT:
  259. abortModel("reading into JSON feed failed");
  260. break;
  261. default:
  262. invalidEventError("endJSONHandler", getNextEvent());
  263. }
  264. }
  265. } // end of namespace config
  266. } // end of namespace isc