- adapt first test to mesh test lib
[oweals/gnunet.git] / src / mesh / gnunet-daemon-regexprofiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2011, 2012 Christian Grothoff
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 mesh/gnunet-daemon-regexprofiler.c
23  * @brief daemon that uses mesh to announce a regular expression. Used in
24  * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
25  * without the need to explicitly connect to the mesh service running on the
26  * peer from within the profiler.
27  * @author Maximilian Szengel
28  */
29 #include "platform.h"
30 #include "gnunet_mesh_service.h"
31 #include "gnunet_statistics_service.h"
32
33 /**
34  * Return value from 'main'.
35  */
36 static int global_ret;
37
38 /**
39  * Configuration we use.
40  */
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 /**
44  * Handle to the statistics service.
45  */
46 static struct GNUNET_STATISTICS_Handle *stats_handle;
47
48 /**
49  * Peer's mesh handle.
50  */
51 struct GNUNET_MESH_Handle *mesh_handle;
52
53 /**
54  * Peer's mesh tunnel handle.
55  */
56 struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
57
58 /**
59  * Maximal path compression length for regex announcing.
60  */
61 static unsigned long long max_path_compression;
62
63 /**
64  * Name of the file containing policies that this peer should announce. One
65  * policy per line.
66  */
67 static char * policy_filename;
68
69 /**
70  * Prefix to add before every regex we're announcing.
71  */
72 static char * regex_prefix;
73
74 /**
75  * Task run during shutdown.
76  *
77  * @param cls unused
78  * @param tc unused
79  */
80 static void
81 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
84
85   if (NULL != mesh_tunnel_handle)
86   {
87     GNUNET_MESH_tunnel_destroy (mesh_tunnel_handle);
88     mesh_tunnel_handle = NULL;
89   }
90
91   if (NULL != mesh_handle)
92   {
93     GNUNET_MESH_disconnect (mesh_handle);
94     mesh_handle = NULL;
95   }
96
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
98 }
99
100
101 /**
102  * Announce the given regular expression using Mesh and the path compression
103  * length read from config.
104  *
105  * @param regex regular expression to announce on this peer's mesh.
106  */
107 static void
108 announce_regex (const char * regex)
109 {
110   if (NULL == regex || 0 == strlen (regex))
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
113     return;
114   }
115
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
117   GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
118   GNUNET_MESH_announce_regex (mesh_handle, regex, (unsigned int)max_path_compression);
119 }
120
121
122 /**
123  * Load regular expressions from filename into 'rxes' array. Array needs to be freed.
124  *
125  * @param filename filename of the file containing the regexes, one per line.
126  * @param rx string with the union of all regular expressions.
127  * @return number of regular expressions read from filename and in rxes array.
128  */
129 static unsigned int
130 load_regexes (const char *filename, char **rx)
131 {
132   char *data;
133   char *buf;
134   uint64_t filesize;
135   unsigned int offset;
136   unsigned int rx_cnt;
137
138   if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
139   {
140     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141                 "Could not find policy file %s\n", policy_filename);
142     return 0;
143   }
144   if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
145     filesize = 0;
146   if (0 == filesize)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
149     return 0;
150   }
151   data = GNUNET_malloc (filesize);
152   if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
153   {
154     GNUNET_free (data);
155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
156                 policy_filename);
157     return 0;
158   }
159   buf = data;
160   offset = 0;
161   rx_cnt = 0;
162   while (offset < (filesize - 1))
163   {
164     offset++;
165     if ((data[offset] == '\n') && (buf != &data[offset]))
166     {
167       data[offset] = '|';
168       buf = &data[offset + 1];
169       rx_cnt++;
170     }
171     else if ((data[offset] == '\n') || (data[offset] == '\0'))
172       buf = &data[offset + 1];
173   }
174   data[offset] = '\0';
175   *rx = data;
176
177   return rx_cnt;
178 }
179
180
181 /**
182  * @brief Main function that will be run by the scheduler.
183  *
184  * @param cls closure
185  * @param args remaining command-line arguments
186  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
187  * @param cfg_ configuration
188  */
189 static void
190 run (void *cls, char *const *args GNUNET_UNUSED,
191      const char *cfgfile GNUNET_UNUSED,
192      const struct GNUNET_CONFIGURATION_Handle *cfg_)
193 {
194   char *regex = NULL;
195   char *rx_with_pfx;
196   const GNUNET_MESH_ApplicationType app = (GNUNET_MESH_ApplicationType)0;
197   static struct GNUNET_MESH_MessageHandler handlers[] = {
198     {NULL, 0, 0}
199   };
200
201   cfg = cfg_;
202
203   if (GNUNET_OK !=
204       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
205                                              &max_path_compression))
206   {
207     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208                 _
209                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
210                 "regexprofiler", "max_path_compression");
211     global_ret = GNUNET_SYSERR;
212     GNUNET_SCHEDULER_shutdown ();
213     return;
214   }
215
216   if (GNUNET_OK !=
217       GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER", "POLICY_FILE",
218                                                &policy_filename))
219   {
220     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221                 _
222                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
223                 "regexprofiler", "policy_file");
224     global_ret = GNUNET_SYSERR;
225     GNUNET_SCHEDULER_shutdown ();
226     return;
227   }
228
229   if (GNUNET_OK !=
230       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", "REGEX_PREFIX",
231                                              &regex_prefix))
232   {
233     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234                 _
235                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
236                 "regexprofiler", "regex_prefix");
237     global_ret = GNUNET_SYSERR;
238     GNUNET_SCHEDULER_shutdown ();
239     return;
240   }
241
242   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
243
244   mesh_handle =
245     GNUNET_MESH_connect (cfg, NULL, NULL, NULL, handlers, &app);
246
247   if (NULL == mesh_handle)
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not acquire mesh handle. Exiting.\n");
250     global_ret = GNUNET_SYSERR;
251     GNUNET_SCHEDULER_shutdown ();
252     return;
253   }
254
255   /* Read regexes from policy files */
256   if (0 == load_regexes (policy_filename, &regex))
257   {
258     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259                 "Policy file %s contains no policies. Exiting.\n",
260                 policy_filename);
261     global_ret = GNUNET_SYSERR;
262     GNUNET_SCHEDULER_shutdown ();
263     return;
264   }
265
266   /* Announcing regexes from policy_filename */
267   GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
268   announce_regex (rx_with_pfx);
269   GNUNET_free (rx_with_pfx);
270   GNUNET_free (regex);
271
272   /* Scheduled the task to clean up when shutdown is called */
273   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
274                                 NULL);
275 }
276
277
278 /**
279  * The main function of the regexprofiler service.
280  *
281  * @param argc number of arguments from the command line
282  * @param argv command line arguments
283  * @return 0 ok, 1 on error
284  */
285 int
286 main (int argc, char *const *argv)
287 {
288   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
289     GNUNET_GETOPT_OPTION_END
290   };
291
292   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
293     return 2;
294   sleep (60);
295   return (GNUNET_OK ==
296           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
297                               gettext_noop
298                               ("Daemon to announce regular expressions for the peer using mesh."),
299                               options, &run, NULL)) ? global_ret : 1;
300 }
301
302
303 #ifdef LINUX
304 #include <malloc.h>
305
306 /**
307  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
308  */
309 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
310 {
311   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
312   mallopt (M_TOP_PAD, 1 * 1024);
313   malloc_trim (0);
314 }
315 #endif
316
317
318 /* end of gnunet-daemon-regexprofiler.c */