1 #ifndef EVEL_THROTTLE_INCLUDED
2 #define EVEL_THROTTLE_INCLUDED
4 /**************************************************************************//**
6 * EVEL throttle definitions.
8 * These are internal definitions related to throttling specicications, which
9 * are required within the library but are not intended for external
15 * Copyright(c) <2016>, AT&T Intellectual Property. All other rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice,
23 * this list of conditions and the following disclaimer in the documentation
24 * and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement: This product includes
27 * software developed by the AT&T.
28 * 4. Neither the name of AT&T nor the names of its contributors may be used to
29 * endorse or promote products derived from this software without specific
30 * prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
33 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
36 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *****************************************************************************/
44 #include "evel_internal.h"
47 /*****************************************************************************/
48 /* Maximum depth of JSON response that we can handle. */
49 /*****************************************************************************/
50 #define EVEL_JSON_STACK_DEPTH 10
52 /**************************************************************************//**
53 * Maximum number of tokens that we allow for in a JSON response.
54 *****************************************************************************/
55 #define EVEL_MAX_RESPONSE_TOKENS 1024
57 /**************************************************************************//**
58 * The nature of the next token that we are iterating through. Within an
59 * object, we alternate between collecting keys and values. Within an array,
60 * we only collect items.
61 *****************************************************************************/
68 /**************************************************************************//**
69 * States which we move through during JSON processing, tracking our way
70 * through the supported JSON structure.
71 *****************************************************************************/
74 /***************************************************************************/
76 /***************************************************************************/
79 /***************************************************************************/
80 /* {"commandList": [ */
81 /***************************************************************************/
82 EVEL_JCS_COMMAND_LIST,
84 /***************************************************************************/
85 /* {"commandList": [{ */
86 /***************************************************************************/
87 EVEL_JCS_COMMAND_LIST_ENTRY,
89 /***************************************************************************/
90 /* {"commandList": [{"command": { */
91 /***************************************************************************/
94 /***************************************************************************/
95 /* ... "eventDomainThrottleSpecification": { */
96 /***************************************************************************/
99 /***************************************************************************/
100 /* ... "suppressedFieldNames": [ */
101 /***************************************************************************/
102 EVEL_JCS_FIELD_NAMES,
104 /***************************************************************************/
105 /* ... "suppressedNvPairsList": [ */
106 /***************************************************************************/
109 /***************************************************************************/
110 /* ... "suppressedNvPairsList": [{ */
111 /***************************************************************************/
112 EVEL_JCS_PAIRS_LIST_ENTRY,
114 /***************************************************************************/
115 /* ... "suppressedNvPairNames": [ */
116 /***************************************************************************/
117 EVEL_JCS_NV_PAIR_NAMES,
120 } EVEL_JSON_COMMAND_STATE;
122 /**************************************************************************//**
123 * An entry in the JSON stack.
124 *****************************************************************************/
125 typedef struct evel_json_stack_entry {
127 /***************************************************************************/
128 /* The number of elements required at this level. */
129 /***************************************************************************/
132 /***************************************************************************/
133 /* The number of elements collected at this level. */
134 /***************************************************************************/
137 /***************************************************************************/
138 /* The collection state at this level in the JSON stack. */
139 /***************************************************************************/
140 EVEL_JSON_STATE json_state;
142 /***************************************************************************/
143 /* The key being collected (if json_state is EVEL_JSON_VALUE), or NULL. */
144 /***************************************************************************/
147 } EVEL_JSON_STACK_ENTRY;
149 /**************************************************************************//**
151 *****************************************************************************/
152 typedef struct evel_json_stack {
154 /***************************************************************************/
155 /* The current position of the stack - starting at zero. */
156 /***************************************************************************/
159 /***************************************************************************/
160 /* The stack itself. */
161 /***************************************************************************/
162 EVEL_JSON_STACK_ENTRY entry[EVEL_JSON_STACK_DEPTH];
164 /***************************************************************************/
165 /* The underlying memory chunk. */
166 /***************************************************************************/
167 const MEMORY_CHUNK * chunk;
171 /**************************************************************************//**
172 * Initialize event throttling to the default state.
174 * Called from ::evel_initialize.
175 *****************************************************************************/
176 void evel_throttle_initialize();
178 /**************************************************************************//**
179 * Clean up event throttling.
181 * Called from ::evel_terminate.
182 *****************************************************************************/
183 void evel_throttle_terminate();
185 /**************************************************************************//**
186 * Handle a JSON response from the listener, as a list of tokens from JSMN.
188 * @param chunk Memory chunk containing the JSON buffer.
189 * @param json_tokens Array of tokens to handle.
190 * @param num_tokens The number of tokens to handle.
191 * @param post The memory chunk in which to place any resulting POST.
192 * @return true if the command was handled, false otherwise.
193 *****************************************************************************/
194 bool evel_handle_command_list(const MEMORY_CHUNK * const chunk,
195 const jsmntok_t * const json_tokens,
196 const int num_tokens,
197 MEMORY_CHUNK * const post);
199 /**************************************************************************//**
200 * Return the ::EVEL_THROTTLE_SPEC for a given domain.
202 * @param domain The domain for which to return state.
203 *****************************************************************************/
204 EVEL_THROTTLE_SPEC * evel_get_throttle_spec(EVEL_EVENT_DOMAINS domain);
206 /**************************************************************************//**
207 * Determine whether a field_name should be suppressed.
209 * @param throttle_spec Throttle specification for the domain being encoded.
210 * @param field_name The field name to encoded or suppress.
211 * @return true if the field_name should be suppressed, false otherwise.
212 *****************************************************************************/
213 bool evel_throttle_suppress_field(EVEL_THROTTLE_SPEC * throttle_spec,
214 const char * const field_name);
216 /**************************************************************************//**
217 * Determine whether a name-value pair should be allowed (not suppressed).
219 * @param throttle_spec Throttle specification for the domain being encoded.
220 * @param field_name The field name holding the name-value pairs.
221 * @param name The name of the name-value pair to encoded or suppress.
222 * @return true if the name-value pair should be suppressed, false otherwise.
223 *****************************************************************************/
224 bool evel_throttle_suppress_nv_pair(EVEL_THROTTLE_SPEC * throttle_spec,
225 const char * const field_name,
226 const char * const name);