drft
[oweals/gnunet.git] / src / testing / testing_group.c
1 /*
2       This file is part of GNUnet
3       (C) 2008, 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 testing/testing_group.c
23  * @brief convenience API for writing testcases for GNUnet
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
29
30
31 /**
32  * Handle to a group of GNUnet peers.
33  */
34 struct GNUNET_TESTING_PeerGroup
35 {
36   /**
37    * Our scheduler.
38    */ 
39   struct GNUNET_SCHEDULER_Handle *sched;
40
41   /**
42    * Configuration template.
43    */
44   struct GNUNET_CONFIGURATION_Handle *cfg;
45
46   /**
47    * Function to call on each started daemon.
48    */ 
49   GNUNET_TESTING_NotifyDaemonRunning cb;
50
51   /**
52    * Closure for cb.
53    */
54   void *cb_cls;
55
56   /**
57    * NULL-terminated array of hostnames.
58    */
59   char **hostnames;
60
61   /**
62    * Array of "total" peers.
63    */
64   struct GNUNET_TESTING_Daemon **peers;
65
66   /**
67    * Number of peers in this group.
68    */ 
69   unsigned int total;
70
71 };
72
73
74 /**
75  * Start count gnunetd processes with the same set of transports and
76  * applications.  The port numbers (any option called "PORT") will be
77  * adjusted to ensure that no two peers running on the same system
78  * have the same port(s) in their respective configurations.
79  *
80  * @param sched scheduler to use 
81  * @param cfg configuration template to use
82  * @param total number of daemons to start
83  * @param cb function to call on each daemon that was started
84  * @param cb_cls closure for cb
85  * @param hostname where to run the peers; can be NULL (to run
86  *        everything on localhost).
87  * @param va Additional hosts can be specified using a NULL-terminated list of
88  *        varargs, hosts will then be used round-robin from that
89  *        list; va only contains anything if hostname != NULL.
90  * @return NULL on error, otherwise handle to control peer group
91  */
92 struct GNUNET_TESTING_PeerGroup *
93 GNUNET_TESTING_daemons_start_va (struct GNUNET_SCHEDULER_Handle *sched,
94                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
95                                  unsigned int total,
96                                  GNUNET_TESTING_NotifyDaemonRunning cb,
97                                  void *cb_cls,
98                                  const char *hostname,
99                                  va_list va)
100 {
101   struct GNUNET_TESTING_PeerGroup *pg;
102   
103   pg = GNUNET_malloc (sizeof(struct GNUNET_TESTING_PeerGroup));
104   return pg;
105 }
106
107
108 /**
109  * Start count gnunetd processes with the same set of
110  * transports and applications.  The port numbers will
111  * be computed by adding delta each time (zero
112  * times for the first peer).
113  *
114  * @param sched scheduler to use 
115  * @param cfg configuration template to use
116  * @param total number of daemons to start
117  * @param timeout how long is this allowed to take?
118  * @param cb function to call on each daemon that was started
119  * @param cb_cls closure for cb
120  * @param hostname where to run the peers; can be NULL (to run
121  *        everything on localhost). Additional
122  *        hosts can be specified using a NULL-terminated list of
123  *        varargs, hosts will then be used round-robin from that
124  *        list.
125  * @return NULL on error, otherwise handle to control peer group
126  */
127 struct GNUNET_TESTING_PeerGroup *
128 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
129                               struct GNUNET_CONFIGURATION_Handle *cfg,
130                               unsigned int total,
131                               GNUNET_TESTING_NotifyDaemonRunning cb,
132                               void *cb_cls,
133                               const char *hostname,
134                               ...)
135 {
136   struct GNUNET_TESTING_PeerGroup * ret;
137   va_list va;
138   
139   va_start (va, hostname);
140   ret = GNUNET_TESTING_daemons_start_va (sched, cfg,
141                                          total, cb, cb_cls, hostname,
142                                          va);
143   va_end (va);
144   return ret;
145 }
146
147
148
149 /**
150  * Shutdown all peers started in the given group.
151  * 
152  * @param pg handle to the peer group
153  */
154 void
155 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg)
156 {
157   
158   GNUNET_free (pg);
159 }
160
161
162 /* end of testing_group.c */