KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > channel > ReliableGroupChannelWithGms


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.channel;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.tribe.common.Group;
31 import org.objectweb.tribe.common.GroupIdentifier;
32 import org.objectweb.tribe.common.Member;
33 import org.objectweb.tribe.exceptions.AlreadyMemberException;
34 import org.objectweb.tribe.exceptions.ChannelException;
35 import org.objectweb.tribe.exceptions.NotConnectedException;
36 import org.objectweb.tribe.gms.GroupMembershipService;
37
38 /**
39  * This class defines a ReliableGroupChannel relying on a Group Membership
40  * Service (GMS).
41  *
42  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
43  * @version 1.0
44  */

45 public class ReliableGroupChannelWithGms extends ReliableGroupChannel
46 {
47   protected GroupMembershipService gms = null;
48
49   /**
50    * Creates a new <code>ReliableGroupChannelWithGms</code> using the provided
51    * group membership service.
52    *
53    * @param gms the GMS service to use
54    */

55   public ReliableGroupChannelWithGms(GroupMembershipService gms)
56   {
57     super();
58     this.gms = gms;
59   }
60
61   /**
62    * Join the group that has the given group identifier.
63    *
64    * @param gid the identifier of the group to join
65    * @throws AlreadyMemberException if we are already member of the group
66    * @throws NotConnectedException if the channel is not connected
67    * @throws ChannelException if an error is reported by the channel
68    */

69   public void join(GroupIdentifier gid) throws AlreadyMemberException,
70       ChannelException, NotConnectedException
71   {
72     Group g = getGroup(gid);
73     if (g == null)
74       g = new Group(gid);
75     super.join(g);
76     me = gms.join(this, gid);
77     currentGroup = getGroup(gid);
78   }
79
80   /**
81    * Quit the current group.
82    *
83    * @throws ChannelException if a communication error occurs
84    * @throws NotConnectedException if the channel is not connected to any group
85    */

86   public void quit() throws ChannelException, NotConnectedException
87   {
88     if (currentGroup == null)
89       throw new NotConnectedException();
90     try
91     {
92       gms.quit(this, currentGroup.getGroupIdentifier());
93     }
94     catch (ChannelException ignore)
95     {
96     }
97     super.quit();
98     me = null;
99   }
100
101   /**
102    * Get the group (including its members) corresponding to the given gid. If
103    * the GMS was never asked to join the given group, then null is returned.
104    *
105    * @param gid the group identifier
106    * @return the <code>Group</code> corresponding to this gid.
107    */

108   public Group getGroup(GroupIdentifier gid)
109   {
110     return gms.getGroup(gid);
111   }
112
113   /**
114    * Sends a message to members of group gid without being necessary member of
115    * the group.
116    *
117    * @param msg message to send
118    * @param members <code>ArrayList</code> of <code>Member</code> that are
119    * all part of the group
120    * @return an <code>ArrayList</code> of Members who failed, or null if all
121    * members received successfully the message.
122    * @throws ChannelException if an error occurs
123    * @throws NotConnectedException if the channel is not connected to any group
124    */

125   public ArrayList JavaDoc send(Serializable JavaDoc msg, GroupIdentifier gid, ArrayList JavaDoc members)
126       throws ChannelException, NotConnectedException
127   {
128     ArrayList JavaDoc failed = super.send(msg, gid, members);
129     if (failed != null)
130     { // Removed all failed members from group
131
for (int i = 0; i < failed.size(); i++)
132         gms.quitMember((Member) failed.get(i), gid);
133     }
134     return failed;
135   }
136
137 }
Popular Tags