basic_callout_library.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Basic 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. /// - Only the "version" framework function is supplied.
  22. ///
  23. /// - A context_create callout is supplied.
  24. ///
  25. /// - Three "standard" callouts are supplied corresponding to the hooks
  26. /// "lm_one", "lm_two", "lm_three". All do some trivial calculations
  27. /// on the arguments supplied to it and the context variables, returning
  28. /// intermediate results through the "result" argument. The result of
  29. /// executing all four callouts in order is:
  30. ///
  31. /// @f[ (10 + data_1) * data_2 - data_3 @f]
  32. ///
  33. /// ...where data_1, data_2 and data_3 are the values passed in arguments of
  34. /// the same name to the three callouts (data_1 passed to lm_one, data_2 to
  35. /// lm_two etc.) and the result is returned in the argument "result".
  36. #include <hooks/hooks.h>
  37. #include <fstream>
  38. using namespace isc::hooks;
  39. using namespace std;
  40. extern "C" {
  41. // Callouts. All return their result through the "result" argument.
  42. int
  43. context_create(CalloutHandle& handle) {
  44. handle.setContext("result", static_cast<int>(10));
  45. handle.setArgument("result", static_cast<int>(10));
  46. return (0);
  47. }
  48. // First callout adds the passed "data_1" argument to the initialized context
  49. // value of 10. (Note that the value set by context_create is accessed through
  50. // context and not the argument, so checking that context is correctly passed
  51. // between callouts in the same library.)
  52. int
  53. lm_one(CalloutHandle& handle) {
  54. int data;
  55. handle.getArgument("data_1", data);
  56. int result;
  57. handle.getArgument("result", result);
  58. result += data;
  59. handle.setArgument("result", result);
  60. return (0);
  61. }
  62. // Second callout multiplies the current context value by the "data_2"
  63. // argument.
  64. int
  65. lm_two(CalloutHandle& handle) {
  66. int data;
  67. handle.getArgument("data_2", data);
  68. int result;
  69. handle.getArgument("result", result);
  70. result *= data;
  71. handle.setArgument("result", result);
  72. return (0);
  73. }
  74. // Final callout subtracts the result in "data_3".
  75. int
  76. lm_three(CalloutHandle& handle) {
  77. int data;
  78. handle.getArgument("data_3", data);
  79. int result;
  80. handle.getArgument("result", result);
  81. result -= data;
  82. handle.setArgument("result", result);
  83. return (0);
  84. }
  85. // Framework functions. Only version() is supplied here.
  86. int
  87. version() {
  88. return (BIND10_HOOKS_VERSION);
  89. }
  90. };