001: package org.jgroups.demos;
002:
003: import org.jgroups.*;
004: import org.jgroups.util.Util;
005: import org.jgroups.blocks.PullPushAdapter;
006:
007: import javax.swing.*;
008: import java.awt.*;
009: import java.awt.event.MouseEvent;
010: import java.awt.event.MouseListener;
011: import java.awt.event.WindowEvent;
012: import java.awt.event.WindowListener;
013: import java.util.LinkedList;
014: import java.util.Iterator;
015: import java.io.*;
016:
017: /**
018: * Simple chat demo
019: * @author Bela Ban
020: * @version $Id: Chat.java,v 1.15 2006/09/27 19:21:54 vlada Exp $
021: */
022: public class Chat implements MouseListener, WindowListener,
023: ExtendedMessageListener, ExtendedMembershipListener {
024: Channel channel;
025: PullPushAdapter ad;
026: Thread mainThread;
027: final String group_name = "ChatGroup";
028: String props = null;
029: Frame mainFrame;
030: TextArea ta;
031: TextField tf;
032: Label csLabel;
033: JButton leaveButton;
034: JButton sendButton;
035: JButton clearButton;
036: String username = null;
037: LinkedList history = new LinkedList();
038:
039: public Chat(String props) {
040: this .props = props;
041: try {
042: username = System.getProperty("user.name");
043: } catch (Throwable t) {
044: }
045: }
046:
047: public static void main(String[] args) {
048: String props = null;
049:
050: for (int i = 0; i < args.length; i++) {
051: if ("-props".equals(args[i])) {
052: props = args[++i];
053: continue;
054: }
055: help();
056: return;
057: }
058:
059: Chat chat = new Chat(props);
060: chat.start();
061: }
062:
063: static void help() {
064: System.out.println("Chat [-help] [-props <properties>]");
065: }
066:
067: public void start() {
068: mainFrame = new Frame();
069: mainFrame.setLayout(null);
070: mainFrame.setSize(600, 507);
071: mainFrame.addWindowListener(this );
072:
073: ta = new TextArea();
074: ta.setBounds(12, 36, 550, 348);
075: ta.setEditable(false);
076: mainFrame.add(ta);
077:
078: tf = new TextField();
079: tf.setBounds(100, 392, 400, 30);
080: mainFrame.add(tf);
081:
082: csLabel = new Label("Send:");
083: csLabel.setBounds(12, 392, 85, 30);
084: mainFrame.add(csLabel);
085:
086: leaveButton = new JButton("Leave");
087: leaveButton.setBounds(12, 428, 150, 30);
088: leaveButton.addMouseListener(this );
089: mainFrame.add(leaveButton);
090:
091: sendButton = new JButton("Send");
092: sendButton.setBounds(182, 428, 150, 30);
093: sendButton.addMouseListener(this );
094: mainFrame.add(sendButton);
095:
096: clearButton = new JButton("Clear");
097: clearButton.setBounds(340, 428, 150, 30);
098: clearButton.addMouseListener(this );
099: mainFrame.add(clearButton);
100:
101: try {
102: channel = new JChannel(props);
103: channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
104: channel.setOpt(Channel.AUTO_GETSTATE, Boolean.TRUE);
105: channel.setOpt(Channel.BLOCK, Boolean.TRUE);
106: System.out.println("Connecting to " + group_name);
107: channel.connect(group_name);
108: ad = new PullPushAdapter(channel, this , this );
109: channel.getState(null, 5000);
110: } catch (Exception e) {
111: ta.append(e.toString());
112: }
113: mainFrame.pack();
114: mainFrame.setLocation(15, 25);
115: mainFrame.setBounds(new Rectangle(580, 480));
116: mainFrame.setVisible(true);
117: mainFrame.show();
118: if (history.size() > 0) {
119: for (Iterator it = history.iterator(); it.hasNext();) {
120: String s = (String) it.next();
121: ta.append(s + "\n");
122: }
123: }
124: }
125:
126: /* -------------------- Interface MessageListener ------------------- */
127:
128: public void receive(Message msg) {
129: Object o;
130:
131: try {
132: o = msg.getObject();
133: ta.append(o + " [" + msg.getSrc() + "]\n");
134: history.add(o);
135: } catch (Exception e) {
136: ta.append("Chat.receive(): " + e);
137: }
138: }
139:
140: public byte[] getState(String state_id) {
141: //partial state transfer not used
142: return null;
143: }
144:
145: public byte[] getState() {
146: try {
147: return Util.objectToByteBuffer(history);
148: } catch (Exception e) {
149: return null;
150: }
151: }
152:
153: public void setState(byte[] state) {
154: try {
155: history = (LinkedList) Util.objectFromByteBuffer(state);
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160:
161: public void setState(String state_id, byte[] state) {
162: //partial state transfer not used
163: }
164:
165: public void getState(OutputStream os) {
166: ObjectOutputStream oos = null;
167: try {
168: oos = new ObjectOutputStream(os);
169: oos.writeObject(history);
170: oos.flush();
171: } catch (IOException e) {
172: } finally {
173: try {
174: oos.close();
175: } catch (IOException e) {
176: System.err.println(e);
177: }
178: }
179: }
180:
181: public void setState(InputStream is) {
182: ObjectInputStream ois = null;
183: try {
184: ois = new ObjectInputStream(is);
185: history = (LinkedList) ois.readObject();
186: } catch (Exception e) {
187: } finally {
188: try {
189: ois.close();
190: } catch (IOException e) {
191: System.err.println(e);
192: }
193: }
194: }
195:
196: public void getState(String state_id, OutputStream ostream) {
197: //partial state transfer not used
198:
199: }
200:
201: public void setState(String state_id, InputStream istream) {
202: //partial state transfer not used
203: }
204:
205: /* ----------------- End of Interface MessageListener --------------- */
206:
207: /* ------------------- Interface MembershipListener ----------------- */
208:
209: public void viewAccepted(View new_view) {
210: ta.append("Received view " + new_view + '\n');
211: }
212:
213: public void suspect(Address suspected_mbr) {
214: }
215:
216: public void block() {
217: }
218:
219: public void unblock() {
220: }
221:
222: /* --------------- End of Interface MembershipListener -------------- */
223:
224: private synchronized void handleLeave() {
225: try {
226: System.out.print("Stopping PullPushAdapter");
227: ad.stop();
228: System.out.println(" -- done");
229:
230: System.out.print("Disconnecting the channel");
231: channel.disconnect();
232: System.out.println(" -- done");
233:
234: System.out.print("Closing the channel");
235: channel.close();
236: System.out.println(" -- done");
237: System.exit(0);
238: } catch (Exception e) {
239: e.printStackTrace();
240: ta.append("Failed leaving the group: " + e.toString()
241: + '\n');
242: }
243: }
244:
245: private void handleSend() {
246: try {
247: Message msg = new Message(null, null, username + ": "
248: + tf.getText());
249: channel.send(msg);
250: } catch (Exception e) {
251: ta.append("Failed sending message: " + e.toString() + '\n');
252: }
253: }
254:
255: public void mouseClicked(MouseEvent e) {
256: Object obj = e.getSource();
257:
258: if (obj == leaveButton)
259: handleLeave();
260: else if (obj == sendButton)
261: handleSend();
262: else if (obj == clearButton)
263: ta.setText("");
264: }
265:
266: public void mouseEntered(MouseEvent e) {
267: }
268:
269: public void mouseExited(MouseEvent e) {
270: }
271:
272: public void mousePressed(MouseEvent e) {
273: }
274:
275: public void mouseReleased(MouseEvent e) {
276: }
277:
278: public void windowActivated(WindowEvent e) {
279: }
280:
281: public void windowClosed(WindowEvent e) {
282: }
283:
284: public void windowClosing(WindowEvent e) {
285: System.exit(0);
286: }
287:
288: public void windowDeactivated(WindowEvent e) {
289: }
290:
291: public void windowDeiconified(WindowEvent e) {
292: }
293:
294: public void windowIconified(WindowEvent e) {
295: }
296:
297: public void windowOpened(WindowEvent e) {
298: }
299:
300: }
|