implement boolean json spec
[oweals/gnunet.git] / src / include / gnunet_ats_plugin.h
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2009-2015 GNUnet e.V.
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * API for the ATS solvers.
26  *
27  * @defgroup ats-plugin  ATS service plugin API
28  * Plugin API for the ATS service.
29  *
30  * Specifies the struct that is given to the plugin's entry method and the other
31  * struct that must be returned.  Note that the destructors of ATS plugins will
32  * be given the value returned by the constructor and is expected to return a
33  * NULL pointer.
34  *
35  * @{
36  */
37 #ifndef PLUGIN_ATS_H
38 #define PLUGIN_ATS_H
39
40 #include "gnunet_ats_service.h"
41 #include "gnunet_statistics_service.h"
42
43 /**
44  * Representation of an address the plugin can choose from.
45  */
46 struct ATS_Address;
47
48 /**
49  * Change the preference for a peer
50  *
51  * @param handle the solver handle
52  * @param client the client sending this request
53  * @param peer the peer id
54  * @param kind the preference kind to change
55  * @param score the new preference score
56  * @param pref_rel the normalized preference value for this kind over all clients
57  */
58 typedef void
59 (*GAS_solver_address_change_preference) (void *solver,
60                                          const struct GNUNET_PeerIdentity *peer,
61                                          enum GNUNET_ATS_PreferenceKind kind,
62                                          double pref_rel);
63
64
65 /**
66  * Give feedback about the current assignment
67  *
68  * @param handle the solver handle
69  * @param application the application sending this request
70  * @param peer the peer id
71  * @param scope the time interval for this feedback: [now - scope .. now]
72  * @param kind the preference kind for this feedback
73  * @param score the feedback score
74  */
75 typedef void
76 (*GAS_solver_address_feedback_preference) (void *solver,
77                                            struct GNUNET_SERVICE_Client *application,
78                                            const struct GNUNET_PeerIdentity *peer,
79                                            const struct GNUNET_TIME_Relative scope,
80                                            enum GNUNET_ATS_PreferenceKind kind,
81                                            double score);
82
83 /**
84  * Notify the solver about a bulk operation changing possibly a lot of values
85  * Solver will not resolve until all bulk operations are marked as done
86  *
87  * @param solver the solver
88  */
89 typedef void
90 (*GAS_solver_bulk_start) (void *solver);
91
92
93 /**
94  * Mark a bulk operation as done
95  * Solver will resolve if values have changed
96  *
97  * @param solver the solver
98  */
99 typedef void
100 (*GAS_solver_bulk_stop) (void *solver);
101
102
103 /**
104  * Add a single address within a network to the solver
105  *
106  * @param solver the solver Handle
107  * @param address the address to add
108  * @param network network type of this address
109  */
110 typedef void
111 (*GAS_solver_address_add) (void *solver,
112                            struct ATS_Address *address,
113                            uint32_t network);
114
115
116 /**
117  * Delete an address or just the session from the solver
118  *
119  * @param solver the solver Handle
120  * @param address the address to delete
121  */
122 typedef void
123 (*GAS_solver_address_delete) (void *solver,
124                               struct ATS_Address *address);
125
126
127 /**
128  * Transport properties for this address have changed
129  *
130  * @param solver solver handle
131  * @param address the address
132  */
133 typedef void
134 (*GAS_solver_address_property_changed) (void *solver,
135                                         struct ATS_Address *address);
136
137
138 /**
139  * Get the prefered address for a peer from solver
140  *
141  * @param solver the solver to use
142  * @param peer the peer
143  */
144 typedef void
145 (*GAS_solver_get_preferred_address) (void *solver,
146                                      const struct GNUNET_PeerIdentity *peer);
147
148
149 /**
150  * Stop getting the prefered address for a peer from solver
151  *
152  * @param solver the solver to use
153  * @param peer the peer
154  */
155 typedef void
156 (*GAS_solver_stop_get_preferred_address) (void *solver,
157                                           const struct GNUNET_PeerIdentity *peer);
158
159
160 /**
161  * Solver functions.
162  *
163  * Each solver is required to set up and return an instance
164  * of this struct during initialization.
165  */
166 struct GNUNET_ATS_SolverFunctions
167 {
168
169   /**
170    * Closure to pass to all solver functions in this struct.
171    */
172   void *cls;
173
174   /**
175    * Add a new address for a peer to the solver
176    *
177    * The address is already contained in the addresses hashmap!
178    */
179   GAS_solver_address_add s_add;
180
181   /**
182    * Update the properties of an address in the solver
183    */
184   GAS_solver_address_property_changed s_address_update_property;
185
186   /**
187    * Tell solver to notify ATS if the address to use changes for a specific
188    * peer using the bandwidth changed callback
189    *
190    * The solver must only notify about changes for peers with pending address
191    * requests!
192    */
193   GAS_solver_get_preferred_address s_get;
194
195   /**
196    * Tell solver stop notifying ATS about changes for this peers
197    *
198    * The solver must only notify about changes for peers with pending address
199    * requests!
200    */
201   GAS_solver_stop_get_preferred_address s_get_stop;
202
203   /**
204    * Delete an address in the solver
205    *
206    * The address is not contained in the address hashmap anymore!
207    */
208   GAS_solver_address_delete s_del;
209
210   /**
211    * Change relative preference for quality in solver
212    */
213   GAS_solver_address_change_preference s_pref;
214
215   /**
216    * Give feedback about the current assignment
217    */
218   GAS_solver_address_feedback_preference s_feedback;
219
220   /**
221    * Start a bulk operation
222    *
223    * Used if many values have to be updated at the same time.
224    * When a bulk operation is pending the solver does not have to resolve
225    * the problem since more updates will follow anyway
226    *
227    * For each call to bulk_start, a call to bulk_stop is required!
228    */
229   GAS_solver_bulk_start s_bulk_start;
230
231   /**
232    * Bulk operation done
233    *
234    * If no more bulk operations are pending, the solver can solve the problem
235    * with the updated values
236    */
237   GAS_solver_bulk_stop s_bulk_stop;
238 };
239
240
241 /**
242  * Operation codes for solver information callback
243  *
244  * Order of calls is expected to be:
245  * #GAS_OP_SOLVE_START
246  * #GAS_OP_SOLVE_STOP
247  * #GAS_OP_SOLVE_UPDATE_NOTIFICATION_START
248  * #GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP
249  *
250  */
251 enum GAS_Solver_Operation
252 {
253   /**
254    * A solution iteration has been started
255    */
256   GAS_OP_SOLVE_START,
257
258   /**
259    * A solution iteration has been finished
260    */
261   GAS_OP_SOLVE_STOP,
262
263   /**
264    * The setup of the problem as a preparation to solve it was started
265    */
266   GAS_OP_SOLVE_SETUP_START,
267
268   /**
269    * The setup of the problem as a preparation to solve is finished
270    */
271   GAS_OP_SOLVE_SETUP_STOP,
272
273   /**
274    * Solving of the LP problem was started
275    * MLP solver only
276    */
277   GAS_OP_SOLVE_MLP_LP_START,
278
279   /**
280    * Solving of the LP problem is done
281    * MLP solver only
282    */
283   GAS_OP_SOLVE_MLP_LP_STOP,
284
285   /**
286    * Solving of the MLP problem was started
287    * MLP solver only
288    */
289   GAS_OP_SOLVE_MLP_MLP_START,
290
291   /**
292    * Solving of the MLP problem is done
293    * MLP solver only
294    */
295   GAS_OP_SOLVE_MLP_MLP_STOP,
296
297   /**
298    * After the problem was finished, start notifications about changes
299    * to addresses
300    */
301   GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
302
303   /**
304    * After the problem was finished, notifications about changes to addresses
305    * are done
306    */
307   GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP
308 };
309
310
311 /**
312  * Status of a GAS_Solver_Operation operation
313  */
314 enum GAS_Solver_Status
315 {
316   /**
317    * Success
318    */
319   GAS_STAT_SUCCESS,
320
321   /**
322    * Failure
323    */
324   GAS_STAT_FAIL
325 };
326
327
328 /**
329  * Status of the operation
330  */
331 enum GAS_Solver_Additional_Information
332 {
333   /**
334    * No more specific information
335    */
336   GAS_INFO_NONE,
337
338   /**
339    * A full solution process is performed
340    * Quite specific to the MLP solver
341    */
342   GAS_INFO_FULL,
343
344   /**
345    * An existing solution was reused
346    * Quite specific to the MLP solver
347    */
348   GAS_INFO_UPDATED,
349
350   /**
351    * The proportional solver had to recalculate for a single network
352    */
353   GAS_INFO_PROP_SINGLE,
354
355   /**
356    * The proportional solver had to recalculate for all networks
357    */
358   GAS_INFO_PROP_ALL
359 };
360
361
362 /**
363  * Callback to call with additional information
364  * Used for measurement
365  *
366  * @param cls the closure
367  * @param op the operation
368  */
369 typedef void
370 (*GAS_solver_information_callback) (void *cls,
371                                     enum GAS_Solver_Operation op,
372                                     enum GAS_Solver_Status stat,
373                                     enum GAS_Solver_Additional_Information);
374
375
376 /**
377  * Callback to call from solver when bandwidth for address has changed
378  *
379  * @param address the with changed bandwidth assigned
380  */
381 typedef void
382 (*GAS_bandwidth_changed_cb) (void *cls,
383                              struct ATS_Address *address);
384
385
386 /**
387  * Callback to call from solver to obtain application preference
388  * values for a peer.
389  *
390  * @param cls the cls
391  * @param id the peer id
392  * @return carry of double values containing the preferences with
393  *      GNUNET_ATS_PreferenceCount elements
394  */
395 typedef const double *
396 (*GAS_get_preferences) (void *cls,
397                         const struct GNUNET_PeerIdentity *id);
398
399
400 /**
401  * Callback to call from solver to obtain application connectivity
402  * preferences for a peer.
403  *
404  * @param cls the cls
405  * @param id the peer id
406  * @return 0 if connectivity is not desired, non-null if address
407  *      suggestions are requested
408  */
409 typedef unsigned int
410 (*GAS_get_connectivity) (void *cls,
411                          const struct GNUNET_PeerIdentity *id);
412
413
414 /**
415  * The ATS plugin will pass a pointer to a struct
416  * of this type as to the initialization function
417  * of the ATS plugins.
418  */
419 struct GNUNET_ATS_PluginEnvironment
420 {
421   /**
422    * Configuration handle to be used by the solver
423    */
424   const struct GNUNET_CONFIGURATION_Handle *cfg;
425
426   /**
427    * Statistics handle to be used by the solver
428    */
429   struct GNUNET_STATISTICS_Handle *stats;
430
431   /**
432    * Closure to pass to all callbacks in this struct.
433    */
434   void *cls;
435
436   /**
437    * Hashmap containing all addresses available
438    */
439   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
440
441   /**
442    * ATS addresses callback to be notified about bandwidth assignment changes
443    */
444   GAS_bandwidth_changed_cb bandwidth_changed_cb;
445
446   /**
447    * ATS addresses function to obtain preference values
448    */
449   GAS_get_preferences get_preferences;
450
451   /**
452    * ATS addresses function to obtain preference values
453    */
454   GAS_get_connectivity get_connectivity;
455
456   /**
457    * Callback for solver to call with status information,
458    * can be NULL
459    */
460   GAS_solver_information_callback info_cb;
461
462   /**
463    * Number of networks available, size of the @e out_quota
464    * and @e in_quota arrays.
465    */
466   unsigned int network_count;
467
468   /**
469    * Array of configured outbound quotas
470    * Order according to networks in network array
471    */
472   unsigned long long out_quota[GNUNET_ATS_NetworkTypeCount];
473
474   /**
475    * Array of configured inbound quotas
476    * Order according to networks in network array
477    */
478   unsigned long long in_quota[GNUNET_ATS_NetworkTypeCount];
479 };
480
481 #endif
482
483 /** @} */  /* end of group */