1 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 ; 12 import java.awt.event.WindowListener ; 13 import java.util.Vector ; 14 15 16 17 18 29 public class Topology extends Frame implements WindowListener , MembershipListener { 30 private final Vector members=new Vector (); 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 my_addr=null; 39 private static final String channel_name="FD-Heartbeat"; 40 41 42 public Topology() { 43 addWindowListener(this); 44 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 member) { 52 Object 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 member) { 64 Object 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 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 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 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 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 e) { 158 } 159 160 public void windowClosed(WindowEvent e) { 161 } 162 163 public void windowClosing(WindowEvent e) { 164 channel.close(); 165 System.exit(0); 166 } 167 168 public void windowDeactivated(WindowEvent e) { 169 } 170 171 public void windowDeiconified(WindowEvent e) { 172 } 173 174 public void windowIconified(WindowEvent e) { 175 } 176 177 public void windowOpened(WindowEvent e) { 178 } 179 180 181 public void start() throws Exception { 182 183 188 189 194 195 200 201 206 208 String 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 [] args) { 234 try { 235 Topology top=new Topology(); 236 top.setLayout(null); 237 top.setSize(240, 507); 238 top.start(); 239 } 240 catch(Exception e) { 241 System.err.println(e); 242 e.printStackTrace(); 243 System.exit(0); 244 } 245 } 246 247 248 } 249 250 251 | Popular Tags |