master_loader_callbacks.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef MASTER_LOADER_CALLBACKS_H
  15. #define MASTER_LOADER_CALLBACKS_H
  16. #include <exceptions/exceptions.h>
  17. #include <string>
  18. #include <boost/function.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. namespace isc {
  21. namespace dns {
  22. class AbstractRRset;
  23. typedef boost::shared_ptr<AbstractRRset> RRsetPtr;
  24. /// \brief Type of callback to add a RRset.
  25. ///
  26. /// This type of callback is used by the loader to report another loaded
  27. /// RRset. The RRset is no longer preserved by the loader and is fully
  28. /// owned by the callback.
  29. ///
  30. /// \param RRset The rrset to add. It does not contain the accompanying
  31. /// RRSIG (if the zone is signed), they are reported with separate
  32. /// calls to the callback.
  33. typedef boost::function<void(const RRsetPtr& rrset)> AddRRsetCallback;
  34. /// \brief Set of issue callbacks for a loader.
  35. ///
  36. /// This holds a set of callbacks by which a loader (such as MasterLoader)
  37. /// can report loaded RRsets, errors and other unusual conditions.
  38. ///
  39. /// All the callbacks must be set.
  40. class MasterLoaderCallbacks {
  41. public:
  42. /// \brief Type of one callback to report problems.
  43. ///
  44. /// This is the type of one callback used to report an unusual
  45. /// condition or error.
  46. ///
  47. /// \param source_name The name of the source where the problem happened.
  48. /// This is usually a file name.
  49. /// \param source_line Position of the problem, counted in lines from the
  50. /// beginning of the source.
  51. /// \param reason Human readable description of what happened.
  52. typedef boost::function<void(const std::string& source_name,
  53. size_t source_line,
  54. const std::string& reason)> IssueCallback;
  55. /// \brief Constructor
  56. ///
  57. /// Initializes the callbacks.
  58. ///
  59. /// \param error The error callback to use.
  60. /// \param warning The warning callback to use.
  61. /// \throw isc::InvalidParameter if any of the callbacks is empty.
  62. MasterLoaderCallbacks(const IssueCallback& error,
  63. const IssueCallback& warning) :
  64. error_(error),
  65. warning_(warning)
  66. {
  67. if (error_.empty() || warning_.empty()) {
  68. isc_throw(isc::InvalidParameter,
  69. "Empty function passed as callback");
  70. }
  71. }
  72. /// \brief Call callback for serious errors
  73. ///
  74. /// This is called whenever there's a serious problem which makes the data
  75. /// being loaded unusable. Further processing may or may not happen after
  76. /// this (for example to detect further errors), but the data should not
  77. /// be used.
  78. ///
  79. /// It calls whatever was passed to the error parameter to the constructor.
  80. ///
  81. /// If the caller of the loader wants to abort, it is possible to throw
  82. /// from the callback, which aborts the load.
  83. void error(const std::string& source_name, size_t source_line,
  84. const std::string& reason)
  85. {
  86. error_(source_name, source_line, reason);
  87. }
  88. /// \brief Call callback for potential problems
  89. ///
  90. /// This is called whenever a minor problem is discovered. This might mean
  91. /// the data is completely OK, it just looks suspicious.
  92. ///
  93. /// It calls whatever was passed to the warn parameter to the constructor.
  94. ///
  95. /// The loading will continue after the callback. If the caller wants to
  96. /// abort (which is probably not a very good idea, since warnings
  97. /// may be false positives), it is possible to throw from inside the
  98. /// callback.
  99. void warning(const std::string& source_name, size_t source_line,
  100. const std::string& reason)
  101. {
  102. warning_(source_name, source_line, reason);
  103. }
  104. private:
  105. IssueCallback error_, warning_;
  106. };
  107. }
  108. }
  109. #endif // LOADER_CALLBACKS_H