issuer
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_experiments.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 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 /**
22  * @file experimentation/gnunet-daemon-experimentation_experiments.c
23  * @brief experimentation daemon: experiment management
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-daemon-experimentation.h"
33
34 struct GNUNET_CONTAINER_MultiHashMap *issuer;
35
36 /**
37  * Is peer a valid issuer
38  *
39  * @return GNUNET_YES or GNUNET_NO
40  */
41 int
42 GNUNET_EXPERIMENTATION_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
43 {
44         if (GNUNET_CONTAINER_multihashmap_contains (issuer, &issuer_ID->hashPubKey))
45                 return GNUNET_YES;
46         else
47                 return GNUNET_NO;
48 }
49
50
51 /**
52  * Start experiments management
53  */
54 int
55 GNUNET_EXPERIMENTATION_experiments_start ()
56 {
57         char *issuers;
58         char *pos;
59         struct GNUNET_PeerIdentity issuer_ID;
60
61         /* Load valid issuer */
62         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
63         {
64                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
65                         return GNUNET_SYSERR;
66         }
67
68         issuer = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
69
70   for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
71   {
72
73                 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
74                 {
75                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
76                 }
77                 else
78                 {
79                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
80                                 GNUNET_CONTAINER_multihashmap_put (issuer, &issuer_ID.hashPubKey,
81                                                 NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
82                 }
83   }
84   GNUNET_free (issuers);
85
86   if (0 == GNUNET_CONTAINER_multihashmap_size (issuer))
87   {
88                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
89                 GNUNET_EXPERIMENTATION_experiments_stop ();
90                 return GNUNET_SYSERR;
91   }
92
93   GNUNET_STATISTICS_set (GSE_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (issuer), GNUNET_NO);
94         return GNUNET_OK;
95 }
96
97 /**
98  * Stop experiments management
99  */
100 void
101 GNUNET_EXPERIMENTATION_experiments_stop ()
102 {
103         if (NULL != issuer)
104                 GNUNET_CONTAINER_multihashmap_destroy (issuer);
105         issuer = NULL;
106 }
107
108 /* end of gnunet-daemon-experimentation_experiments.c */