1 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 ; 16 import java.awt.event.ActionListener ; 17 import java.awt.event.WindowEvent ; 18 import java.awt.event.WindowListener ; 19 import java.util.Enumeration ; 20 import java.util.Map ; 21 import java.util.Vector ; 22 23 24 25 26 33 public class DistributedHashtableDemo extends Frame implements WindowListener , ActionListener , 34 DistributedHashtable.Notification { 35 static final String 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 listbox=new java.awt.List (); 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 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 s=listbox.getSelectedItem(); 73 String 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 key; 85 Float val; 86 87 for(Enumeration en=h.keys(); en.hasMoreElements();) { 88 key=(String )en.nextElement(); 89 val=(Float )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 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 150 155 158 159 160 } 161 162 163 164 165 public void windowActivated(WindowEvent e) {} 166 public void windowClosed(WindowEvent e) {} 167 public void windowClosing(WindowEvent e) {System.exit(0);} 168 public void windowDeactivated(WindowEvent e) {} 169 public void windowDeiconified(WindowEvent e) {} 170 public void windowIconified(WindowEvent e) {} 171 public void windowOpened(WindowEvent e) {} 172 173 174 public void actionPerformed(ActionEvent e) { 175 String command=e.getActionCommand(); 176 try { 177 if(command == "Get") { 178 String 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 val=(Float )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 stock_name=stock_field.getText(); 196 String 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 val=new Float (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 ex) { 219 value_field.setText(""); 220 showMsg(ex.toString()); 221 } 222 } 223 224 225 226 public void entrySet(Object key, Object value) {showAll();} 227 228 public void entryRemoved(Object key) {showAll();} 229 230 public void viewChange(Vector joined, Vector left) { 231 System.out.println("New members: " + joined + ", left members: " + left); 232 } 233 234 public void contentsSet(Map 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 args[]) { 244 DistributedHashtableDemo client=new DistributedHashtableDemo(); 245 ChannelFactory factory=new JChannelFactory(); 246 String arg; 247 boolean persist=false; 248 249 261 262 String 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 e) { 289 help(); 290 return; 291 } 292 try { 293 client.start(factory, props, persist); 294 } 295 catch(Throwable 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 |