KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@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.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import org.jgroups.Address;
32 import org.jgroups.ChannelClosedException;
33 import org.jgroups.ChannelNotConnectedException;
34 import org.jgroups.JChannel;
35 import org.jgroups.Message;
36 import org.jgroups.TimeoutException;
37 import org.objectweb.tribe.common.Group;
38 import org.objectweb.tribe.common.GroupIdentifier;
39 import org.objectweb.tribe.exceptions.AlreadyMemberException;
40 import org.objectweb.tribe.exceptions.ChannelException;
41 import org.objectweb.tribe.exceptions.NotConnectedException;
42 import org.objectweb.tribe.messages.GroupMessage;
43
44 /**
45  * This class defines a JGroupsReliableChannel which is a ReliableGroupChannel
46  * wrapper on top of a JGroups JChannel.
47  *
48  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
49  * @version 1.0
50  */

51 public class JGroupsReliableChannel extends ReliableGroupChannel
52 {
53   // The underlying JGroups channel
54
private JChannel jgroupsChannel;
55
56   /**
57    * Creates a new <code>JGroupsReliableChannel</code> object
58    */

59   public JGroupsReliableChannel(URL JavaDoc jgroupsConfigFile) throws ChannelException
60   {
61     try
62     {
63       jgroupsChannel = new JChannel(jgroupsConfigFile);
64     }
65     catch (org.jgroups.ChannelException e)
66     {
67       throw new ChannelException(e);
68     }
69   }
70
71   /**
72    * @see org.objectweb.tribe.channel.ReliableGroupChannel#join(org.objectweb.tribe.common.Group)
73    */

74   public void join(Group g) throws AlreadyMemberException, ChannelException,
75       NotConnectedException
76   {
77     if ((currentGroup != null) && currentGroup.equals(g))
78       throw new AlreadyMemberException();
79     // Quit the previous group first if needed
80
if (currentGroup != null)
81       try
82       {
83         quit();
84       }
85       catch (Exception JavaDoc ignore)
86       {
87       }
88     currentGroup = g;
89     try
90     {
91       jgroupsChannel.connect(g.getGroupIdentifier().getGroupName());
92     }
93     catch (ChannelClosedException e)
94     {
95       throw new NotConnectedException(e);
96     }
97     catch (org.jgroups.ChannelException e)
98     {
99       throw new ChannelException(e);
100     }
101   }
102
103   /**
104    * @see org.objectweb.tribe.channel.ReliableGroupChannel#quit()
105    */

106   public void quit() throws ChannelException, NotConnectedException
107   {
108     if (currentGroup == null)
109       throw new NotConnectedException();
110     jgroupsChannel.close();
111     currentGroup = null;
112   }
113
114   /**
115    * @see org.objectweb.tribe.channel.ReliableGroupChannel#receive()
116    */

117   public Serializable JavaDoc receive() throws ChannelException, NotConnectedException
118   {
119     if (currentGroup == null)
120       throw new NotConnectedException();
121     try
122     {
123       GroupMessage groupMessage = (GroupMessage) jgroupsChannel.receive(0);
124       if (groupMessage == null)
125         return null;
126       else
127         return groupMessage.getMessage();
128     }
129     catch (ChannelNotConnectedException e)
130     {
131       throw new NotConnectedException(e);
132     }
133     catch (ChannelClosedException e)
134     {
135       throw new NotConnectedException(e);
136     }
137     catch (TimeoutException e)
138     {
139       throw new ChannelException(
140           "Timeout while retrieving message from channel", e);
141     }
142   }
143
144   /**
145    * @see org.objectweb.tribe.channel.ReliableGroupChannel#send(java.io.Serializable,
146    * org.objectweb.tribe.common.GroupIdentifier, java.util.ArrayList)
147    */

148   public ArrayList JavaDoc send(Serializable JavaDoc msg, GroupIdentifier gid, ArrayList JavaDoc members)
149       throws ChannelException, NotConnectedException
150   {
151     Address dest = null;
152     Message jgroupsMsg = new Message(dest, null, new GroupMessage(msg, gid));
153     try
154     {
155       jgroupsChannel.send(jgroupsMsg);
156       // Note that JGroups channel does not offer the facility of finding which
157
// node have failed, it will be reported by a view change
158
return null;
159     }
160     catch (ChannelNotConnectedException e)
161     {
162       throw new NotConnectedException(e);
163     }
164     catch (ChannelClosedException e)
165     {
166       throw new NotConnectedException(e);
167     }
168   }
169
170   /**
171    * @see java.lang.Object#toString()
172    */

173   public String JavaDoc toString()
174   {
175     return "JGroups channel wrapper: " + jgroupsChannel;
176   }
177
178   /**
179    * Return the JGroups channel properties
180    *
181    * @return JGroups channel properties
182    */

183   public String JavaDoc getProperties()
184   {
185     return jgroupsChannel.getProperties();
186   }
187 }
Popular Tags