fix test case, implement base32 argument parser logic
[oweals/gnunet.git] / src / util / server_tc.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 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  * @file util/server_tc.c
23  * @brief convenience functions for transmission of
24  *        complex responses as a server
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "util-server-tc", __VA_ARGS__)
33
34
35 /**
36  * How much buffer space do we want to have at least
37  * before transmitting another increment?
38  */
39 #define MIN_BLOCK_SIZE 128
40
41
42
43 struct GNUNET_SERVER_TransmitContext
44 {
45   /**
46    * Which client are we transmitting to?
47    */
48   struct GNUNET_SERVER_Client *client;
49
50   /**
51    * Transmission buffer. (current offset for writing).
52    */
53   char *buf;
54
55   /**
56    * Number of bytes in buf.
57    */
58   size_t total;
59
60   /**
61    * Offset for writing in buf.
62    */
63   size_t off;
64
65   /**
66    * Timeout for this request.
67    */
68   struct GNUNET_TIME_Absolute timeout;
69 };
70
71
72 /**
73  * Helper function for incremental transmission of the response.
74  */
75 static size_t
76 transmit_response (void *cls, size_t size, void *buf)
77 {
78   struct GNUNET_SERVER_TransmitContext *tc = cls;
79   size_t msize;
80
81   if (NULL == buf)
82   {
83     GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
84     return 0;
85   }
86   if (tc->total - tc->off > size)
87     msize = size;
88   else
89     msize = tc->total - tc->off;
90   GNUNET_memcpy (buf, &tc->buf[tc->off], msize);
91   tc->off += msize;
92   if (tc->total == tc->off)
93   {
94     GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
95     GNUNET_SERVER_client_drop (tc->client);
96     GNUNET_free_non_null (tc->buf);
97     GNUNET_free (tc);
98   }
99   else
100   {
101     if (NULL ==
102         GNUNET_SERVER_notify_transmit_ready (tc->client,
103                                              GNUNET_MIN (MIN_BLOCK_SIZE,
104                                                          tc->total - tc->off),
105                                              GNUNET_TIME_absolute_get_remaining
106                                              (tc->timeout), &transmit_response,
107                                              tc))
108     {
109       GNUNET_break (0);
110       GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
111     }
112   }
113   return msize;
114 }
115
116
117 /**
118  * Create a new transmission context for the
119  * given client.
120  *
121  * @param client client to create the context for.
122  * @return NULL on error
123  */
124 struct GNUNET_SERVER_TransmitContext *
125 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
126 {
127   struct GNUNET_SERVER_TransmitContext *tc;
128
129   GNUNET_assert (NULL != client);
130   tc = GNUNET_new (struct GNUNET_SERVER_TransmitContext);
131   GNUNET_SERVER_client_keep (client);
132   tc->client = client;
133   return tc;
134 }
135
136
137 /**
138  * Append a message to the transmission context.
139  * All messages in the context will be sent by
140  * the transmit_context_run method.
141  *
142  * @param tc context to use
143  * @param data what to append to the result message
144  * @param length length of data
145  * @param type type of the message
146  */
147 void
148 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
149                                             *tc, const void *data,
150                                             size_t length, uint16_t type)
151 {
152   struct GNUNET_MessageHeader *msg;
153   size_t size;
154
155   GNUNET_assert (length < GNUNET_SERVER_MAX_MESSAGE_SIZE);
156   size = length + sizeof (struct GNUNET_MessageHeader);
157   GNUNET_assert (size > length);
158   tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
159   msg = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
160   tc->total += size;
161   msg->size = htons (size);
162   msg->type = htons (type);
163   GNUNET_memcpy (&msg[1], data, length);
164 }
165
166
167 /**
168  * Append a message to the transmission context.
169  * All messages in the context will be sent by
170  * the transmit_context_run method.
171  *
172  * @param tc context to use
173  * @param msg message to append
174  */
175 void
176 GNUNET_SERVER_transmit_context_append_message (struct
177                                                GNUNET_SERVER_TransmitContext
178                                                *tc,
179                                                const struct GNUNET_MessageHeader
180                                                *msg)
181 {
182   struct GNUNET_MessageHeader *m;
183   uint16_t size;
184
185   size = ntohs (msg->size);
186   tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
187   m = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
188   tc->total += size;
189   GNUNET_memcpy (m, msg, size);
190 }
191
192
193 /**
194  * Execute a transmission context.  If there is
195  * an error in the transmission, the #GNUNET_SERVER_receive_done()
196  * method will be called with an error code (#GNUNET_SYSERR),
197  * otherwise with #GNUNET_OK.
198  *
199  * @param tc transmission context to use
200  * @param timeout when to time out and abort the transmission
201  */
202 void
203 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
204                                     struct GNUNET_TIME_Relative timeout)
205 {
206   tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
207   if (NULL ==
208       GNUNET_SERVER_notify_transmit_ready (tc->client,
209                                            GNUNET_MIN (MIN_BLOCK_SIZE,
210                                                        tc->total), timeout,
211                                            &transmit_response, tc))
212   {
213     GNUNET_break (0);
214     GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
215   }
216 }
217
218
219 /**
220  * Destroy a transmission context. This function must not be called
221  * after 'GNUNET_SERVER_transmit_context_run'.
222  *
223  * @param tc transmission context to destroy
224  * @param success code to give to 'GNUNET_SERVER_receive_done' for
225  *        the client:  GNUNET_OK to keep the connection open and
226  *                          continue to receive
227  *                GNUNET_NO to close the connection (normal behavior)
228  *                GNUNET_SYSERR to close the connection (signal
229  *                          serious error)
230  */
231 void
232 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
233                                         *tc, int success)
234 {
235   GNUNET_SERVER_receive_done (tc->client, success);
236   GNUNET_SERVER_client_drop (tc->client);
237   GNUNET_free_non_null (tc->buf);
238   GNUNET_free (tc);
239 }
240
241
242 /* end of server_tc.c */