bugfixes and redesigning scheduler API
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist.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  * @file hostlist/test_gnunet_daemon_hostlist.c
22  * @brief test for gnunet_daemon_hostslist.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_transport_service.h"
29
30 #define VERBOSE GNUNET_YES
31
32 #define START_ARM GNUNET_YES
33
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
39
40 static int ok;
41
42 static struct GNUNET_SCHEDULER_Handle *sched;
43     
44 struct PeerContext
45 {
46   struct GNUNET_CONFIGURATION_Handle *cfg;
47   struct GNUNET_TRANSPORT_Handle *th;
48   struct GNUNET_MessageHeader *hello;
49 #if START_ARM
50   pid_t arm_pid;
51 #endif
52 };
53
54 static struct PeerContext p1;
55
56 static struct PeerContext p2;
57
58
59
60 static void
61 process_hello (void *cls,
62                struct GNUNET_TIME_Relative latency,
63                const struct GNUNET_PeerIdentity *peer,
64                const struct GNUNET_MessageHeader *message)
65 {
66   struct PeerContext *p = cls;
67
68   GNUNET_assert (peer != NULL);
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70               "Received (my) `%s' from transport service of `%4s'\n",
71               "HELLO", GNUNET_i2s (peer));
72   GNUNET_assert (message != NULL);
73   p->hello = GNUNET_malloc (ntohs (message->size));
74   memcpy (p->hello, message, ntohs (message->size));
75   if ((p == &p1) && (p2.th != NULL))
76     GNUNET_TRANSPORT_offer_hello (p2.th, message);
77   if ((p == &p2) && (p1.th != NULL))
78     GNUNET_TRANSPORT_offer_hello (p1.th, message);
79
80   if ((p == &p1) && (p2.hello != NULL))
81     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
82   if ((p == &p2) && (p1.hello != NULL))
83     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
84 }
85
86
87 static void
88 setup_peer (struct PeerContext *p, const char *cfgname)
89 {
90   p->cfg = GNUNET_CONFIGURATION_create ();
91 #if START_ARM
92   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
93                                         "gnunet-service-arm",
94 #if VERBOSE
95                                         "-L", "DEBUG",
96 #endif
97                                         "-c", cfgname, NULL);
98 #endif
99   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
100   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
101   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
102   GNUNET_assert (p->th != NULL);
103   GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
104 }
105
106
107 static void
108 run (void *cls,
109      struct GNUNET_SCHEDULER_Handle *s,
110      char *const *args,
111      const char *cfgfile, 
112      const struct GNUNET_CONFIGURATION_Handle *cfg)
113 {
114   GNUNET_assert (ok == 1);
115   ok++;
116   sched = s;
117   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
118   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
119 }
120
121
122 static void
123 stop_arm (struct PeerContext *p)
124 {
125   GNUNET_ARM_stop_services (p->cfg, sched, "core", NULL);
126 #if START_ARM
127   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
128     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
129   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
130     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132               "ARM process %u stopped\n", p->arm_pid);
133 #endif
134   GNUNET_CONFIGURATION_destroy (p->cfg);
135 }
136
137
138 static int
139 check ()
140 {
141   char *const argv[] = { "test-gnunet-daemon-hostlist",
142     "-c", "test_gnunet_daemon_hostlist_data.conf",
143 #if VERBOSE
144     "-L", "DEBUG",
145 #endif
146     NULL
147   };
148   struct GNUNET_GETOPT_CommandLineOption options[] = {
149     GNUNET_GETOPT_OPTION_END
150   };
151   ok = 1;
152   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
153                       argv, "test-gnunet-daemon-hostlist",
154                       "nohelp", options, &run, &ok);
155   stop_arm (&p1);
156   stop_arm (&p2);
157   return ok;
158 }
159
160
161 int
162 main (int argc, char *argv[])
163 {
164   
165   int ret;
166
167   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
168 #if VERBOSE
169                     "DEBUG",
170 #else
171                     "WARNING",
172 #endif
173                     NULL);
174   ret = check ();
175   return ret; 
176 }
177
178 /* end of test_gnunet_daemon_hostlist.c */