1 3 4 package org.jgroups.tests; 5 6 7 import org.jgroups.*; 8 import org.jgroups.util.Util; 9 10 11 12 13 18 public class GetStateTest implements Runnable { 19 Channel channel; 20 int[] state; Thread getter=null; 22 boolean rc=false; 23 24 25 26 public void start() throws Exception { 27 String props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" + 30 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" + 31 "PING(timeout=2000;num_initial_members=3):" + 32 "MERGE2(min_interval=5000;max_interval=10000):" + 33 "FD_SOCK:" + 34 "VERIFY_SUSPECT(timeout=1500):" + 35 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" + 36 "UNICAST(timeout=5000):" + 37 "pbcast.STABLE(desired_avg_gossip=20000):" + 38 "FRAG(frag_size=4096;down_thread=false;up_thread=false):" + 39 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" + 40 "shun=false;print_local_addr=true):" + 41 "pbcast.STATE_TRANSFER"; 42 43 44 45 channel=new JChannel(props); 46 channel.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE); 47 channel.connect("TestChannel"); 48 49 System.out.println("Getting state"); 50 rc=channel.getState(null, 3000); 51 System.out.println("getState(), rc=" + rc); 52 if(rc == false) { 53 state=new int[3]; 54 state[0]=1; state[1]=2; state[2]=3; 55 } 56 57 System.out.println("State is\n" + printState(state)); 58 Util.sleep(2000); 59 60 getter=new Thread (this, "Getter"); 61 getter.start(); 62 63 64 while(true) { 65 Message update=new Message(null, null, null); 66 int index=(int) ((Math.random() * 10) % 3); 67 68 try { 69 update.setBuffer(Util.objectToByteBuffer(new Integer (index))); 70 } 71 catch(Exception e) { 72 System.err.println(e); 73 } 74 System.out.println("Sending update for index " + index); 75 channel.send(update); 76 Util.sleep(2000); 77 } 78 79 } 80 81 82 public void run() { 83 Object ret; 84 85 try { 86 while(true) { 87 ret=channel.receive(0); 88 89 if(ret instanceof Message) { 90 Message m=(Message)ret; 91 Integer index; 92 int in; 93 94 try { 95 index=(Integer )m.getObject(); 96 in=index.intValue(); 97 98 if(state != null) { 99 System.out.println("state[" + in + "]=" + (state[in]+1)); 100 state[index.intValue()]++; 101 } 102 } 103 catch(ClassCastException cast_ex) { 104 System.out.println("Contents of buffer was no Integer !"); 105 } 106 catch(Exception e) { 107 } 109 110 } 111 else if(ret instanceof GetStateEvent) { 112 System.out.println("----> State transfer: " + ret); 113 channel.returnState(Util.objectToByteBuffer(state)); 114 } 115 else if(ret instanceof SetStateEvent) { 116 Object new_state=Util.objectFromByteBuffer(((SetStateEvent)ret).getArg()); 117 if(new_state != null) 118 state=(int[])new_state; 119 } 120 } 121 } 122 catch(Exception e) { 123 } 124 } 125 126 127 String printState(int[] vec) { 128 StringBuffer ret=new StringBuffer (); 129 if(vec != null) 130 for(int i=0; i < vec.length; i++) 131 ret.append("state[" + i + "]: " + vec[i] + '\n'); 132 return ret.toString(); 133 } 134 135 136 public static void main(String [] args) { 137 try { 138 new GetStateTest().start(); 139 } 140 catch(Exception e) { 141 System.err.println(e); 142 } 143 } 144 145 } 146 | Popular Tags |