aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-05-30 14:26:40 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-05-30 14:26:40 +0000
commitddbe674198e8c37f65d6a954bc820fdc9e871a37 (patch)
treee6e9adc0e0890a336e65dd28125a02d7c4b82e30
parentfd65db578913be3c6612fb8777a179d98af48991 (diff)
downloadgnunet-ddbe674198e8c37f65d6a954bc820fdc9e871a37.tar.gz
gnunet-ddbe674198e8c37f65d6a954bc820fdc9e871a37.zip
refined GNUNET_TESTING_service_run
-rw-r--r--src/testing/testing_new.c176
1 files changed, 110 insertions, 66 deletions
diff --git a/src/testing/testing_new.c b/src/testing/testing_new.c
index 2828e0f16..3a1f9cf60 100644
--- a/src/testing/testing_new.c
+++ b/src/testing/testing_new.c
@@ -168,7 +168,10 @@ GNUNET_TESTING_system_create (const char *tmppath,
168 struct GNUNET_TESTING_System *system; 168 struct GNUNET_TESTING_System *system;
169 169
170 if (NULL == tmppath) 170 if (NULL == tmppath)
171 {
172 LOG (GNUNET_ERROR_TYPE_ERROR, _("tmppath cannot be NULL\n"));
171 return NULL; 173 return NULL;
174 }
172 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System)); 175 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System));
173 system->tmppath = GNUNET_strdup (tmppath); 176 system->tmppath = GNUNET_strdup (tmppath);
174 if (NULL != controller) 177 if (NULL != controller)
@@ -225,6 +228,15 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
225 uint16_t open_port; 228 uint16_t open_port;
226 uint16_t pos; 229 uint16_t pos;
227 230
231 /*
232 FIXME: Instead of using getaddrinfo we should try to determine the port
233 status by the following heurestics.
234
235 On systems which support both IPv4 and IPv6, only ports open on both
236 address families are considered open.
237 On system with either IPv4 or IPv6. A port is considered open if it's
238 open in the respective address family
239 */
228 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ 240 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
229 hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM; 241 hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM;
230 hint.ai_protocol = 0; 242 hint.ai_protocol = 0;
@@ -654,32 +666,44 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
654 char *service_home; 666 char *service_home;
655 char hostkey_filename[128]; 667 char hostkey_filename[128];
656 char *config_filename; 668 char *config_filename;
669 char *emsg_;
657 670
671 if (NULL != emsg)
672 *emsg = NULL;
658 if (GNUNET_OK != GNUNET_TESTING_configuration_create (system, cfg)) 673 if (GNUNET_OK != GNUNET_TESTING_configuration_create (system, cfg))
659 { 674 {
660 GNUNET_asprintf (emsg, 675 GNUNET_asprintf (&emsg_,
661 _("Failed to create configuration for peer (not enough free ports?)\n")); 676 _("Failed to create configuration for peer (not enough free ports?)\n"));
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
663 "%s", *emsg); 678 if (NULL != emsg)
679 *emsg = emsg_;
680 else
681 GNUNET_free (emsg_);
664 return NULL; 682 return NULL;
665 } 683 }
666 if (key_number >= system->total_hostkeys) 684 if (key_number >= system->total_hostkeys)
667 { 685 {
668 GNUNET_asprintf (emsg, 686 GNUNET_asprintf (&emsg_,
669 _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"), 687 _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
670 (unsigned int) system->total_hostkeys); 688 (unsigned int) system->total_hostkeys);
671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 689 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
672 "%s", *emsg); 690 if (NULL != emsg)
691 *emsg = emsg_;
692 else
693 GNUNET_free (emsg_);
673 return NULL; 694 return NULL;
674 } 695 }
675 if ((NULL != id) && 696 if ((NULL != id) &&
676 (GNUNET_SYSERR == GNUNET_TESTING_hostkey_get (system, key_number, id))) 697 (GNUNET_SYSERR == GNUNET_TESTING_hostkey_get (system, key_number, id)))
677 { 698 {
678 GNUNET_asprintf (emsg, 699 GNUNET_asprintf (&emsg_,
679 _("Failed to initialize hostkey for peer %u\n"), 700 _("Failed to initialize hostkey for peer %u\n"),
680 (unsigned int) key_number); 701 (unsigned int) key_number);
681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
682 "%s", *emsg); 703 if (NULL != emsg)
704 *emsg = emsg_;
705 else
706 GNUNET_free (emsg_);
683 return NULL; 707 return NULL;
684 } 708 }
685 GNUNET_assert (GNUNET_OK == 709 GNUNET_assert (GNUNET_OK ==
@@ -702,10 +726,15 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
702 + (key_number * HOSTKEYFILESIZE), 726 + (key_number * HOSTKEYFILESIZE),
703 HOSTKEYFILESIZE)) 727 HOSTKEYFILESIZE))
704 { 728 {
705 GNUNET_asprintf (emsg, 729 GNUNET_asprintf (&emsg_,
706 _("Failed to write hostkey file for peer %u: %s\n"), 730 _("Failed to write hostkey file for peer %u: %s\n"),
707 (unsigned int) key_number, 731 (unsigned int) key_number,
708 STRERROR (errno)); 732 STRERROR (errno));
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
734 if (NULL != emsg)
735 *emsg = emsg_;
736 else
737 GNUNET_free (emsg_);
709 GNUNET_DISK_file_close (fd); 738 GNUNET_DISK_file_close (fd);
710 return NULL; 739 return NULL;
711 } 740 }
@@ -714,11 +743,16 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
714 GNUNET_free (service_home); 743 GNUNET_free (service_home);
715 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config_filename)) 744 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config_filename))
716 { 745 {
717 GNUNET_asprintf (emsg, 746 GNUNET_asprintf (&emsg_,
718 _("Failed to write configuration file `%s' for peer %u: %s\n"), 747 _("Failed to write configuration file `%s' for peer %u: %s\n"),
719 config_filename, 748 config_filename,
720 (unsigned int) key_number, 749 (unsigned int) key_number,
721 STRERROR (errno)); 750 STRERROR (errno));
751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
752 if (NULL != emsg)
753 *emsg = emsg_;
754 else
755 GNUNET_free (emsg_);
722 return NULL; 756 return NULL;
723 } 757 }
724 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Peer)); 758 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Peer));
@@ -833,9 +867,9 @@ GNUNET_TESTING_peer_run (const char *tmppath,
833struct ServiceContext 867struct ServiceContext
834{ 868{
835 /** 869 /**
836 * The service's main process 870 * The configuration of the peer in which the service is run
837 */ 871 */
838 struct GNUNET_OS_Process *main_process; 872 const struct GNUNET_CONFIGURATION_Handle *cfg;
839 873
840 /** 874 /**
841 * Callback to signal service startup 875 * Callback to signal service startup
@@ -849,43 +883,15 @@ struct ServiceContext
849}; 883};
850 884
851 885
852/**
853 * Scheduler callback to stop service upon call to GNUNET_SCHEDULER_shutdown
854 *
855 * @param cls the ServiceContext
856 * @param tc the TaskContext
857 */
858static void
859stop_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
860{
861 GNUNET_break (0);
862}
863
864
865/**
866 * Scheduler callback to stop service upon call to GNUNET_SCHEDULER_shutdown
867 *
868 * @param cls the ServiceContext
869 * @param tc the TaskContext
870 */
871static void
872check_service_status (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
873{
874 GNUNET_break (0);
875}
876
877
878static void 886static void
879service_run_main (void *cls, 887service_run_main (void *cls,
880 const struct GNUNET_SCHEDULER_TaskContext *tc) 888 const struct GNUNET_SCHEDULER_TaskContext *tc)
881{ 889{
882 struct ServiceContext *sc = cls; 890 struct ServiceContext *sc = cls;
883 891
884 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 892 if (NULL == sc->tm)
885 &stop_service, sc); 893 return;
886 GNUNET_SCHEDULER_add_delayed (TIME_REL_SEC(3), &check_service_status, sc); 894 sc->tm (sc->tm_cls, sc->cfg);
887
888 // sc->tm (tm_cls);
889} 895}
890 896
891 897
@@ -915,30 +921,68 @@ GNUNET_TESTING_service_run (const char *tmppath,
915 GNUNET_TESTING_TestMain tm, 921 GNUNET_TESTING_TestMain tm,
916 void *tm_cls) 922 void *tm_cls)
917{ 923{
918 struct ServiceContext *sc; 924 struct ServiceContext sc;
919 char uval[128]; 925 struct GNUNET_TESTING_System *system;
920 926 struct GNUNET_TESTING_Peer *peer;
921 GNUNET_assert (NULL != service_name); 927 struct GNUNET_CONFIGURATION_Handle *cfg;
922 GNUNET_snprintf (uval, sizeof (uval), "gnunet-service-%s", service_name); 928 char *data_dir;
923 sc = GNUNET_malloc (sizeof (struct ServiceContext)); 929 char *hostkeys_file;
924 930
925 // FIXME: GNUNET_TESTING_peer_start... 931 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
926 if (NULL == cfgfilename) 932 GNUNET_asprintf (&hostkeys_file, "%s\\testing_hostkeys.dat", data_dir);
927 sc->main_process = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, uval, NULL); 933 GNUNET_free (data_dir);
928 else 934 system = GNUNET_TESTING_system_create (tmppath, "localhost");
929 sc->main_process = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, uval, 935 if (NULL == system)
930 "-c", cfgfilename, NULL); 936 {
931 if (NULL == sc->main_process) 937 GNUNET_free (hostkeys_file);
938 return 1;
939 }
940 if (GNUNET_OK != GNUNET_TESTING_hostkeys_load (system, hostkeys_file))
941 {
942 GNUNET_free (hostkeys_file);
943 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
944 return 1;
945 }
946 GNUNET_free (hostkeys_file);
947 cfg = GNUNET_CONFIGURATION_create ();
948 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename))
949 {
950 LOG (GNUNET_ERROR_TYPE_ERROR,
951 _("Failed to load configuration from %s\n"), cfgfilename);
952 GNUNET_CONFIGURATION_destroy (cfg);
953 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
954 return 1;
955 }
956 peer = GNUNET_TESTING_peer_configure (system, cfg, 0, NULL, NULL);
957 if (NULL == peer)
958 {
959 GNUNET_CONFIGURATION_destroy (cfg);
960 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
961 return 1;
962 }
963 GNUNET_free (peer->main_binary);
964 GNUNET_asprintf (&peer->main_binary, "gnunet-service-%s", service_name);
965 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
966 {
967 GNUNET_TESTING_peer_destroy (peer);
968 GNUNET_CONFIGURATION_destroy (cfg);
969 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
970 return 1;
971 }
972 sc.cfg = cfg;
973 sc.tm = tm;
974 sc.tm_cls = tm_cls;
975 GNUNET_SCHEDULER_run (&service_run_main, &sc); /* Scheduler loop */
976 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer))
932 { 977 {
933 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start process %s\n", service_name); 978 GNUNET_TESTING_peer_destroy (peer);
934 GNUNET_free (sc); 979 GNUNET_CONFIGURATION_destroy (cfg);
980 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
935 return 1; 981 return 1;
936 } 982 }
937 sc->tm = tm; 983 GNUNET_TESTING_peer_destroy (peer);
938 sc->tm_cls = tm_cls; 984 GNUNET_CONFIGURATION_destroy (cfg);
939 GNUNET_SCHEDULER_run (&service_run_main, sc); 985 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
940 // FIXME: GNUNET_TESTING_peer_stop...
941 GNUNET_free (sc);
942 return 0; 986 return 0;
943} 987}
944 988