1 24 25 package org.objectweb.tribe.adapters; 26 27 import java.io.Serializable ; 28 import java.net.InetAddress ; 29 import java.util.ArrayList ; 30 31 import org.objectweb.tribe.channel.ReliableGroupChannelWithGms; 32 import org.objectweb.tribe.channel.tcp.TcpChannelPool; 33 import org.objectweb.tribe.common.GroupIdentifier; 34 import org.objectweb.tribe.common.IpAddress; 35 import org.objectweb.tribe.common.Member; 36 import org.objectweb.tribe.exceptions.AlreadyMemberException; 37 import org.objectweb.tribe.exceptions.ChannelException; 38 import org.objectweb.tribe.exceptions.NotConnectedException; 39 import org.objectweb.tribe.gms.GroupMembershipService; 40 import org.objectweb.tribe.gms.discovery.UdpDiscoveryService; 41 import org.objectweb.tribe.messages.MessageListener; 42 43 49 public class MulticastRequestAdapterTest 50 implements 51 MessageListener, 52 MulticastRequestListener 53 { 54 55 private ReliableGroupChannelWithGms channel1; 56 private ReliableGroupChannelWithGms channel2; 57 private UdpDiscoveryService discovery; 58 private GroupMembershipService gms; 59 private final GroupIdentifier gid1 = new GroupIdentifier("group1"); 60 private final String msg1 = "Hello from 1"; 61 private final String msg2 = "Hello from 2"; 62 private MulticastRequestAdapter adapter1; 63 private MulticastRequestAdapter adapter2; 64 65 70 public MulticastRequestAdapterTest() throws Exception 71 { 72 initializeChannels(); 73 } 74 75 private void initializeChannels() throws Exception 76 { 77 final InetAddress MULTICAST_ADDRESS = InetAddress.getByName("224.7.65.23"); 78 final int MULTICAST_PORT = 2288; 79 final IpAddress MULTICAST_IP = new IpAddress(MULTICAST_ADDRESS, 80 MULTICAST_PORT); 81 final InetAddress REPLY_ADDRESS = InetAddress.getLocalHost(); 82 final int REPLY_PORT = 0; final IpAddress REPLY_IP = new IpAddress(REPLY_ADDRESS, REPLY_PORT); 84 85 discovery = new UdpDiscoveryService(MULTICAST_IP, REPLY_IP); 86 gms = new GroupMembershipService(REPLY_IP, TcpChannelPool.getChannelPool(), 87 discovery); 88 channel1 = new ReliableGroupChannelWithGms(gms); 89 channel2 = new ReliableGroupChannelWithGms(gms); 90 91 93 try 94 { 95 channel1.join(gid1); 96 } 97 catch (AlreadyMemberException e) 98 { 99 e.printStackTrace(); 100 throw e; 101 } 102 try 103 { 104 channel2.join(gid1); 105 } 106 catch (AlreadyMemberException e) 107 { 108 e.printStackTrace(); 109 throw e; 110 } 111 112 adapter1 = new MulticastRequestAdapter(channel1, this, this); 113 adapter2 = new MulticastRequestAdapter(channel2, this, this); 114 115 } 116 117 120 public void sendMessages() 121 { 122 try 123 { 124 ArrayList members = channel1.getCurrentGroup().getMembers(); 125 System.out.println("Sending '" + msg1 + "' to " + members.size() 127 + " members."); 128 MulticastResponse replies = adapter1.multicastMessage(members, msg1, 129 MulticastRequestAdapter.WAIT_ALL, 0); 130 131 for (int i = 0; i < members.size(); i++) 133 { 134 Member m = (Member) members.get(i); 135 Object answer = replies.getResult(m); 136 if (answer == null) 137 System.out.println("Member " + m + " failed."); 138 else 139 System.out.println("Member " + m + " replied: '" + answer + "'"); 140 } 141 142 System.out.println("Sending '" + msg2 + "' to " + members.size() 144 + " members."); 145 adapter2.multicastMessage(members, msg2, 146 MulticastRequestAdapter.WAIT_ALL, 0); 147 148 for (int i = 0; i < members.size(); i++) 150 { 151 Member m = (Member) members.get(i); 152 Object answer = replies.getResult(m); 153 if (answer == null) 154 System.out.println("Member " + m + " failed."); 155 else 156 System.out.println("Member " + m + " replied: '" + answer + "'"); 157 } 158 } 159 catch (Exception e1) 160 { 161 e1.printStackTrace(); 162 } 163 } 164 165 168 public void close() 169 { 170 System.out.println("Stopping adapter1"); 172 adapter1.stop(); 173 System.out.println("Stopping adapter2"); 174 adapter2.stop(); 175 176 System.out.println("Stopping GMS"); 178 gms.stop(); 179 System.out.println("Stopping discovery service"); 180 discovery.kill(); 181 182 try 184 { 185 System.out.println("Closing channel1"); 186 channel1.close(); 187 } 188 catch (ChannelException e) 189 { 190 e.printStackTrace(); 191 } 192 catch (NotConnectedException e) 193 { 194 e.printStackTrace(); 195 } 196 try 197 { 198 System.out.println("Closing channel2"); 199 channel2.close(); 200 } 201 catch (ChannelException e1) 202 { 203 e1.printStackTrace(); 204 } 205 catch (NotConnectedException e1) 206 { 207 e1.printStackTrace(); 208 } 209 210 } 211 212 216 219 public void receive(Serializable msg) 220 { 221 System.out.println("Received msg: " + msg); 222 } 223 224 228 233 public static void main(String [] args) 234 { 235 try 236 { 237 MulticastRequestAdapterTest adapter = new MulticastRequestAdapterTest(); 238 adapter.sendMessages(); 239 adapter.sendMessages(); 240 adapter.sendMessages(); 241 adapter.close(); 242 } 243 catch (Exception e) 244 { 245 e.printStackTrace(); 246 } 247 } 248 249 253 public Object handleMessageSingleThreaded(Serializable msg, Member sender) 254 { 255 System.out.println("Received single threaded '" + msg + "' from " + sender); 256 return "singleDone"; 257 } 258 259 263 public Serializable handleMessageMultiThreaded(Serializable msg, 264 Member sender, Object handleMessageSingleThreadedResult) 265 { 266 System.out.println("Received multithreaded '" + msg + "' from " + sender 267 + " (" + handleMessageSingleThreadedResult + ")"); 268 return "singleMulti"; 269 } 270 271 } | Popular Tags |