KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc;
14 import java.awt.event.ActionListener JavaDoc;
15 import java.awt.event.WindowEvent JavaDoc;
16 import java.awt.event.WindowListener JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Hashtable JavaDoc;
19
20
21
22
23 /**
24  * Used in conjunction with QuoteServer: a client is member of a group of quote servers which replicate
25  * stock quotes among themselves. The client broadcasts its request (set, get quotes) and (in the case of get
26  * waits for the first reply received (usually the one from the quote server closest to it). The client
27  * can get and set quotes as long as a minimum of 1 server (in the group) is running.
28  * @author Bela Ban
29  */

30 public class QuoteClient extends Frame implements WindowListener JavaDoc, ActionListener JavaDoc,
31         MembershipListener {
32     static final String JavaDoc 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 JavaDoc listbox=new java.awt.List JavaDoc();
45     final Font default_font=new Font("Helvetica", Font.PLAIN, 12);
46     // String props="UDP:PING:FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:"+
47
// "VIEW_ENFORCER:STATE_TRANSFER:QUEUE";
48

49     final String JavaDoc 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 JavaDoc e) {
68             System.err.println("QuoteClient(): " + e);
69         }
70         addWindowListener(this);
71     }
72
73     private void showMsg(String JavaDoc 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         // stock_field.requestFocus();
118
setVisible(true);
119     }
120
121
122     public void windowActivated(WindowEvent JavaDoc e) {
123     }
124
125     public void windowClosed(WindowEvent JavaDoc e) {
126     }
127
128     public void windowClosing(WindowEvent JavaDoc e) {
129         System.exit(0);
130     }
131
132     public void windowDeactivated(WindowEvent JavaDoc e) {
133     }
134
135     public void windowDeiconified(WindowEvent JavaDoc e) {
136     }
137
138     public void windowIconified(WindowEvent JavaDoc e) {
139     }
140
141     public void windowOpened(WindowEvent JavaDoc e) {
142     }
143
144
145     public void actionPerformed(ActionEvent JavaDoc e) {
146         String JavaDoc command=e.getActionCommand();
147         RspList rsp_list;
148         Rsp first_rsp;
149
150         try {
151             if(command == "Get") {
152                 String JavaDoc 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 JavaDoc[]{stock_name},
159                                                 new String JavaDoc[]{String JavaDoc.class.getName()},
160                                                 GroupRequest.GET_FIRST, 10000);
161
162                 first_rsp=(Rsp)rsp_list.elementAt(0);
163                 Float JavaDoc val=first_rsp != null? (Float JavaDoc)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 JavaDoc stock_name=stock_field.getText();
176                     String JavaDoc 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 JavaDoc val=new Float JavaDoc(stock_val);
183                     disp.callRemoteMethods(null, "setQuote", new Object JavaDoc[]{stock_name, val},
184                                            new Class JavaDoc[]{String JavaDoc.class, Float JavaDoc.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 JavaDoc[])null, (Class JavaDoc[])null,
195                                                         GroupRequest.GET_NONE, 0);
196
197                         System.out.println("rsp_list is " + rsp_list);
198
199
200                         Hashtable JavaDoc all_stocks=(Hashtable JavaDoc)rsp_list.getFirst();
201                         if(all_stocks == null) {
202                             showMsg("No stocks found");
203                             return;
204                         }
205                         clearMsg();
206                         listbox.removeAll();
207                         String JavaDoc key;
208                         Float JavaDoc val;
209                         for(Enumeration JavaDoc en=all_stocks.keys(); en.hasMoreElements();) {
210                             key=(String JavaDoc)en.nextElement();
211                             val=(Float JavaDoc)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 JavaDoc 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 JavaDoc args[]) {
246         QuoteClient client=new QuoteClient();
247         client.start();
248     }
249
250 }
251
Popular Tags