1 package org.jgroups.tests; 2 3 4 import org.jgroups.Address; 5 import org.jgroups.JChannel; 6 import org.jgroups.Message; 7 import org.jgroups.View; 8 import org.jgroups.debug.Debugger; 9 10 import javax.swing.*; 11 import javax.swing.border.BevelBorder ; 12 import javax.swing.border.Border ; 13 import javax.swing.border.EtchedBorder ; 14 import javax.swing.border.TitledBorder ; 15 import java.awt.*; 16 import java.awt.event.ActionEvent ; 17 import java.awt.event.KeyEvent ; 18 import java.awt.event.WindowEvent ; 19 import java.util.Hashtable ; 20 import java.util.Map ; 21 22 23 45 46 47 public class PartitionerTest extends JFrame implements Runnable { 48 static String channel_properties="UDP:PARTITIONER:SHUFFLE:MERGE" + 49 ":PING:FD(shun=false):" + 50 "STABLE:" + 51 "NAKACK:FRAG:FLUSH:GMS:" + 52 "VIEW_ENFORCER:" + 53 "QUEUE"; 54 JChannel channel; 55 Debugger debugger; 56 57 boolean connected=false; 58 Hashtable ht=new Hashtable (); 59 60 JPanel jPanel1=new JPanel(); 61 BorderLayout borderLayout1=new BorderLayout(); 62 JPanel statusbar=new JPanel(); 63 JLabel status=new JLabel(); 64 BorderLayout borderLayout2=new BorderLayout(); 65 JPanel jPanel3=new JPanel(); 66 JScrollPane jScrollPane2=new JScrollPane(); 67 JList partitionsList=new JList(); 68 BorderLayout borderLayout3=new BorderLayout(); 69 JLabel partitionLabel=new JLabel(); 70 JTextField partitionField=new JTextField(); 71 JButton execute=new JButton(); 72 JButton set=new JButton(); 73 JPanel jPanel2=new JPanel(); 74 GridLayout gridLayout1=new GridLayout(); 75 TitledBorder titledBorder1; 76 Border border1; 77 Border border2; 78 TitledBorder titledBorder2; 79 Border border3; 80 TitledBorder titledBorder3; 81 BorderLayout borderLayout5=new BorderLayout(); 82 JButton connect=new JButton(); 83 JPanel applicationPanel=new JPanel(); 84 JTextArea output=new JTextArea(); 85 JScrollPane jScrollPane1=new JScrollPane(); 86 JTextField commandTextField=new JTextField(); 87 BorderLayout borderLayout4=new BorderLayout(); 88 89 90 public PartitionerTest(JChannel channel) { 91 this.channel=channel; 92 init(); 93 } 94 95 public PartitionerTest() { 96 try { 97 channel=new JChannel(channel_properties); 98 } 99 catch(Exception ex) { 100 ex.printStackTrace(); 101 System.exit(4); 102 } 103 init(); 104 } 105 106 109 110 void init() { 111 try { 112 channel.connect("prova"); 113 connected=true; 114 } 119 catch(Exception ex) { 120 System.exit(4); 121 } 122 try { 123 jbInit(); 124 } 125 catch(Exception e) { 126 e.printStackTrace(); 127 } 128 Thread receiver=new Thread (this, "Receiver"); 129 receiver.start(); 130 } 131 132 136 137 private void jbInit() throws Exception { 138 titledBorder1=new TitledBorder (""); 139 border1=BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145, 140)), BorderFactory.createEmptyBorder(4, 4, 4, 4)); 140 border2=BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color(148, 145, 140), new Color(103, 101, 98)); 141 titledBorder2=new TitledBorder (new EtchedBorder (EtchedBorder.RAISED, Color.white, new Color(148, 145, 140)), "Processes"); 142 border3=BorderFactory.createLineBorder(Color.black, 2); 143 titledBorder3=new TitledBorder (new EtchedBorder (EtchedBorder.RAISED, Color.white, new Color(148, 145, 140)), "Channel Output"); 144 this.setSize(400, 400); 145 this.setTitle("Partitioner Test :" + channel.getLocalAddress()); 146 this.addWindowListener(new java.awt.event.WindowAdapter () { 147 public void windowClosing(WindowEvent e) { 148 this_windowClosing(e); 149 } 150 }); 151 this.getContentPane().setLayout(borderLayout1); 152 jPanel1.setLayout(borderLayout3); 153 statusbar.setLayout(borderLayout2); 154 status.setMinimumSize(new Dimension(100, 20)); 155 status.setText("ciao"); 156 statusbar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color(148, 145, 140), new Color(103, 101, 98))); 157 jPanel3.setLayout(borderLayout5); 158 partitionLabel.setHorizontalAlignment(SwingConstants.CENTER); 159 partitionLabel.setText("Partition"); 160 execute.setText("Execute"); 161 execute.addActionListener(new java.awt.event.ActionListener () { 162 public void actionPerformed(ActionEvent e) { 163 execute_actionPerformed(e); 164 } 165 }); 166 set.addActionListener(new java.awt.event.ActionListener () { 167 public void actionPerformed(ActionEvent e) { 168 set_actionPerformed(e); 169 } 170 }); 171 set.setText("Set"); 172 set.setActionCommand("Set"); 173 jPanel2.setLayout(gridLayout1); 174 partitionField.setMinimumSize(new Dimension(20, 20)); 175 gridLayout1.setRows(8); 176 gridLayout1.setColumns(1); 177 gridLayout1.setVgap(4); 178 jPanel3.setBorder(border1); 179 jPanel3.setMinimumSize(new Dimension(50, 50)); 180 jPanel3.setPreferredSize(new Dimension(200, 200)); 181 jScrollPane2.setBorder(titledBorder2); 182 jScrollPane2.setMinimumSize(new Dimension(100, 80)); 183 jPanel2.setMinimumSize(new Dimension(77, 100)); 184 partitionsList.setBorder(BorderFactory.createLoweredBevelBorder()); 185 partitionsList.setMinimumSize(new Dimension(100, 80)); 186 connect.setActionCommand("Connect"); 187 connect.setText("Disconnect"); 188 connect.addActionListener(new java.awt.event.ActionListener () { 189 public void actionPerformed(ActionEvent e) { 190 connect_actionPerformed(e); 191 } 192 }); 193 jScrollPane1.setAutoscrolls(true); 194 jScrollPane1.setBorder(titledBorder3); 195 jScrollPane1.setMinimumSize(new Dimension(100, 100)); 196 jScrollPane1.setPreferredSize(new Dimension(300, 100)); 197 applicationPanel.setLayout(borderLayout4); 198 commandTextField.addKeyListener(new java.awt.event.KeyAdapter () { 199 public void keyTyped(KeyEvent e) { 200 commandTextField_keyTyped(e); 201 } 202 }); 203 commandTextField.setMaximumSize(new Dimension(300, 21)); 204 this.getContentPane().add(statusbar, BorderLayout.SOUTH); 205 statusbar.add(status, BorderLayout.CENTER); 206 this.getContentPane().add(jPanel1, BorderLayout.NORTH); 207 jPanel1.add(jPanel3, BorderLayout.CENTER); 208 jPanel3.add(jScrollPane2, BorderLayout.NORTH); 209 jScrollPane2.getViewport().add(partitionsList, null); 210 jPanel1.add(jPanel2, BorderLayout.WEST); 211 jPanel2.add(partitionLabel, null); 212 jPanel2.add(partitionField, null); 213 jPanel2.add(set, null); 214 jPanel2.add(execute, null); 215 jPanel2.add(connect, null); 216 this.getContentPane().add(applicationPanel, BorderLayout.CENTER); 217 applicationPanel.add(jScrollPane1, BorderLayout.CENTER); 218 applicationPanel.add(commandTextField, BorderLayout.SOUTH); 219 jScrollPane1.getViewport().add(output, null); 220 } 221 222 public void run() { 223 boolean running=true; 224 Object received=null; 225 View view; 226 Message msg; 227 String payload; 228 while(running) { 229 try { 230 received=channel.receive(0); 231 } 232 catch(Exception ex) { 233 System.out.println("PartitionerTest.run() :" + ex); 234 System.exit(-1); 235 } 236 if(received instanceof View) { 237 view=(View)received; 238 status.setText(view.toString()); 239 for(int i=0; i < view.size(); i++) { 240 Address member=(Address)view.getMembers().elementAt(i); 241 if(ht.get(member) == null) { 242 ht.put(member, new Integer (1)); 243 } 244 } 245 partitionsList.setListData(ht.entrySet().toArray()); 246 output.append(view + "\n"); 247 output.setCaretPosition(output.getText().length()); 248 } 249 250 if(received instanceof Message) { 251 msg=(Message)received; 252 Object tmp=null; 253 tmp=msg.getObject(); 254 if(tmp instanceof String ) { 255 payload=(String )tmp; 256 if("stop".equals(payload)) { 257 running=false; 258 } 259 output.append(':' + payload + '\n'); 260 } 261 else 262 output.append("Received not a String\n"); 263 } 264 output.setCaretPosition(output.getText().length()); 265 } 266 channel.close(); 267 output.append("Channel Closed\n"); 268 } 270 273 274 void set_actionPerformed(ActionEvent e) { 275 if(partitionsList.getSelectedValue() != null) { 276 Map.Entry entry=(Map.Entry )partitionsList.getSelectedValue(); 277 entry.setValue(new Integer (partitionField.getText())); 278 partitionsList.setListData(ht.entrySet().toArray()); 279 } 280 } 281 282 285 286 void sendButton_actionPerformed(ActionEvent e) { 287 try { 288 channel.send(new Message(null, null, commandTextField.getText())); 289 commandTextField.setText(""); 290 } 291 catch(Exception ex) { 292 ex.printStackTrace(); 293 } 294 295 } 296 297 void this_windowClosing(WindowEvent e) { 298 channel.close(); 299 System.out.println("Exiting"); 300 System.exit(0); 301 } 302 303 void execute_actionPerformed(ActionEvent e) { 304 channel.down(new org.jgroups.Event(org.jgroups.Event.SET_PARTITIONS, ht)); 305 } 306 307 static void main(String [] args) { 308 309 PartitionerTest frame=new PartitionerTest(); 310 frame.validate(); 311 System.out.println("Frame validated"); 312 Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); 314 Dimension frameSize=frame.getSize(); 315 if(frameSize.height > screenSize.height) 316 frameSize.height=screenSize.height; 317 if(frameSize.width > screenSize.width) 318 frameSize.width=screenSize.width; 319 frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 320 frame.setVisible(true); 321 System.out.println("Frame Visible"); 322 } 323 324 void connect_actionPerformed(ActionEvent e) { 325 try { 326 if(!connected) { 327 channel.connect("prova"); 328 connected=true; 329 connect.setText("Disconnect"); 330 } 331 else { 332 channel.disconnect(); 333 connected=false; 334 connect.setText("Connect"); 335 status.setText("Disconnected"); 336 } 337 } 338 catch(Exception ex) { 339 ex.printStackTrace(); 340 } 341 } 342 343 void commandTextField_keyTyped(KeyEvent e) { 344 if(e.getKeyChar() == '\n') sendButton_actionPerformed(null); 345 } 346 } 347 | Popular Tags |