- Remove printf, use GNUNET_log INFO
[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_util_lib.h"
28 #include "gnunet_testing_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 /**
44  * Identity of this peer.
45  */
46 static struct GNUNET_PeerIdentity myself;
47
48 /**
49  * The handle to core
50  */
51 struct GNUNET_CORE_Handle *core;
52
53
54 /**
55  * Function scheduled as very last function, cleans up after us
56  */
57 static void
58 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
59 {
60   die_task = GNUNET_SCHEDULER_NO_TASK;
61
62   if (core != NULL)
63   {
64     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n");
65     GNUNET_CORE_disconnect (core);
66     core = NULL;
67   }
68   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
69 }
70
71
72 static int
73 receive (void *cls, const struct GNUNET_PeerIdentity *other,
74          const struct GNUNET_MessageHeader *message,
75          const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
76 {
77   if (die_task != GNUNET_SCHEDULER_NO_TASK)
78     GNUNET_SCHEDULER_cancel (die_task);
79   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n",
80               GNUNET_i2s (other));
81   GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (message->type));
82   GNUNET_assert (0 == memcmp (other, &myself, sizeof (myself)));
83   GNUNET_SCHEDULER_add_now (&cleanup, NULL);
84   ret = 0;
85   return GNUNET_OK;
86 }
87
88
89 static size_t
90 send_message (void *cls, size_t size, void *buf)
91 {
92   if (size == 0 || buf == NULL)
93   {
94     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
95     return 0;
96   }
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
98   struct GNUNET_MessageHeader *hdr = buf;
99
100   hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
101   hdr->type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
102   return ntohs (hdr->size);
103 }
104
105
106 static void
107 init (void *cls, struct GNUNET_CORE_Handle *core,
108       const struct GNUNET_PeerIdentity *my_identity)
109 {
110   if (core == NULL)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
113     return;
114   }
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116               "Correctly connected to CORE; we are the peer %s.\n",
117               GNUNET_i2s (my_identity));
118   memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
119 }
120
121
122 static void
123 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
124             const struct GNUNET_ATS_Information *atsi, unsigned int atsi_count)
125 {
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
127               GNUNET_i2s (peer));
128   if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
129   {
130     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131                 "Connected to myself; sending message!\n");
132     GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0,
133                                        GNUNET_TIME_UNIT_FOREVER_REL, peer,
134                                        sizeof (struct GNUNET_MessageHeader),
135                                        send_message, NULL);
136   }
137 }
138
139
140 /**
141  * Main function that will be run by the scheduler.
142  *
143  * @param cls closure
144  * @param cfg configuration
145  */
146 static void
147 run (void *cls,
148      const struct GNUNET_CONFIGURATION_Handle *cfg,
149      struct GNUNET_TESTING_Peer *peer)
150 {
151   const static struct GNUNET_CORE_MessageHandler handlers[] = {
152     {&receive, GNUNET_MESSAGE_TYPE_DUMMY, 0},
153     {NULL, 0, 0}
154   };
155   core =
156     GNUNET_CORE_connect (cfg, NULL, &init, &connect_cb, NULL, NULL,
157                          0, NULL, 0, handlers); 
158   die_task =
159       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
160                                     (GNUNET_TIME_UNIT_SECONDS, 300), &cleanup,
161                                     NULL);
162 }
163
164
165 /**
166  * The main function to test sending a message to the local peer via core
167  *
168  * @param argc number of arguments from the command line
169  * @param argv command line arguments
170  * @return 0 ok, 1 on error
171  */
172 int
173 main (int argc, char *argv[])
174 {
175   if (0 != GNUNET_TESTING_peer_run ("test-core-api-send-to-self",
176                                     "test_core_api_peer1.conf",
177                                     &run, NULL))
178     return 1;
179   return ret;
180 }
181
182 /* end of test_core_api_send_to_self.c */