master_loader_callbacks.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Name;
  23. class RRClass;
  24. class RRType;
  25. class RRTTL;
  26. namespace rdata {
  27. class Rdata;
  28. typedef boost::shared_ptr<Rdata> RdataPtr;
  29. }
  30. /// \brief Type of callback to add a RR.
  31. ///
  32. /// This type of callback is used by the loader to report another loaded
  33. /// RR. The Rdata is no longer preserved by the loader and is fully
  34. /// owned by the callback.
  35. ///
  36. /// \param name The domain name where the RR belongs.
  37. /// \param rrclass The class of the RR.
  38. /// \param rrtype Type of the RR.
  39. /// \param rrttl Time to live of the RR.
  40. /// \param rdata The actual carried data of the RR.
  41. typedef boost::function<void(const Name& name, const RRClass& rrclass,
  42. const RRType& rrtype, const RRTTL& rrttl,
  43. const rdata::RdataPtr& rdata)>
  44. AddRRCallback;
  45. /// \brief Set of issue callbacks for a loader.
  46. ///
  47. /// This holds a set of callbacks by which a loader (such as MasterLoader)
  48. /// can report loaded RRsets, errors and other unusual conditions.
  49. ///
  50. /// All the callbacks must be set.
  51. class MasterLoaderCallbacks {
  52. public:
  53. /// \brief Type of one callback to report problems.
  54. ///
  55. /// This is the type of one callback used to report an unusual
  56. /// condition or error.
  57. ///
  58. /// \param source_name The name of the source where the problem happened.
  59. /// This is usually a file name.
  60. /// \param source_line Position of the problem, counted in lines from the
  61. /// beginning of the source.
  62. /// \param reason Human readable description of what happened.
  63. typedef boost::function<void(const std::string& source_name,
  64. size_t source_line,
  65. const std::string& reason)> IssueCallback;
  66. /// \brief Constructor
  67. ///
  68. /// Initializes the callbacks.
  69. ///
  70. /// \param error The error callback to use.
  71. /// \param warning The warning callback to use.
  72. /// \throw isc::InvalidParameter if any of the callbacks is empty.
  73. MasterLoaderCallbacks(const IssueCallback& error,
  74. const IssueCallback& warning) :
  75. error_(error),
  76. warning_(warning)
  77. {
  78. if (error_.empty() || warning_.empty()) {
  79. isc_throw(isc::InvalidParameter,
  80. "Empty function passed as callback");
  81. }
  82. }
  83. /// \brief Call callback for serious errors
  84. ///
  85. /// This is called whenever there's a serious problem which makes the data
  86. /// being loaded unusable. Further processing may or may not happen after
  87. /// this (for example to detect further errors), but the data should not
  88. /// be used.
  89. ///
  90. /// It calls whatever was passed to the error parameter to the constructor.
  91. ///
  92. /// If the caller of the loader wants to abort, it is possible to throw
  93. /// from the callback, which aborts the load.
  94. void error(const std::string& source_name, size_t source_line,
  95. const std::string& reason)
  96. {
  97. error_(source_name, source_line, reason);
  98. }
  99. /// \brief Call callback for potential problems
  100. ///
  101. /// This is called whenever a minor problem is discovered. This might mean
  102. /// the data is completely OK, it just looks suspicious.
  103. ///
  104. /// It calls whatever was passed to the warn parameter to the constructor.
  105. ///
  106. /// The loading will continue after the callback. If the caller wants to
  107. /// abort (which is probably not a very good idea, since warnings
  108. /// may be false positives), it is possible to throw from inside the
  109. /// callback.
  110. void warning(const std::string& source_name, size_t source_line,
  111. const std::string& reason)
  112. {
  113. warning_(source_name, source_line, reason);
  114. }
  115. /// \brief Return a callbacks instance with null callbacks
  116. ///
  117. /// This is a convenience wrapper to generate a
  118. /// \c MasterLoaderCallbacks object with both callbacks being nothing.
  119. /// This will be useful for applications that only need to run
  120. /// \c MasterLoader and get the end result.
  121. ///
  122. /// \throw None
  123. static MasterLoaderCallbacks getNullCallbacks();
  124. private:
  125. IssueCallback error_, warning_;
  126. };
  127. }
  128. }
  129. #endif // MASTER_LOADER_CALLBACKS_H