2 This file is part of GNUnet.
3 (C) 2010 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 core/test_core_api_send_to_self.c
24 * @author Philipp Toelke
27 #include <gnunet_common.h>
28 #include <gnunet_program_lib.h>
29 #include <gnunet_protocols.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_constants.h>
39 * Handle to the cleanup task.
41 GNUNET_SCHEDULER_TaskIdentifier die_task;
43 static struct GNUNET_PeerIdentity myself;
46 * Configuration to load for the new peer.
48 struct GNUNET_CONFIGURATION_Handle *core_cfg;
53 struct GNUNET_CORE_Handle *core;
56 * Handle to gnunet-service-arm.
58 struct GNUNET_OS_Process *arm_proc;
61 * Function scheduled as very last function, cleans up after us
64 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
66 die_task = GNUNET_SCHEDULER_NO_TASK;
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n");
71 GNUNET_CORE_disconnect (core);
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peer\n");
76 if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
77 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
79 if (GNUNET_OS_process_wait (arm_proc) != GNUNET_OK)
80 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
83 GNUNET_OS_process_get_pid (arm_proc));
84 GNUNET_OS_process_close (arm_proc);
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
91 receive (void *cls, const struct GNUNET_PeerIdentity *other,
92 const struct GNUNET_MessageHeader *message,
93 const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
95 if (die_task != GNUNET_SCHEDULER_NO_TASK)
96 GNUNET_SCHEDULER_cancel (die_task);
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n",
99 GNUNET_SCHEDULER_add_now (&cleanup, NULL);
105 send_message (void *cls, size_t size, void *buf)
107 if (size == 0 || buf == NULL)
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
113 struct GNUNET_MessageHeader *hdr = buf;
115 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
116 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
117 return ntohs (hdr->size);
121 init (void *cls, struct GNUNET_CORE_Handle *core,
122 const struct GNUNET_PeerIdentity *my_identity)
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130 "Correctly connected to CORE; we are the peer %s.\n",
131 GNUNET_i2s (my_identity));
132 memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
136 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
137 const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
141 if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 "Connected to myself; sending message!\n");
145 GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0,
146 GNUNET_TIME_UNIT_FOREVER_REL, peer,
147 sizeof (struct GNUNET_MessageHeader),
154 * Main function that will be run by the scheduler.
157 * @param args remaining command-line arguments
158 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
159 * @param cfg configuration
162 run (void *cls, char *const *args, const char *cfgfile,
163 const struct GNUNET_CONFIGURATION_Handle *cfg)
165 const static struct GNUNET_CORE_MessageHandler handlers[] = {
166 {&receive, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
170 core_cfg = GNUNET_CONFIGURATION_create ();
173 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
174 "gnunet-service-arm",
178 "-c", "test_core_api_peer1.conf", NULL);
180 GNUNET_assert (GNUNET_OK ==
181 GNUNET_CONFIGURATION_load (core_cfg,
182 "test_core_api_peer1.conf"));
185 GNUNET_CORE_connect (core_cfg, 42, NULL, &init, &connect_cb, NULL, NULL,
186 0, NULL, 0, handlers);
189 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
190 (GNUNET_TIME_UNIT_SECONDS, 60), &cleanup,
198 char *const argv[] = { "test-core-api-send-to-self",
200 "test_core_api_data.conf",
207 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
208 GNUNET_GETOPT_OPTION_END
214 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
215 "test_core_api_send_to_self",
216 gettext_noop ("help text"), options, &run,
221 * The main function to obtain template from gnunetd.
223 * @param argc number of arguments from the command line
224 * @param argv command line arguments
225 * @return 0 ok, 1 on error
228 main (int argc, char *argv[])
230 GNUNET_log_setup ("test-core-api-send-to-self",
238 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
242 /* end of test_core_api_send_to_self.c */