make sure stats that are being set can be committed on destroy
[oweals/gnunet.git] / src / hostlist / gnunet-daemon-hostlist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2007, 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 hostlist/gnunet-daemon-hostlist.c
23  * @brief code for bootstrapping via hostlist servers
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - implement -a and -e switches (send P2P messages about our hostlist URL,
28  *   receive such messages and automatically update our hostlist URL config
29  *   value).
30  */
31
32 #include <stdlib.h>
33 #include "platform.h"
34 #include "hostlist-client.h"
35 #include "hostlist-server.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_getopt_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_program_lib.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_strings_lib.h"
42 #include "gnunet_time_lib.h"
43 #include "gnunet_util_lib.h"
44
45
46 /**
47  * Set if we are allowed to advertise our hostlist to others.
48  */
49 static int advertising;
50
51 /**
52  * Set if we are allowed to learn about peers by accessing
53  * hostlist servers.
54  */
55 static int bootstrapping;
56
57 /**
58  * Set if the user allows us to learn about new hostlists
59  * from the network.
60  */
61 static int learning;
62
63 /**
64  * Set if the user wants us to run a hostlist server.
65  */
66 static int provide_hostlist;
67
68 /**
69  * Statistics handle.
70  */
71 static struct GNUNET_STATISTICS_Handle *stats;
72
73 /**
74  * gnunet-daemon-hostlist command line options.
75  */
76 static struct GNUNET_GETOPT_CommandLineOption options[] = {
77   { 'a', "advertise", NULL, 
78     gettext_noop ("advertise our hostlist to other peers"),
79     GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
80   { 'b', "bootstrap", NULL, 
81     gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
82     GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
83   { 'e', "enable-learning", NULL,
84     gettext_noop ("enable learning about hostlist servers from other peers"),
85     GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
86   { 'p', "provide-hostlist", NULL, 
87     gettext_noop ("provide a hostlist server"),
88     GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
89   GNUNET_GETOPT_OPTION_END
90 };
91
92
93 static void
94 core_init (void *cls,
95            struct GNUNET_CORE_Handle * server,
96            const struct GNUNET_PeerIdentity *
97            my_identity,
98            const struct
99            GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
100            publicKey)
101 {
102   if (advertising && (NULL != server))
103     {    
104       /* FIXME: provide "server" to 'hostlist' module */
105     }
106 }
107
108
109 /**
110  * Last task run during shutdown.  Disconnects us from
111  * the other services.
112  */
113 static void
114 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   if (stats != NULL)
117     {
118       GNUNET_STATISTICS_destroy (stats,
119                                  GNUNET_NO);
120       stats = NULL;
121     }
122 }
123
124
125 /**
126  * Main function that will be run.
127  *
128  * @param cls closure
129  * @param sched the scheduler to use
130  * @param args remaining command-line arguments
131  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
132  * @param cfg configuration
133  */
134 static void 
135 run (void *cls,
136      struct GNUNET_SCHEDULER_Handle * sched,
137      char *const *args,
138      const char *cfgfile,
139      const struct GNUNET_CONFIGURATION_Handle * cfg)
140 {
141   GNUNET_CORE_ClientEventHandler ch = NULL;
142   GNUNET_CORE_ClientEventHandler dh = NULL;
143   struct GNUNET_CORE_MessageHandler handlers[] = 
144     {
145       { NULL, 0, 0 }
146     };
147
148   if ( (! bootstrapping) &&
149        (! learning) &&
150        (! provide_hostlist) )
151     {
152       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
153                   _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
154       return;
155     }
156   stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
157   if (learning)
158     {
159       /* FIXME (register handler with core for hostlist ads) */
160     }
161   if (bootstrapping)
162     {
163       GNUNET_HOSTLIST_client_start (cfg, sched, stats,
164                                     &ch, &dh);
165     }
166   if (provide_hostlist)
167     {      
168       GNUNET_HOSTLIST_server_start (cfg, sched, stats);
169     }
170   GNUNET_CORE_connect (sched, cfg,
171                        GNUNET_TIME_UNIT_FOREVER_REL,
172                        NULL,
173                        &core_init,
174                        ch, dh,
175                        NULL,
176                        NULL, GNUNET_NO,
177                        NULL, GNUNET_NO,
178                        handlers);
179   GNUNET_SCHEDULER_add_delayed (sched,
180                                 GNUNET_YES,
181                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
182                                 GNUNET_SCHEDULER_NO_TASK,
183                                 GNUNET_TIME_UNIT_FOREVER_REL,
184                                 &cleaning_task, NULL);
185 }
186
187
188 /**
189  * The main function for the hostlist daemon.
190  *
191  * @param argc number of arguments from the command line
192  * @param argv command line arguments
193  * @return 0 ok, 1 on error
194  */
195 int
196 main (int argc, char *const *argv)
197 {
198   int ret;
199
200   ret = (GNUNET_OK ==
201          GNUNET_PROGRAM_run (argc,
202                              argv,
203                              "hostlist", 
204                              _("GNUnet hostlist server and client"),
205                              options,
206                              &run, NULL)) ? 0 : 1;
207   return ret;
208 }
209
210 /* end of gnunet-daemon-hostlist.c */