full_callout_library.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2013 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. /// @file
  15. /// @brief Full callout library
  16. ///
  17. /// This is source of a test library for various test (LibraryManager and
  18. /// HooksManager). The characteristics of the library produced from this
  19. /// file are:
  20. ///
  21. /// The characteristics of this library are:
  22. ///
  23. /// - All three framework functions are supplied (version(), load() and
  24. /// unload()), with unload() creating a marker file. The test code checks
  25. /// for the presence of this file, so verifying that unload() has been run.
  26. ///
  27. /// - One standard and two non-standard callouts are supplied, with the latter
  28. /// being registered by the load() function.
  29. ///
  30. /// All callouts do trivial calculations, the result of all being called in
  31. /// sequence being
  32. ///
  33. /// @f[ ((7 * data_1) - data_2) * data_3 @f]
  34. ///
  35. /// ...where data_1, data_2 and data_3 are the values passed in arguments of
  36. /// the same name to the three callouts (data_1 passed to hookpt_one, data_2
  37. /// to hookpt_two etc.) and the result is returned in the argument "result".
  38. #include <hooks/hooks.h>
  39. #include <hooks/tests/marker_file.h>
  40. #include <fstream>
  41. using namespace isc::hooks;
  42. extern "C" {
  43. // Callouts
  44. int
  45. context_create(CalloutHandle& handle) {
  46. handle.setContext("result", static_cast<int>(7));
  47. handle.setArgument("result", static_cast<int>(7));
  48. return (0);
  49. }
  50. // First callout adds the passed "data_1" argument to the initialized context
  51. // value of 7. (Note that the value set by context_create is accessed through
  52. // context and not the argument, so checking that context is correctly passed
  53. // between callouts in the same library.)
  54. int
  55. hookpt_one(CalloutHandle& handle) {
  56. int data;
  57. handle.getArgument("data_1", data);
  58. int result;
  59. handle.getArgument("result", result);
  60. result *= data;
  61. handle.setArgument("result", result);
  62. return (0);
  63. }
  64. // Second callout subtracts the passed value of data_2 from the current
  65. // running total.
  66. static int
  67. hook_nonstandard_two(CalloutHandle& handle) {
  68. int data;
  69. handle.getArgument("data_2", data);
  70. int result;
  71. handle.getArgument("result", result);
  72. result -= data;
  73. handle.setArgument("result", result);
  74. return (0);
  75. }
  76. // Final callout multplies the current running total by data_3.
  77. static int
  78. hook_nonstandard_three(CalloutHandle& handle) {
  79. int data;
  80. handle.getArgument("data_3", data);
  81. int result;
  82. handle.getArgument("result", result);
  83. result *= data;
  84. handle.setArgument("result", result);
  85. return (0);
  86. }
  87. // Framework functions
  88. int
  89. version() {
  90. return (KEA_HOOKS_VERSION);
  91. }
  92. int
  93. load(LibraryHandle& handle) {
  94. // Initialize if the main image was statically linked
  95. #ifdef USE_STATIC_LINK
  96. hooksStaticLinkInit();
  97. #endif
  98. // Register the non-standard functions
  99. handle.registerCallout("hookpt_two", hook_nonstandard_two);
  100. handle.registerCallout("hookpt_three", hook_nonstandard_three);
  101. return (0);
  102. }
  103. int
  104. unload() {
  105. // Create the marker file.
  106. std::fstream marker;
  107. marker.open(MARKER_FILE, std::fstream::out);
  108. marker.close();
  109. return (0);
  110. }
  111. };