KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > PartitionerTest


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 JavaDoc;
12 import javax.swing.border.Border JavaDoc;
13 import javax.swing.border.EtchedBorder JavaDoc;
14 import javax.swing.border.TitledBorder JavaDoc;
15 import java.awt.*;
16 import java.awt.event.ActionEvent JavaDoc;
17 import java.awt.event.KeyEvent JavaDoc;
18 import java.awt.event.WindowEvent JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Map JavaDoc;
21
22
23 /**
24  * Title: Demos of PARTITIONER protocol. Simulates network partitions.
25  * Description: The usage of PartitionerTestFrame is quite simple:
26  * As the program finds new members in the view it adds them to the Hashtable and
27  * to Processes list.
28  * You can chose a Process from the processes list and digit a number over the set button.
29  * Once you push the Set button the number is stored in the local Hastable.
30  * Once you push the Execute button the local Hashtable is sent with a SET_PARTITIONS Event.
31  * <p/>
32  * The TextArea under the output area is to send messages over the channel and you view
33  * the results on the output area.
34  * <p/>
35  * In the future it may be a good thing let the PARTITIONER layer spawn a control Frame
36  * so that the use of this layer is transparent to the application, but it is also possible
37  * to use a partitionerTestFrame (a little modified) in a Group of other Processes just
38  * to control their behaviour against partitions.
39  * Copyright: Copyright (c) 2000
40  * Company: Computer Network Laboratory
41  *
42  * @author Gianluca Collot
43  * @version 1.1
44  */

45
46
47 public class PartitionerTest extends JFrame implements Runnable JavaDoc {
48     static String JavaDoc 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 JavaDoc ht=new Hashtable JavaDoc();
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 JavaDoc titledBorder1;
76     Border JavaDoc border1;
77     Border JavaDoc border2;
78     TitledBorder JavaDoc titledBorder2;
79     Border JavaDoc border3;
80     TitledBorder JavaDoc 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 JavaDoc ex) {
100             ex.printStackTrace();
101             System.exit(4);
102         }
103         init();
104     }
105
106     /**
107      * Initializes the channel and application threads
108      */

109
110     void init() {
111         try {
112             channel.connect("prova");
113             connected=true;
114 // if (channel.getView().getMembers().elementAt(0).equals(channel.getLocalAddress())) {
115
// Debugger debugger = new Debugger(channel);
116
// debugger.start();
117
// }
118
}
119         catch(Exception JavaDoc ex) {
120             System.exit(4);
121         }
122         try {
123             jbInit();
124         }
125         catch(Exception JavaDoc e) {
126             e.printStackTrace();
127         }
128         Thread JavaDoc receiver=new Thread JavaDoc(this, "Receiver");
129         receiver.start();
130     }
131
132     /**
133      * jbInit()
134      * This function provides initialization for the gui objects
135      */

136
137     private void jbInit() throws Exception JavaDoc {
138         titledBorder1=new TitledBorder JavaDoc("");
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 JavaDoc(new EtchedBorder JavaDoc(EtchedBorder.RAISED, Color.white, new Color(148, 145, 140)), "Processes");
142         border3=BorderFactory.createLineBorder(Color.black, 2);
143         titledBorder3=new TitledBorder JavaDoc(new EtchedBorder JavaDoc(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 JavaDoc() {
147             public void windowClosing(WindowEvent JavaDoc 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 JavaDoc() {
162             public void actionPerformed(ActionEvent JavaDoc e) {
163                 execute_actionPerformed(e);
164             }
165         });
166         set.addActionListener(new java.awt.event.ActionListener JavaDoc() {
167             public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc() {
189             public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc() {
199             public void keyTyped(KeyEvent JavaDoc 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 JavaDoc received=null;
225         View view;
226         Message msg;
227         String JavaDoc payload;
228         while(running) {
229             try {
230                 received=channel.receive(0);
231             }
232             catch(Exception JavaDoc 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 JavaDoc(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 JavaDoc tmp=null;
253                 tmp=msg.getObject();
254                 if(tmp instanceof String JavaDoc) {
255                     payload=(String JavaDoc)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     } //run()
269

270     /**
271      * Sets the partition for the selected component on the hashtable
272      */

273
274     void set_actionPerformed(ActionEvent JavaDoc e) {
275         if(partitionsList.getSelectedValue() != null) {
276             Map.Entry JavaDoc entry=(Map.Entry JavaDoc)partitionsList.getSelectedValue();
277             entry.setValue(new Integer JavaDoc(partitionField.getText()));
278             partitionsList.setListData(ht.entrySet().toArray());
279         }
280     }
281
282     /**
283      * Sends a message over the channel
284      */

285
286     void sendButton_actionPerformed(ActionEvent JavaDoc e) {
287         try {
288             channel.send(new Message(null, null, commandTextField.getText()));
289             commandTextField.setText("");
290         }
291         catch(Exception JavaDoc ex) {
292             ex.printStackTrace();
293         }
294
295     }
296
297     void this_windowClosing(WindowEvent JavaDoc e) {
298         channel.close();
299         System.out.println("Exiting");
300         System.exit(0);
301     }
302
303     void execute_actionPerformed(ActionEvent JavaDoc e) {
304         channel.down(new org.jgroups.Event(org.jgroups.Event.SET_PARTITIONS, ht));
305     }
306
307     static void main(String JavaDoc[] args) {
308
309         PartitionerTest frame=new PartitionerTest();
310         frame.validate();
311         System.out.println("Frame validated");
312         // Center the frame
313
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 JavaDoc 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 JavaDoc ex) {
339             ex.printStackTrace();
340         }
341     }
342
343     void commandTextField_keyTyped(KeyEvent JavaDoc e) {
344         if(e.getKeyChar() == '\n') sendButton_actionPerformed(null);
345     }
346 }
347
Popular Tags