001: // $Id: QuoteClient.java,v 1.11 2006/05/03 08:45:19 belaban Exp $
002:
003: package org.jgroups.demos;
004:
005: import org.jgroups.*;
006: import org.jgroups.blocks.GroupRequest;
007: import org.jgroups.blocks.RpcDispatcher;
008: import org.jgroups.util.Rsp;
009: import org.jgroups.util.RspList;
010:
011: import java.awt.*;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014: import java.awt.event.WindowEvent;
015: import java.awt.event.WindowListener;
016: import java.util.Enumeration;
017: import java.util.Hashtable;
018:
019: /**
020: * Used in conjunction with QuoteServer: a client is member of a group of quote servers which replicate
021: * stock quotes among themselves. The client broadcasts its request (set, get quotes) and (in the case of get
022: * waits for the first reply received (usually the one from the quote server closest to it). The client
023: * can get and set quotes as long as a minimum of 1 server (in the group) is running.
024: * @author Bela Ban
025: */
026: public class QuoteClient extends Frame implements WindowListener,
027: ActionListener, MembershipListener {
028: static final String channel_name = "Quotes";
029: RpcDispatcher disp;
030: Channel channel;
031: final Button get = new Button("Get");
032: final Button set = new Button("Set");
033: final Button quit = new Button("Quit");
034: final Button get_all = new Button("All");
035: final Label stock = new Label("Stock");
036: final Label value = new Label("Value");
037: final Label err_msg = new Label("Error");
038: final TextField stock_field = new TextField();
039: final TextField value_field = new TextField();
040: final java.awt.List listbox = new java.awt.List();
041: final Font default_font = new Font("Helvetica", Font.PLAIN, 12);
042:
043: final String props = null; // default stack from JChannel
044:
045: public QuoteClient() {
046: super ();
047: try {
048: channel = new JChannel(props);
049: channel.setOpt(Channel.LOCAL, Boolean.FALSE);
050: disp = new RpcDispatcher(channel, null, this , this );
051: channel.connect(channel_name);
052: } catch (Exception e) {
053: System.err.println("QuoteClient(): " + e);
054: }
055: addWindowListener(this );
056: }
057:
058: private void showMsg(String msg) {
059: err_msg.setText(msg);
060: err_msg.setVisible(true);
061: }
062:
063: private void clearMsg() {
064: err_msg.setVisible(false);
065: }
066:
067: public void start() {
068: setLayout(null);
069: setSize(400, 300);
070: setFont(default_font);
071:
072: stock.setBounds(new Rectangle(10, 30, 60, 30));
073: value.setBounds(new Rectangle(10, 60, 60, 30));
074: stock_field.setBounds(new Rectangle(100, 30, 100, 30));
075: value_field.setBounds(new Rectangle(100, 60, 100, 30));
076: listbox.setBounds(210, 30, 150, 160);
077: err_msg.setBounds(new Rectangle(10, 200, 350, 30));
078: err_msg.setFont(new Font("Helvetica", Font.ITALIC, 12));
079: err_msg.setForeground(Color.red);
080: err_msg.setVisible(false);
081: get.setBounds(new Rectangle(10, 250, 80, 30));
082: set.setBounds(new Rectangle(100, 250, 80, 30));
083: quit.setBounds(new Rectangle(190, 250, 80, 30));
084: get_all.setBounds(new Rectangle(280, 250, 80, 30));
085:
086: get.addActionListener(this );
087: set.addActionListener(this );
088: quit.addActionListener(this );
089: get_all.addActionListener(this );
090:
091: add(stock);
092: add(value);
093: add(stock_field);
094: add(value_field);
095: add(err_msg);
096: add(get);
097: add(set);
098: add(quit);
099: add(get_all);
100: add(listbox);
101: // stock_field.requestFocus();
102: setVisible(true);
103: }
104:
105: public void windowActivated(WindowEvent e) {
106: }
107:
108: public void windowClosed(WindowEvent e) {
109: }
110:
111: public void windowClosing(WindowEvent e) {
112: System.exit(0);
113: }
114:
115: public void windowDeactivated(WindowEvent e) {
116: }
117:
118: public void windowDeiconified(WindowEvent e) {
119: }
120:
121: public void windowIconified(WindowEvent e) {
122: }
123:
124: public void windowOpened(WindowEvent e) {
125: }
126:
127: public void actionPerformed(ActionEvent e) {
128: String command = e.getActionCommand();
129: RspList rsp_list;
130: Rsp first_rsp;
131:
132: try {
133: if (command.equals("Get")) {
134: String stock_name = stock_field.getText();
135: if (stock_name == null || stock_name.length() == 0) {
136: showMsg("Stock name is empty !");
137: return;
138: }
139: showMsg("Looking up value for " + stock_name + ':');
140: rsp_list = disp.callRemoteMethods(null, "getQuote",
141: new Object[] { stock_name },
142: new String[] { String.class.getName() },
143: GroupRequest.GET_ALL, 10000);
144:
145: Float val = null;
146: for (int i = 0; i < rsp_list.size(); i++) {
147: Rsp rsp = (Rsp) rsp_list.elementAt(i);
148: Object obj = rsp.getValue();
149: if (obj == null || obj instanceof Throwable)
150: continue;
151: val = (Float) obj;
152: break;
153: }
154:
155: if (val != null) {
156: value_field.setText(val.toString());
157: clearMsg();
158: } else {
159: value_field.setText("");
160: showMsg("Value for " + stock_name + " not found");
161: }
162: } else if (command.equals("Set")) {
163: String stock_name = stock_field.getText();
164: String stock_val = value_field.getText();
165: if (stock_name == null || stock_val == null
166: || stock_name.length() == 0
167: || stock_val.length() == 0) {
168: showMsg("Stock name and value have to be present to enter a new value");
169: return;
170: }
171: Float val = new Float(stock_val);
172: disp.callRemoteMethods(null, "setQuote", new Object[] {
173: stock_name, val }, new Class[] { String.class,
174: Float.class }, GroupRequest.GET_FIRST, 0);
175:
176: showMsg("Stock " + stock_name + " set to " + val);
177: } else if (command.equals("All")) {
178: listbox.removeAll();
179: showMsg("Getting all stocks:");
180: rsp_list = disp.callRemoteMethods(null, "getAllStocks",
181: (Object[]) null, (Class[]) null,
182: GroupRequest.GET_ALL, 5000);
183:
184: System.out.println("rsp_list is " + rsp_list);
185:
186: Hashtable all_stocks = null;
187: for (int i = 0; i < rsp_list.size(); i++) {
188: Rsp rsp = (Rsp) rsp_list.elementAt(i);
189: Object obj = rsp.getValue();
190: if (obj == null || obj instanceof Throwable)
191: continue;
192: all_stocks = (Hashtable) obj;
193: break;
194: }
195:
196: if (all_stocks == null) {
197: showMsg("No stocks found");
198: return;
199: }
200: clearMsg();
201: listbox.removeAll();
202: String key;
203: Float val;
204: for (Enumeration en = all_stocks.keys(); en
205: .hasMoreElements();) {
206: key = (String) en.nextElement();
207: val = (Float) all_stocks.get(key);
208: if (val == null)
209: continue;
210: listbox.add(key + ": " + val.toString());
211: }
212: } else if (command.equals("Quit")) {
213: setVisible(false);
214: channel.close();
215: System.exit(0);
216: } else
217: System.out.println("Unknown action");
218: } catch (Exception ex) {
219: value_field.setText("");
220: ex.printStackTrace();
221: showMsg(ex.toString());
222: }
223: }
224:
225: public void setQuote(String stock_name, Float value) {
226: ;
227: }
228:
229: public void printAllStocks() {
230: }
231:
232: public void viewAccepted(View new_view) {
233: setTitle("Members in " + channel_name + ": "
234: + (new_view.size() - 1));
235: }
236:
237: public void suspect(Address suspected_mbr) {
238: }
239:
240: public void block() {
241: }
242:
243: public static void main(String args[]) {
244: QuoteClient client = new QuoteClient();
245: client.start();
246: }
247:
248: }
|