KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: QuoteServer.java,v 1.5 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.RpcDispatcher;
8 import org.jgroups.util.Util;
9
10 import java.util.Date JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13
14
15
16
17 /**
18  * Example of a replicated quote server. The server maintains state which consists of a list
19  * of quotes and their corresponding values. When it is started, it tries to reach other
20  * quote servers to get its initial state. If it does not receive any response after 5
21  * seconds, it assumes it is the first server and starts processing requests. When it
22  * receives a view notification it checks whether there are more members in the view than in
23  * its previous view. If this is the case, it broadcasts a request for the state to all
24  * members. Integration of different states is simply the union of all states (with the
25  * danger of overwriting mutually inconsistent state).<p> This mechanism allows both for
26  * fast discovery of initial servers, and - in the case of partition merging - for
27  * reintegration of existing servers. Broadcasting the state request upon every view change
28  * (only when new members are joined) causes potentially a lot of network traffic, but it is
29  * assumes that there will not be more than 5 quote servers at most.
30  * @author Bela Ban
31  */

32
33 public class QuoteServer implements MembershipListener, MessageListener {
34     final Hashtable JavaDoc stocks=new Hashtable JavaDoc();
35     Channel channel;
36     RpcDispatcher disp;
37     static final String JavaDoc channel_name="Quotes";
38     final int num_members=1;
39     //String props="UDP:PING:FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:"+
40
// "VIEW_ENFORCER:STATE_TRANSFER:QUEUE";
41

42     final String JavaDoc props=
43             "UDP:"
44             + "PING(num_initial_members=2;timeout=3000):"
45             + "FD:"
46             + "pbcast.PBCAST(gossip_interval=5000;gc_lag=50):"
47             + "UNICAST:"
48             + "FRAG:"
49             + "pbcast.GMS:"
50             + "pbcast.STATE_TRANSFER";
51
52     private void integrate(Hashtable JavaDoc state) {
53         String JavaDoc key;
54         if(state == null)
55             return;
56         for(Enumeration JavaDoc e=state.keys(); e.hasMoreElements();) {
57             key=(String JavaDoc)e.nextElement();
58             stocks.put(key, state.get(key)); // just overwrite
59
}
60     }
61
62     public void viewAccepted(View new_view) {
63         System.out.println("Accepted view (" + new_view.size() + new_view.getMembers() + ')');
64     }
65
66     public void suspect(Address suspected_mbr) {
67     }
68
69     public void block() {
70     }
71
72     public void start() {
73         try {
74             channel=new JChannel(props);
75             channel.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE);
76             disp=new RpcDispatcher(channel, this, this, this);
77             channel.connect(channel_name);
78             System.out.println("\nQuote Server started at " + new Date JavaDoc());
79             System.out.println("Joined channel '" + channel_name + "' (" + channel.getView().size() + " members)");
80             channel.getState(null, 0);
81             System.out.println("Ready to serve requests");
82         }
83         catch(Exception JavaDoc e) {
84             System.err.println("QuoteServer.start() : " + e);
85             System.exit(-1);
86         }
87     }
88
89     /* Quote methods: */
90
91     public float getQuote(String JavaDoc stock_name) throws Exception JavaDoc {
92         System.out.print("Getting quote for " + stock_name + ": ");
93         Float JavaDoc retval=(Float JavaDoc)stocks.get(stock_name);
94         if(retval == null) {
95             System.out.println("not found");
96             throw new Exception JavaDoc("Stock " + stock_name + " not found");
97         }
98         System.out.println(retval.floatValue());
99         return retval.floatValue();
100     }
101
102     public void setQuote(String JavaDoc stock_name, Float JavaDoc value) {
103         System.out.println("Setting quote for " + stock_name + ": " + value);
104         stocks.put(stock_name, value);
105     }
106
107     public Hashtable JavaDoc getAllStocks() {
108         System.out.print("getAllStocks: ");
109         printAllStocks();
110         return stocks;
111     }
112
113     public void printAllStocks() {
114         System.out.println(stocks);
115     }
116
117     public void receive(Message msg) {
118     }
119
120     public byte[] getState() {
121         try {
122             return Util.objectToByteBuffer(stocks.clone());
123         }
124         catch(Exception JavaDoc ex) {
125             ex.printStackTrace();
126             return null;
127         }
128     }
129
130     public void setState(byte[] state) {
131         try {
132             integrate((Hashtable JavaDoc)Util.objectFromByteBuffer(state));
133         }
134         catch(Exception JavaDoc ex) {
135             ex.printStackTrace();
136         }
137     }
138
139     public static void main(String JavaDoc args[]) {
140         try {
141             QuoteServer server=new QuoteServer();
142             server.start();
143         }
144         catch(Throwable JavaDoc t) {
145             t.printStackTrace();
146         }
147     }
148
149 }
150
Popular Tags