KickJava   Java API By Example, From Geeks To Geeks.

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


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.channel.tcp.TcpChannelPool;
31 import org.objectweb.tribe.common.Group;
32 import org.objectweb.tribe.common.GroupIdentifier;
33 import org.objectweb.tribe.common.Member;
34 import org.objectweb.tribe.exceptions.AlreadyMemberException;
35 import org.objectweb.tribe.exceptions.ChannelException;
36 import org.objectweb.tribe.exceptions.EmptyBufferException;
37 import org.objectweb.tribe.exceptions.NotConnectedException;
38 import org.objectweb.tribe.messages.GroupMessage;
39
40 /**
41  * This class defines a ReliableGroupChannel
42  *
43  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
44  * @version 1.0
45  */

46 public class ReliableGroupChannel
47 {
48   protected AbstractChannelPool channelPool;
49   protected ReceiveBuffer incomingBuffer;
50   protected Group currentGroup;
51   protected Member me = null;
52
53   /**
54    * Creates a new <code>ReliableGroupChannel</code> object without group
55    * membership service.
56    */

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

70   public void join(Group g) throws AlreadyMemberException, ChannelException,
71       NotConnectedException
72   {
73     if ((currentGroup != null) && currentGroup.equals(g))
74       throw new AlreadyMemberException();
75     // Quit the previous group first if needed
76
if (currentGroup != null)
77       try
78       {
79         quit();
80       }
81       catch (Exception JavaDoc ignore)
82       {
83       }
84     currentGroup = g;
85     incomingBuffer = new ReceiveBuffer(currentGroup.getGroupIdentifier());
86     channelPool.registerReceiveBuffer(incomingBuffer);
87   }
88
89   /**
90    * Quit the current group without closing the channel.
91    *
92    * @throws ChannelException if a communication error occurs
93    * @throws NotConnectedException if the channel is not connected to any group
94    */

95   public void quit() throws ChannelException, NotConnectedException
96   {
97     if (currentGroup == null)
98       throw new NotConnectedException();
99     channelPool.unregisterReceiveBuffer(incomingBuffer);
100     currentGroup = null;
101   }
102
103   /**
104    * Returns the current group we are connected to or null if we are not
105    * connected to any group.
106    *
107    * @return Returns the current group or null.
108    */

109   public Group getCurrentGroup()
110   {
111     return currentGroup;
112   }
113
114   /**
115    * Sends the given message to all members of the group.
116    *
117    * @param msg message to send
118    * @return an <code>ArrayList</code> of Members who failed, or null if all
119    * members received successfully the message.
120    * @throws ChannelException if an error occurs
121    * @throws NotConnectedException if the channel is not connected to any group
122    */

123   public ArrayList JavaDoc send(Serializable JavaDoc msg) throws ChannelException,
124       NotConnectedException
125   {
126     if (currentGroup == null)
127       throw new NotConnectedException();
128     return send(msg, currentGroup.getGroupIdentifier(), currentGroup
129         .getMembers());
130   }
131
132   /**
133    * Sends a message to a subset of group members.
134    *
135    * @param msg message to send
136    * @param members <code>ArrayList</code> of <code>Member</code> that are
137    * all part of the group
138    * @return an <code>ArrayList</code> of Members who failed, or null if all
139    * members received successfully the message.
140    * @throws ChannelException if an error occurs
141    * @throws NotConnectedException if the channel is not connected to any group
142    */

143   public ArrayList JavaDoc send(Serializable JavaDoc msg, ArrayList JavaDoc members)
144       throws ChannelException, NotConnectedException
145   {
146     if (currentGroup == null)
147       throw new NotConnectedException();
148     return send(msg, currentGroup.getGroupIdentifier(), members);
149   }
150
151   /**
152    * Sends a message to members of group gid without being necessary member of
153    * the group.
154    *
155    * @param msg message to send
156    * @param members <code>ArrayList</code> of <code>Member</code> that are
157    * all part of the group
158    * @return an <code>ArrayList</code> of Members who failed, or null if all
159    * members received successfully the message.
160    * @throws ChannelException if an error occurs
161    * @throws NotConnectedException if the channel is not connected to any group
162    */

163   public ArrayList JavaDoc send(Serializable JavaDoc msg, GroupIdentifier gid, ArrayList JavaDoc members)
164       throws ChannelException, NotConnectedException
165   {
166     return channelPool.send(new GroupMessage(msg, gid), members);
167   }
168
169   /**
170    * Receives a new message from the channel.
171    *
172    * @return the new message
173    * @throws ChannelException if an error occurs
174    * @throws NotConnectedException if the channel is not connected to any group
175    */

176   public Serializable JavaDoc receive() throws ChannelException, NotConnectedException
177   {
178     if (currentGroup == null)
179       throw new NotConnectedException();
180     try
181     {
182       GroupMessage groupMessage = (GroupMessage) incomingBuffer.getMessage();
183       if (groupMessage == null)
184         return null;
185       else
186         return groupMessage.getMessage();
187     }
188     catch (EmptyBufferException e)
189     {
190       throw new ChannelException("Error while retrieving message from buffer",
191           e);
192     }
193   }
194
195   /**
196    * Closes the channel (also quits the current group if any).
197    *
198    * @throws ChannelException if a network error occurs
199    * @throws NotConnectedException if the channel is not connected
200    */

201   public void close() throws ChannelException, NotConnectedException
202   {
203     if (currentGroup != null)
204       try
205       {
206         this.quit();
207       }
208       catch (Exception JavaDoc ignore)
209       {
210       }
211   }
212
213   /**
214    * Returns the local membership value (Member object representing this channel
215    * in the group).
216    *
217    * @return Returns the local membership (me).
218    */

219   public Member getLocalMembership()
220   {
221     return me;
222   }
223
224   /**
225    * Sets the membership value (me as a Member of the group).
226    *
227    * @param membership The me to set.
228    * @throws NotConnectedException if the channel is not currently connected to
229    * any group
230    */

231   protected void setMembership(Member membership) throws NotConnectedException
232   {
233     if (currentGroup == null)
234       throw new NotConnectedException();
235     this.me = membership;
236   }
237 }
Popular Tags