2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
21 * @file nse/test_nse_api.c
22 * @brief testcase for nse_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_nse_service.h"
32 #define DEBUG_NSE GNUNET_YES
34 #define START_ARM GNUNET_YES
36 static struct GNUNET_NSE_Handle *h;
38 static GNUNET_SCHEDULER_TaskIdentifier die_task;
42 struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_OS_Process *arm_proc;
48 static struct PeerContext p1;
52 stop_arm (struct PeerContext *p)
55 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
56 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
57 GNUNET_OS_process_wait (p->arm_proc);
58 GNUNET_OS_process_close (p->arm_proc);
61 GNUNET_CONFIGURATION_destroy (p->cfg);
65 * Signature of the main function of a task.
68 * @param tc context information (why was this task triggered now)
71 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from NSE service.\n");
76 GNUNET_NSE_disconnect (h);
81 * Callback to call when network size estimate is updated.
84 * @param timestamp time when the estimate was received from the server (or created by the server)
85 * @param estimate the value of the current network size estimate
86 * @param std_dev standard deviation (rounded down to nearest integer)
87 * of the size estimation values seen
91 check_nse_message (void *cls, struct GNUNET_TIME_Absolute timestamp,
92 double estimate, double std_dev)
97 "Received NSE message, estimate %f, standard deviation %f.\n",
99 /* Fantastic check below. Expect NaN, the only thing not equal to itself. */
101 if (die_task != GNUNET_SCHEDULER_NO_TASK)
102 GNUNET_SCHEDULER_cancel (die_task);
103 die_task = GNUNET_SCHEDULER_add_now (&end_test, NULL);
108 setup_peer (struct PeerContext *p, const char *cfgname)
110 p->cfg = GNUNET_CONFIGURATION_create ();
113 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
114 "gnunet-service-arm",
118 "-c", cfgname, NULL);
120 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
127 run (void *cls, char *const *args, const char *cfgfile,
128 const struct GNUNET_CONFIGURATION_Handle *cfg)
131 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
132 (GNUNET_TIME_UNIT_MINUTES, 1), &end_test,
135 setup_peer (&p1, cfgfile);
136 h = GNUNET_NSE_connect (cfg, &check_nse_message, cls);
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to NSE service.\n");
138 GNUNET_assert (h != NULL);
147 char *const argv[] = { "test-nse-api",
157 struct GNUNET_GETOPT_CommandLineOption options[] = {
158 GNUNET_GETOPT_OPTION_END
161 GNUNET_PROGRAM_run (5, argv, "test-nse-api", "nohelp", options, &run, &ok);
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping arm.\n");
169 main (int argc, char *argv[])
173 GNUNET_log_setup ("test_nse_api",
185 /* end of test_nse_api.c */