1 3 4 package org.jgroups.tests; 5 6 7 import org.jgroups.*; 8 9 import java.io.Serializable ; 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 15 16 17 32 public class UnicastTest2 implements Runnable { 33 Channel channel; 34 String groupname="UnicastTest2Group"; 35 String props="UDP:PING:FD:DISCARD(down=0.1):NAKACK(retransmit_timeout=1000):"+ 36 "UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:QUEUE"; 37 Thread writer=null; 38 Vector mbrs=new Vector (); 39 Hashtable senders=new Hashtable (); 40 boolean running=true; 41 final int NUM_MSGS=100; 42 43 44 45 46 public void start() throws Exception { 47 Object obj; 48 Message msg; 49 View view; 50 Vector tmp; 51 UnicastTest2Info info, myinfo; 52 Object sender; 53 54 channel=new JChannel(props); 55 channel.connect(groupname); 56 System.out.println("[ready]"); 57 58 while(true) { 59 try { 60 obj=channel.receive(0); 61 if(obj instanceof View) { 62 view=(View)obj; 63 tmp=view.getMembers(); 64 mbrs.removeAllElements(); 65 for(int i=0; i < tmp.size(); i++) 66 mbrs.addElement(tmp.elementAt(i)); 67 68 for(Enumeration e=senders.keys(); e.hasMoreElements();) { 69 sender=e.nextElement(); 70 if(!mbrs.contains(sender)) { 71 mbrs.removeElement(sender); 72 } 73 } 74 75 if(mbrs.size() > 1) { 76 if(writer == null) { 77 writer=new Thread (this, "WriterThread"); 78 writer.start(); 79 } 80 } 81 else { 82 if(writer != null) { 83 running=false; 84 writer.interrupt(); 85 } 86 writer=null; 87 } 88 } 89 else if(obj instanceof Message) { 90 msg=(Message)obj; 91 info=(UnicastTest2Info)msg.getObject(); 92 System.out.println("Received msg: " + info); 93 94 myinfo=(UnicastTest2Info)senders.get(info.sender); 95 if(myinfo == null) { if(info.msgno == 1) { 97 senders.put(info.sender, info); 99 } 100 else { 101 System.err.println("UnicastTest2.start(): first seqno must be 1"); 103 } 104 105 } 106 else { 107 if(info.msgno -1 != myinfo.msgno) { 108 System.err.println("UnicastTest2.start(): received msg " + info.sender + ':' + 109 info.msgno + ", but last received was " + 110 myinfo.sender + ':' + myinfo.msgno); 111 } 112 else { 113 System.out.println("UnicastTest2.start(): OK received " + info.sender + ':' + 114 info.msgno + ", prev seqno=" + myinfo.sender + ':' + myinfo.msgno); 115 myinfo.msgno++; 116 } 117 } 118 119 } 120 else 121 ; 122 } 123 catch(ChannelClosedException closed) { 124 System.err.println("Channel closed"); 125 break; 126 } 127 catch(ChannelNotConnectedException not_conn) { 128 System.err.println("Channel not connected"); 129 break; 130 } 131 catch(Exception e) { 132 System.err.println(e); 133 } 134 } 135 136 137 138 139 } 140 141 142 Address selectTarget() { 143 Vector tmp=new Vector (); 144 Address ret; 145 int t; 146 147 if(mbrs == null || mbrs.size() < 2) 148 return null; 149 150 for(int i=0; i < mbrs.size(); i++) { 151 if(!(mbrs.elementAt(i).equals(channel.getLocalAddress()))) 152 tmp.addElement(mbrs.elementAt(i)); 153 } 154 t=(int)((Math.random() * 100)); 155 ret=(Address)tmp.elementAt(t % tmp.size()); 156 return ret; 157 } 158 159 160 161 public void run() { 162 Address target=selectTarget(); 163 UnicastTest2Info info=null; 164 int msgno=1; 165 166 if(target == null) 167 return; 168 169 while(running && msgno <= NUM_MSGS) { 170 try { 171 info=new UnicastTest2Info(msgno++, channel.getLocalAddress()); 172 System.out.println("Sending message #" + (msgno-1) + " to " + target); 173 channel.send(new Message(target, null, info)); 174 Thread.sleep(500); 175 } 176 catch(ChannelClosedException closed) { 177 System.err.println(closed); 178 break; 179 } 180 catch(ChannelNotConnectedException not_conn) { 181 System.err.println(not_conn); 182 } 183 catch(Exception e) { 184 System.err.println(e); 185 } 186 } 187 System.out.println("UnicastTest2Info.run(): writer thread terminated"); 188 } 189 190 191 192 193 public static void main(String [] args) { 194 try { 195 196 new UnicastTest2().start(); 197 } 198 catch(Exception e) { 199 System.err.println(e); 200 } 201 } 202 203 204 private static class UnicastTest2Info implements Serializable { 205 int msgno=0; 206 Object sender=null; 207 208 public UnicastTest2Info() { 209 210 } 211 212 public UnicastTest2Info(int msgno, Object sender) { 213 this.msgno=msgno; 214 this.sender=sender; 215 } 216 217 218 public String toString() { 219 return "#" + msgno + " (sender=" + sender + ')'; 220 } 221 } 222 223 224 225 226 } 227 228 229 230 | Popular Tags |