001: // $Id: Whiteboard.java,v 1.5 2005/05/30 16:14:37 belaban Exp $
002:
003: package org.jgroups.demos.wb;
004:
005: import org.jgroups.*;
006: import org.jgroups.blocks.*;
007: import org.jgroups.blocks.GroupRequest;
008: import org.jgroups.blocks.RpcDispatcher;
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import java.applet.Applet;
013: import java.awt.*;
014: import java.awt.event.*;
015:
016: /**
017: * Shared whiteboard: members are represented by rectangles that contain their names and the OS/arch of
018: * the machine they are working on. The boxes can be moved by anyone and by clicking on them, messages can
019: * be sent to specific or all members. Whiteboard is both an application and an applet.
020: * @author Bela Ban
021: */
022: public class Whiteboard extends Applet implements ActionListener,
023: MessageListener, MembershipListener, ComponentListener,
024: FocusListener {
025: public RpcDispatcher disp;
026: Channel channel;
027: GraphPanel panel;
028: private Button leave_button;
029: private Label mbr_label;
030: private final Font default_font = new Font("Helvetica", Font.PLAIN,
031: 12);
032: private String props = null;
033: public static final String groupname = "WbGrp";
034: private boolean application = false;
035: Log log = LogFactory.getLog(getClass());
036:
037: public void receive(Message m) {
038: ;
039: }
040:
041: public byte[] getState() {
042: panel.saveState();
043: return panel.getState();
044: }
045:
046: public void setState(byte[] new_state) {
047: panel.setState(new_state);
048: }
049:
050: private String getInfo() {
051: StringBuffer ret = new StringBuffer();
052: ret.append(" (" + System.getProperty("os.name") + ' '
053: + System.getProperty("os.version") + ' '
054: + System.getProperty("os.arch") + ')');
055: return ret.toString();
056: }
057:
058: private Frame findParent() {
059: Component retval = getParent();
060:
061: while (retval != null) {
062: if (retval instanceof Frame)
063: return (Frame) retval;
064: retval = retval.getParent();
065: }
066: return null;
067: }
068:
069: public Whiteboard() { // called when started as applet
070: }
071:
072: public Whiteboard(String properties) { // called when started as application
073: application = true;
074: props = properties;
075:
076: }
077:
078: public void init() {
079: setLayout(new BorderLayout());
080: panel = new GraphPanel(this );
081: panel.setBackground(Color.white);
082: add("Center", panel);
083: Panel p = new Panel();
084: leave_button = new Button("Exit");
085: leave_button.setFont(default_font);
086: leave_button.addActionListener(this );
087: mbr_label = new Label("1 mbr(s)");
088: mbr_label.setFont(default_font);
089: p.add("South", leave_button);
090: p.add("South", mbr_label);
091: add("South", p);
092:
093: if (!application)
094: props = getParameter("properties");
095: if (props == null) {
096: // props="UDP:PING:FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:STATE_TRANSFER:QUEUE";
097: props = "UDP:PING:FD:"
098: + "pbcast.PBCAST:UNICAST:FRAG:pbcast.GMS:"
099: + "pbcast.STATE_TRANSFER";
100:
101: //props="TCP:" +
102: //"TCPPING(port_range=3;initial_hosts=localhost[8880]):" +
103: //"FD:STABLE:NAKACK:FLUSH:GMS:VIEW_ENFORCER:STATE_TRANSFER:QUEUE";
104: }
105:
106: System.out.println("properties are " + props);
107:
108: try {
109: channel = new JChannel(props);
110: disp = new RpcDispatcher(channel, this , this , this );
111: channel.connect(groupname);
112: System.out.println("INIT()");
113: channel.getState(null, 0);
114: } catch (Exception e) {
115: log.error("Whiteboard.init(): " + e);
116: }
117: panel.my_addr = channel.getLocalAddress();
118:
119: UserInfoDialog dlg = new UserInfoDialog(findParent());
120: String n = dlg.getUserName();
121: String info = getInfo();
122: panel.start(n + info);
123:
124: addComponentListener(this );
125: addFocusListener(this );
126: }
127:
128: public void destroy() {
129: if (disp != null) {
130: try {
131: MethodCall call = new MethodCall("removeNode",
132: new Object[] { panel.my_addr },
133: new String[] { Object.class.getName() });
134: disp.callRemoteMethods(null, call,
135: GroupRequest.GET_ALL, 0);
136: } catch (Exception e) {
137: log.error(e);
138: }
139: channel.close();
140: disp = null;
141: if (panel != null) {
142: panel.stop();
143: panel = null;
144: }
145: }
146:
147: }
148:
149: public void repaint() {
150: if (panel != null)
151: panel.repaint();
152: }
153:
154: public void actionPerformed(ActionEvent e) {
155: String command = e.getActionCommand();
156:
157: if ("Exit".equals(command)) {
158: try {
159: setVisible(false);
160: destroy();
161: if (application) {
162: ((Frame) getParent()).dispose();
163: System.exit(0);
164: }
165: } catch (Exception ex) {
166: log.error(ex);
167: }
168:
169: } else
170: System.out.println("Unknown action");
171: }
172:
173: public void viewAccepted(View v) {
174: if (v != null) {
175: if (mbr_label != null)
176: mbr_label.setText(v.size() + " mbr(s)");
177: }
178: panel.adjustNodes(v.getMembers());
179: }
180:
181: public void suspect(Address obj) {
182: }
183:
184: public void block() {
185: }
186:
187: public void moveNode(Node n) {
188: panel.moveNode(n);
189: }
190:
191: public void addNode(String lbl, Address addr, int xloc, int yloc) {
192: panel.addNode(lbl, addr, xloc, yloc);
193: }
194:
195: public void removeNode(Object addr) {
196: panel.removeNode(addr);
197: }
198:
199: public void displayMessage(String sender, String msg) {
200: new MessageDialog(findParent(), sender, msg);
201: panel.repaint();
202: }
203:
204: public void componentResized(ComponentEvent e) {
205: if (panel != null)
206: panel.repaint();
207: }
208:
209: public void componentMoved(ComponentEvent e) {
210: }
211:
212: public void componentShown(ComponentEvent e) {
213: if (panel != null)
214: panel.repaint();
215: }
216:
217: public void componentHidden(ComponentEvent e) {
218: }
219:
220: public void focusGained(FocusEvent e) {
221: if (panel != null)
222: panel.repaint();
223: }
224:
225: public void focusLost(FocusEvent e) {
226: }
227:
228: public static void main(String[] args) {
229: String props = null;
230:
231: for (int i = 0; i < args.length; i++) {
232: if ("-props".equals(args[i])) {
233: props = args[++i];
234: continue;
235: }
236: help();
237: return;
238: }
239:
240: Whiteboard wb = new Whiteboard(props);
241: new ApplFrame("Whiteboard Application", wb);
242: }
243:
244: static void help() {
245: System.out.println("Whiteboard [-help] [-props <props>]");
246: }
247:
248: }
249:
250: class ApplFrame extends Frame implements WindowListener,
251: ComponentListener {
252: Whiteboard wb = null;
253:
254: public ApplFrame(String title, Whiteboard wb) {
255: super (title);
256: this .wb = wb;
257: add(wb);
258: setSize(299, 299);
259: setVisible(true);
260: wb.init();
261: setSize(300, 300);
262: addWindowListener(this );
263: addComponentListener(this );
264: }
265:
266: public void windowOpened(WindowEvent e) {
267: }
268:
269: public void windowClosing(WindowEvent e) {
270: dispose();
271: System.exit(0);
272: }
273:
274: public void windowClosed(WindowEvent e) {
275: }
276:
277: public void windowIconified(WindowEvent e) {
278: }
279:
280: public void windowDeiconified(WindowEvent e) {
281: wb.repaint();
282: }
283:
284: public void windowActivated(WindowEvent e) {
285: wb.repaint();
286: }
287:
288: public void windowDeactivated(WindowEvent e) {
289: }
290:
291: public void componentResized(ComponentEvent e) {
292: wb.repaint();
293: }
294:
295: public void componentMoved(ComponentEvent e) {
296: }
297:
298: public void componentShown(ComponentEvent e) {
299: }
300:
301: public void componentHidden(ComponentEvent e) {
302: }
303:
304: }
|