-trying to fix double-cancel
[oweals/gnunet.git] / src / core / test_core_api_mq.c
1 /*
2   This file is part of GNUnet.
3   (C) 2013 Christian Grothoff (and other contributing authors)
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 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testing_lib.h"
24 #include "gnunet_core_service.h"
25
26
27 #define NUM_MSG 5
28
29 /**
30  * Has the test been successful?
31  */
32 int result;
33
34 unsigned int num_received;
35
36 struct GNUNET_CORE_Handle *core;
37
38 struct GNUNET_MQ_Handle *mq;
39
40 struct GNUNET_PeerIdentity myself;
41
42
43 static void
44 init_cb (void *cls,
45          const struct GNUNET_PeerIdentity *my_identity)
46 {
47   if (NULL == my_identity)
48   {
49     GNUNET_break (0);
50     return;
51   }
52   myself = *my_identity;
53   mq = GNUNET_CORE_mq_create (core, my_identity);
54 }
55
56
57 static void
58 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
59 {
60   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
61               GNUNET_i2s (peer));
62   if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
63   {
64     unsigned int i;
65     struct GNUNET_MQ_Envelope *ev;
66     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing messages.\n");
67     for (i = 0; i < NUM_MSG; i++)
68     { 
69       ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_TEST);
70       GNUNET_MQ_send (mq, ev);
71     }
72   }
73 }
74
75
76 static int
77 handle_test (void *cls,
78              const struct GNUNET_PeerIdentity *other,
79              const struct GNUNET_MessageHeader *message)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got test message %d\n", num_received);
82   num_received++;
83   if (NUM_MSG == num_received)
84   {
85     result = GNUNET_OK;
86     GNUNET_SCHEDULER_shutdown ();
87     return GNUNET_SYSERR;
88   }
89   if (num_received > NUM_MSG)
90   {
91     GNUNET_abort ();
92     return GNUNET_SYSERR;
93   }
94   return GNUNET_OK;
95 }
96
97
98 static void
99 shutdown_task (void *cls,
100                const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
103   GNUNET_MQ_destroy (mq);
104   GNUNET_CORE_disconnect (core);
105 }
106
107
108 /**
109  * Initialize framework and start test
110  * 
111  * @param cls Closure (unused).
112  * @param cfg Configuration handle.
113  * @param peer Testing peer handle.
114  */
115 static void
116 run (void *cls, 
117      const struct GNUNET_CONFIGURATION_Handle *cfg,
118      struct GNUNET_TESTING_Peer *peer)
119 {
120   static const struct GNUNET_CORE_MessageHandler handlers[] = {
121     {&handle_test, GNUNET_MESSAGE_TYPE_TEST, 0},
122     {NULL, 0, 0}
123   };
124   core = GNUNET_CORE_connect (cfg,
125                               NULL, &init_cb, &connect_cb, NULL,
126                               NULL, GNUNET_NO, NULL,
127                               GNUNET_NO, handlers);
128   if (NULL == core)
129   {
130     GNUNET_abort ();
131     return;
132   }
133   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
134 }
135
136 int
137 main (int argc, char *argv1[])
138 {
139   if (0 != GNUNET_TESTING_peer_run ("test-core-api-mq",
140                                     "test_core_api_peer1.conf",
141                                     &run, NULL))
142     return 2;
143   return (result == GNUNET_OK) ? 0 : 1;
144 }