convenience api for peergroup running
[oweals/gnunet.git] / contrib / peerStartHelper.pl
1
2 #  This file is part of GNUnet
3 #  (C) 2008, 2009 Christian Grothoff (and other contributing authors)
4
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
7 #  by the Free Software Foundation; either version 3, or (at your
8 #  option) any later version.
9
10 #  GNUnet is distributed in the hope that it will be useful, but
11 #  WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  General Public License for more details.
14
15 #  You should have received a copy of the GNU General Public License
16 #  along with GNUnet; see the file COPYING.  If not, write to the
17 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 #  Boston, MA 02111-1307, USA.
19
20
21
22 #  @file contrib/peerStartHelper.pl
23 #  @brief Helper process for starting gnunet-testing peers.
24 #  @author Nathan Evans
25 #
26 # Finds configuration files (or any files) of the format
27 # /path/*/gnunet-testing-config* and runs gnunet-arm with
28 # each as the given configuration.
29 #
30 # usage: peerStartHelper.pl /path/to/testing_dir/
31 #!/usr/bin/perl
32 use strict;
33
34 my $max_outstanding = 300;
35
36 $ARGV[0] || die "No directory provided for peer information, exiting!\n";
37
38 my $directory = $ARGV[0];
39 my @config_files = `find $directory -iname gnunet-testing-config*`;
40 my @child_arr = {};
41 my $count = 0;
42 my $outstanding = 0;
43 foreach my $file (@config_files)
44 {
45   chomp($file);
46   #print "Starting GNUnet peer with config file $file\n";
47   my $pid = fork();
48   if ($pid == -1) 
49   {
50    die;
51   } 
52   elsif ($pid == 0) 
53   {
54     exec "gnunet-arm -q -c $file -s"  or die;
55   }
56
57   if ($pid != 0)
58   {
59     push @child_arr, $pid;
60     $count++;
61     $outstanding++;
62     if ($outstanding > $max_outstanding)
63     {
64       for (my $i = 0; $i < $max_outstanding / 5; $i++)
65       {
66         #print "Too many outstanding peers, waiting!\n";
67         waitpid($child_arr[0], 0);
68         shift(@child_arr);
69         $outstanding--;
70       }
71     }
72   }
73 }
74
75 print "All $count peers started (waiting for them to finish!\n";
76
77 while ($outstanding > 0)
78 {
79   waitpid($child_arr[0], 0);
80   shift(@child_arr);
81   $outstanding--;
82   if ($outstanding % 50 == 0)
83   {
84     print "All $count peers started (waiting for $outstanding to finish!\n";
85   }
86 }
87
88 while (wait() != -1) {sleep 1}
89
90 print "All $count peers started!\n";
91
92