KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > GetStateTest


1 // $Id: GetStateTest.java,v 1.6 2004/07/05 14:15:11 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.*;
8 import org.jgroups.util.Util;
9
10
11
12
13 /**
14  * Example of the STATE_TRANSFER protocol and API. The member periodically updates a shared state
15  * (consisting of an array of length 3). New members join the group and fetch the current state before
16  * they become operational. Existing members do not stop while new members fetch the group state.
17  */

18 public class GetStateTest implements Runnable JavaDoc {
19     Channel channel;
20     int[] state; // dice results, it *is* serializable !
21
Thread JavaDoc getter=null;
22     boolean rc=false;
23
24
25     
26     public void start() throws Exception JavaDoc {
27     //String props="UDP:PING:FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:"+
28
// "STATE_TRANSFER:QUEUE";
29
String JavaDoc 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 JavaDoc(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 JavaDoc(index)));
70         }
71         catch(Exception JavaDoc 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 JavaDoc 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 JavaDoc index;
92             int in;
93
94             try {
95             index=(Integer JavaDoc)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 JavaDoc cast_ex) {
104             System.out.println("Contents of buffer was no Integer !");
105             }
106             catch(Exception JavaDoc e) {
107             // System.err.println(e);
108
}
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 JavaDoc new_state=Util.objectFromByteBuffer(((SetStateEvent)ret).getArg());
117             if(new_state != null)
118             state=(int[])new_state;
119         }
120         }
121     }
122     catch(Exception JavaDoc e) {
123     }
124     }
125     
126
127     String JavaDoc printState(int[] vec) {
128     StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
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 JavaDoc[] args) {
137     try {
138         new GetStateTest().start();
139     }
140     catch(Exception JavaDoc e) {
141         System.err.println(e);
142     }
143     }
144
145 }
146
Popular Tags