001: package org.jgroups.tests;
002:
003: import org.jgroups.Address;
004: import org.jgroups.JChannel;
005: import org.jgroups.Message;
006: import org.jgroups.View;
007: import org.jgroups.debug.Debugger;
008:
009: import javax.swing.*;
010: import javax.swing.border.BevelBorder;
011: import javax.swing.border.Border;
012: import javax.swing.border.EtchedBorder;
013: import javax.swing.border.TitledBorder;
014: import java.awt.*;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.KeyEvent;
017: import java.awt.event.WindowEvent;
018: import java.util.Hashtable;
019: import java.util.Map;
020:
021: /**
022: * Title: Demos of PARTITIONER protocol. Simulates network partitions.
023: * Description: The usage of PartitionerTestFrame is quite simple:
024: * As the program finds new members in the view it adds them to the Hashtable and
025: * to Processes list.
026: * You can chose a Process from the processes list and digit a number over the set button.
027: * Once you push the Set button the number is stored in the local Hastable.
028: * Once you push the Execute button the local Hashtable is sent with a SET_PARTITIONS Event.
029: * <p/>
030: * The TextArea under the output area is to send messages over the channel and you view
031: * the results on the output area.
032: * <p/>
033: * In the future it may be a good thing let the PARTITIONER layer spawn a control Frame
034: * so that the use of this layer is transparent to the application, but it is also possible
035: * to use a partitionerTestFrame (a little modified) in a Group of other Processes just
036: * to control their behaviour against partitions.
037: * Copyright: Copyright (c) 2000
038: * Company: Computer Network Laboratory
039: *
040: * @author Gianluca Collot
041: * @version 1.1
042: */
043:
044: public class PartitionerTest extends JFrame implements Runnable {
045: static String channel_properties = "UDP:PARTITIONER:SHUFFLE:MERGE"
046: + ":PING:FD(shun=false):" + "STABLE:"
047: + "NAKACK:FRAG:FLUSH:GMS:" + "VIEW_ENFORCER:" + "QUEUE";
048: JChannel channel;
049: Debugger debugger;
050:
051: boolean connected = false;
052: Hashtable ht = new Hashtable();
053:
054: JPanel jPanel1 = new JPanel();
055: BorderLayout borderLayout1 = new BorderLayout();
056: JPanel statusbar = new JPanel();
057: JLabel status = new JLabel();
058: BorderLayout borderLayout2 = new BorderLayout();
059: JPanel jPanel3 = new JPanel();
060: JScrollPane jScrollPane2 = new JScrollPane();
061: JList partitionsList = new JList();
062: BorderLayout borderLayout3 = new BorderLayout();
063: JLabel partitionLabel = new JLabel();
064: JTextField partitionField = new JTextField();
065: JButton execute = new JButton();
066: JButton set = new JButton();
067: JPanel jPanel2 = new JPanel();
068: GridLayout gridLayout1 = new GridLayout();
069: TitledBorder titledBorder1;
070: Border border1;
071: Border border2;
072: TitledBorder titledBorder2;
073: Border border3;
074: TitledBorder titledBorder3;
075: BorderLayout borderLayout5 = new BorderLayout();
076: JButton connect = new JButton();
077: JPanel applicationPanel = new JPanel();
078: JTextArea output = new JTextArea();
079: JScrollPane jScrollPane1 = new JScrollPane();
080: JTextField commandTextField = new JTextField();
081: BorderLayout borderLayout4 = new BorderLayout();
082:
083: public PartitionerTest(JChannel channel) {
084: this .channel = channel;
085: init();
086: }
087:
088: public PartitionerTest() {
089: try {
090: channel = new JChannel(channel_properties);
091: } catch (Exception ex) {
092: ex.printStackTrace();
093: System.exit(4);
094: }
095: init();
096: }
097:
098: /**
099: * Initializes the channel and application threads
100: */
101:
102: void init() {
103: try {
104: channel.connect("prova");
105: connected = true;
106: // if (channel.getView().getMembers().elementAt(0).equals(channel.getLocalAddress())) {
107: // Debugger debugger = new Debugger(channel);
108: // debugger.start();
109: // }
110: } catch (Exception ex) {
111: System.exit(4);
112: }
113: try {
114: jbInit();
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: Thread receiver = new Thread(this , "Receiver");
119: receiver.start();
120: }
121:
122: /**
123: * jbInit()
124: * This function provides initialization for the gui objects
125: */
126:
127: private void jbInit() throws Exception {
128: titledBorder1 = new TitledBorder("");
129: border1 = BorderFactory.createCompoundBorder(BorderFactory
130: .createEtchedBorder(Color.white, new Color(148, 145,
131: 140)), BorderFactory.createEmptyBorder(4, 4, 4,
132: 4));
133: border2 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
134: Color.white, Color.white, new Color(148, 145, 140),
135: new Color(103, 101, 98));
136: titledBorder2 = new TitledBorder(new EtchedBorder(
137: EtchedBorder.RAISED, Color.white, new Color(148, 145,
138: 140)), "Processes");
139: border3 = BorderFactory.createLineBorder(Color.black, 2);
140: titledBorder3 = new TitledBorder(new EtchedBorder(
141: EtchedBorder.RAISED, Color.white, new Color(148, 145,
142: 140)), "Channel Output");
143: this .setSize(400, 400);
144: this .setTitle("Partitioner Test :" + channel.getLocalAddress());
145: this .addWindowListener(new java.awt.event.WindowAdapter() {
146: public void windowClosing(WindowEvent e) {
147: this _windowClosing(e);
148: }
149: });
150: this .getContentPane().setLayout(borderLayout1);
151: jPanel1.setLayout(borderLayout3);
152: statusbar.setLayout(borderLayout2);
153: status.setMinimumSize(new Dimension(100, 20));
154: status.setText("ciao");
155: statusbar.setBorder(BorderFactory.createBevelBorder(
156: BevelBorder.LOWERED, Color.white, Color.white,
157: new Color(148, 145, 140), new Color(103, 101, 98)));
158: jPanel3.setLayout(borderLayout5);
159: partitionLabel.setHorizontalAlignment(SwingConstants.CENTER);
160: partitionLabel.setText("Partition");
161: execute.setText("Execute");
162: execute.addActionListener(new java.awt.event.ActionListener() {
163: public void actionPerformed(ActionEvent e) {
164: execute_actionPerformed(e);
165: }
166: });
167: set.addActionListener(new java.awt.event.ActionListener() {
168: public void actionPerformed(ActionEvent e) {
169: set_actionPerformed(e);
170: }
171: });
172: set.setText("Set");
173: set.setActionCommand("Set");
174: jPanel2.setLayout(gridLayout1);
175: partitionField.setMinimumSize(new Dimension(20, 20));
176: gridLayout1.setRows(8);
177: gridLayout1.setColumns(1);
178: gridLayout1.setVgap(4);
179: jPanel3.setBorder(border1);
180: jPanel3.setMinimumSize(new Dimension(50, 50));
181: jPanel3.setPreferredSize(new Dimension(200, 200));
182: jScrollPane2.setBorder(titledBorder2);
183: jScrollPane2.setMinimumSize(new Dimension(100, 80));
184: jPanel2.setMinimumSize(new Dimension(77, 100));
185: partitionsList.setBorder(BorderFactory
186: .createLoweredBevelBorder());
187: partitionsList.setMinimumSize(new Dimension(100, 80));
188: connect.setActionCommand("Connect");
189: connect.setText("Disconnect");
190: connect.addActionListener(new java.awt.event.ActionListener() {
191: public void actionPerformed(ActionEvent e) {
192: connect_actionPerformed(e);
193: }
194: });
195: jScrollPane1.setAutoscrolls(true);
196: jScrollPane1.setBorder(titledBorder3);
197: jScrollPane1.setMinimumSize(new Dimension(100, 100));
198: jScrollPane1.setPreferredSize(new Dimension(300, 100));
199: applicationPanel.setLayout(borderLayout4);
200: commandTextField
201: .addKeyListener(new java.awt.event.KeyAdapter() {
202: public void keyTyped(KeyEvent e) {
203: commandTextField_keyTyped(e);
204: }
205: });
206: commandTextField.setMaximumSize(new Dimension(300, 21));
207: this .getContentPane().add(statusbar, BorderLayout.SOUTH);
208: statusbar.add(status, BorderLayout.CENTER);
209: this .getContentPane().add(jPanel1, BorderLayout.NORTH);
210: jPanel1.add(jPanel3, BorderLayout.CENTER);
211: jPanel3.add(jScrollPane2, BorderLayout.NORTH);
212: jScrollPane2.getViewport().add(partitionsList, null);
213: jPanel1.add(jPanel2, BorderLayout.WEST);
214: jPanel2.add(partitionLabel, null);
215: jPanel2.add(partitionField, null);
216: jPanel2.add(set, null);
217: jPanel2.add(execute, null);
218: jPanel2.add(connect, null);
219: this .getContentPane()
220: .add(applicationPanel, BorderLayout.CENTER);
221: applicationPanel.add(jScrollPane1, BorderLayout.CENTER);
222: applicationPanel.add(commandTextField, BorderLayout.SOUTH);
223: jScrollPane1.getViewport().add(output, null);
224: }
225:
226: public void run() {
227: boolean running = true;
228: Object received = null;
229: View view;
230: Message msg;
231: String payload;
232: while (running) {
233: try {
234: received = channel.receive(0);
235: } catch (Exception ex) {
236: System.out.println("PartitionerTest.run() :" + ex);
237: System.exit(-1);
238: }
239: if (received instanceof View) {
240: view = (View) received;
241: status.setText(view.toString());
242: for (int i = 0; i < view.size(); i++) {
243: Address member = (Address) view.getMembers()
244: .elementAt(i);
245: if (ht.get(member) == null) {
246: ht.put(member, new Integer(1));
247: }
248: }
249: partitionsList.setListData(ht.entrySet().toArray());
250: output.append(view + "\n");
251: output.setCaretPosition(output.getText().length());
252: }
253:
254: if (received instanceof Message) {
255: msg = (Message) received;
256: Object tmp = null;
257: tmp = msg.getObject();
258: if (tmp instanceof String) {
259: payload = (String) tmp;
260: if ("stop".equals(payload)) {
261: running = false;
262: }
263: output.append(':' + payload + '\n');
264: } else
265: output.append("Received not a String\n");
266: }
267: output.setCaretPosition(output.getText().length());
268: }
269: channel.close();
270: output.append("Channel Closed\n");
271: } //run()
272:
273: /**
274: * Sets the partition for the selected component on the hashtable
275: */
276:
277: void set_actionPerformed(ActionEvent e) {
278: if (partitionsList.getSelectedValue() != null) {
279: Map.Entry entry = (Map.Entry) partitionsList
280: .getSelectedValue();
281: entry.setValue(new Integer(partitionField.getText()));
282: partitionsList.setListData(ht.entrySet().toArray());
283: }
284: }
285:
286: /**
287: * Sends a message over the channel
288: */
289:
290: void sendButton_actionPerformed(ActionEvent e) {
291: try {
292: channel.send(new Message(null, null, commandTextField
293: .getText()));
294: commandTextField.setText("");
295: } catch (Exception ex) {
296: ex.printStackTrace();
297: }
298:
299: }
300:
301: void this _windowClosing(WindowEvent e) {
302: channel.close();
303: System.out.println("Exiting");
304: System.exit(0);
305: }
306:
307: void execute_actionPerformed(ActionEvent e) {
308: channel.down(new org.jgroups.Event(
309: org.jgroups.Event.SET_PARTITIONS, ht));
310: }
311:
312: static void main(String[] args) {
313:
314: PartitionerTest frame = new PartitionerTest();
315: frame.validate();
316: System.out.println("Frame validated");
317: // Center the frame
318: Dimension screenSize = Toolkit.getDefaultToolkit()
319: .getScreenSize();
320: Dimension frameSize = frame.getSize();
321: if (frameSize.height > screenSize.height)
322: frameSize.height = screenSize.height;
323: if (frameSize.width > screenSize.width)
324: frameSize.width = screenSize.width;
325: frame.setLocation((screenSize.width - frameSize.width) / 2,
326: (screenSize.height - frameSize.height) / 2);
327: frame.setVisible(true);
328: System.out.println("Frame Visible");
329: }
330:
331: void connect_actionPerformed(ActionEvent e) {
332: try {
333: if (!connected) {
334: channel.connect("prova");
335: connected = true;
336: connect.setText("Disconnect");
337: } else {
338: channel.disconnect();
339: connected = false;
340: connect.setText("Connect");
341: status.setText("Disconnected");
342: }
343: } catch (Exception ex) {
344: ex.printStackTrace();
345: }
346: }
347:
348: void commandTextField_keyTyped(KeyEvent e) {
349: if (e.getKeyChar() == '\n')
350: sendButton_actionPerformed(null);
351: }
352: }
|