2 This file is part of GNUnet.
3 (C) 2010, 2011, 2012 Christian Grothoff
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.
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.
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.
22 * @file mesh/gnunet-service-regexprofiler.c
23 * @brief service 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
30 #include "gnunet_mesh_service.h"
31 #include "gnunet_statistics_service.h"
34 * Return value from 'main'.
36 static int global_ret;
39 * Configuration we use.
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * Handle to the statistics service.
46 static struct GNUNET_STATISTICS_Handle *stats_handle;
51 struct GNUNET_MESH_Handle *mesh_handle;
54 * Peer's mesh tunnel handle.
56 struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
59 * Maximal path compression length for regex announcing.
61 static unsigned long long max_path_compression;
64 * Name of the file containing policies that this peer should announce. One
67 static char * policy_filename;
71 * Task run during shutdown.
77 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
81 if (NULL != mesh_tunnel_handle)
83 GNUNET_MESH_tunnel_destroy (mesh_tunnel_handle);
84 mesh_tunnel_handle = NULL;
87 if (NULL != mesh_handle)
89 GNUNET_MESH_disconnect (mesh_handle);
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
98 * Announce the given regular expression using Mesh and the path compression
99 * length read from config.
101 * @param regex regular expression to announce on this peer's mesh.
104 announce_regex (const char * regex)
106 if (NULL == regex || 0 == strlen (regex))
108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
113 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
114 GNUNET_MESH_announce_regex (mesh_handle, regex, (unsigned int)max_path_compression);
119 * Main function that will be run by the scheduler.
122 * @param server the initialized server
123 * @param cfg_ configuration
127 struct GNUNET_SERVER_Handle *server,
128 const struct GNUNET_CONFIGURATION_Handle *cfg_)
135 GNUNET_MESH_ApplicationType app;
137 static struct GNUNET_MESH_MessageHandler handlers[] = {
144 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
145 &max_path_compression))
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
150 "regexprofiler", "max_path_compression");
151 global_ret = GNUNET_SYSERR;
152 GNUNET_SCHEDULER_shutdown ();
157 GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER", "POLICY_FILE",
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
163 "regexprofiler", "policy_file");
164 global_ret = GNUNET_SYSERR;
165 GNUNET_SCHEDULER_shutdown ();
169 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
171 app = (GNUNET_MESH_ApplicationType)0;
174 GNUNET_MESH_connect (cfg, NULL, NULL, NULL, handlers, &app);
176 if (NULL == mesh_handle)
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not acquire mesh handle. Exiting.\n");
179 global_ret = GNUNET_SYSERR;
180 GNUNET_SCHEDULER_shutdown ();
184 /* Announcing regexes from policy_filename */
185 if (GNUNET_YES != GNUNET_DISK_file_test (policy_filename))
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188 "Could not find policy file %s\n", policy_filename);
191 if (GNUNET_OK != GNUNET_DISK_file_size (policy_filename, &filesize, GNUNET_YES, GNUNET_YES))
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Policy file %s is empty.\n", policy_filename);
198 data = GNUNET_malloc (filesize);
199 if (filesize != GNUNET_DISK_fn_read (policy_filename, data, filesize))
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not read policy file %s.\n",
209 while (offset < (filesize - 1))
212 if (((data[offset] == '\n')) && (buf != &data[offset]))
216 GNUNET_assert (NULL != regex);
217 announce_regex (regex);
218 buf = &data[offset + 1];
220 else if ((data[offset] == '\n') || (data[offset] == '\0'))
221 buf = &data[offset + 1];
225 /* Scheduled the task to clean up when shutdown is called */
226 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
232 * The main function of the regexprofiler service.
234 * @param argc number of arguments from the command line
235 * @param argv command line arguments
236 * @return 0 ok, 1 on error
239 main (int argc, char *const *argv)
242 GNUNET_SERVICE_run (argc, argv, "regexprofiler",
243 GNUNET_SERVICE_OPTION_NONE,
244 &run, NULL)) ? global_ret : 1;
247 /* end of gnunet-service-regexprofiler.c */