diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-02-07 18:27:41 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-02-07 18:27:41 +0000 |
commit | 71fae9884ef8e28a3baf09e5ab08747185c263f1 (patch) | |
tree | 04050947e1db581accc82a4ee5c9d2f5dac431dc /src | |
parent | a1263aac5a46d05279c715d791b2977f7439cefb (diff) | |
download | gnunet-71fae9884ef8e28a3baf09e5ab08747185c263f1.tar.gz gnunet-71fae9884ef8e28a3baf09e5ab08747185c263f1.zip |
deduplicate code for quota parsing
Diffstat (limited to 'src')
-rw-r--r-- | src/ats/gnunet-service-ats_plugins.c | 231 |
1 files changed, 110 insertions, 121 deletions
diff --git a/src/ats/gnunet-service-ats_plugins.c b/src/ats/gnunet-service-ats_plugins.c index 900183c1b..79b13fdd1 100644 --- a/src/ats/gnunet-service-ats_plugins.c +++ b/src/ats/gnunet-service-ats_plugins.c | |||
@@ -269,6 +269,108 @@ bandwidth_changed_cb (void *cls, | |||
269 | 269 | ||
270 | 270 | ||
271 | /** | 271 | /** |
272 | * Convert quota from text to numeric value. | ||
273 | * | ||
274 | * @param quota_str the value found in the configuration | ||
275 | * @param direction direction of the quota | ||
276 | * @param network network the quota applies to | ||
277 | * @return numeric quota value to use | ||
278 | */ | ||
279 | static unsigned long long | ||
280 | parse_quota (const char *quota_str, | ||
281 | const char *direction, | ||
282 | enum GNUNET_ATS_Network_Type network) | ||
283 | { | ||
284 | int res; | ||
285 | unsigned long long ret; | ||
286 | |||
287 | res = GNUNET_NO; | ||
288 | if (0 == strcmp (quota_str, GNUNET_ATS_MaxBandwidthString)) | ||
289 | { | ||
290 | ret = GNUNET_ATS_MaxBandwidth; | ||
291 | res = GNUNET_YES; | ||
292 | } | ||
293 | if ((GNUNET_NO == res) && | ||
294 | (GNUNET_OK == | ||
295 | GNUNET_STRINGS_fancy_size_to_bytes (quota_str, | ||
296 | &ret))) | ||
297 | res = GNUNET_YES; | ||
298 | if ((GNUNET_NO == res) && | ||
299 | (1 == | ||
300 | sscanf (quota_str, | ||
301 | "%llu", | ||
302 | &ret))) | ||
303 | res = GNUNET_YES; | ||
304 | if (GNUNET_NO == res) | ||
305 | { | ||
306 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
307 | _("Could not load %s quota for network `%s': `%s', assigning default bandwidth %llu\n"), | ||
308 | direction, | ||
309 | GNUNET_ATS_print_network_type (network), | ||
310 | quota_str, | ||
311 | GNUNET_ATS_DefaultBandwidth); | ||
312 | ret = GNUNET_ATS_DefaultBandwidth; | ||
313 | } | ||
314 | else | ||
315 | { | ||
316 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
317 | _("%s quota configured for network `%s' is %llu\n"), | ||
318 | direction, | ||
319 | GNUNET_ATS_print_network_type (network), | ||
320 | ret); | ||
321 | } | ||
322 | return ret; | ||
323 | } | ||
324 | |||
325 | |||
326 | /** | ||
327 | * Load quota value from the configuration @a cfg for the | ||
328 | * given network @a type and @a direction. | ||
329 | * | ||
330 | * @param cfg configuration to parse | ||
331 | * @param type network type to parse for | ||
332 | * @param direction traffic direction to parse for | ||
333 | * @return quota to apply | ||
334 | */ | ||
335 | static unsigned long long | ||
336 | load_quota (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
337 | enum GNUNET_ATS_Network_Type type, | ||
338 | const char *direction) | ||
339 | { | ||
340 | char *entry; | ||
341 | char *quota_str; | ||
342 | unsigned long long ret; | ||
343 | |||
344 | GNUNET_asprintf (&entry, | ||
345 | "%s_QUOTA_%s", | ||
346 | GNUNET_ATS_print_network_type (type), | ||
347 | direction); | ||
348 | if (GNUNET_OK == | ||
349 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
350 | "ats", | ||
351 | entry, | ||
352 | "a_str)) | ||
353 | { | ||
354 | ret = parse_quota (quota_str, | ||
355 | direction, | ||
356 | type); | ||
357 | GNUNET_free (quota_str); | ||
358 | } | ||
359 | else | ||
360 | { | ||
361 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
362 | _("No %s-quota configured for network `%s', assigning default bandwidth %llu\n"), | ||
363 | direction, | ||
364 | GNUNET_ATS_print_network_type (type), | ||
365 | GNUNET_ATS_DefaultBandwidth); | ||
366 | ret = GNUNET_ATS_DefaultBandwidth; | ||
367 | } | ||
368 | GNUNET_free (entry); | ||
369 | return ret; | ||
370 | } | ||
371 | |||
372 | |||
373 | /** | ||
272 | * Load quotas for networks from configuration | 374 | * Load quotas for networks from configuration |
273 | * | 375 | * |
274 | * @param cfg configuration handle | 376 | * @param cfg configuration handle |
@@ -283,136 +385,23 @@ load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
283 | unsigned long long *in_dest, | 385 | unsigned long long *in_dest, |
284 | int dest_length) | 386 | int dest_length) |
285 | { | 387 | { |
286 | char *entry_in = NULL; | 388 | unsigned int c; |
287 | char *entry_out = NULL; | ||
288 | char *quota_out_str; | ||
289 | char *quota_in_str; | ||
290 | int c; | ||
291 | int res; | ||
292 | 389 | ||
293 | for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++) | 390 | for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++) |
294 | { | 391 | { |
295 | in_dest[c] = 0; | 392 | in_dest[c] = load_quota (cfg, |
296 | out_dest[c] = 0; | 393 | c, |
297 | GNUNET_asprintf (&entry_out, | 394 | "out"); |
298 | "%s_QUOTA_OUT", | 395 | out_dest[c] = load_quota (cfg, |
299 | GNUNET_ATS_print_network_type (c)); | 396 | c, |
300 | GNUNET_asprintf (&entry_in, | 397 | "in"); |
301 | "%s_QUOTA_IN", | ||
302 | GNUNET_ATS_print_network_type (c)); | ||
303 | |||
304 | /* quota out */ | ||
305 | if (GNUNET_OK == | ||
306 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
307 | "ats", | ||
308 | entry_out, | ||
309 | "a_out_str)) | ||
310 | { | ||
311 | res = GNUNET_NO; | ||
312 | if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString)) | ||
313 | { | ||
314 | out_dest[c] = GNUNET_ATS_MaxBandwidth; | ||
315 | res = GNUNET_YES; | ||
316 | } | ||
317 | if ((GNUNET_NO == res) | ||
318 | && (GNUNET_OK | ||
319 | == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, | ||
320 | &out_dest[c]))) | ||
321 | res = GNUNET_YES; | ||
322 | if ((GNUNET_NO == res) | ||
323 | && (GNUNET_OK | ||
324 | == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, | ||
325 | &out_dest[c]))) | ||
326 | res = GNUNET_YES; | ||
327 | |||
328 | if (GNUNET_NO == res) | ||
329 | { | ||
330 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
331 | _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"), | ||
332 | GNUNET_ATS_print_network_type (c), | ||
333 | quota_out_str, | ||
334 | GNUNET_ATS_DefaultBandwidth); | ||
335 | out_dest[c] = GNUNET_ATS_DefaultBandwidth; | ||
336 | } | ||
337 | else | ||
338 | { | ||
339 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
340 | _("Outbound quota configure for network `%s' is %llu\n"), | ||
341 | GNUNET_ATS_print_network_type (c), | ||
342 | out_dest[c]); | ||
343 | } | ||
344 | GNUNET_free (quota_out_str); | ||
345 | } | ||
346 | else | ||
347 | { | ||
348 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
349 | _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"), | ||
350 | GNUNET_ATS_print_network_type (c), | ||
351 | GNUNET_ATS_DefaultBandwidth); | ||
352 | out_dest[c] = GNUNET_ATS_DefaultBandwidth; | ||
353 | } | ||
354 | |||
355 | /* quota in */ | ||
356 | if (GNUNET_OK == | ||
357 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
358 | "ats", | ||
359 | entry_in, | ||
360 | "a_in_str)) | ||
361 | { | ||
362 | res = GNUNET_NO; | ||
363 | if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString)) | ||
364 | { | ||
365 | in_dest[c] = GNUNET_ATS_MaxBandwidth; | ||
366 | res = GNUNET_YES; | ||
367 | } | ||
368 | if ((GNUNET_NO == res) && | ||
369 | (GNUNET_OK == | ||
370 | GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, | ||
371 | &in_dest[c]))) | ||
372 | res = GNUNET_YES; | ||
373 | if ((GNUNET_NO == res) && | ||
374 | (GNUNET_OK == | ||
375 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
376 | "ats", | ||
377 | entry_in, | ||
378 | &in_dest[c]))) | ||
379 | res = GNUNET_YES; | ||
380 | |||
381 | if (GNUNET_NO == res) | ||
382 | { | ||
383 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
384 | _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"), | ||
385 | GNUNET_ATS_print_network_type (c), | ||
386 | quota_in_str, | ||
387 | GNUNET_ATS_DefaultBandwidth); | ||
388 | in_dest[c] = GNUNET_ATS_DefaultBandwidth; | ||
389 | } | ||
390 | else | ||
391 | { | ||
392 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
393 | _("Inbound quota configured for network `%s' is %llu\n"), | ||
394 | GNUNET_ATS_print_network_type (c), | ||
395 | in_dest[c]); | ||
396 | } | ||
397 | GNUNET_free (quota_in_str); | ||
398 | } | ||
399 | else | ||
400 | { | ||
401 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
402 | _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"), | ||
403 | GNUNET_ATS_print_network_type (c), | ||
404 | GNUNET_ATS_DefaultBandwidth); | ||
405 | in_dest[c] = GNUNET_ATS_DefaultBandwidth; | ||
406 | } | ||
407 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 398 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
408 | "Loaded quota for network `%s' (in/out): %llu %llu\n", | 399 | "Loaded quota for network `%s' (in/out): %llu %llu\n", |
409 | GNUNET_ATS_print_network_type (c), | 400 | GNUNET_ATS_print_network_type (c), |
410 | in_dest[c], | 401 | in_dest[c], |
411 | out_dest[c]); | 402 | out_dest[c]); |
412 | GNUNET_free(entry_out); | ||
413 | GNUNET_free(entry_in); | ||
414 | } | 403 | } |
415 | return GNUNET_ATS_NetworkTypeCount; | 404 | return c; |
416 | } | 405 | } |
417 | 406 | ||
418 | 407 | ||