639c0ef173acc926d6fcfbcddb1d2aea387339fc
[oweals/gnunet.git] / src / fragmentation / test_fragmentation.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 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 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 fragmentation/test_fragmentation.c
22  * @brief test for fragmentation.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fragmentation_lib.h"
27
28 #define NUM_MSGS 1
29
30 #define MTU 1111
31
32 static int ret = 1; 
33
34 static struct GNUNET_DEFRAGMENT_Context *defrag;
35
36 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
37
38 static void
39 proc_msgs (void *cls,
40            const struct GNUNET_MessageHeader *hdr)
41 {
42   static unsigned int total;
43
44   fprintf (stderr, "!");
45   total++;
46   if (total == NUM_MSGS)
47     {
48       ret = 0;
49       GNUNET_DEFRAGMENT_context_destroy (defrag);
50       defrag = NULL;
51     }
52 }
53
54 /**
55  * Process ACK (by passing to fragmenter)
56  */
57 static void
58 proc_acks (void *cls,
59            const struct GNUNET_MessageHeader *hdr)
60 {
61   unsigned int i;
62   int ret;
63
64   fprintf (stderr, "@");
65   for (i=0;i<NUM_MSGS;i++)
66     {
67       if (frags[i] == NULL)
68         return;     
69       ret = GNUNET_FRAGMENT_process_ack (frags[i],
70                                          hdr);
71       if (ret == GNUNET_OK)
72         {
73           GNUNET_FRAGMENT_context_destroy (frags[i]);
74           frags[i] = NULL;
75           return;
76         }
77       if (ret == GNUNET_NO)
78         return;
79     }
80   fprintf (stderr, "Got ACK that nobody feels responsible for...\n");
81 }
82
83
84 /**
85  * Process fragment (by passing to defrag).
86  */
87 static void
88 proc_frac (void *cls,
89            const struct GNUNET_MessageHeader *hdr)
90 {
91   fprintf (stderr, ".");
92   if (NULL == defrag)
93     return;
94   GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
95 }
96
97
98 /**
99  * Main function run with scheduler.
100  */
101 static void
102 run (void *cls,
103      char *const *args,
104      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
105 {
106   unsigned int i;
107   struct GNUNET_MessageHeader *msg;
108   char buf[MTU + 32 * 1024];
109
110   defrag = GNUNET_DEFRAGMENT_context_create (NULL,
111                                              MTU,
112                                              NUM_MSGS /* enough space for all */,
113                                              NULL,
114                                              &proc_msgs,
115                                              &proc_acks);
116   for (i=0;i<sizeof(buf);i++)
117     buf[i] = (char) i;
118   msg = (struct GNUNET_MessageHeader* ) buf;
119   for (i=0;i<NUM_MSGS;i++)
120     {
121       msg->type = htons ((uint16_t) i);
122       msg->size = htons (MTU + 1 + i % (32 * 1024));
123       frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */, 
124                                                  MTU,
125                                                  NULL /* no tracker -- infinite BW */,
126                                                  GNUNET_TIME_UNIT_MILLISECONDS,
127                                                  msg,
128                                                  &proc_frac,
129                                                  NULL);
130     }
131 }
132
133
134 int
135 main (int argc, char *argv[])
136 {
137   struct GNUNET_GETOPT_CommandLineOption options[] = {
138     GNUNET_GETOPT_OPTION_END
139   };
140   char *const argv_prog[] = {
141     "test-fragmentation",
142     "-c",
143     "test_fragmentation_data.conf",
144     "-L",
145 #if VERBOSE
146     "DEBUG",
147 #else
148     "WARNING",
149 #endif
150     NULL
151   };
152
153   GNUNET_log_setup ("test-fragmentation",
154 #if VERBOSE
155                     "DEBUG",
156 #else
157                     "WARNING",
158 #endif
159                     NULL);
160   GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options, &run, NULL);
161   return ret;
162 }