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