fix
[oweals/gnunet.git] / src / mesh / mesh_api_new.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 /**
22  * @file mesh/mesh_api_new.c
23  * @brief mesh api: client implementation of mesh service
24  * @author Bartlomiej Polot
25  */
26
27 #ifdef __cplusplus
28
29 extern "C"
30 {
31 #if 0                           /* keep Emacsens' auto-indent happy */
32 }
33 #endif
34 #endif
35
36
37 #include <stdint.h>
38 #include "gnunet_mesh_service.h"
39
40 /**
41  * Opaque handle to the service.
42  */
43 struct GNUNET_MESH_Handle {
44     struct GNUNET_MESH_Tunnel           *head;
45     struct GNUNET_MESH_Tunnel           *tail;
46     GNUNET_MESH_TunnelEndHandler        cleaner;
47 };
48
49 /**
50  * Opaque handle to a tunnel.
51  */
52 struct GNUNET_MESH_Tunnel {
53     GNUNET_PEER_Id                              owner;
54     GNUNET_PEER_Id                              destination;
55     GNUNET_MESH_TunnelConnectHandler            connect_handler;
56     GNUNET_MESH_TunnelDisconnectHandler         disconnect_handler;
57     GNUNET_PEER_Id                              *peers;
58     void                                        *cls;
59 };
60
61
62 /**
63  * Connect to the mesh service.
64  *
65  * @param cfg configuration to use
66  * @param cls closure for the various callbacks that follow
67  *            (including handlers in the handlers array)
68  * @param cleaner function called when an *inbound* tunnel is destroyed
69  * @param handlers callbacks for messages we care about, NULL-terminated
70  *                 note that the mesh is allowed to drop notifications about
71  *                 inbound messages if the client does not process them fast
72  *                 enough (for this notification type, a bounded queue is used)
73  * @return handle to the mesh service 
74  *         NULL on error (in this case, init is never called)
75  */
76 struct GNUNET_MESH_Handle *
77 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
78                      void *cls,
79                      GNUNET_MESH_TunnelEndHandler cleaner,
80                      const struct GNUNET_MESH_MessageHandler *handlers, 
81                      const GNUNET_MESH_ApplicationType *stypes) {
82     GNUNET_MESH_Handle *h;
83     h = GNUNET_malloc(sizeof(GNUNET_MESH_Handle));
84
85     h->cleaner = cleaner;
86     return h;
87 }
88
89
90 /**
91  * Disconnect from the mesh service.
92  *
93  * @param handle connection to mesh to disconnect
94  */
95 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) {
96     
97     GNUNET_free(handle);
98     return;
99 }
100
101
102 /**
103  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
104  * and to broadcast).
105  *
106  * @param h mesh handle
107  * @param connect_handler function to call when peers are actually connected
108  * @param disconnect_handler function to call when peers are disconnected
109  * @param handler_cls closure for connect/disconnect handlers
110  */
111 struct GNUNET_MESH_Tunnel *
112 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
113                            GNUNET_MESH_TunnelConnectHandler
114                            connect_handler,
115                            GNUNET_MESH_TunnelDisconnectHandler
116                            disconnect_handler,
117                            void *handler_cls) {
118     GNUNET_MESH_Tunnel *tunnel;
119     tunnel = GNUNET_malloc(sizeof(GNUNET_MESH_Tunnel));
120
121     tunnel->connect_handler = connect_handler;
122     tunnel->disconnect_handler = disconnect_handler;
123     tunnel->peers = NULL;
124     tunnel->cls = handler_cls;
125
126     return tunnel;
127 }
128
129
130 /**
131  * Request that a peer should be added to the tunnel.  The existing
132  * connect handler will be called ONCE with either success or failure.
133  *
134  * @param tunnel handle to existing tunnel
135  * @param timeout how long to try to establish a connection
136  * @param peer peer to add
137  */
138 void
139 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
140                                       struct GNUNET_TIME_Relative timeout,
141                                       const struct GNUNET_PeerIdentity *peer) {
142     static GNUNET_PEER_Id       peer_id;
143     
144     peer_id = GNUNET_PEER_intern(peer);
145     
146     /* FIXME ACTUALLY DO STUFF */
147     tunnel->peers = &peer;
148     tunnel->connect_handler(tunnel->cls, peer, NULL);
149     return;
150 }
151
152
153 /**
154  * Request that a peer should be removed from the tunnel.  The existing
155  * disconnect handler will be called ONCE if we were connected.
156  *
157  * @param tunnel handle to existing tunnel
158  * @param peer peer to remove
159  */
160 void
161 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
162                                       const struct GNUNET_PeerIdentity *peer) {
163     /* FIXME ACTUALLY DO STUFF */
164     tunnel->peers = NULL;
165     tunnel->disconnect_handler(tunnel->cls, peer);
166     return;
167 }
168
169
170 /**
171  * Request that the mesh should try to connect to a peer supporting the given
172  * message type.
173  *
174  * @param tunnel handle to existing tunnel
175  * @param timeout how long to try to establish a connection
176  * @param app_type application type that must be supported by the peer (MESH
177  *                 should discover peer in proximity handling this type)
178  */
179 void
180 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
181                                           struct GNUNET_TIME_Relative timeout,
182                                           GNUNET_MESH_ApplicationType
183                                           app_type) {
184     return;
185 }
186
187
188 /**
189  * Ask the mesh to call "notify" once it is ready to transmit the
190  * given number of bytes to the specified "target".  If we are not yet
191  * connected to the specified peer, a call to this function will cause
192  * us to try to establish a connection.
193  *
194  * @param tunnel tunnel to use for transmission
195  * @param cork is corking allowed for this transmission?
196  * @param priority how important is the message?
197  * @param maxdelay how long can the message wait?
198  * @param target destination for the message,
199  *               NULL for multicast to all tunnel targets 
200  * @param notify_size how many bytes of buffer space does notify want?
201  * @param notify function to call when buffer space is available;
202  *        will be called with NULL on timeout or if the overall queue
203  *        for this peer is larger than queue_size and this is currently
204  *        the message with the lowest priority
205  * @param notify_cls closure for notify
206  * @return non-NULL if the notify callback was queued,
207  *         NULL if we can not even queue the request (insufficient
208  *         memory); if NULL is returned, "notify" will NOT be called.
209  */
210 struct GNUNET_MESH_TransmitHandle *
211 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel,
212                                    int cork,
213                                    uint32_t priority,
214                                    struct GNUNET_TIME_Relative maxdelay,
215                                    const struct GNUNET_PeerIdentity *target,
216                                    size_t notify_size,
217                                    GNUNET_CONNECTION_TransmitReadyNotify
218                                    notify,
219                                    void *notify_cls) {
220     struct GNUNET_MESH_Handle   *handle;
221
222     handle = GNUNET_malloc(sizeof(GNUNET_MESH_Handle));
223
224     return handle;
225 }
226
227
228 #if 0                           /* keep Emacsens' auto-indent happy */
229 {
230 #endif
231 #ifdef __cplusplus
232 }
233 #endif