diff options
author | Christian Grothoff <christian@grothoff.org> | 2014-12-03 15:37:28 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2014-12-03 15:37:28 +0000 |
commit | 0de7931d8c76cccdfc98290c89fd91a502e5f012 (patch) | |
tree | 5a32a7f33e9665f4ea0675216b462d8a15a1d462 /src/scalarproduct/gnunet-scalarproduct.c | |
parent | 7c4b0e7fb12bdf7e6cadfa4cf8e97ee84f128ce0 (diff) | |
download | gnunet-0de7931d8c76cccdfc98290c89fd91a502e5f012.tar.gz gnunet-0de7931d8c76cccdfc98290c89fd91a502e5f012.zip |
-simplify logic
Diffstat (limited to 'src/scalarproduct/gnunet-scalarproduct.c')
-rw-r--r-- | src/scalarproduct/gnunet-scalarproduct.c | 183 |
1 files changed, 89 insertions, 94 deletions
diff --git a/src/scalarproduct/gnunet-scalarproduct.c b/src/scalarproduct/gnunet-scalarproduct.c index 88069d41d..c6ed365f8 100644 --- a/src/scalarproduct/gnunet-scalarproduct.c +++ b/src/scalarproduct/gnunet-scalarproduct.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2013 Christian Grothoff (and other contributing authors) | 3 | (C) 2013, 2014 Christian Grothoff (and other contributing authors) |
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 |
@@ -34,23 +34,17 @@ | |||
34 | #include "scalarproduct.h" | 34 | #include "scalarproduct.h" |
35 | 35 | ||
36 | #define LOG(kind,...) GNUNET_log_from (kind, "gnunet-scalarproduct",__VA_ARGS__) | 36 | #define LOG(kind,...) GNUNET_log_from (kind, "gnunet-scalarproduct",__VA_ARGS__) |
37 | #define INPUTSTRINGLENGTH 1024 | 37 | |
38 | 38 | ||
39 | /** | 39 | /** |
40 | * A primitive closure structure holding information about our session | 40 | * the session key identifying this computation |
41 | */ | 41 | */ |
42 | struct ScalarProductCallbackClosure | 42 | static struct GNUNET_HashCode session_key; |
43 | { | ||
44 | /** | ||
45 | * the session key identifying this computation | ||
46 | */ | ||
47 | struct GNUNET_HashCode session_key; | ||
48 | 43 | ||
49 | /** | 44 | /** |
50 | * PeerID we want to compute a scalar product with | 45 | * PeerID we want to compute a scalar product with |
51 | */ | 46 | */ |
52 | struct GNUNET_PeerIdentity peer_id; | 47 | static struct GNUNET_PeerIdentity peer_id; |
53 | }; | ||
54 | 48 | ||
55 | /** | 49 | /** |
56 | * Option -p: destination peer identity for checking message-ids with | 50 | * Option -p: destination peer identity for checking message-ids with |
@@ -75,7 +69,8 @@ static int ret = -1; | |||
75 | /** | 69 | /** |
76 | * our Scalarproduct Computation handle | 70 | * our Scalarproduct Computation handle |
77 | */ | 71 | */ |
78 | struct GNUNET_SCALARPRODUCT_ComputationHandle * computation; | 72 | static struct GNUNET_SCALARPRODUCT_ComputationHandle *computation; |
73 | |||
79 | 74 | ||
80 | /** | 75 | /** |
81 | * Callback called if we are initiating a new computation session | 76 | * Callback called if we are initiating a new computation session |
@@ -87,39 +82,37 @@ static void | |||
87 | responder_callback (void *cls, | 82 | responder_callback (void *cls, |
88 | enum GNUNET_SCALARPRODUCT_ResponseStatus status) | 83 | enum GNUNET_SCALARPRODUCT_ResponseStatus status) |
89 | { | 84 | { |
90 | struct ScalarProductCallbackClosure * closure = cls; | ||
91 | |||
92 | switch (status) | 85 | switch (status) |
93 | { | 86 | { |
94 | case GNUNET_SCALARPRODUCT_Status_Success: | 87 | case GNUNET_SCALARPRODUCT_Status_Success: |
95 | ret = 0; | 88 | ret = 0; |
96 | LOG (GNUNET_ERROR_TYPE_INFO, | 89 | LOG (GNUNET_ERROR_TYPE_INFO, |
97 | "Session %s concluded.\n", | 90 | "Session %s concluded.\n", |
98 | GNUNET_h2s (&closure->session_key)); | 91 | GNUNET_h2s (&session_key)); |
99 | break; | 92 | break; |
100 | case GNUNET_SCALARPRODUCT_Status_InvalidResponse: | 93 | case GNUNET_SCALARPRODUCT_Status_InvalidResponse: |
101 | LOG (GNUNET_ERROR_TYPE_ERROR, | 94 | LOG (GNUNET_ERROR_TYPE_ERROR, |
102 | "Session %s failed: invalid response\n", | 95 | "Session %s failed: invalid response\n", |
103 | GNUNET_h2s (&closure->session_key)); | 96 | GNUNET_h2s (&session_key)); |
104 | break; | 97 | break; |
105 | case GNUNET_SCALARPRODUCT_Status_Failure: | 98 | case GNUNET_SCALARPRODUCT_Status_Failure: |
106 | LOG (GNUNET_ERROR_TYPE_ERROR, | 99 | LOG (GNUNET_ERROR_TYPE_ERROR, |
107 | "Session %s failed: service failure\n", | 100 | "Session %s failed: service failure\n", |
108 | GNUNET_h2s (&closure->session_key)); | 101 | GNUNET_h2s (&session_key)); |
109 | break; | 102 | break; |
110 | case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected: | 103 | case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected: |
111 | LOG (GNUNET_ERROR_TYPE_ERROR, | 104 | LOG (GNUNET_ERROR_TYPE_ERROR, |
112 | "Session %s failed: service disconnect!\n", | 105 | "Session %s failed: service disconnect!\n", |
113 | GNUNET_h2s (&closure->session_key)); | 106 | GNUNET_h2s (&session_key)); |
114 | break; | 107 | break; |
115 | default: | 108 | default: |
116 | LOG (GNUNET_ERROR_TYPE_ERROR, | 109 | LOG (GNUNET_ERROR_TYPE_ERROR, |
117 | "Session %s failed: return code %d\n", | 110 | "Session %s failed: return code %d\n", |
118 | GNUNET_h2s (&closure->session_key), | 111 | GNUNET_h2s (&session_key), |
119 | status); | 112 | status); |
120 | } | 113 | } |
121 | computation = NULL; | 114 | computation = NULL; |
122 | GNUNET_SCHEDULER_shutdown(); | 115 | GNUNET_SCHEDULER_shutdown (); |
123 | } | 116 | } |
124 | 117 | ||
125 | 118 | ||
@@ -135,8 +128,7 @@ requester_callback (void *cls, | |||
135 | enum GNUNET_SCALARPRODUCT_ResponseStatus status, | 128 | enum GNUNET_SCALARPRODUCT_ResponseStatus status, |
136 | gcry_mpi_t result) | 129 | gcry_mpi_t result) |
137 | { | 130 | { |
138 | struct ScalarProductCallbackClosure * closure = cls; | 131 | unsigned char *buf; |
139 | unsigned char * buf; | ||
140 | gcry_error_t rc; | 132 | gcry_error_t rc; |
141 | 133 | ||
142 | switch (status) | 134 | switch (status) |
@@ -155,30 +147,30 @@ requester_callback (void *cls, | |||
155 | case GNUNET_SCALARPRODUCT_Status_InvalidResponse: | 147 | case GNUNET_SCALARPRODUCT_Status_InvalidResponse: |
156 | LOG (GNUNET_ERROR_TYPE_ERROR, | 148 | LOG (GNUNET_ERROR_TYPE_ERROR, |
157 | "Session %s with peer %s failed: invalid response received\n", | 149 | "Session %s with peer %s failed: invalid response received\n", |
158 | GNUNET_h2s (&closure->session_key), | 150 | GNUNET_h2s (&session_key), |
159 | GNUNET_i2s (&closure->peer_id)); | 151 | GNUNET_i2s (&peer_id)); |
160 | break; | 152 | break; |
161 | case GNUNET_SCALARPRODUCT_Status_Failure: | 153 | case GNUNET_SCALARPRODUCT_Status_Failure: |
162 | LOG (GNUNET_ERROR_TYPE_ERROR, | 154 | LOG (GNUNET_ERROR_TYPE_ERROR, |
163 | "Session %s with peer %s failed: API failure\n", | 155 | "Session %s with peer %s failed: API failure\n", |
164 | GNUNET_h2s (&closure->session_key), | 156 | GNUNET_h2s (&session_key), |
165 | GNUNET_i2s (&closure->peer_id)); | 157 | GNUNET_i2s (&peer_id)); |
166 | break; | 158 | break; |
167 | case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected: | 159 | case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected: |
168 | LOG (GNUNET_ERROR_TYPE_ERROR, | 160 | LOG (GNUNET_ERROR_TYPE_ERROR, |
169 | "Session %s with peer %s was disconnected from service.\n", | 161 | "Session %s with peer %s was disconnected from service.\n", |
170 | GNUNET_h2s (&closure->session_key), | 162 | GNUNET_h2s (&session_key), |
171 | GNUNET_i2s (&closure->peer_id)); | 163 | GNUNET_i2s (&peer_id)); |
172 | break; | 164 | break; |
173 | default: | 165 | default: |
174 | LOG (GNUNET_ERROR_TYPE_ERROR, | 166 | LOG (GNUNET_ERROR_TYPE_ERROR, |
175 | "Session %s with peer %s failed: return code %d\n", | 167 | "Session %s with peer %s failed: return code %d\n", |
176 | GNUNET_h2s (&closure->session_key), | 168 | GNUNET_h2s (&session_key), |
177 | GNUNET_i2s (&closure->peer_id), | 169 | GNUNET_i2s (&peer_id), |
178 | status); | 170 | status); |
179 | } | 171 | } |
180 | computation = NULL; | 172 | computation = NULL; |
181 | GNUNET_SCHEDULER_shutdown(); | 173 | GNUNET_SCHEDULER_shutdown (); |
182 | } | 174 | } |
183 | 175 | ||
184 | 176 | ||
@@ -193,7 +185,10 @@ shutdown_task (void *cls, | |||
193 | const struct GNUNET_SCHEDULER_TaskContext *tc) | 185 | const struct GNUNET_SCHEDULER_TaskContext *tc) |
194 | { | 186 | { |
195 | if (NULL != computation) | 187 | if (NULL != computation) |
196 | GNUNET_SCALARPRODUCT_cancel(computation); | 188 | { |
189 | GNUNET_SCALARPRODUCT_cancel (computation); | ||
190 | ret = 1; /* aborted */ | ||
191 | } | ||
197 | } | 192 | } |
198 | 193 | ||
199 | 194 | ||
@@ -213,42 +208,35 @@ run (void *cls, | |||
213 | { | 208 | { |
214 | char *begin = input_elements; | 209 | char *begin = input_elements; |
215 | char *end; | 210 | char *end; |
216 | int i; | 211 | unsigned int i; |
217 | struct GNUNET_SCALARPRODUCT_Element * elements; | 212 | struct GNUNET_SCALARPRODUCT_Element *elements; |
218 | uint32_t element_count = 0; | 213 | uint32_t element_count = 0; |
219 | struct ScalarProductCallbackClosure * closure; | ||
220 | 214 | ||
221 | if (NULL == input_elements) | 215 | if (NULL == input_elements) |
222 | { | 216 | { |
223 | LOG (GNUNET_ERROR_TYPE_ERROR, | 217 | LOG (GNUNET_ERROR_TYPE_ERROR, |
224 | _ ("You must specify at least one message ID to check!\n")); | 218 | _("You must specify at least one message ID to check!\n")); |
225 | return; | 219 | return; |
226 | } | 220 | } |
227 | 221 | if ( (NULL == input_session_key) || | |
228 | if (NULL == input_session_key) | 222 | (0 == strlen (input_session_key)) ) |
229 | { | 223 | { |
230 | LOG (GNUNET_ERROR_TYPE_ERROR, | 224 | LOG (GNUNET_ERROR_TYPE_ERROR, |
231 | _ ("This program needs a session identifier for comparing vectors.\n")); | 225 | _("This program needs a session identifier for comparing vectors.\n")); |
232 | return; | 226 | return; |
233 | } | 227 | } |
234 | 228 | GNUNET_CRYPTO_hash (input_session_key, | |
235 | if (1 > strnlen (input_session_key, sizeof (struct GNUNET_HashCode))) | 229 | strlen (input_session_key), |
230 | &session_key); | ||
231 | if ( (NULL != input_peer_id) && | ||
232 | (GNUNET_OK != | ||
233 | GNUNET_CRYPTO_eddsa_public_key_from_string (input_peer_id, | ||
234 | strlen (input_peer_id), | ||
235 | &peer_id.public_key)) ) | ||
236 | { | 236 | { |
237 | LOG (GNUNET_ERROR_TYPE_ERROR, | 237 | LOG (GNUNET_ERROR_TYPE_ERROR, |
238 | _ ("Please give a session key for --input_key!\n")); | 238 | _("Tried to set initiator mode, as peer ID was given. " |
239 | return; | 239 | "However, `%s' is not a valid peer identifier.\n"), |
240 | } | ||
241 | closure = GNUNET_new (struct ScalarProductCallbackClosure); | ||
242 | GNUNET_CRYPTO_hash (input_session_key, strlen (input_session_key), &closure->session_key); | ||
243 | |||
244 | if (input_peer_id && | ||
245 | (GNUNET_OK != | ||
246 | GNUNET_CRYPTO_eddsa_public_key_from_string (input_peer_id, | ||
247 | strlen (input_peer_id), | ||
248 | (struct GNUNET_CRYPTO_EddsaPublicKey *) &closure->peer_id))) { | ||
249 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
250 | _ ("Tried to set initiator mode, as peer ID was given. " | ||
251 | "However, `%s' is not a valid peer identifier.\n"), | ||
252 | input_peer_id); | 240 | input_peer_id); |
253 | return; | 241 | return; |
254 | } | 242 | } |
@@ -258,74 +246,80 @@ run (void *cls, | |||
258 | element_count++; | 246 | element_count++; |
259 | if (0 == element_count) { | 247 | if (0 == element_count) { |
260 | LOG (GNUNET_ERROR_TYPE_ERROR, | 248 | LOG (GNUNET_ERROR_TYPE_ERROR, |
261 | _ ("Need elements to compute the vectorproduct, got none.\n")); | 249 | _("Need elements to compute the vectorproduct, got none.\n")); |
262 | return; | 250 | return; |
263 | } | 251 | } |
264 | 252 | ||
265 | elements = (struct GNUNET_SCALARPRODUCT_Element *) | 253 | elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count); |
266 | GNUNET_malloc(sizeof(struct GNUNET_SCALARPRODUCT_Element)*element_count); | ||
267 | 254 | ||
268 | for (i = 0; i < element_count;i++) | 255 | for (i = 0; i < element_count;i++) |
269 | { | 256 | { |
270 | struct GNUNET_SCALARPRODUCT_Element element; | 257 | struct GNUNET_SCALARPRODUCT_Element element; |
271 | char* separator=NULL; | 258 | char* separator = NULL; |
272 | 259 | ||
273 | // get the length of the current key,value; tupel | 260 | /* get the length of the current key,value; tupel */ |
274 | for (end = begin; *end != ';'; end++) | 261 | for (end = begin; *end != ';'; end++) |
275 | if (*end == ',') | 262 | if (*end == ',') |
276 | separator = end; | 263 | separator = end; |
277 | 264 | ||
278 | // final element | 265 | /* final element */ |
279 | if ((NULL == separator) | 266 | if ( (NULL == separator) || |
280 | || (begin == separator) | 267 | (begin == separator) || |
281 | || (separator == end - 1 )) { | 268 | (separator == end - 1) ) |
269 | { | ||
282 | LOG (GNUNET_ERROR_TYPE_ERROR, | 270 | LOG (GNUNET_ERROR_TYPE_ERROR, |
283 | _ ("Malformed input, could not parse `%s'\n"), begin); | 271 | _("Malformed input, could not parse `%s'\n"), |
284 | GNUNET_free(elements); | 272 | begin); |
273 | GNUNET_free (elements); | ||
285 | return; | 274 | return; |
286 | } | 275 | } |
287 | 276 | ||
288 | // read the element's key | 277 | /* read the element's key */ |
289 | *separator = 0; | 278 | *separator = 0; |
290 | GNUNET_CRYPTO_hash (begin, strlen (begin), &element.key); | 279 | GNUNET_CRYPTO_hash (begin, |
291 | 280 | strlen (begin), | |
292 | // read the element's value | 281 | &element.key); |
293 | if (1 != sscanf (separator+1, "%" SCNd64 ";", &element.value)) | 282 | |
283 | /* read the element's value */ | ||
284 | if (1 != | ||
285 | sscanf (separator + 1, | ||
286 | "%" SCNd64 ";", | ||
287 | &element.value) ) | ||
294 | { | 288 | { |
295 | LOG (GNUNET_ERROR_TYPE_ERROR, | 289 | LOG (GNUNET_ERROR_TYPE_ERROR, |
296 | _ ("Could not convert `%s' to int64_t.\n"), begin); | 290 | _("Could not convert `%s' to int64_t.\n"), |
291 | begin); | ||
297 | GNUNET_free(elements); | 292 | GNUNET_free(elements); |
298 | return; | 293 | return; |
299 | } | 294 | } |
300 | 295 | elements[i] = element; | |
301 | elements[i]=element; | 296 | begin = end + 1; |
302 | begin = end+1; | ||
303 | } | 297 | } |
304 | 298 | ||
305 | if (((NULL != input_peer_id) && | 299 | if ( ( (NULL != input_peer_id) && |
306 | (NULL == (computation = GNUNET_SCALARPRODUCT_start_computation (cfg, | 300 | (NULL == (computation |
307 | &closure->session_key, | 301 | = GNUNET_SCALARPRODUCT_start_computation (cfg, |
308 | &closure->peer_id, | 302 | &session_key, |
309 | elements, element_count, | 303 | &peer_id, |
310 | &requester_callback, | 304 | elements, element_count, |
311 | (void *) &closure)))) | 305 | &requester_callback, |
312 | || | 306 | NULL))) ) || |
313 | ((NULL == input_peer_id) && | 307 | ( (NULL == input_peer_id) && |
314 | (NULL == (computation = GNUNET_SCALARPRODUCT_accept_computation (cfg, | 308 | (NULL == (computation |
315 | &closure->session_key, | 309 | = GNUNET_SCALARPRODUCT_accept_computation (cfg, |
316 | elements, element_count, | 310 | &session_key, |
317 | &responder_callback, | 311 | elements, element_count, |
318 | (void *) &closure))))) | 312 | &responder_callback, |
313 | NULL))) ) ) | ||
319 | { | 314 | { |
315 | GNUNET_break (0); | ||
320 | GNUNET_free (elements); | 316 | GNUNET_free (elements); |
321 | return; | 317 | return; |
322 | } | 318 | } |
323 | |||
324 | GNUNET_free (elements); | 319 | GNUNET_free (elements); |
325 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | 320 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, |
326 | &shutdown_task, | 321 | &shutdown_task, |
327 | NULL); | 322 | NULL); |
328 | |||
329 | ret = 0; | 323 | ret = 0; |
330 | } | 324 | } |
331 | 325 | ||
@@ -361,3 +355,4 @@ main (int argc, char *const *argv) | |||
361 | options, &run, NULL)) ? ret : 1; | 355 | options, &run, NULL)) ? ret : 1; |
362 | } | 356 | } |
363 | 357 | ||
358 | /* end of gnunet-scalarproduct.c */ | ||