001: // $Id: DistributedHashtableDemo.java,v 1.7 2006/10/25 16:26:25 belaban Exp $
002:
003: package org.jgroups.demos;
004:
005: import org.jgroups.ChannelException;
006: import org.jgroups.ChannelFactory;
007: import org.jgroups.JChannelFactory;
008: import org.jgroups.blocks.DistributedHashtable;
009: import org.jgroups.persistence.PersistenceFactory;
010:
011: import javax.swing.*;
012: import java.awt.*;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.awt.event.WindowEvent;
016: import java.awt.event.WindowListener;
017: import java.util.Enumeration;
018: import java.util.Map;
019: import java.util.Vector;
020:
021: /**
022: * Uses the DistributedHashtable building block. The latter subclasses java.util.Hashtable and overrides
023: * the methods that modify the hashtable (e.g. put()). Those methods are multicast to the group, whereas
024: * read-only methods such as get() use the local copy. A DistributedHashtable is created given the name
025: * of a group; all hashtables with the same name find each other and form a group.
026: * @author Bela Ban
027: */
028: public class DistributedHashtableDemo extends Frame implements
029: WindowListener, ActionListener,
030: DistributedHashtable.Notification {
031: static final String groupname = "HashDemo";
032: DistributedHashtable h = null;
033: final JButton get = new JButton("Get");
034: final JButton set = new JButton("Set");
035: final JButton quit = new JButton("Quit");
036: final JButton get_all = new JButton("All");
037: final JButton delete = new JButton("Delete");
038: final JLabel stock = new JLabel("Key");
039: final JLabel value = new JLabel("Value");
040: final JLabel err_msg = new JLabel("Error");
041: final JTextField stock_field = new JTextField();
042: final JTextField value_field = new JTextField();
043: final java.awt.List listbox = new java.awt.List();
044: final Font default_font = new Font("Helvetica", Font.PLAIN, 12);
045:
046: public DistributedHashtableDemo() {
047: super ();
048: addWindowListener(this );
049: }
050:
051: private void showMsg(String msg) {
052: err_msg.setText(msg);
053: err_msg.setVisible(true);
054: }
055:
056: private void clearMsg() {
057: err_msg.setVisible(false);
058: }
059:
060: private void removeItem() {
061: int index = listbox.getSelectedIndex();
062: if (index == -1) {
063: showMsg("No item selected in listbox to be deleted !");
064: return;
065: }
066: String s = listbox.getSelectedItem();
067: String key = s.substring(0, s.indexOf(':', 0));
068: if (key != null)
069: h.remove(key);
070: }
071:
072: private void showAll() {
073: if (listbox.getItemCount() > 0)
074: listbox.removeAll();
075: if (h.size() == 0)
076: return;
077: clearMsg();
078: String key;
079: Float val;
080:
081: for (Enumeration en = h.keys(); en.hasMoreElements();) {
082: key = (String) en.nextElement();
083: val = (Float) h.get(key);
084: if (val == null)
085: continue;
086: listbox.add(key + ": " + val.toString());
087: }
088: }
089:
090: public void start(ChannelFactory factory, String props,
091: boolean persist) throws ChannelException {
092: h = new DistributedHashtable(groupname, factory, props,
093: persist, 10000);
094: h.addNotifier(this );
095:
096: setLayout(null);
097: setSize(400, 300);
098: setFont(default_font);
099:
100: stock.setBounds(new Rectangle(10, 30, 60, 30));
101: value.setBounds(new Rectangle(10, 60, 60, 30));
102: stock_field.setBounds(new Rectangle(100, 30, 100, 30));
103: value_field.setBounds(new Rectangle(100, 60, 100, 30));
104: listbox.setBounds(new Rectangle(210, 30, 150, 160));
105: err_msg.setBounds(new Rectangle(10, 200, 350, 30));
106: err_msg.setFont(new Font("Helvetica", Font.ITALIC, 12));
107: err_msg.setForeground(Color.red);
108: err_msg.setVisible(false);
109: get.setBounds(new Rectangle(10, 250, 60, 30));
110: set.setBounds(new Rectangle(80, 250, 60, 30));
111: quit.setBounds(new Rectangle(150, 250, 60, 30));
112: get_all.setBounds(new Rectangle(220, 250, 60, 30));
113: delete.setBounds(new Rectangle(290, 250, 80, 30));
114:
115: get.addActionListener(this );
116: set.addActionListener(this );
117: quit.addActionListener(this );
118: get_all.addActionListener(this );
119: delete.addActionListener(this );
120:
121: add(stock);
122: add(value);
123: add(stock_field);
124: add(value_field);
125: add(err_msg);
126: add(get);
127: add(set);
128: add(quit);
129: add(get_all);
130: add(delete);
131: add(listbox);
132: setTitle("DistributedHashtable Demo");
133: showAll();
134: pack();
135: setVisible(true);
136:
137: // new Thread() {
138: // public void run() {
139: // System.out.println("-- sleeping");
140: // Util.sleep(10000);
141: // for(int i=0; i < 10; i++) {
142: // System.out.println("-- put()");
143: // h.put("Bela#" + i, new Float(i));
144: // }
145:
146: // while(true) {
147: // Util.sleep(500);
148: // System.out.println(h.get("Bela#1"));
149: // }
150:
151: // }
152: // }.start();
153:
154: }
155:
156: public void windowActivated(WindowEvent e) {
157: }
158:
159: public void windowClosed(WindowEvent e) {
160: }
161:
162: public void windowClosing(WindowEvent e) {
163: System.exit(0);
164: }
165:
166: public void windowDeactivated(WindowEvent e) {
167: }
168:
169: public void windowDeiconified(WindowEvent e) {
170: }
171:
172: public void windowIconified(WindowEvent e) {
173: }
174:
175: public void windowOpened(WindowEvent e) {
176: }
177:
178: public void actionPerformed(ActionEvent e) {
179: String command = e.getActionCommand();
180: try {
181: if (command == "Get") {
182: String stock_name = stock_field.getText();
183: if (stock_name == null || stock_name.length() == 0) {
184: showMsg("Key is empty !");
185: return;
186: }
187: showMsg("Looking up value for " + stock_name + ':');
188: Float val = (Float) h.get(stock_name);
189: if (val != null) {
190: value_field.setText(val.toString());
191: clearMsg();
192: } else {
193: value_field.setText("");
194: showMsg("Value for " + stock_name + " not found");
195: }
196: } else if (command == "Set") {
197: String stock_name = stock_field.getText();
198: String stock_val = value_field.getText();
199: if (stock_name == null || stock_val == null
200: || stock_name.length() == 0
201: || stock_val.length() == 0) {
202: showMsg("Both key and value have to be present to create a new entry");
203: return;
204: }
205: Float val = new Float(stock_val);
206: h.put(stock_name, val);
207: showMsg("Key " + stock_name + " set to " + val);
208: } else if (command == "All") {
209: showAll();
210: } else if (command == "Quit") {
211: setVisible(false);
212: System.exit(0);
213: } else if (command == "Delete")
214: removeItem();
215: else
216: System.out.println("Unknown action");
217: } catch (Exception ex) {
218: value_field.setText("");
219: showMsg(ex.toString());
220: }
221: }
222:
223: public void entrySet(Object key, Object value) {
224: showAll();
225: }
226:
227: public void entryRemoved(Object key) {
228: showAll();
229: }
230:
231: public void viewChange(Vector joined, Vector left) {
232: System.out.println("New members: " + joined
233: + ", left members: " + left);
234: }
235:
236: public void contentsSet(Map m) {
237: System.out.println("new contents: " + m);
238: }
239:
240: public void contentsCleared() {
241: System.out.println("contents cleared");
242: }
243:
244: public static void main(String args[]) {
245: DistributedHashtableDemo client = new DistributedHashtableDemo();
246: ChannelFactory factory = new JChannelFactory();
247: String arg;
248: boolean persist = false;
249:
250: // test for pbcast
251: /*
252: String props="UDP:" +
253: "PING(num_initial_members=2;timeout=3000):" +
254: "FD:" +
255: // "DISCARD(down=0.1):" + // this is for discarding of 10% of the up messages !
256: "pbcast.PBCAST(gossip_interval=5000;gc_lag=50):" +
257: "UNICAST:" +
258: "FRAG:" +
259: "pbcast.GMS:" +
260: "pbcast.STATE_TRANSFER";
261: */
262:
263: String props = "UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;"
264: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):"
265: + "PING(timeout=2000;num_initial_members=3):"
266: + "MERGE2(min_interval=5000;max_interval=10000):"
267: + "FD_SOCK:"
268: + "VERIFY_SUSPECT(timeout=1500):"
269: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):"
270: + "UNICAST(timeout=5000):"
271: + "pbcast.STABLE(desired_avg_gossip=20000):"
272: + "FRAG(frag_size=4096;down_thread=false;up_thread=false):"
273: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
274: + "shun=false;print_local_addr=true):"
275: + "pbcast.STATE_TRANSFER";
276:
277: try {
278: for (int i = 0; i < args.length; i++) {
279: arg = args[i];
280: if ("-persist".equals(arg) && i + 1 < args.length) {
281: persist = true;
282: PersistenceFactory.getInstance().createManager(
283: args[++i]);
284: continue;
285: }
286: if ("-props".equals(arg)) {
287: props = args[++i];
288: continue;
289: }
290: help();
291: return;
292: }
293: } catch (Exception e) {
294: help();
295: return;
296: }
297: try {
298: client.start(factory, props, persist);
299: } catch (Throwable t) {
300: t.printStackTrace();
301: }
302: }
303:
304: static void help() {
305: System.out
306: .println("DistributedHashtableDemo [-help] [-persist] [-props <properties>]");
307: }
308:
309: }
|