KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > DistributedHashtableDemo


1 // $Id: DistributedHashtableDemo.java,v 1.6 2004/09/23 16:29:35 belaban Exp $
2

3
4 package org.jgroups.demos;
5
6
7 import org.jgroups.ChannelException;
8 import org.jgroups.ChannelFactory;
9 import org.jgroups.JChannelFactory;
10 import org.jgroups.blocks.DistributedHashtable;
11 import org.jgroups.persistence.PersistenceFactory;
12
13 import javax.swing.*;
14 import java.awt.*;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.awt.event.WindowEvent JavaDoc;
18 import java.awt.event.WindowListener JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Vector JavaDoc;
22
23
24
25
26 /**
27  * Uses the DistributedHashtable building block. The latter subclasses java.util.Hashtable and overrides
28  * the methods that modify the hashtable (e.g. put()). Those methods are multicast to the group, whereas
29  * read-only methods such as get() use the local copy. A DistributedHashtable is created given the name
30  * of a group; all hashtables with the same name find each other and form a group.
31  * @author Bela Ban
32  */

33 public class DistributedHashtableDemo extends Frame implements WindowListener JavaDoc, ActionListener JavaDoc,
34                                           DistributedHashtable.Notification {
35     static final String JavaDoc groupname="HashDemo";
36     DistributedHashtable h=null;
37     final JButton get=new JButton("Get");
38     final JButton set=new JButton("Set");
39     final JButton quit=new JButton("Quit");
40     final JButton get_all=new JButton("All");
41     final JButton delete=new JButton("Delete");
42     final JLabel stock=new JLabel("Key");
43     final JLabel value=new JLabel("Value");
44     final JLabel err_msg=new JLabel("Error");
45     final JTextField stock_field=new JTextField();
46     final JTextField value_field=new JTextField();
47     final java.awt.List JavaDoc listbox=new java.awt.List JavaDoc();
48     final Font default_font=new Font("Helvetica", Font.PLAIN,12);
49
50
51
52
53     public DistributedHashtableDemo() {
54         super();
55         addWindowListener(this);
56     }
57
58     private void showMsg(String JavaDoc msg) {
59         err_msg.setText(msg);
60         err_msg.setVisible(true);
61     }
62
63     private void clearMsg() {err_msg.setVisible(false);}
64
65
66     private void removeItem() {
67         int index=listbox.getSelectedIndex();
68         if(index == -1) {
69             showMsg("No item selected in listbox to be deleted !");
70             return;
71         }
72         String JavaDoc s=listbox.getSelectedItem();
73         String JavaDoc key=s.substring(0, s.indexOf(':', 0));
74         if(key != null)
75             h.remove(key);
76     }
77
78     private void showAll() {
79         if(listbox.getItemCount() > 0)
80             listbox.removeAll();
81         if(h.size() == 0)
82             return;
83         clearMsg();
84         String JavaDoc key;
85         Float JavaDoc val;
86
87         for(Enumeration JavaDoc en=h.keys(); en.hasMoreElements();) {
88             key=(String JavaDoc)en.nextElement();
89             val=(Float JavaDoc)h.get(key);
90             if(val == null)
91                 continue;
92             listbox.add(key + ": " + val.toString());
93         }
94     }
95
96
97
98
99
100     public void start(ChannelFactory factory, String JavaDoc props, boolean persist)
101             throws ChannelException {
102         h=new DistributedHashtable(groupname, factory, props, persist, 10000);
103         h.addNotifier(this);
104
105         setLayout(null);
106         setSize(400, 300);
107         setFont(default_font);
108
109         stock.setBounds(new Rectangle(10, 30, 60, 30));
110         value.setBounds(new Rectangle(10, 60, 60, 30));
111         stock_field.setBounds(new Rectangle(100, 30, 100, 30));
112         value_field.setBounds(new Rectangle(100, 60, 100, 30));
113         listbox.setBounds(new Rectangle(210, 30, 150, 160));
114         err_msg.setBounds(new Rectangle(10, 200, 350, 30));
115         err_msg.setFont(new Font("Helvetica",Font.ITALIC,12));
116         err_msg.setForeground(Color.red);
117         err_msg.setVisible(false);
118         get.setBounds(new Rectangle(10, 250, 60, 30));
119         set.setBounds(new Rectangle(80, 250, 60, 30));
120         quit.setBounds(new Rectangle(150, 250, 60, 30));
121         get_all.setBounds(new Rectangle(220, 250, 60, 30));
122         delete.setBounds(new Rectangle(290, 250, 80, 30));
123
124         get.addActionListener(this);
125         set.addActionListener(this);
126         quit.addActionListener(this);
127         get_all.addActionListener(this);
128         delete.addActionListener(this);
129
130         add(stock); add(value);
131         add(stock_field); add(value_field);
132         add(err_msg);
133         add(get); add(set); add(quit); add(get_all); add(delete);
134         add(listbox);
135         setTitle("DistributedHashtable Demo");
136         showAll();
137         pack();
138         setVisible(true);
139
140
141 // new Thread() {
142
// public void run() {
143
// System.out.println("-- sleeping");
144
// Util.sleep(10000);
145
// for(int i=0; i < 10; i++) {
146
// System.out.println("-- put()");
147
// h.put("Bela#" + i, new Float(i));
148
// }
149

150 // while(true) {
151
// Util.sleep(500);
152
// System.out.println(h.get("Bela#1"));
153
// }
154

155 // }
156
// }.start();
157

158
159
160     }
161
162
163
164
165     public void windowActivated(WindowEvent JavaDoc e) {}
166     public void windowClosed(WindowEvent JavaDoc e) {}
167     public void windowClosing(WindowEvent JavaDoc e) {System.exit(0);}
168     public void windowDeactivated(WindowEvent JavaDoc e) {}
169     public void windowDeiconified(WindowEvent JavaDoc e) {}
170     public void windowIconified(WindowEvent JavaDoc e) {}
171     public void windowOpened(WindowEvent JavaDoc e) {}
172
173
174     public void actionPerformed(ActionEvent JavaDoc e) {
175         String JavaDoc command=e.getActionCommand();
176         try {
177             if(command == "Get") {
178                 String JavaDoc stock_name=stock_field.getText();
179                 if(stock_name == null || stock_name.length() == 0) {
180                     showMsg("Key is empty !");
181                     return;
182                 }
183                 showMsg("Looking up value for " + stock_name + ':');
184                 Float JavaDoc val=(Float JavaDoc)h.get(stock_name);
185                 if(val != null) {
186                     value_field.setText(val.toString());
187                     clearMsg();
188                 }
189                 else {
190                     value_field.setText("");
191                     showMsg("Value for " + stock_name + " not found");
192                 }
193             }
194             else if(command == "Set") {
195                 String JavaDoc stock_name=stock_field.getText();
196                 String JavaDoc stock_val=value_field.getText();
197                 if(stock_name == null || stock_val == null || stock_name.length() == 0 ||
198                    stock_val.length() == 0) {
199                     showMsg("Both key and value have to be present to create a new entry");
200                     return;
201                 }
202                 Float JavaDoc val=new Float JavaDoc(stock_val);
203                 h.put(stock_name, val);
204                 showMsg("Key " + stock_name + " set to " + val);
205             }
206             else if(command == "All") {
207                 showAll();
208             }
209             else if(command == "Quit") {
210                 setVisible(false);
211                 System.exit(0);
212             }
213             else if(command == "Delete")
214                 removeItem();
215             else
216                 System.out.println("Unknown action");
217         }
218         catch(Exception JavaDoc ex) {
219             value_field.setText("");
220             showMsg(ex.toString());
221         }
222     }
223
224
225
226     public void entrySet(Object JavaDoc key, Object JavaDoc value) {showAll();}
227
228     public void entryRemoved(Object JavaDoc key) {showAll();}
229
230     public void viewChange(Vector JavaDoc joined, Vector JavaDoc left) {
231         System.out.println("New members: " + joined + ", left members: " + left);
232     }
233
234     public void contentsSet(Map JavaDoc m) {
235         System.out.println("new contents: " + m);
236     }
237
238     public void contentsCleared() {
239         System.out.println("contents cleared");
240     }
241
242
243     public static void main(String JavaDoc args[]) {
244         DistributedHashtableDemo client=new DistributedHashtableDemo();
245         ChannelFactory factory=new JChannelFactory();
246         String JavaDoc arg;
247         boolean persist=false;
248
249         // test for pbcast
250
/*
251         String props="UDP:" +
252             "PING(num_initial_members=2;timeout=3000):" +
253             "FD:" +
254             // "DISCARD(down=0.1):" + // this is for discarding of 10% of the up messages !
255             "pbcast.PBCAST(gossip_interval=5000;gc_lag=50):" +
256             "UNICAST:" +
257             "FRAG:" +
258             "pbcast.GMS:" +
259             "pbcast.STATE_TRANSFER";
260         */

261
262         String JavaDoc props="UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;" +
263                 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
264                 "PING(timeout=2000;num_initial_members=3):" +
265                 "MERGE2(min_interval=5000;max_interval=10000):" +
266                 "FD_SOCK:" +
267                 "VERIFY_SUSPECT(timeout=1500):" +
268                 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
269                 "UNICAST(timeout=5000):" +
270                 "pbcast.STABLE(desired_avg_gossip=20000):" +
271                 "FRAG(frag_size=4096;down_thread=false;up_thread=false):" +
272                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
273                 "shun=false;print_local_addr=true):" +
274                 "pbcast.STATE_TRANSFER";
275
276         try {
277             for(int i=0; i < args.length; i++) {
278                 arg=args[i];
279                 if("-persist".equals(arg) && i+1<args.length) {
280                     persist=true;
281                     PersistenceFactory.getInstance().createManager(args[++i]);
282                     continue;
283                 }
284                 help();
285                 return;
286             }
287         }
288         catch(Exception JavaDoc e) {
289             help();
290             return;
291         }
292         try {
293             client.start(factory, props, persist);
294         }
295         catch(Throwable JavaDoc t) {
296             t.printStackTrace();
297         }
298     }
299
300
301     static void help() {
302         System.out.println("DistributedHashtableDemo [-help] [-persist]");
303     }
304
305 }
306
Popular Tags