KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > ViewDemo


1 // $Id: ViewDemo.java,v 1.7 2005/04/23 14:36:40 belaban Exp $
2

3 package org.jgroups.demos;
4
5
6 import org.jgroups.*;
7 import org.jgroups.blocks.PullPushAdapter;
8 import org.jgroups.util.Util;
9
10 import java.util.HashMap JavaDoc;
11
12
13 /**
14  * Demos the reception of views using a PullPushAdapter. Just start a number of members, and kill them
15  * randomly. The view should always be correct.
16  */

17 public class ViewDemo implements MembershipListener {
18     private Channel channel;
19
20
21     public void viewAccepted(View new_view) {
22         System.out.println("** New view: " + new_view);
23         // ", channel.getView()=" + channel.getView());
24
}
25
26
27     /**
28      * Called when a member is suspected
29      */

30     public void suspect(Address suspected_mbr) {
31         System.out.println("Suspected(" + suspected_mbr + ')');
32     }
33
34
35     /**
36      * Block sending and receiving of messages until ViewAccepted is called
37      */

38     public void block() {
39
40     }
41
42
43     public void start(String JavaDoc props, boolean use_additional_data) throws Exception JavaDoc {
44
45         channel=new JChannel(props);
46         channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
47
48         if(use_additional_data) {
49             HashMap JavaDoc m=new HashMap JavaDoc();
50             m.put("additional_data", "bela".getBytes());
51             channel.down(new Event(Event.CONFIG, m));
52         }
53
54         channel.connect("ViewDemo");
55         channel.setOpt(Channel.VIEW, Boolean.TRUE);
56         new PullPushAdapter(channel, this);
57
58         while(true) {
59             Util.sleep(10000);
60         }
61     }
62
63
64     public static void main(String JavaDoc args[]) {
65         ViewDemo t=new ViewDemo();
66         boolean use_additional_data=false;
67         String JavaDoc props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
68                 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
69                 "PING(timeout=2000;num_initial_members=3):" +
70                 "MERGE2(min_interval=5000;max_interval=10000):" +
71                 "FD_SOCK:" +
72                 "VERIFY_SUSPECT(timeout=1500):" +
73                 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
74                 "UNICAST(timeout=600,1200,2400):" +
75                 "pbcast.STABLE(desired_avg_gossip=20000):" +
76                 "FRAG(frag_size=4096;down_thread=false;up_thread=false):" +
77                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
78                 "shun=false;print_local_addr=true)";
79
80
81
82         /*
83           String props="TCP(start_port=7800):" +
84           "TCPPING(initial_hosts=66.87.163.92[7800];port_range=2;" +
85           "timeout=5000;num_initial_members=3;up_thread=true;down_thread=true):" +
86           "VERIFY_SUSPECT(timeout=1500):" +
87           "pbcast.STABLE(desired_avg_gossip=200000;down_thread=false;up_thread=false):"+
88           "pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):" +
89           "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;" +
90           "print_local_addr=true;down_thread=true;up_thread=true)";
91         */

92
93
94         for(int i=0; i < args.length; i++) {
95             if("-help".equals(args[i])) {
96                 help();
97                 return;
98             }
99             if("-props".equals(args[i])) {
100                 props=args[++i];
101                 continue;
102             }
103             if("-use_additional_data".equals(args[i])) {
104                 use_additional_data=new Boolean JavaDoc(args[++i]).booleanValue();
105                 continue;
106             }
107             help();
108             return;
109         }
110
111         try {
112             t.start(props, use_additional_data);
113         }
114         catch(Exception JavaDoc e) {
115             System.err.println(e);
116         }
117     }
118
119     static void help() {
120         System.out.println("ViewDemo [-props <properties>] [-help] [-use_additional_data <flag>]");
121     }
122
123 }
124
Popular Tags