View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.relacs.simulator;
20  
21  import java.io.BufferedReader;
22  import java.io.FileReader;
23  import java.net.InetAddress;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import jgroup.core.ConfigManager;
29  import jgroup.core.registry.BootstrapRegistry;
30  
31  /**
32   *  Parser for the partition simulator files.  The file format is as follows:
33   * 
34   *  <pre>
35   *    hostname  link-to-host  probability-of-link-working  link-to-host  prob ...
36   *  </pre>
37   *
38   *  @author  Alberto Montresor
39   *  @author  Hein Meling
40   *  @since   Jgroup 0.5
41   */
42  public class Simulator
43  {
44  
45    ////////////////////////////////////////////////////////////////////////////////////////////
46    // Constants
47    ////////////////////////////////////////////////////////////////////////////////////////////
48  
49  
50    public static void main(String[] argv)
51    {
52      InetAddress         inet;
53      int                 prob;
54      SocketStatus        status;
55  
56      try {
57        ConfigManager.init();
58        if (argv.length == 0)
59          throw new IllegalArgumentException("Wrong number of arguments");
60        BufferedReader in = new BufferedReader(new FileReader(argv[0]));
61        String line = null;
62        int start, stop;
63        List<SocketStatus> hosts = new ArrayList<SocketStatus>(20);
64        while ((line = in.readLine()) != null) {
65          start = 0;
66          stop = line.indexOf(" ", start); stop = (stop >= 0 ? stop : line.length());
67          String hostname = line.substring(start, stop);
68          if (hostname.startsWith("#")) {
69            // Ignore commented lines
70            continue;
71          }
72          System.out.print(hostname + ": ");
73          try {
74            status = (SocketStatus) BootstrapRegistry.lookup(hostname, "SocketStatus");
75            hosts.add(status);
76            while (stop < line.length()) {
77              start = stop+1;
78              stop  = line.indexOf(" ", start); stop = (stop >= 0 ? stop : line.length());
79              String linkTo = line.substring(start, stop);
80              inet  = InetAddress.getByName(linkTo);
81              start = stop+1;
82              stop  = line.indexOf(" ", start); stop = (stop >= 0 ? stop : line.length());
83              prob  = Integer.parseInt(line.substring(start, stop));
84              status.setStatus(inet, prob);
85              System.out.print(" link-to " + linkTo + "(" + prob + ")  ");
86            }
87            System.out.println();
88          } catch (java.lang.Exception e) {
89  //          e.printStackTrace();
90            System.out.println(" Not available: " + hostname);
91          }
92        }
93        for (Iterator<SocketStatus> iter = hosts.iterator(); iter.hasNext();) {
94          status = iter.next();
95          status.commit();
96        }
97      } catch (IllegalArgumentException e) {
98        System.out.println(e.getMessage());
99        System.out.println("Usage: simulator <partition-file>");
100     } catch (Exception e) {
101       e.printStackTrace();
102     }
103   }
104 
105 } // END of Simulator