KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: NotificationBusDemo.java,v 1.4 2004/09/23 16:29:35 belaban Exp $
2

3
4 package org.jgroups.demos;
5
6
7 import org.jgroups.Address;
8 import org.jgroups.blocks.NotificationBus;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16
17
18 /**
19  * Demoes the NotificationBus (without caching). Start a number of members and type in messages. All members will
20  * receive the messages. View changes will also be displayed (e.g. member joined, left).
21  * @author Bela Ban
22  */

23 public class NotificationBusDemo implements NotificationBus.Consumer {
24     NotificationBus bus=null;
25     BufferedReader JavaDoc in=null;
26     String JavaDoc line;
27     final long timeout=0;
28     final Vector JavaDoc cache=null;
29
30
31
32     public void start(String JavaDoc bus_name, String JavaDoc props) {
33     try {
34
35         bus=new NotificationBus(bus_name, props);
36         bus.start();
37         //System.out.println("Getting the cache from coordinator:");
38
//cache=(Vector)bus.getCacheFromCoordinator(3000, 3);
39
//if(cache == null) cache=new Vector();
40
//System.out.println("cache is " + cache);
41
bus.setConsumer(this);
42         in=new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
43         while(true) {
44         try {
45             System.out.print("> "); System.out.flush();
46             line=in.readLine();
47             if(line.startsWith("quit") || line.startsWith("exit")) {
48             bus.stop();
49             bus=null;
50             break;
51             }
52             bus.sendNotification(line);
53         }
54         catch(Exception JavaDoc e) {
55             System.err.println(e);
56         }
57         }
58     }
59     catch(Exception JavaDoc ex) {
60         System.err.println(ex);
61     }
62     finally {
63         if(bus != null)
64         bus.stop();
65     }
66     }
67
68
69     
70     public void handleNotification(Serializable JavaDoc n) {
71     System.out.println("** Received notification: " + n);
72     //if(cache != null)
73
// cache.addElement(n);
74
//System.out.println("cache is " + cache);
75
}
76
77     
78     public Serializable JavaDoc getCache() {
79     // return cache;
80
return null;
81     }
82
83
84     public void memberJoined(Address mbr) {
85     System.out.println("** Member joined: " + mbr);
86     }
87
88     public void memberLeft(Address mbr) {
89     System.out.println("** Member left: " + mbr);
90     }
91
92
93
94     public static void main(String JavaDoc[] args) {
95     String JavaDoc name="BusDemo";
96         String JavaDoc props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
97                 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
98                 "PING(timeout=2000;num_initial_members=3):" +
99                 "MERGE2(min_interval=5000;max_interval=10000):" +
100                 "FD_SOCK:" +
101                 "VERIFY_SUSPECT(timeout=1500):" +
102                 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
103                 "UNICAST(timeout=5000):" +
104                 "pbcast.STABLE(desired_avg_gossip=20000):" +
105                 "FRAG(frag_size=8096;down_thread=false;up_thread=false):" +
106                 // "CAUSAL:" +
107
"pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
108                 "shun=false;print_local_addr=true)";
109
110     for(int i=0; i < args.length; i++) {
111         if("-bus_name".equals(args[i])) {
112         name=args[++i];
113         continue;
114         }
115         if("-props".equals(args[i])) {
116         props=args[++i];
117         continue;
118         }
119         System.out.println("NotificationBusDemo [-help] [-bus_name <name>] " +
120                    "[-props <properties>]");
121         return;
122     }
123     System.out.println("Starting NotificationBus with name " + name);
124     new NotificationBusDemo().start(name, props);
125     }
126
127    
128 }
129
Popular Tags