aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_hosts.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-10-11 20:45:08 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-10-11 20:45:08 +0000
commit3e820b51780e04f85a341d1f9780e15b467cc08f (patch)
tree2d26cf0f84909ddcdd762d5ea65864347a43f05f /src/testbed/testbed_api_hosts.c
parent99629a9c829ae596f84ac0969cff26cb0f649ba9 (diff)
downloadgnunet-3e820b51780e04f85a341d1f9780e15b467cc08f.tar.gz
gnunet-3e820b51780e04f85a341d1f9780e15b467cc08f.zip
host loading from file
Diffstat (limited to 'src/testbed/testbed_api_hosts.c')
-rw-r--r--src/testbed/testbed_api_hosts.c96
1 files changed, 81 insertions, 15 deletions
diff --git a/src/testbed/testbed_api_hosts.c b/src/testbed/testbed_api_hosts.c
index 989e7d1d8..91fffc92d 100644
--- a/src/testbed/testbed_api_hosts.c
+++ b/src/testbed/testbed_api_hosts.c
@@ -295,18 +295,81 @@ unsigned int
295GNUNET_TESTBED_hosts_load_from_file (const char *filename, 295GNUNET_TESTBED_hosts_load_from_file (const char *filename,
296 struct GNUNET_TESTBED_Host ***hosts) 296 struct GNUNET_TESTBED_Host ***hosts)
297{ 297{
298#if 0 298 //struct GNUNET_TESTBED_Host **host_array;
299 struct GNUNET_TESTBED_Host **host_array; 299 struct GNUNET_TESTBED_Host *starting_host;
300 300 char *data;
301 host_array = GNUNET_malloc (num_hosts * sizeof (struct GNUNET_TESTBED_Host *)); 301 char *buf;
302 host_array[foo] = GNUNET_TESTBED_host_create (FIXME); 302 char *username;
303 *hosts = host_array; 303 char *hostname;
304 304 uint64_t fs;
305 return num_hosts; 305 short int port;
306#endif 306 int ret;
307 // see testing_group.c, GNUNET_TESTING_hosts_load 307 unsigned int offset;
308 GNUNET_break (0); 308 unsigned int count;
309 return 0; 309
310
311 GNUNET_assert (NULL != filename);
312 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
313 {
314 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
315 return 0;
316 }
317 if (GNUNET_OK !=
318 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
319 fs = 0;
320 if (0 == fs)
321 {
322 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
323 return 0;
324 }
325 data = GNUNET_malloc (fs);
326 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
327 {
328 GNUNET_free (data);
329 LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
330 filename);
331 return 0;
332 }
333 buf = data;
334 offset = 0;
335 starting_host = NULL;
336 while (offset < (fs - 1))
337 {
338 offset++;
339 if (((data[offset] == '\n')) && (buf != &data[offset]))
340 {
341 data[offset] = '\0';
342 ret = SSCANF (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
343 &username, &hostname, &port);
344 if (3 == ret)
345 {
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Successfully read host %s, port %d and user %s from file\n",
348 hostname, port, username);
349 /* We store hosts in a static list; hence we only require the starting
350 host pointer in that list to get the newly created list of hosts */
351 if (NULL == starting_host)
352 starting_host = GNUNET_TESTBED_host_create (hostname, username,
353 port);
354 else
355 (void) GNUNET_TESTBED_host_create (hostname, username, port);
356 count++;
357 GNUNET_free (hostname);
358 GNUNET_free (username);
359 }
360 else
361 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
362 "Error reading line `%s' in hostfile\n", buf);
363 buf = &data[offset + 1];
364 }
365 else if ((data[offset] == '\n') || (data[offset] == '\0'))
366 buf = &data[offset + 1];
367 }
368 GNUNET_free (data);
369 if (NULL == starting_host)
370 return 0;
371 *hosts = &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)];
372 return count;
310} 373}
311 374
312 375
@@ -321,6 +384,7 @@ GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
321{ 384{
322 struct RegisteredController *rc; 385 struct RegisteredController *rc;
323 uint32_t id; 386 uint32_t id;
387 uint32_t orig_size;
324 388
325 GNUNET_assert (host->id < host_list_size); 389 GNUNET_assert (host->id < host_list_size);
326 GNUNET_assert (host_list[host->id] == host); 390 GNUNET_assert (host_list[host->id] == host);
@@ -334,6 +398,7 @@ GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
334 GNUNET_free_non_null ((char *) host->username); 398 GNUNET_free_non_null ((char *) host->username);
335 GNUNET_free_non_null ((char *) host->hostname); 399 GNUNET_free_non_null ((char *) host->hostname);
336 GNUNET_free (host); 400 GNUNET_free (host);
401 orig_size = host_list_size;
337 while (host_list_size >= HOST_LIST_GROW_STEP) 402 while (host_list_size >= HOST_LIST_GROW_STEP)
338 { 403 {
339 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP; 404 for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
@@ -346,9 +411,10 @@ GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
346 break; 411 break;
347 host_list_size -= HOST_LIST_GROW_STEP; 412 host_list_size -= HOST_LIST_GROW_STEP;
348 } 413 }
349 host_list = 414 if (orig_size != host_list_size)
350 GNUNET_realloc (host_list, 415 host_list =
351 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size); 416 GNUNET_realloc (host_list,
417 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
352} 418}
353 419
354 420