001: // $Id: DistributedQueueDemo.java,v 1.6 2004/09/23 16:29:35 belaban Exp $
002: package org.jgroups.demos;
003:
004: import org.jgroups.ChannelException;
005: import org.jgroups.ChannelFactory;
006: import org.jgroups.JChannelFactory;
007: import org.jgroups.blocks.DistributedQueue;
008:
009: import javax.swing.*;
010: import java.awt.*;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.awt.event.WindowEvent;
014: import java.awt.event.WindowListener;
015: import java.util.Collection;
016: import java.util.Vector;
017:
018: /**
019: * Uses the DistributedQueue building block. The latter subclasses org.jgroups.util.Queue and overrides
020: * the methods that modify the queue (e.g. add()). Those methods are multicast to the group, whereas
021: * read-only methods such as peek() use the local copy. A DistributedQueue is created given the name
022: * of a group; all queues with the same name find each other and form a group.
023: * @author Romuald du Song
024: */
025: public class DistributedQueueDemo extends Frame implements
026: WindowListener, ActionListener, DistributedQueue.Notification {
027: DistributedQueue h = null;
028: final JButton add = new JButton("Add");
029: final JButton quit = new JButton("Quit");
030: final JButton get_all = new JButton("All");
031: final JButton remove = new JButton("Remove");
032: final JLabel value = new JLabel("Value");
033: final JLabel err_msg = new JLabel("Error");
034: final JTextField value_field = new JTextField();
035: final java.awt.List listbox = new java.awt.List();
036: final Font default_font = new Font("Helvetica", Font.PLAIN, 12);
037:
038: public DistributedQueueDemo() {
039: super ();
040: addWindowListener(this );
041: }
042:
043: private void showMsg(String msg) {
044: err_msg.setText(msg);
045: err_msg.setVisible(true);
046: }
047:
048: private void clearMsg() {
049: err_msg.setVisible(false);
050: }
051:
052: private void removeItem() {
053: h.remove();
054: }
055:
056: private void showAll() {
057: if (listbox.getItemCount() > 0) {
058: listbox.removeAll();
059: }
060:
061: if (h.size() == 0) {
062: return;
063: }
064:
065: clearMsg();
066:
067: String key;
068:
069: Vector v = h.getContents();
070:
071: for (int i = 0; i < v.size(); i++) {
072: listbox.add((String) v.elementAt(i));
073: }
074: }
075:
076: public void start(String groupname, ChannelFactory factory,
077: String props) throws ChannelException {
078: h = new DistributedQueue(groupname, factory, props, 10000);
079: h.addNotifier(this );
080:
081: setLayout(null);
082: setSize(400, 300);
083: setFont(default_font);
084:
085: value.setBounds(new Rectangle(10, 60, 60, 30));
086: value_field.setBounds(new Rectangle(100, 60, 100, 30));
087: listbox.setBounds(new Rectangle(210, 30, 150, 160));
088: err_msg.setBounds(new Rectangle(10, 200, 350, 30));
089: err_msg.setFont(new Font("Helvetica", Font.ITALIC, 12));
090: err_msg.setForeground(Color.red);
091: err_msg.setVisible(false);
092: add.setBounds(new Rectangle(60, 250, 60, 30));
093: quit.setBounds(new Rectangle(130, 250, 70, 30));
094: get_all.setBounds(new Rectangle(210, 250, 60, 30));
095: remove.setBounds(new Rectangle(280, 250, 90, 30));
096:
097: add.addActionListener(this );
098: quit.addActionListener(this );
099: get_all.addActionListener(this );
100: remove.addActionListener(this );
101:
102: add(value);
103: add(value_field);
104: add(err_msg);
105: add(add);
106: add(quit);
107: add(get_all);
108: add(remove);
109: add(listbox);
110: setTitle("DistributedQueue Demo");
111: showAll();
112: pack();
113: setVisible(true);
114:
115: /*
116: new Thread() {
117: public void run() {
118: System.out.println("-- sleeping");
119: Util.sleep(10000);
120: for(int i=0; i < 10; i++) {
121: System.out.println("-- add()");
122: h.add("Bela#" + i);
123: }
124:
125: while(true) {
126: Util.sleep(500);
127: try
128: {
129: System.out.println(h.remove());
130: }
131: catch (QueueClosedException e)
132: {
133: e.printStackTrace();
134: }
135: }
136:
137: }
138: }.start();
139: */
140: }
141:
142: public void windowActivated(WindowEvent e) {
143: }
144:
145: public void windowClosed(WindowEvent e) {
146: }
147:
148: public void windowClosing(WindowEvent e) {
149: System.exit(0);
150: }
151:
152: public void windowDeactivated(WindowEvent e) {
153: }
154:
155: public void windowDeiconified(WindowEvent e) {
156: }
157:
158: public void windowIconified(WindowEvent e) {
159: }
160:
161: public void windowOpened(WindowEvent e) {
162: }
163:
164: public void actionPerformed(ActionEvent e) {
165: String command = e.getActionCommand();
166:
167: try {
168: if (command == "Add") {
169: String value_name = value_field.getText();
170:
171: if ((value_name == null) || (value_name.length() == 0)) {
172: showMsg("Value is empty !");
173:
174: return;
175: }
176:
177: showMsg("Adding value " + value_name + ':');
178: h.add(value_name);
179: } else if (command == "All") {
180: showAll();
181: } else if (command == "Quit") {
182: setVisible(false);
183: System.exit(0);
184: } else if (command == "Remove") {
185: removeItem();
186: } else {
187: System.out.println("Unknown action");
188: }
189: } catch (Exception ex) {
190: value_field.setText("");
191: showMsg(ex.toString());
192: }
193: }
194:
195: public void entryAdd(Object value) {
196: showAll();
197: }
198:
199: public void entryRemoved(Object key) {
200: showAll();
201: }
202:
203: public void viewChange(Vector joined, Vector left) {
204: System.out.println("New members: " + joined
205: + ", left members: " + left);
206: }
207:
208: public void contentsSet(Collection new_entries) {
209: System.out.println("Contents Set:" + new_entries);
210: }
211:
212: public void contentsCleared() {
213: System.out.println("Contents cleared()");
214: }
215:
216: public static void main(String[] args) {
217: String groupname = "QueueDemo";
218: DistributedQueueDemo client = new DistributedQueueDemo();
219: ChannelFactory factory = new JChannelFactory();
220: String arg;
221: String next_arg;
222: boolean trace = false;
223: boolean persist = false;
224:
225: String props = "UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;"
226: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):"
227: + "PING(timeout=2000;num_initial_members=3):"
228: + "MERGE2(min_interval=5000;max_interval=10000):"
229: + "FD_SOCK:"
230: + "VERIFY_SUSPECT(timeout=1500):"
231: + "UNICAST(timeout=5000):"
232: + "FRAG(frag_size=8192;down_thread=false;up_thread=false):"
233: + "TOTAL_TOKEN(block_sending=50;unblock_sending=10):"
234: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
235: + "shun=false;print_local_addr=true):"
236: + "STATE_TRANSFER:" + "QUEUE";
237:
238: try {
239: for (int i = 0; i < args.length; i++) {
240: arg = args[i];
241:
242: if ("-trace".equals(arg)) {
243: trace = true;
244: continue;
245: }
246:
247: if ("-groupname".equals(args[i])) {
248: groupname = args[++i];
249: continue;
250: }
251:
252: help();
253: return;
254: }
255: } catch (Exception e) {
256: help();
257:
258: return;
259: }
260:
261: try {
262: client.start(groupname, factory, props);
263: } catch (Throwable t) {
264: t.printStackTrace();
265: }
266: }
267:
268: static void help() {
269: System.out.println("DistributedQueueDemo [-help]");
270: }
271: }
|