1 3 package org.jgroups.demos; 4 5 6 import org.jgroups.*; 7 import org.jgroups.blocks.PullPushAdapter; 8 9 import javax.swing.*; 10 import java.awt.*; 11 import java.awt.event.MouseEvent ; 12 import java.awt.event.MouseListener ; 13 import java.awt.event.WindowEvent ; 14 import java.awt.event.WindowListener ; 15 16 17 22 public class Chat implements MouseListener , WindowListener , MessageListener, MembershipListener { 23 Channel channel; 24 PullPushAdapter ad; 25 Thread mainThread; 26 final String group_name="ChatGroup"; 27 String props=null; 28 Frame mainFrame; 29 TextArea ta; 30 TextField tf; 31 Label csLabel; 32 JButton leaveButton; 33 JButton sendButton; 34 JButton clearButton; 35 36 37 public Chat(String props) { 38 this.props=props; 39 } 40 41 42 public static void main(String [] args) { 43 String props=null; 44 45 for(int i=0; i < args.length; i++) { 46 if("-props".equals(args[i])) { 47 props=args[++i]; 48 continue; 49 } 50 help(); 51 return; 52 } 53 54 Chat chat=new Chat(props); 55 chat.start(); 56 } 57 58 59 static void help() { 60 System.out.println("Chat [-help] [-props <properties>]"); 61 } 62 63 64 public void start() { 65 mainFrame=new Frame(); 66 mainFrame.setLayout(null); 67 mainFrame.setSize(600, 507); 68 mainFrame.addWindowListener(this); 69 70 ta=new TextArea(); 71 ta.setBounds(12, 36, 550, 348); 72 ta.setEditable(false); 73 mainFrame.add(ta); 74 75 tf=new TextField(); 76 tf.setBounds(100, 392, 400, 30); 77 mainFrame.add(tf); 78 79 csLabel=new Label("Send:"); 80 csLabel.setBounds(12, 392, 85, 30); 81 mainFrame.add(csLabel); 82 83 leaveButton=new JButton("Leave"); 84 leaveButton.setBounds(12, 428, 150, 30); 85 leaveButton.addMouseListener(this); 86 mainFrame.add(leaveButton); 87 88 sendButton=new JButton("Send"); 89 sendButton.setBounds(182, 428, 150, 30); 90 sendButton.addMouseListener(this); 91 mainFrame.add(sendButton); 92 93 clearButton=new JButton("Clear"); 94 clearButton.setBounds(340, 428, 150, 30); 95 clearButton.addMouseListener(this); 96 mainFrame.add(clearButton); 97 98 try { 99 channel=new JChannel(props); 100 channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE); 101 System.out.println("Connecting to " + group_name); 102 channel.connect(group_name); 103 ad=new PullPushAdapter(channel, this, this); 104 } 105 catch(Exception e) { 106 ta.append(e.toString()); 107 } 108 mainFrame.pack(); 109 mainFrame.setLocation(15, 25); 110 mainFrame.setBounds(new Rectangle(580, 480)); 111 mainFrame.setVisible(true); 112 mainFrame.show(); 113 } 114 115 116 117 118 119 public void receive(Message msg) { 120 Object o; 121 122 try { 123 o=msg.getObject(); 124 ta.append(o.toString() + " [" + msg.getSrc() + "]\n"); 125 } 126 catch(Exception e) { 127 ta.append("Chat.receive(): " + e); 128 } 129 } 130 131 public byte[] getState() { 132 return null; 133 } 134 135 public void setState(byte[] state) { 136 } 137 138 139 140 141 142 143 144 145 146 public void viewAccepted(View new_view) { 147 ta.append("Received view " + new_view + '\n'); 148 } 149 150 151 public void suspect(Address suspected_mbr) { 152 153 } 154 155 156 public void block() { 157 158 } 159 160 161 162 163 164 private synchronized void handleLeave() { 165 try { 166 System.out.print("Stopping PullPushAdapter"); 167 ad.stop(); 168 System.out.println(" -- done"); 169 170 System.out.print("Disconnecting the channel"); 171 channel.disconnect(); 172 System.out.println(" -- done"); 173 174 System.out.print("Closing the channel"); 175 channel.close(); 176 System.out.println(" -- done"); 177 } 178 catch(Exception e) { 179 e.printStackTrace(); 180 ta.append("Failed leaving the group: " + e.toString() + '\n'); 181 } 182 } 183 184 185 private void handleSend() { 186 try { 187 Message msg=new Message(null, null, tf.getText()); 188 channel.send(msg); 189 } 190 catch(Exception e) { 191 ta.append("Failed sending message: " + e.toString() + '\n'); 192 } 193 } 194 195 196 public void mouseClicked(MouseEvent e) { 197 Object obj=e.getSource(); 198 199 if(obj == leaveButton) 200 handleLeave(); 201 else if(obj == sendButton) 202 handleSend(); 203 else if(obj == clearButton) 204 ta.setText(""); 205 } 206 207 public void mouseEntered(MouseEvent e) {} 208 public void mouseExited(MouseEvent e) {} 209 public void mousePressed(MouseEvent e) {} 210 public void mouseReleased(MouseEvent e) {} 211 212 public void windowActivated(WindowEvent e) {} 213 public void windowClosed(WindowEvent e) {} 214 public void windowClosing(WindowEvent e) { System.exit(0); } 215 public void windowDeactivated(WindowEvent e) {} 216 public void windowDeiconified(WindowEvent e) {} 217 public void windowIconified(WindowEvent e) {} 218 public void windowOpened(WindowEvent e) {} 219 220 } 221 | Popular Tags |