KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: Topology.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.*;
8 import org.jgroups.blocks.PullPushAdapter;
9
10 import java.awt.*;
11 import java.awt.event.WindowEvent JavaDoc;
12 import java.awt.event.WindowListener JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16
17
18 /**
19  * Demonstrates the membership service. Each member is represented by a rectangle that contains the
20  * addresses of all the members. The coordinator (= oldest member in the group) is painted in blue.
21  * New members can be started; all existing members will update their graphical appearance to reflect
22  * the new membership. When the coordinator itself is killed, another one will take over (the next in rank).<p>
23  * A nice demo is to start a number of Topology instances at the same time. All of them will be blue (all are
24  * coordinators since they don't find each other). Then the MERGE2 protocol sets in and only one will retain
25  * its coordinator role.
26  * todo: needs to be ported to Swing
27  * @author Bela Ban
28  */

29 public class Topology extends Frame implements WindowListener JavaDoc, MembershipListener {
30     private final Vector JavaDoc members=new Vector JavaDoc();
31     private final Font font;
32     private final FontMetrics fm;
33     private final Color node_color=new Color(250, 220, 100);
34     private boolean coordinator=false;
35     private static final int NormalStyle=0;
36     private static final int CheckStyle=1;
37     private Channel channel;
38     private Object JavaDoc my_addr=null;
39     private static final String JavaDoc channel_name="FD-Heartbeat";
40
41
42     public Topology() {
43         addWindowListener(this);
44         //g=getGraphics();
45
fm=getFontMetrics(new Font("Helvetica", Font.PLAIN, 12));
46         font=new Font("Helvetica", Font.PLAIN, 12);
47
48     }
49
50
51     public void addNode(Object JavaDoc member) {
52         Object JavaDoc tmp;
53         for(int i=0; i < members.size(); i++) {
54             tmp=members.elementAt(i);
55             if(member.equals(tmp))
56                 return;
57         }
58         members.addElement(member);
59         repaint();
60     }
61
62
63     public void removeNode(Object JavaDoc member) {
64         Object JavaDoc tmp;
65         for(int i=0; i < members.size(); i++) {
66             tmp=members.elementAt(i);
67             if(member.equals(tmp)) {
68                 members.removeElement(members.elementAt(i));
69                 break;
70             }
71         }
72         repaint();
73     }
74
75
76     public void drawNode(Graphics g, int x, int y, String JavaDoc label, int style) {
77         Color old=g.getColor();
78         int width, height;
79         width=fm.stringWidth(label) + 10;
80         height=fm.getHeight() + 5;
81
82         g.setColor(node_color);
83
84         g.fillRect(x, y, width, height);
85         g.setColor(old);
86         g.drawString(label, x + 5, y + 15);
87         g.drawRoundRect(x - 1, y - 1, width + 1, height + 1, 10, 10);
88         if(style == CheckStyle) {
89             g.drawRoundRect(x - 2, y - 2, width + 2, height + 2, 10, 10);
90             g.drawRoundRect(x - 3, y - 3, width + 3, height + 3, 10, 10);
91         }
92     }
93
94
95     public void drawTopology(Graphics g) {
96         int x=20, y=50;
97         String JavaDoc label;
98         Dimension box=getSize();
99         Color old=g.getColor();
100
101         if(coordinator) {
102             g.setColor(Color.cyan);
103             g.fillRect(11, 31, box.width - 21, box.height - 61);
104             g.setColor(old);
105         }
106
107         g.drawRect(10, 30, box.width - 20, box.height - 60);
108         g.setFont(font);
109
110         for(int i=0; i < members.size(); i++) {
111             label=members.elementAt(i).toString();
112             drawNode(g, x, y, label, NormalStyle);
113             y+=50;
114         }
115
116
117     }
118
119
120     public void paint(Graphics g) {
121         drawTopology(g);
122     }
123
124
125     /* ------------ Callbacks ------------- */
126
127
128     public void viewAccepted(View view) {
129         setState(view.getMembers());
130     }
131
132     public void suspect(Address suspected_mbr) {
133     }
134
135     public void block() {
136     }
137
138
139     public void setState(Vector JavaDoc mbrs) {
140         members.removeAllElements();
141         for(int i=0; i < mbrs.size(); i++)
142             addNode(mbrs.elementAt(i));
143         if(mbrs.size() <= 1 || (mbrs.size() > 1 && mbrs.elementAt(0).equals(my_addr)))
144             coordinator=true;
145         else
146             coordinator=false;
147         repaint();
148     }
149
150
151     public void coordinatorChosen() {
152         coordinator=true;
153         repaint();
154     }
155
156
157     public void windowActivated(WindowEvent JavaDoc e) {
158     }
159
160     public void windowClosed(WindowEvent JavaDoc e) {
161     }
162
163     public void windowClosing(WindowEvent JavaDoc e) {
164         channel.close();
165         System.exit(0);
166     }
167
168     public void windowDeactivated(WindowEvent JavaDoc e) {
169     }
170
171     public void windowDeiconified(WindowEvent JavaDoc e) {
172     }
173
174     public void windowIconified(WindowEvent JavaDoc e) {
175     }
176
177     public void windowOpened(WindowEvent JavaDoc e) {
178     }
179
180
181     public void start() throws Exception JavaDoc {
182
183         //String props="TCP:" +
184
// "TCPPING(timeout=2000;num_initial_members=1;port_range=3;" +
185
// "initial_hosts=localhost[8880]):" +
186
// "FD:STABLE:NAKACK:FLUSH:GMS(join_timeout=12000):VIEW_ENFORCER:QUEUE";
187

188
189         // test for pbcast
190
//String props="UDP:PING:FD:" +
191
// "pbcast.PBCAST:UNICAST:FRAG:pbcast.GMS:" +
192
// "STATE_TRANSFER:QUEUE";
193

194
195         // test for pbcast
196
//String props="TCP:TCPPING(port_range=2;initial_hosts=daddy[8880],terrapin[8880]):FD:" +
197
// "pbcast.PBCAST:UNICAST:FRAG:pbcast.GMS:" +
198
// "STATE_TRANSFER:QUEUE";
199

200
201         // test2 for pbcast
202
//String props="UDP:PING:FD:" +
203
// "pbcast.PBCAST:UNICAST:FRAG:pbcast.GMS:" +
204
// "pbcast.STATE_TRANSFER";
205

206         // String props=null; // default properties
207

208         String JavaDoc props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
209                 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
210                 "PING(timeout=2000;num_initial_members=3):" +
211                 "MERGE2(min_interval=5000;max_interval=10000):" +
212                 "FD_SOCK:" +
213                 "VERIFY_SUSPECT(timeout=1500):" +
214                 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
215                 "UNICAST(timeout=5000):" +
216                 "pbcast.STABLE(desired_avg_gossip=20000):" +
217                 "FRAG(frag_size=4096;down_thread=false;up_thread=false):" +
218                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
219                 "shun=false;print_local_addr=true)";
220
221
222         channel=new JChannel(props);
223         channel.connect(channel_name);
224         new PullPushAdapter(channel, this);
225         my_addr=channel.getLocalAddress();
226         if(my_addr != null)
227             setTitle(my_addr.toString());
228         pack();
229         show();
230     }
231
232
233     public static void main(String JavaDoc[] args) {
234         try {
235             Topology top=new Topology();
236             top.setLayout(null);
237             top.setSize(240, 507);
238             top.start();
239         }
240         catch(Exception JavaDoc e) {
241             System.err.println(e);
242             e.printStackTrace();
243             System.exit(0);
244         }
245     }
246
247
248 }
249
250
251
Popular Tags