1 3 package org.jgroups.demos.wb; 4 5 import org.jgroups.*; 6 import org.jgroups.blocks.*; 7 import org.jgroups.blocks.GroupRequest; 8 import org.jgroups.blocks.RpcDispatcher; 9 10 import java.applet.Applet ; 11 import java.awt.*; 12 import java.awt.event.*; 13 14 15 16 22 public class Whiteboard extends Applet implements ActionListener, MessageListener, MembershipListener, 23 ComponentListener, FocusListener { 24 public RpcDispatcher disp; 25 Channel channel; 26 GraphPanel panel; 27 private Button leave_button; 28 private Label mbr_label; 29 private final Font default_font=new Font("Helvetica",Font.PLAIN,12); 30 private String props=null; 31 public static final String groupname="WbGrp"; 32 private boolean application=false; 33 34 35 public void receive(Message m) { 36 ; 37 } 38 39 public byte[] getState() { 40 panel.saveState(); 41 return panel.getState(); 42 } 43 44 public void setState(byte[] new_state) { 45 panel.setState(new_state); 46 } 47 48 49 private String getInfo() { 50 StringBuffer ret = new StringBuffer (); 51 ret.append(" (" + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + 52 ' ' + System.getProperty("os.arch") + ')'); 53 return ret.toString(); 54 } 55 56 private Frame findParent() { 57 Component retval = getParent(); 58 59 while (retval != null) { 60 if (retval instanceof Frame) 61 return (Frame) retval; 62 retval = retval.getParent(); 63 } 64 return null; 65 } 66 67 68 public Whiteboard() { } 70 71 public Whiteboard(String properties) { application = true; 73 props = properties; 74 75 } 76 77 78 public void init() { 79 setLayout(new BorderLayout()); 80 panel = new GraphPanel(this); 81 panel.setBackground(Color.white); 82 add("Center", panel); 83 Panel p = new Panel(); 84 leave_button = new Button("Exit"); 85 leave_button.setFont(default_font); 86 leave_button.addActionListener(this); 87 mbr_label = new Label("1 mbr(s)"); 88 mbr_label.setFont(default_font); 89 p.add("South", leave_button); 90 p.add("South", mbr_label); 91 add("South", p); 92 93 if (!application) 94 props = getParameter("properties"); 95 if (props == null) { 96 props = "UDP:PING:FD:" + 98 "pbcast.PBCAST:UNICAST:FRAG:pbcast.GMS:" + 99 "pbcast.STATE_TRANSFER"; 100 101 102 103 104 } 108 109 System.out.println("properties are " + props); 110 111 try { 112 channel = new JChannel(props); 113 disp = new RpcDispatcher(channel, this, this, this); 114 channel.connect(groupname); 115 System.out.println("INIT()"); 116 channel.getState(null, 0); 117 } catch (Exception e) { 118 System.err.println("Whiteboard.init(): " + e); 119 } 120 panel.my_addr = channel.getLocalAddress(); 121 122 123 UserInfoDialog dlg = new UserInfoDialog(findParent()); 124 String n = dlg.getUserName(); 125 String info = getInfo(); 126 panel.start(n + info); 127 128 129 addComponentListener(this); 130 addFocusListener(this); 131 } 132 133 134 public void destroy() { 135 if (disp != null) { 136 try { 137 MethodCall call = new MethodCall("removeNode", new Object [] {panel.my_addr}, new String [] {Object .class.getName()}); 138 disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0); 139 } catch (Exception e) { 140 System.err.println(e); 141 } 142 channel.close(); 143 disp = null; 144 if (panel != null) { 145 panel.stop(); 146 panel = null; 147 } 148 } 149 150 } 151 152 153 public void repaint() { 154 if (panel != null) 155 panel.repaint(); 156 } 157 158 159 public void actionPerformed(ActionEvent e) { 160 String command = e.getActionCommand(); 161 162 if ("Exit".equals(command)) { 163 try { 164 setVisible(false); 165 destroy(); 166 if (application) { 167 ((Frame) getParent()).dispose(); 168 System.exit(0); 169 } 170 } catch (Exception ex) { 171 System.err.println(ex); 172 } 173 174 } else 175 System.out.println("Unknown action"); 176 } 177 178 179 public void viewAccepted(View v) { 180 if (v != null) { 181 if (mbr_label != null) 182 mbr_label.setText(v.size() + " mbr(s)"); 183 } 184 panel.adjustNodes(v.getMembers()); 185 } 186 187 public void suspect(Address obj) { 188 } 189 190 public void block() { 191 } 192 193 194 public void moveNode(Node n) { 195 panel.moveNode(n); 196 } 197 198 199 public void addNode(String lbl, Address addr, int xloc, int yloc) { 200 panel.addNode(lbl, addr, xloc, yloc); 201 } 202 203 204 public void removeNode(Object addr) { 205 panel.removeNode(addr); 206 } 207 208 209 public void displayMessage(String sender, String msg) { 210 new MessageDialog(findParent(), sender, msg); 211 panel.repaint(); 212 } 213 214 215 public void componentResized(ComponentEvent e) { 216 if (panel != null) panel.repaint(); 217 } 218 219 public void componentMoved(ComponentEvent e) { 220 } 221 222 public void componentShown(ComponentEvent e) { 223 if (panel != null) panel.repaint(); 224 } 225 226 public void componentHidden(ComponentEvent e) { 227 } 228 229 230 public void focusGained(FocusEvent e) { 231 if (panel != null) panel.repaint(); 232 } 233 234 235 public void focusLost(FocusEvent e) { 236 } 237 238 239 public static void main(String [] args) { 240 String props = null; 241 242 for (int i = 0; i < args.length; i++) { 243 if ("-props".equals(args[i])) { 244 props = args[++i]; 245 continue; 246 } 247 help(); 248 return; 249 } 250 251 Whiteboard wb = new Whiteboard(props); 252 new ApplFrame("Whiteboard Application", wb); 253 } 254 255 static void help() { 256 System.out.println("Whiteboard [-help] [-props <props>]"); 257 } 258 259 260 } 261 262 263 class ApplFrame extends Frame implements WindowListener, ComponentListener { 264 Whiteboard wb = null; 265 266 public ApplFrame(String title, Whiteboard wb) { 267 super(title); 268 this.wb = wb; 269 add(wb); 270 setSize(299, 299); 271 setVisible(true); 272 wb.init(); 273 setSize(300, 300); 274 addWindowListener(this); 275 addComponentListener(this); 276 } 277 278 279 public void windowOpened(WindowEvent e) { 280 } 281 282 public void windowClosing(WindowEvent e) { 283 dispose(); 284 System.exit(0); 285 } 286 287 public void windowClosed(WindowEvent e) { 288 } 289 290 public void windowIconified(WindowEvent e) { 291 } 292 293 public void windowDeiconified(WindowEvent e) { 294 wb.repaint(); 295 } 296 297 public void windowActivated(WindowEvent e) { 298 wb.repaint(); 299 } 300 301 public void windowDeactivated(WindowEvent e) { 302 } 303 304 public void componentResized(ComponentEvent e) { 305 wb.repaint(); 306 } 307 308 public void componentMoved(ComponentEvent e) { 309 } 310 311 312 public void componentShown(ComponentEvent e) { 313 } 314 315 public void componentHidden(ComponentEvent e) { 316 } 317 318 319 } 320 321 322 | Popular Tags |