diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2012-11-22 17:03:48 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2012-11-22 17:03:48 +0000 |
commit | 3238f6daafe108e9faaa56df8bc4c10ff8af56c2 (patch) | |
tree | 7748e9a4e7dda2282ab74de8bbdddb88c5ed2bfe /src/testbed/testbed_api_topology.c | |
parent | c2d88b4c711713ee4d5511cb30142aa4c7738548 (diff) | |
download | gnunet-3238f6daafe108e9faaa56df8bc4c10ff8af56c2.tar.gz gnunet-3238f6daafe108e9faaa56df8bc4c10ff8af56c2.zip |
topology loading from file
Diffstat (limited to 'src/testbed/testbed_api_topology.c')
-rw-r--r-- | src/testbed/testbed_api_topology.c | 167 |
1 files changed, 164 insertions, 3 deletions
diff --git a/src/testbed/testbed_api_topology.c b/src/testbed/testbed_api_topology.c index a49cabd46..d2ecdf4de 100644 --- a/src/testbed/testbed_api_topology.c +++ b/src/testbed/testbed_api_topology.c | |||
@@ -502,9 +502,6 @@ gen_topo_random (struct TopologyContext *tc, unsigned int links, int append) | |||
502 | * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999. | 502 | * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999. |
503 | * | 503 | * |
504 | * @param tc the topology context | 504 | * @param tc the topology context |
505 | * @param links the number of random links to establish | ||
506 | * @param append GNUNET_YES to add links to existing link array; GNUNET_NO to | ||
507 | * create a new link array | ||
508 | */ | 505 | */ |
509 | static void | 506 | static void |
510 | gen_scale_free (struct TopologyContext *tc) | 507 | gen_scale_free (struct TopologyContext *tc) |
@@ -547,6 +544,159 @@ gen_scale_free (struct TopologyContext *tc) | |||
547 | 544 | ||
548 | 545 | ||
549 | /** | 546 | /** |
547 | * Generates topology from the given file | ||
548 | * | ||
549 | * @param tc the topology context | ||
550 | * @param filename the filename of the file containing topology data | ||
551 | */ | ||
552 | static void | ||
553 | gen_topo_from_file (struct TopologyContext *tc, const char *filename) | ||
554 | { | ||
555 | char *data; | ||
556 | char *end; | ||
557 | char *buf; | ||
558 | uint64_t fs; | ||
559 | uint64_t offset; | ||
560 | unsigned long int peer_id; | ||
561 | unsigned long int other_peer_id; | ||
562 | enum ParseState { | ||
563 | |||
564 | /** | ||
565 | * We read the peer index | ||
566 | */ | ||
567 | PEER_INDEX, | ||
568 | |||
569 | /** | ||
570 | * We read the other peer indices | ||
571 | */ | ||
572 | OTHER_PEER_INDEX, | ||
573 | |||
574 | } state; | ||
575 | int status; | ||
576 | |||
577 | status = GNUNET_SYSERR; | ||
578 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
579 | { | ||
580 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Topology file %s not found\n"), filename); | ||
581 | return; | ||
582 | } | ||
583 | if (GNUNET_OK != | ||
584 | GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) | ||
585 | { | ||
586 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Topology file %s has no data\n"), filename); | ||
587 | return; | ||
588 | } | ||
589 | data = GNUNET_malloc (fs); | ||
590 | if (fs != GNUNET_DISK_fn_read (filename, data, fs)) | ||
591 | { | ||
592 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Topology file %s cannot be read\n"), | ||
593 | filename); | ||
594 | goto _exit; | ||
595 | } | ||
596 | |||
597 | offset = 0; | ||
598 | state = PEER_INDEX; | ||
599 | buf = data; | ||
600 | while (offset < fs) | ||
601 | { | ||
602 | if (0 != isspace (data[offset])) | ||
603 | { | ||
604 | offset++; | ||
605 | continue; | ||
606 | } | ||
607 | switch (state) | ||
608 | { | ||
609 | case PEER_INDEX: | ||
610 | buf = strchr (&data[offset], ':'); | ||
611 | if (NULL == buf) | ||
612 | { | ||
613 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
614 | _("Failed to read peer index from toology file: %s"), filename); | ||
615 | goto _exit; | ||
616 | } | ||
617 | *buf = '\0'; | ||
618 | errno = 0; | ||
619 | peer_id = (unsigned int) strtoul (&data[offset], &end, 10); | ||
620 | if (0 != errno) | ||
621 | { | ||
622 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
623 | _("Value in given topology file: %s out of range\n"), filename); | ||
624 | goto _exit; | ||
625 | } | ||
626 | if (&data[offset] == end) | ||
627 | { | ||
628 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
629 | _("Failed to read peer index from topology file: %s"), filename); | ||
630 | goto _exit; | ||
631 | } | ||
632 | if (tc->num_peers <= peer_id) | ||
633 | { | ||
634 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
635 | _("Topology file need more peers than the given ones\n"), | ||
636 | filename); | ||
637 | goto _exit; | ||
638 | } | ||
639 | state = OTHER_PEER_INDEX; | ||
640 | offset += ((unsigned int) (buf - &data[offset])) + 1; | ||
641 | break; | ||
642 | case OTHER_PEER_INDEX: | ||
643 | errno = 0; | ||
644 | other_peer_id = (unsigned int) strtoul (&data[offset], &end, 10); | ||
645 | if (0 != errno) | ||
646 | { | ||
647 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
648 | _("Value in given topology file: %s out of range\n"), filename); | ||
649 | goto _exit; | ||
650 | } | ||
651 | if (&data[offset] == end) | ||
652 | { | ||
653 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
654 | _("Failed to read peer index from topology file: %s"), filename); | ||
655 | goto _exit; | ||
656 | } | ||
657 | if (tc->num_peers <= other_peer_id) | ||
658 | { | ||
659 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
660 | _("Topology file need more peers than the given ones\n"), | ||
661 | filename); | ||
662 | goto _exit; | ||
663 | } | ||
664 | tc->link_array_size++; | ||
665 | tc->link_array = GNUNET_realloc (tc->link_array, | ||
666 | sizeof (struct OverlayLink) * | ||
667 | tc->link_array_size); | ||
668 | offset += end - &data[offset]; | ||
669 | make_link (&tc->link_array[tc->link_array_size - 1], peer_id, | ||
670 | other_peer_id, tc); | ||
671 | while (('\n' != data[offset]) && ('|' != data[offset]) | ||
672 | && (offset < fs)) | ||
673 | offset++; | ||
674 | if ('\n' == data[offset]) | ||
675 | state = PEER_INDEX; | ||
676 | else if ('|' == data[offset]) | ||
677 | { | ||
678 | state = OTHER_PEER_INDEX; | ||
679 | offset++; | ||
680 | } | ||
681 | break; | ||
682 | } | ||
683 | } | ||
684 | status = GNUNET_OK; | ||
685 | |||
686 | _exit: | ||
687 | GNUNET_free (data); | ||
688 | if (GNUNET_OK != status) | ||
689 | { | ||
690 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
691 | "Removing and link data read from the file\n"); | ||
692 | tc->link_array_size = 0; | ||
693 | GNUNET_free_non_null (tc->link_array); | ||
694 | tc->link_array = NULL; | ||
695 | } | ||
696 | } | ||
697 | |||
698 | |||
699 | /** | ||
550 | * Configure overall network topology to have a particular shape. | 700 | * Configure overall network topology to have a particular shape. |
551 | * | 701 | * |
552 | * @param op_cls closure argument to give with the operation event | 702 | * @param op_cls closure argument to give with the operation event |
@@ -686,6 +836,15 @@ GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls, | |||
686 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: | 836 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: |
687 | gen_scale_free (tc); | 837 | gen_scale_free (tc); |
688 | break; | 838 | break; |
839 | case GNUNET_TESTBED_TOPOLOGY_FROM_FILE: | ||
840 | { | ||
841 | const char *filename; | ||
842 | |||
843 | filename = va_arg (va, const char *); | ||
844 | GNUNET_assert (NULL != filename); | ||
845 | gen_topo_from_file (tc, filename); | ||
846 | } | ||
847 | break; | ||
689 | default: | 848 | default: |
690 | GNUNET_break (0); | 849 | GNUNET_break (0); |
691 | GNUNET_free (tc); | 850 | GNUNET_free (tc); |
@@ -715,6 +874,8 @@ GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls, | |||
715 | GNUNET_TESTBED_operation_queue_insert_ | 874 | GNUNET_TESTBED_operation_queue_insert_ |
716 | (c->opq_parallel_topology_config_operations, op); | 875 | (c->opq_parallel_topology_config_operations, op); |
717 | GNUNET_TESTBED_operation_begin_wait_ (op); | 876 | GNUNET_TESTBED_operation_begin_wait_ (op); |
877 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
878 | "Generated %u connections\n", tc->link_array_size); | ||
718 | if (NULL != max_connections) | 879 | if (NULL != max_connections) |
719 | *max_connections = tc->link_array_size; | 880 | *max_connections = tc->link_array_size; |
720 | return op; | 881 | return op; |