1 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 ; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 14 15 16 17 32 33 public class QuoteServer implements MembershipListener, MessageListener { 34 final Hashtable stocks=new Hashtable (); 35 Channel channel; 36 RpcDispatcher disp; 37 static final String channel_name="Quotes"; 38 final int num_members=1; 39 42 final String 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 state) { 53 String key; 54 if(state == null) 55 return; 56 for(Enumeration e=state.keys(); e.hasMoreElements();) { 57 key=(String )e.nextElement(); 58 stocks.put(key, state.get(key)); } 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 ()); 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 e) { 84 System.err.println("QuoteServer.start() : " + e); 85 System.exit(-1); 86 } 87 } 88 89 90 91 public float getQuote(String stock_name) throws Exception { 92 System.out.print("Getting quote for " + stock_name + ": "); 93 Float retval=(Float )stocks.get(stock_name); 94 if(retval == null) { 95 System.out.println("not found"); 96 throw new Exception ("Stock " + stock_name + " not found"); 97 } 98 System.out.println(retval.floatValue()); 99 return retval.floatValue(); 100 } 101 102 public void setQuote(String stock_name, Float value) { 103 System.out.println("Setting quote for " + stock_name + ": " + value); 104 stocks.put(stock_name, value); 105 } 106 107 public Hashtable 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 ex) { 125 ex.printStackTrace(); 126 return null; 127 } 128 } 129 130 public void setState(byte[] state) { 131 try { 132 integrate((Hashtable )Util.objectFromByteBuffer(state)); 133 } 134 catch(Exception ex) { 135 ex.printStackTrace(); 136 } 137 } 138 139 public static void main(String args[]) { 140 try { 141 QuoteServer server=new QuoteServer(); 142 server.start(); 143 } 144 catch(Throwable t) { 145 t.printStackTrace(); 146 } 147 } 148 149 } 150 | Popular Tags |