1 3 4 package org.jgroups.tests; 5 6 7 import org.jgroups.*; 8 9 10 25 public class LargeState { 26 Channel channel; 27 byte[] state=null; 28 Thread getter=null; 29 boolean rc=false; 30 String props; 31 long start, stop; 32 boolean provider=true; 33 34 35 public void start(boolean provider, long size, String props) throws Exception { 36 this.provider=provider; 37 channel=new JChannel(props); 38 channel.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE); 39 channel.connect("TestChannel"); 40 41 if(provider) { 42 43 48 System.out.println("Creating state of " + size + " bytes"); 49 state=createLargeState(size); 50 System.out.println("Done. Waiting for other members to join and fetch large state"); 51 } 52 else { 53 54 59 System.out.println("Getting state"); 60 start=System.currentTimeMillis(); 61 rc=channel.getState(null, 20000); 62 System.out.println("getState(), rc=" + rc); 63 } 64 65 mainLoop(); 66 channel.close(); 67 68 } 69 70 71 public void mainLoop() { 72 Object ret; 73 74 try { 75 while(true) { 76 ret=channel.receive(0); 77 78 if(ret instanceof Message) { 79 System.out.println("-- received msg " + ((Message)ret).getObject() + " from " + 80 ((Message)ret).getSrc()); 81 } 82 else if(ret instanceof GetStateEvent) { 83 System.out.println("--> returned state: " + ret); 84 channel.returnState(state); 85 } 86 else if(ret instanceof SetStateEvent) { 87 stop=System.currentTimeMillis(); 88 byte[] new_state=((SetStateEvent)ret).getArg(); 89 if(new_state != null) { 90 state=new_state; 91 System.out.println("<-- Received state, size =" + state.length + 92 " (took " + (stop-start) + "ms)"); 93 } 94 if(!provider) 95 break; 96 } 97 } 98 } 99 catch(Exception e) { 100 } 101 } 102 103 104 byte[] createLargeState(long size) { 105 StringBuffer ret=new StringBuffer (); 106 for(int i=0; i < size; i++) 107 ret.append('.'); 108 return ret.toString().getBytes(); 109 } 110 111 112 public static void main(String [] args) { 113 boolean provider=false; 114 long size=1024 * 1024; 115 String props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" + 116 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;" + 117 "ucast_send_buf_size=80000;ucast_recv_buf_size=150000):" + 118 "AUTOCONF:" + 119 "PING(timeout=2000;num_initial_members=3):" + 120 "MERGE2(min_interval=5000;max_interval=10000):" + 121 "FD_SOCK:" + 122 "VERIFY_SUSPECT(timeout=1500):" + 123 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" + 124 "UNICAST(timeout=1000):" + 125 "pbcast.STABLE(desired_avg_gossip=20000):" + 126 "FRAG(frag_size=16000;down_thread=false;up_thread=false):" + 127 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" + 128 "shun=false;print_local_addr=true):" + 129 "pbcast.STATE_TRANSFER"; 130 131 132 133 for(int i=0; i < args.length; i++) { 134 if("-help".equals(args[i])) { 135 help(); 136 return; 137 } 138 if("-provider".equals(args[i])) { 139 provider=true; 140 continue; 141 } 142 if("-size".equals(args[i])) { 143 size=Long.parseLong(args[++i]); 144 continue; 145 } 146 if("-props".equals(args[i])) { 147 props=args[++i]; 148 continue; 149 } 150 } 151 152 153 try { 154 new LargeState().start(provider, size, props); 155 } 156 catch(Exception e) { 157 System.err.println(e); 158 } 159 } 160 161 static void help() { 162 System.out.println("LargeState [-help] [-size <size of state in bytes] [-provider] [-props <properties>]"); 163 } 164 165 } 166 | Popular Tags |