summaryrefslogtreecommitdiffstats
path: root/src/lib/hooks/tests/load_callout_library.cc
blob: 613ec2c79f3fc255e7a66885e22d5c79f22f25cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

/// @file
/// @brief Basic library with load() function
///
/// This is source of a test library for various test (LibraryManager and
/// HooksManager).  The characteristics of the library produced from this
/// file are:
///
/// - The "version" and "load" framework functions are supplied.  One "standard"
///   callout is supplied ("hookpt_one") and two non-standard ones which are
///   registered during the call to "load" on the hooks "hookpt_two" and
///   "hookpt_three".
///
///   All callouts do trivial calculations, the result of all being called in
///   sequence being
///
///   @f[ ((5 * data_1) + data_2) * data_3 @f]
///
///   ...where data_1, data_2 and data_3 are the values passed in arguments of
///   the same name to the three callouts (data_1 passed to hookpt_one, data_2
///   to hookpt_two etc.) and the result is returned in the argument "result".

#include <config.h>
#include <hooks/hooks.h>

using namespace isc::hooks;

extern "C" {

// Callouts

int
context_create(CalloutHandle& handle) {
    handle.setContext("result", static_cast<int>(5));
    handle.setArgument("result", static_cast<int>(5));
    return (0);
}

// First callout adds the passed "data_1" argument to the initialized context
// value of 5. (Note that the value set by context_create is accessed through
// context and not the argument, so checking that context is correctly passed
// between callouts in the same library.)

int
hookpt_one(CalloutHandle& handle) {
    int data;
    handle.getArgument("data_1", data);

    int result;
    handle.getContext("result", result);

    result *= data;
    handle.setArgument("result", result);

    return (0);
}

// Second callout multiplies the current context value by the "data_2"
// argument.

static int
hook_nonstandard_two(CalloutHandle& handle) {
    int data;
    handle.getArgument("data_2", data);

    int result;
    handle.getArgument("result", result);

    result += data;
    handle.setArgument("result", result);

    return (0);
}

// Third callout adds "data_3" to the result.

static int
hook_nonstandard_three(CalloutHandle& handle) {
    int data;
    handle.getArgument("data_3", data);

    int result;
    handle.getArgument("result", result);

    result *= data;
    handle.setArgument("result", result);

    return (0);
}

// First command handler assigns data to a result.

static int
command_handler_one(CalloutHandle& handle) {
    int data;
    handle.getArgument("data_1", data);

    int result;
    handle.getArgument("result", result);

    result = data;
    handle.setArgument("result", result);

    return (0);
}

// Second command handler multiples the result by data by 10.

static int
command_handler_two(CalloutHandle& handle) {
    int data;
    handle.getArgument("data_2", data);

    int result;
    handle.getArgument("result", result);

    result *= data * 10;
    handle.setArgument("result", result);

    return (0);
}

// Framework functions

int
version() {
    return (KEA_HOOKS_VERSION);
}

int load(LibraryHandle& handle) {
    // Initialize the user library if the main image was statically linked
#ifdef USE_STATIC_LINK
    hooksStaticLinkInit();
#endif
    // Register the non-standard functions
    handle.registerCallout("hookpt_two", hook_nonstandard_two);
    handle.registerCallout("hookpt_three", hook_nonstandard_three);

    // Register command_handler_one as control command handler.
    handle.registerCommandCallout("command-one", command_handler_one);
    handle.registerCommandCallout("command-two", command_handler_two);

    return (0);
}

};