1 3 package org.jgroups.demos; 4 5 6 import org.jgroups.*; 7 import org.jgroups.blocks.GroupRequest; 8 import org.jgroups.blocks.RpcDispatcher; 9 import org.jgroups.util.Rsp; 10 import org.jgroups.util.RspList; 11 12 import java.awt.*; 13 import java.awt.event.ActionEvent ; 14 import java.awt.event.ActionListener ; 15 import java.awt.event.WindowEvent ; 16 import java.awt.event.WindowListener ; 17 import java.util.Enumeration ; 18 import java.util.Hashtable ; 19 20 21 22 23 30 public class QuoteClient extends Frame implements WindowListener , ActionListener , 31 MembershipListener { 32 static final String channel_name="Quotes"; 33 RpcDispatcher disp; 34 Channel channel; 35 final Button get=new Button("Get"); 36 final Button set=new Button("Set"); 37 final Button quit=new Button("Quit"); 38 final Button get_all=new Button("All"); 39 final Label stock=new Label("Stock"); 40 final Label value=new Label("Value"); 41 final Label err_msg=new Label("Error"); 42 final TextField stock_field=new TextField(); 43 final TextField value_field=new TextField(); 44 final java.awt.List listbox=new java.awt.List (); 45 final Font default_font=new Font("Helvetica", Font.PLAIN, 12); 46 49 final String props="UDP:" + 50 "PING(num_initial_members=2;timeout=3000):" + 51 "FD:" + 52 "pbcast.PBCAST(gossip_interval=5000;gc_lag=50):" + 53 "UNICAST:" + 54 "FRAG:" + 55 "pbcast.GMS:" + 56 "pbcast.STATE_TRANSFER"; 57 58 59 public QuoteClient() { 60 super(); 61 try { 62 channel=new JChannel(props); 63 channel.setOpt(Channel.LOCAL, Boolean.FALSE); 64 disp=new RpcDispatcher(channel, null, this, this); 65 channel.connect(channel_name); 66 } 67 catch(Exception e) { 68 System.err.println("QuoteClient(): " + e); 69 } 70 addWindowListener(this); 71 } 72 73 private void showMsg(String msg) { 74 err_msg.setText(msg); 75 err_msg.setVisible(true); 76 } 77 78 private void clearMsg() { 79 err_msg.setVisible(false); 80 } 81 82 83 public void start() { 84 setLayout(null); 85 setSize(400, 300); 86 setFont(default_font); 87 88 stock.setBounds(new Rectangle(10, 30, 60, 30)); 89 value.setBounds(new Rectangle(10, 60, 60, 30)); 90 stock_field.setBounds(new Rectangle(100, 30, 100, 30)); 91 value_field.setBounds(new Rectangle(100, 60, 100, 30)); 92 listbox.setBounds(210, 30, 150, 160); 93 err_msg.setBounds(new Rectangle(10, 200, 350, 30)); 94 err_msg.setFont(new Font("Helvetica", Font.ITALIC, 12)); 95 err_msg.setForeground(Color.red); 96 err_msg.setVisible(false); 97 get.setBounds(new Rectangle(10, 250, 80, 30)); 98 set.setBounds(new Rectangle(100, 250, 80, 30)); 99 quit.setBounds(new Rectangle(190, 250, 80, 30)); 100 get_all.setBounds(new Rectangle(280, 250, 80, 30)); 101 102 get.addActionListener(this); 103 set.addActionListener(this); 104 quit.addActionListener(this); 105 get_all.addActionListener(this); 106 107 add(stock); 108 add(value); 109 add(stock_field); 110 add(value_field); 111 add(err_msg); 112 add(get); 113 add(set); 114 add(quit); 115 add(get_all); 116 add(listbox); 117 setVisible(true); 119 } 120 121 122 public void windowActivated(WindowEvent e) { 123 } 124 125 public void windowClosed(WindowEvent e) { 126 } 127 128 public void windowClosing(WindowEvent e) { 129 System.exit(0); 130 } 131 132 public void windowDeactivated(WindowEvent e) { 133 } 134 135 public void windowDeiconified(WindowEvent e) { 136 } 137 138 public void windowIconified(WindowEvent e) { 139 } 140 141 public void windowOpened(WindowEvent e) { 142 } 143 144 145 public void actionPerformed(ActionEvent e) { 146 String command=e.getActionCommand(); 147 RspList rsp_list; 148 Rsp first_rsp; 149 150 try { 151 if(command == "Get") { 152 String stock_name=stock_field.getText(); 153 if(stock_name == null || stock_name.length() == 0) { 154 showMsg("Stock name is empty !"); 155 return; 156 } 157 showMsg("Looking up value for " + stock_name + ':'); 158 rsp_list=disp.callRemoteMethods(null, "getQuote", new Object []{stock_name}, 159 new String []{String .class.getName()}, 160 GroupRequest.GET_FIRST, 10000); 161 162 first_rsp=(Rsp)rsp_list.elementAt(0); 163 Float val=first_rsp != null? (Float )first_rsp.getValue() : null; 164 if(val != null) { 165 value_field.setText(val.toString()); 166 clearMsg(); 167 } 168 else { 169 value_field.setText(""); 170 showMsg("Value for " + stock_name + " not found"); 171 } 172 } 173 else 174 if(command == "Set") { 175 String stock_name=stock_field.getText(); 176 String stock_val=value_field.getText(); 177 if(stock_name == null || stock_val == null || stock_name.length() == 0 || 178 stock_val.length() == 0) { 179 showMsg("Stock name and value have to be present to enter a new value"); 180 return; 181 } 182 Float val=new Float (stock_val); 183 disp.callRemoteMethods(null, "setQuote", new Object []{stock_name, val}, 184 new Class []{String .class, Float .class}, 185 GroupRequest.GET_FIRST, 0); 186 187 showMsg("Stock " + stock_name + " set to " + val); 188 } 189 else 190 if(command == "All") { 191 listbox.removeAll(); 192 showMsg("Getting all stocks:"); 193 rsp_list=disp.callRemoteMethods(null, "printAllStocks", 194 (Object [])null, (Class [])null, 195 GroupRequest.GET_NONE, 0); 196 197 System.out.println("rsp_list is " + rsp_list); 198 199 200 Hashtable all_stocks=(Hashtable )rsp_list.getFirst(); 201 if(all_stocks == null) { 202 showMsg("No stocks found"); 203 return; 204 } 205 clearMsg(); 206 listbox.removeAll(); 207 String key; 208 Float val; 209 for(Enumeration en=all_stocks.keys(); en.hasMoreElements();) { 210 key=(String )en.nextElement(); 211 val=(Float )all_stocks.get(key); 212 if(val == null) 213 continue; 214 listbox.add(key + ": " + val.toString()); 215 } 216 } 217 else 218 if(command == "Quit") { 219 setVisible(false); 220 channel.close(); 221 System.exit(0); 222 } 223 else 224 System.out.println("Unknown action"); 225 } 226 catch(Exception ex) { 227 value_field.setText(""); 228 ex.printStackTrace(); 229 showMsg(ex.toString()); 230 } 231 } 232 233 234 public void viewAccepted(View new_view) { 235 setTitle("Members in " + channel_name + ": " + (new_view.size() - 1)); 236 } 237 238 public void suspect(Address suspected_mbr) { 239 } 240 241 public void block() { 242 } 243 244 245 public static void main(String args[]) { 246 QuoteClient client=new QuoteClient(); 247 client.start(); 248 } 249 250 } 251 | Popular Tags |