make testcase end more gracefully
[oweals/gnunet.git] / src / core / test_core_api_send_to_self.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 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 core/test_core_api_send_to_self.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
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>
32
33 /**
34  * Final status code.
35  */
36 static int ret;
37
38 /**
39  * Handle to the cleanup task.
40  */
41 GNUNET_SCHEDULER_TaskIdentifier die_task;
42
43 static struct GNUNET_PeerIdentity myself;
44
45 /**
46  * The handle to core
47  */
48 struct GNUNET_CORE_Handle *core;
49
50 /**
51  * Function scheduled as very last function, cleans up after us
52  */
53 static void
54 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
55 {
56   die_task = GNUNET_SCHEDULER_NO_TASK;
57
58   if (core != NULL)
59     {
60       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n");
61       GNUNET_CORE_disconnect (core);
62       core = NULL;
63     }
64
65   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
66 }
67
68 static int
69 receive(void* cls, const struct GNUNET_PeerIdentity* other, const struct GNUNET_MessageHeader* message, const struct GNUNET_TRANSPORT_ATS_Information* atsi)
70 {
71   if (die_task != GNUNET_SCHEDULER_NO_TASK)
72     GNUNET_SCHEDULER_cancel(die_task);
73   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n", GNUNET_i2s(other));
74   GNUNET_SCHEDULER_add_now(&cleanup, NULL);
75   ret = 0;
76   return GNUNET_OK;
77 }
78
79 static size_t
80 send_message (void* cls, size_t size, void* buf)
81 {
82   if (size == 0 || buf == NULL)
83     {
84       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
85       return 0;
86     }
87   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
88   struct GNUNET_MessageHeader *hdr = buf;
89   hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
90   hdr->type = htons(GNUNET_MESSAGE_TYPE_SERVICE_UDP);
91   return ntohs(hdr->size);
92 }
93
94 static void
95 init (void *cls, struct GNUNET_CORE_Handle *core,
96       const struct GNUNET_PeerIdentity *my_identity,
97       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pk)
98 {
99   if (core == NULL)
100     {
101       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
102       return;
103     }
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105               "Correctly connected to CORE; we are the peer %s.\n",
106               GNUNET_i2s (my_identity));
107   memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
108 }
109
110 static void
111 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
112             const struct GNUNET_TRANSPORT_ATS_Information *atsi)
113 {
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
115               GNUNET_i2s (peer));
116   if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
117     {
118       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119                   "Connected to myself; sending message!\n");
120       GNUNET_CORE_notify_transmit_ready (core,
121                                          0, GNUNET_TIME_UNIT_FOREVER_REL,
122                                          peer,
123                                          sizeof (struct GNUNET_MessageHeader),
124                                          send_message, NULL);
125     }
126 }
127
128
129 /**
130  * Main function that will be run by the scheduler.
131  *
132  * @param cls closure
133  * @param args remaining command-line arguments
134  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
135  * @param cfg configuration
136  */
137 static void
138 run (void *cls,
139      char *const *args,
140      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg_)
141 {
142   const static struct GNUNET_CORE_MessageHandler handlers[] = {
143     {receive, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
144     {NULL, 0, 0}
145   };
146   core = GNUNET_CORE_connect (cfg_,
147                               42,
148                               NULL,
149                               init,
150                               connect_cb,
151                               NULL, NULL, NULL, 0, NULL, 0, handlers);
152   die_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &cleanup, cls);
153 }
154
155 /**
156  * The main function to obtain template from gnunetd.
157  *
158  * @param argc number of arguments from the command line
159  * @param argv command line arguments
160  * @return 0 ok, 1 on error
161  */
162 int
163 main (int argc, char *const *argv)
164 {
165   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
166     GNUNET_GETOPT_OPTION_END
167   };
168
169   return (GNUNET_OK ==
170           GNUNET_PROGRAM_run (argc,
171                               argv,
172                               "test_core_api_send_to_self",
173                               gettext_noop ("help text"),
174                               options, &run, NULL)) ? ret : 1;
175 }
176
177 /* end of test_core_api_send_to_self.c */