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.daemon;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import jgroup.core.EndPoint;
28  
29  import org.apache.log4j.Logger;
30  
31  
32  /**
33   * The <code>GroupTable</code> class
34   *
35   * @author  Alberto Montresor
36   * @since   Jgroup 1.2
37   */
38  final class GroupTable
39  {
40  
41    ////////////////////////////////////////////////////////////////////////////////////////////
42    // Logger
43    ////////////////////////////////////////////////////////////////////////////////////////////
44  
45    /** Obtain logger for this class */
46    private static final Logger log = Logger.getLogger(GroupTable.class);
47  
48  
49    ////////////////////////////////////////////////////////////////////////////////////////////
50    // Fields
51    ////////////////////////////////////////////////////////////////////////////////////////////
52  
53    /** Hash table containing the groups in the system */
54    private final Map<Integer,Group> totgroups = new HashMap<Integer,Group>();
55  
56    /** Hash map: host to a list of Group objects on that host */
57    private final Map<EndPoint,List<Group>> grouplists = new HashMap<EndPoint,List<Group>>();
58  
59    /** Hash table that maps host to symmetry sets */ 
60    private final Map<EndPoint,EndPoint[]> symsets = new HashMap<EndPoint,EndPoint[]>();
61  
62  
63    ////////////////////////////////////////////////////////////////////////////////////////////
64    // Constructors
65    ////////////////////////////////////////////////////////////////////////////////////////////
66  
67    GroupTable() { }
68  
69  
70    ////////////////////////////////////////////////////////////////////////////////////////////
71    // Methods
72    ////////////////////////////////////////////////////////////////////////////////////////////
73  
74    @Override
75    public String toString()
76    {
77      StringBuilder buf = new StringBuilder("GroupTable: groups=");
78      for (Group group : totgroups.values()) {
79        buf.append(group.getGid());
80        buf.append(" ");
81      }
82      for (Map.Entry<EndPoint,List<Group>> groupEntry : grouplists.entrySet()) {
83        buf.append("endpoint=");
84        buf.append(groupEntry.getKey());
85        buf.append(": ");
86        for (Group group : groupEntry.getValue()) {
87          buf.append(group.getGid());
88        }
89        buf.append("; ");
90      }
91      return buf.toString();
92    }
93  
94    /**
95     *  Add a new host/endpoint to the given group.
96     */
97    void addEndpointToGroup(EndPoint endpoint, Group group)
98    {
99      if (log.isDebugEnabled()) 
100       log.debug("GroupTable: addGroupEndPoint: " + endpoint);
101     if (!containsGroup(group.getGid()))
102       throw new IllegalStateException("Cannot add group endpoint to a non-local group.");
103     List<Group> groups = grouplists.get(endpoint);
104     if (groups == null) {
105       groups = new ArrayList<Group>(10);
106       grouplists.put(endpoint, groups);
107     }
108     if (!groups.contains(group))
109       groups.add(group);
110 
111     if (log.isDebugEnabled())
112       log.debug(toString());
113   }
114 
115   /**
116    *  Insert a new group in the group table.
117    */
118   void insertGroup(Group group)
119   {
120     totgroups.put(group.getGid(), group);
121   }
122 
123   /**
124    *  Remove the given group from the group table.
125    */
126   void removeGroup(int gid)
127   {
128     if (log.isDebugEnabled())
129       log.debug("Removing group: " + gid);
130 
131     Group group = getGroup(gid);
132     for (Iterator iter = grouplists.entrySet().iterator(); iter.hasNext();) {
133       Map.Entry groupEntry = (Map.Entry) iter.next();
134       List groups = (List) groupEntry.getValue();
135       groups.remove(group);
136       if (log.isDebugEnabled())
137         log.debug("Removed group " + group.getGid() + " from host " + groupEntry.getKey());
138       if (groups.isEmpty())
139         iter.remove();
140     }
141     totgroups.remove(gid);
142 
143     if (log.isDebugEnabled())
144       log.debug(toString());
145   }
146 
147   /**
148    *  Return the group object for the given group identifier.
149    */
150   Group getGroup(int gid)
151   {
152     return totgroups.get(gid);
153   }
154 
155   /**
156    *  Returns true if the given group is contained in this group table.
157    */
158   boolean containsGroup(int gid)
159   {
160     return totgroups.containsKey(gid);
161   }
162 
163   /**
164    *  Returns true if the group table contains no local group members.
165    */
166   boolean isEmpty()
167   {
168     return totgroups.isEmpty();
169   }
170     
171   /**
172    *  Returns an iterator containing all the the groups contained in
173    *  this group table.
174    */
175   Iterator iterator()
176   {
177     return totgroups.values().iterator();
178   }
179   
180   /**
181    *  Returns an iterator containing the list of groups
182    *  joined by the specified host.
183    */
184   Iterator getGroups(EndPoint endpoint) 
185   {
186     List<Group> groups = grouplists.get(endpoint);
187     /* 
188      * Ensures that we return an empty iterator if there are no groups
189      * for the specified host.
190      */
191     if (groups == null)
192       groups = new ArrayList<Group>(0);
193     return groups.iterator();
194   }
195 
196   EndPoint[] getSymset(EndPoint endpoint)
197   {
198     return symsets.get(endpoint);
199   }
200   
201   void setSymset(EndPoint host, EndPoint[] symset)
202   {
203     symsets.put(host, symset);
204   }
205 
206 } // END GroupTable