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.rmi.RemoteException;
22  import java.util.Iterator;
23  
24  import jgroup.core.ConfigManager;
25  import jgroup.core.ConfigurationException;
26  import jgroup.relacs.config.DistributedSystemConfig;
27  import jgroup.relacs.config.Domain;
28  import jgroup.relacs.config.DomainSet;
29  import jgroup.relacs.config.Host;
30  import jgroup.relacs.config.HostSet;
31  
32  /**
33   *  Partition simulator that will examine domains, and generate partitions
34   *  between domains and reconnect them as instructed through commandline
35   *  arguments.
36   *
37   *  @author  Hein Meling
38   *  @since   Jgroup 2.1
39   */
40  public class PartitionSimulator
41  {
42  
43    public static void main(String[] argv)
44    {
45      if (argv.length == 0)
46        throw new IllegalArgumentException("Wrong number of arguments");
47      int reachability = Integer.parseInt(argv[0]);
48      System.out.println("Domain reachability=" + reachability);
49      try {
50        ConfigManager.init();
51      } catch (ConfigurationException e) {
52        e.printStackTrace();
53      }
54      DistributedSystemConfig dsc = ConfigManager.getDistributedSystem();
55      DomainSet domains = dsc.getDomainSet();
56      for (Iterator<Domain> iter = domains.iterator(); iter.hasNext();) {
57        Domain domain = iter.next();
58        for (Iterator<Domain> iterator = domains.iterator(); iterator.hasNext();) {
59          Domain otherDomain = iterator.next();
60          if (!domain.equals(otherDomain)) {
61            setReachability(reachability, domain, otherDomain);
62          }
63        }
64        HostSet d1Hosts = domain.getHostSet();
65        for (Iterator<Host> i = d1Hosts.iterator(); i.hasNext();) {
66          Host host = i.next();
67          try {
68            ((SocketStatus) host.get("SocketStatus")).commit();
69          } catch (RemoteException e) {
70            e.printStackTrace();
71          }
72        }
73      }
74    }
75  
76    private static void setReachability(int reachability, Domain d1, Domain d2)
77    {
78      HostSet d1Hosts = d1.getHostSet();
79      for (Iterator<Host> iter = d1Hosts.iterator(); iter.hasNext();) {
80        Host h1 = iter.next();
81        System.out.print(h1.getHostName() + "(" + reachability + "): ");
82        try {
83          SocketStatus status = (SocketStatus) h1.lookup("SocketStatus");
84          h1.put("SocketStatus", status);
85          for (Iterator<Host> iterator = d2.getHostSet().iterator(); iterator.hasNext();) {
86            Host h2 = iterator.next();
87            status.setStatus(h2.getAddress(), reachability);
88            System.out.print(h2.getHostName() + " ");
89          }
90          System.out.println();
91        } catch (Exception e) {
92          iter.remove();
93          System.out.println(h1.getHostName() + " is not available");
94        }
95      }
96    }
97  
98  } // END of PartitionSimulator