1 24 25 package org.objectweb.tribe.channel; 26 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 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 46 public class ReliableGroupChannel 47 { 48 protected AbstractChannelPool channelPool; 49 protected ReceiveBuffer incomingBuffer; 50 protected Group currentGroup; 51 protected Member me = null; 52 53 57 public ReliableGroupChannel() 58 { 59 channelPool = TcpChannelPool.getChannelPool(); 60 } 61 62 70 public void join(Group g) throws AlreadyMemberException, ChannelException, 71 NotConnectedException 72 { 73 if ((currentGroup != null) && currentGroup.equals(g)) 74 throw new AlreadyMemberException(); 75 if (currentGroup != null) 77 try 78 { 79 quit(); 80 } 81 catch (Exception ignore) 82 { 83 } 84 currentGroup = g; 85 incomingBuffer = new ReceiveBuffer(currentGroup.getGroupIdentifier()); 86 channelPool.registerReceiveBuffer(incomingBuffer); 87 } 88 89 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 109 public Group getCurrentGroup() 110 { 111 return currentGroup; 112 } 113 114 123 public ArrayList send(Serializable 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 143 public ArrayList send(Serializable msg, ArrayList members) 144 throws ChannelException, NotConnectedException 145 { 146 if (currentGroup == null) 147 throw new NotConnectedException(); 148 return send(msg, currentGroup.getGroupIdentifier(), members); 149 } 150 151 163 public ArrayList send(Serializable msg, GroupIdentifier gid, ArrayList members) 164 throws ChannelException, NotConnectedException 165 { 166 return channelPool.send(new GroupMessage(msg, gid), members); 167 } 168 169 176 public Serializable 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 201 public void close() throws ChannelException, NotConnectedException 202 { 203 if (currentGroup != null) 204 try 205 { 206 this.quit(); 207 } 208 catch (Exception ignore) 209 { 210 } 211 } 212 213 219 public Member getLocalMembership() 220 { 221 return me; 222 } 223 224 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 |