diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-19 21:29:20 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-19 21:29:20 +0000 |
commit | 8b1a351c049deec3226aa40a883b97c76916bea7 (patch) | |
tree | d507edb09031a9c9025ad5eb927fc1a9c36452d7 /src/core/test_core_api_send_to_self.c | |
parent | 80c4558f3742ce78f1f511ea13d8941c46b0e88b (diff) | |
download | gnunet-8b1a351c049deec3226aa40a883b97c76916bea7.tar.gz gnunet-8b1a351c049deec3226aa40a883b97c76916bea7.zip |
refactoring core API to use new MQ lib
Diffstat (limited to 'src/core/test_core_api_send_to_self.c')
-rw-r--r-- | src/core/test_core_api_send_to_self.c | 99 |
1 files changed, 66 insertions, 33 deletions
diff --git a/src/core/test_core_api_send_to_self.c b/src/core/test_core_api_send_to_self.c index 54dd29ca8..4889a038f 100644 --- a/src/core/test_core_api_send_to_self.c +++ b/src/core/test_core_api_send_to_self.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2010 Christian Grothoff | 3 | Copyright (C) 2010, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -20,7 +20,7 @@ | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file core/test_core_api_send_to_self.c | 22 | * @file core/test_core_api_send_to_self.c |
23 | * @brief | 23 | * @brief test that sending a message to ourselves via CORE works |
24 | * @author Philipp Toelke | 24 | * @author Philipp Toelke |
25 | */ | 25 | */ |
26 | #include "platform.h" | 26 | #include "platform.h" |
@@ -38,7 +38,7 @@ static int ret; | |||
38 | /** | 38 | /** |
39 | * Handle to the cleanup task. | 39 | * Handle to the cleanup task. |
40 | */ | 40 | */ |
41 | struct GNUNET_SCHEDULER_Task * die_task; | 41 | static struct GNUNET_SCHEDULER_Task *die_task; |
42 | 42 | ||
43 | /** | 43 | /** |
44 | * Identity of this peer. | 44 | * Identity of this peer. |
@@ -48,7 +48,7 @@ static struct GNUNET_PeerIdentity myself; | |||
48 | /** | 48 | /** |
49 | * The handle to core | 49 | * The handle to core |
50 | */ | 50 | */ |
51 | struct GNUNET_CORE_Handle *core; | 51 | static struct GNUNET_CORE_Handle *core; |
52 | 52 | ||
53 | 53 | ||
54 | /** | 54 | /** |
@@ -57,45 +57,64 @@ struct GNUNET_CORE_Handle *core; | |||
57 | static void | 57 | static void |
58 | cleanup (void *cls) | 58 | cleanup (void *cls) |
59 | { | 59 | { |
60 | die_task = NULL; | 60 | if (NULL != die_task) |
61 | 61 | { | |
62 | if (core != NULL) | 62 | GNUNET_SCHEDULER_cancel (die_task); |
63 | die_task = NULL; | ||
64 | } | ||
65 | if (NULL != core) | ||
63 | { | 66 | { |
64 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n"); | ||
65 | GNUNET_CORE_disconnect (core); | 67 | GNUNET_CORE_disconnect (core); |
66 | core = NULL; | 68 | core = NULL; |
67 | } | 69 | } |
68 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n"); | 70 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
71 | "Ending test.\n"); | ||
72 | } | ||
73 | |||
74 | |||
75 | /** | ||
76 | * Function scheduled as very last function, cleans up after us | ||
77 | */ | ||
78 | static void | ||
79 | do_timeout (void *cls) | ||
80 | { | ||
81 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
82 | "Test timeout.\n"); | ||
83 | die_task = NULL; | ||
84 | GNUNET_SCHEDULER_shutdown (); | ||
69 | } | 85 | } |
70 | 86 | ||
71 | 87 | ||
72 | static int | 88 | static int |
73 | receive (void *cls, const struct GNUNET_PeerIdentity *other, | 89 | receive (void *cls, |
90 | const struct GNUNET_PeerIdentity *other, | ||
74 | const struct GNUNET_MessageHeader *message) | 91 | const struct GNUNET_MessageHeader *message) |
75 | { | 92 | { |
76 | if (die_task != NULL) | 93 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
77 | GNUNET_SCHEDULER_cancel (die_task); | 94 | "Received message from peer %s\n", |
78 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n", | ||
79 | GNUNET_i2s (other)); | 95 | GNUNET_i2s (other)); |
80 | GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (message->type)); | 96 | GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (message->type)); |
81 | GNUNET_assert (0 == memcmp (other, &myself, sizeof (myself))); | 97 | GNUNET_assert (0 == memcmp (other, &myself, sizeof (myself))); |
82 | GNUNET_SCHEDULER_add_now (&cleanup, NULL); | 98 | GNUNET_SCHEDULER_shutdown (); |
83 | ret = 0; | 99 | ret = 0; |
84 | return GNUNET_OK; | 100 | return GNUNET_OK; |
85 | } | 101 | } |
86 | 102 | ||
87 | 103 | ||
88 | static size_t | 104 | static size_t |
89 | send_message (void *cls, size_t size, void *buf) | 105 | send_message (void *cls, |
106 | size_t size, | ||
107 | void *buf) | ||
90 | { | 108 | { |
91 | if (size == 0 || buf == NULL) | 109 | struct GNUNET_MessageHeader *hdr = buf; |
110 | if ( (size == 0) || (buf == NULL) ) | ||
92 | { | 111 | { |
93 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n"); | 112 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
113 | "Could not send; got 0 buffer\n"); | ||
94 | return 0; | 114 | return 0; |
95 | } | 115 | } |
96 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending!\n"); | 116 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
97 | struct GNUNET_MessageHeader *hdr = buf; | 117 | "Sending!\n"); |
98 | |||
99 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | 118 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); |
100 | hdr->type = htons (GNUNET_MESSAGE_TYPE_DUMMY); | 119 | hdr->type = htons (GNUNET_MESSAGE_TYPE_DUMMY); |
101 | return ntohs (hdr->size); | 120 | return ntohs (hdr->size); |
@@ -114,23 +133,32 @@ init (void *cls, | |||
114 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 133 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
115 | "Correctly connected to CORE; we are the peer %s.\n", | 134 | "Correctly connected to CORE; we are the peer %s.\n", |
116 | GNUNET_i2s (my_identity)); | 135 | GNUNET_i2s (my_identity)); |
117 | memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity)); | 136 | memcpy (&myself, |
137 | my_identity, | ||
138 | sizeof (struct GNUNET_PeerIdentity)); | ||
118 | } | 139 | } |
119 | 140 | ||
120 | 141 | ||
121 | static void | 142 | static void |
122 | connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) | 143 | connect_cb (void *cls, |
144 | const struct GNUNET_PeerIdentity *peer) | ||
123 | { | 145 | { |
124 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n", | 146 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
147 | "Connected to peer %s.\n", | ||
125 | GNUNET_i2s (peer)); | 148 | GNUNET_i2s (peer)); |
126 | if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity))) | 149 | if (0 == memcmp (peer, |
150 | &myself, | ||
151 | sizeof (struct GNUNET_PeerIdentity))) | ||
127 | { | 152 | { |
128 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 153 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
129 | "Connected to myself; sending message!\n"); | 154 | "Connected to myself; sending message!\n"); |
130 | GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0, | 155 | GNUNET_CORE_notify_transmit_ready (core, |
131 | GNUNET_TIME_UNIT_FOREVER_REL, peer, | 156 | GNUNET_YES, |
157 | 0, | ||
158 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
159 | peer, | ||
132 | sizeof (struct GNUNET_MessageHeader), | 160 | sizeof (struct GNUNET_MessageHeader), |
133 | send_message, NULL); | 161 | &send_message, NULL); |
134 | } | 162 | } |
135 | } | 163 | } |
136 | 164 | ||
@@ -147,16 +175,20 @@ run (void *cls, | |||
147 | struct GNUNET_TESTING_Peer *peer) | 175 | struct GNUNET_TESTING_Peer *peer) |
148 | { | 176 | { |
149 | const static struct GNUNET_CORE_MessageHandler handlers[] = { | 177 | const static struct GNUNET_CORE_MessageHandler handlers[] = { |
150 | {&receive, GNUNET_MESSAGE_TYPE_DUMMY, 0}, | 178 | { &receive, |
179 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
180 | sizeof (struct GNUNET_MessageHeader) }, | ||
151 | {NULL, 0, 0} | 181 | {NULL, 0, 0} |
152 | }; | 182 | }; |
153 | core = | 183 | core = |
154 | GNUNET_CORE_connect (cfg, NULL, &init, &connect_cb, NULL, NULL, | 184 | GNUNET_CORE_connect (cfg, NULL, &init, |
185 | &connect_cb, NULL, NULL, | ||
155 | 0, NULL, 0, handlers); | 186 | 0, NULL, 0, handlers); |
156 | die_task = | 187 | GNUNET_SCHEDULER_add_shutdown (&cleanup, |
157 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | 188 | NULL); |
158 | (GNUNET_TIME_UNIT_SECONDS, 300), &cleanup, | 189 | die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, |
159 | NULL); | 190 | &do_timeout, |
191 | NULL); | ||
160 | } | 192 | } |
161 | 193 | ||
162 | 194 | ||
@@ -170,6 +202,7 @@ run (void *cls, | |||
170 | int | 202 | int |
171 | main (int argc, char *argv[]) | 203 | main (int argc, char *argv[]) |
172 | { | 204 | { |
205 | ret = 1; | ||
173 | if (0 != GNUNET_TESTING_peer_run ("test-core-api-send-to-self", | 206 | if (0 != GNUNET_TESTING_peer_run ("test-core-api-send-to-self", |
174 | "test_core_api_peer1.conf", | 207 | "test_core_api_peer1.conf", |
175 | &run, NULL)) | 208 | &run, NULL)) |