client_manager: add API for async operations
[oweals/gnunet.git] / src / include / gnunet_peerinfo_service.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file include/gnunet_peerinfo_service.h
22  * @brief Code to maintain the list of currently known hosts
23  *   (in memory structure of data/hosts).
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_PEERINFO_SERVICE_H
28 #define GNUNET_PEERINFO_SERVICE_H
29
30 #include "gnunet_common.h"
31 #include "gnunet_configuration_lib.h"
32 #include "gnunet_crypto_lib.h"
33 #include "gnunet_hello_lib.h"
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43
44 /**
45  * Handle to the peerinfo service.
46  */
47 struct GNUNET_PEERINFO_Handle;
48
49
50 /**
51  * Connect to the peerinfo service.
52  *
53  * @param cfg configuration to use
54  * @return NULL on error (configuration related, actual connection
55  *         etablishment may happen asynchronously).
56  */
57 struct GNUNET_PEERINFO_Handle *
58 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
59
60
61 /**
62  * Disconnect from the peerinfo service.  Note that all iterators must
63  * have completed or have been cancelled by the time this function is
64  * called (otherwise, calling this function is a serious error).
65  * Furthermore, if #GNUNET_PEERINFO_add_peer() operations are still
66  * pending, they will be cancelled silently on disconnect.
67  *
68  * @param h handle to disconnect
69  */
70 void
71 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h);
72
73
74 /**
75  * Continuation called with a status result.
76  *
77  * @param cls closure
78  * @param emsg error message, NULL on success
79  */
80 typedef void
81 (*GNUNET_PEERINFO_Continuation)(void *cls,
82                                 const char *emsg);
83
84
85 /**
86  * Opaque handle to cancel 'add' operation.
87  */
88 struct GNUNET_PEERINFO_AddContext;
89
90
91 /**
92  * Add a host to the persistent list.  This method operates in
93  * semi-reliable mode: if the transmission is not completed by
94  * the time #GNUNET_PEERINFO_disconnect() is called, it will be
95  * aborted.  Furthermore, if a second HELLO is added for the
96  * same peer before the first one was transmitted, PEERINFO may
97  * merge the two HELLOs prior to transmission to the service.
98  *
99  * @param h handle to the peerinfo service
100  * @param hello the verified (!) HELLO message
101  * @param cont continuation to call when done, NULL is allowed
102  * @param cont_cls closure for @a cont
103  * @return handle to cancel add operation; all pending
104  *         'add' operations will be cancelled automatically
105  *        on disconnect, so it is not necessary to keep this
106  *        handle (unless @a cont is NULL and at some point
107  *        calling @a cont must be prevented)
108  */
109 struct GNUNET_PEERINFO_AddContext *
110 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
111                           const struct GNUNET_HELLO_Message *hello,
112                           GNUNET_PEERINFO_Continuation cont,
113                           void *cont_cls);
114
115
116 /**
117  * Cancel pending 'add' operation.  Must only be called before
118  * either 'cont' or #GNUNET_PEERINFO_disconnect() are invoked.
119  *
120  * @param ac handle for the add operation to cancel
121  */
122 void
123 GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac);
124
125
126 /**
127  * Type of an iterator over the hosts.  Note that each
128  * host will be called with each available protocol.
129  *
130  * @param cls closure
131  * @param peer id of the peer, NULL for last call
132  * @param hello hello message for the peer (can be NULL)
133  * @param error message
134  */
135 typedef void
136 (*GNUNET_PEERINFO_Processor) (void *cls,
137                               const struct GNUNET_PeerIdentity *peer,
138                               const struct GNUNET_HELLO_Message *hello,
139                               const char *err_msg);
140
141
142 /**
143  * Handle for cancellation of iteration over peers.
144  */
145 struct GNUNET_PEERINFO_IteratorContext;
146
147
148 /**
149  * Call a method for each known matching host.  The callback method
150  * will be invoked once for each matching host and then finally once
151  * with a NULL pointer.  After that final invocation, the iterator
152  * context must no longer be used.
153  *
154  * Instead of calling this function with `peer == NULL` it is often
155  * better to use #GNUNET_PEERINFO_notify().
156  *
157  * @param h handle to the peerinfo service
158  * @param include_friend_only include HELLO messages for friends only
159  * @param peer restrict iteration to this peer only (can be NULL)
160  * @param timeout how long to wait until timing out
161  * @param callback the method to call for each peer
162  * @param callback_cls closure for @a callback
163  * @return iterator context
164  */
165 struct GNUNET_PEERINFO_IteratorContext *
166 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
167                          int include_friend_only,
168                          const struct GNUNET_PeerIdentity *peer,
169                          struct GNUNET_TIME_Relative timeout,
170                          GNUNET_PEERINFO_Processor callback, void *callback_cls);
171
172
173 /**
174  * Cancel an iteration over peer information.
175  *
176  * @param ic context of the iterator to cancel
177  */
178 void
179 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic);
180
181
182 /**
183  * Handle for notifications about changes to the set of known peers.
184  */
185 struct GNUNET_PEERINFO_NotifyContext;
186
187
188 /**
189  * Call a method whenever our known information about peers
190  * changes.  Initially calls the given function for all known
191  * peers and then only signals changes.
192  *
193  * If include_friend_only is set to GNUNET_YES peerinfo will include HELLO
194  * messages which are intended for friend to friend mode and which do not
195  * have to be gossiped. Otherwise these messages are skipped.
196  *
197  * @param cfg configuration to use
198  * @param include_friend_only include HELLO messages for friends only
199  * @param callback the method to call for each peer
200  * @param callback_cls closure for @a callback
201  * @return NULL on error
202  */
203 struct GNUNET_PEERINFO_NotifyContext *
204 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
205                         int include_friend_only,
206                         GNUNET_PEERINFO_Processor callback,
207                         void *callback_cls);
208
209
210 /**
211  * Stop notifying about changes.
212  *
213  * @param nc context to stop notifying
214  */
215 void
216 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc);
217
218
219 #if 0                           /* keep Emacsens' auto-indent happy */
220 {
221 #endif
222 #ifdef __cplusplus
223 }
224 #endif
225
226
227 /* end of gnunet_peerinfo_service.h */
228 #endif