Source of QuotePushServerImpl.java


  1: 
  2: import java.util.*;
  3: import java.rmi.*;
  4: import java.rmi.server.UnicastRemoteObject;
  5: 
  6: public class QuotePushServerImpl extends UnicastRemoteObject 
  7:      implements QuotePushServer, Runnable {
  8: 
  9:   public QuotePushServerImpl() throws java.rmi.RemoteException {} 
 10: 
 11:   static protected StockQuote quote[] = { 
 12:     new StockQuote("IBM", 100), 
 13:     new StockQuote("Sun", 100),  
 14:     new StockQuote("Intel", 100), 
 15:     new StockQuote("Apple", 100), 
 16:     new StockQuote("Compaq", 100) }; 
 17:   static protected int n = 5; 
 18: 
 19:   public StockQuote[] getQuote() throws java.rmi.RemoteException {
 20:     return quote; 
 21:   }
 22:   
 23:   public void watch(QuoteClient client, String[] list) throws java.rmi.RemoteException {
 24:     clients.addElement(new ClientInfo(client, list)); 
 25:   }
 26: 
 27:   public static void main(String[] args) {
 28:     System.out.println("Start RMI QuotePushServer."); 
 29:     System.setSecurityManager(new RMISecurityManager());
 30: 
 31:     try {
 32:       QuotePushServerImpl obj = new QuotePushServerImpl();
 33:       System.out.println("QuotePushServerImpl created");
 34:       Naming.rebind("QuotePushServer", obj);
 35:       System.out.println("QuotePushServerImpl bound in the registry to the name QuotePushServer");
 36:       new Thread(obj).start();       
 37:     } catch (Exception e) {
 38:       System.out.println("QuotePushServerImpl.main: an exception occurred:");
 39:       System.out.println(e);
 40:     }
 41:   }
 42: 
 43:   public void run() {
 44:     while (true) {
 45:       try {
 46:         Thread.currentThread().sleep(10000); 
 47:         System.out.println("New quote:");
 48:         for (int i = 0; i < n; i++) {
 49:           int dq = 0; 
 50:           if (Math.random() < 0.5) {
 51:             dq = (int) ((Math.random() - 0.4) * ( 10.0 * Math.random()));   
 52:           }
 53:           if (dq > 0) {
 54:             quote[i].quote += dq; 
 55:             System.out.println("Changed: " + quote[i].name + " " + 
 56:                                quote[i].quote);
 57:             Enumeration enum = clients.elements();
 58:             while  (enum.hasMoreElements()) {
 59:               ClientInfo ci = (ClientInfo) enum.nextElement(); 
 60:               for (int k = 0; k < ci.watch.length; k++) {
 61:                 if (ci.watch[k].equals(quote[i].name)) {
 62:                   ci.client.newQuote(quote[i]); 
 63:                   break; 
 64:                 }
 65:               }              
 66:             }
 67:           }
 68:         }
 69:         System.out.println("End quote.");
 70:       } catch (Exception e) {
 71:         System.err.println(e); 
 72:       }
 73:     }
 74:   }
 75: 
 76:   protected Vector clients = new Vector(); 
 77: 
 78:   class ClientInfo {
 79:     ClientInfo(QuoteClient client, String[] watch) {
 80:       this.client = client; 
 81:       this.watch = watch; 
 82:     }
 83: 
 84:     QuoteClient client; 
 85:     String watch[]; 
 86:   }
 87: 
 88: }