Source of TickerPushClient.java


  1: 
  2: import java.awt.*;
  3: import java.util.*;
  4: import java.net.*;
  5: import java.io.*;
  6: 
  7: /* 
  8:   Protocol --- Ticker client pull 
  9: 
 10:   Port:   8002 
 11:  
 12:   Client: connect
 13:   Client: WATCH name1 name2 ...  
 14:   Server: name1 quote1             // send all quotes the first time 
 15:           name2 quote2 
 16:           ...
 17:   Server: DONE 
 18:   Server: name quote               // resend quote when changed  
 19:   Server: name quote
 20:   ... 
 21:   Client: CLOSE 
 22: 
 23:   Client design: 
 24:   two threads: animator, quote listener   
 25:   animator waits until all quotes are in 
 26:   no synchronization necassary for quote[] 
 27: */ 
 28: 
 29: public class TickerPushClient extends Ticker { 
 30:         
 31:   protected Socket socket; 
 32:   protected PrintWriter out; 
 33:   protected QuoteListener quotelistener;
 34:   
 35:   public void initQuotes() {
 36:     try {
 37:       socket = new Socket(url.getHost(), 8002);     
 38:       out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 
 39:       out.println("WATCH " + watch); 
 40:       out.flush(); 
 41:       System.out.println("Send: " + "WATCH " + watch); 
 42: 
 43:       BufferedReader in = 
 44:           new BufferedReader(new InputStreamReader(socket.getInputStream())); 
 45:       String line; 
 46:       while ((line = in.readLine()) != null) {
 47:         System.out.println("Receive: " + line); 
 48: 
 49:         if (line.trim().equals("DONE")) {
 50:           break; 
 51:         }
 52:         StringTokenizer tk = new StringTokenizer(line); 
 53:         String name = tk.nextToken(); 
 54:         for (int i = 0; i < n; i++) {
 55:           if (symbol[i].equals(name)) {
 56:             String newquote = tk.nextToken();
 57:             quote[i] = newquote; 
 58:           }
 59:         } 
 60:       }
 61:       out.flush();
 62:       quotelistener = new QuoteListener(this, in); 
 63:       quotelistener.start(); 
 64:     } catch (IOException e) {
 65:       System.err.println(e); 
 66:     }
 67:   }
 68: 
 69:   public void destroy() {
 70:     out.println("CLOSE"); 
 71:     quotelistener.interrupt(); 
 72:   }
 73: 
 74: }
 75: 
 76: class QuoteListener extends Thread {
 77: 
 78:   public QuoteListener(TickerPushClient ticker, BufferedReader in) {
 79:     this.ticker = ticker; 
 80:     this.in = in; 
 81:   }
 82: 
 83:   public void run() {
 84:     String line; 
 85:     try {
 86:       while ((line = in.readLine()) != null) {
 87:         System.out.println("Receive: " + line);
 88: 
 89:         StringTokenizer tk = new StringTokenizer(line); 
 90:         String name = tk.nextToken(); 
 91:         for (int i = 0; i < ticker.n; i++) {
 92:           if (ticker.symbol[i].equals(name)) {
 93:             String newquote = tk.nextToken();
 94:             ticker.quote[i] = newquote; 
 95:           }
 96:         } 
 97:         if (isInterrupted()) {
 98:           break; 
 99:         }
100:       }
101:       in.close();
102:     } catch (IOException e) {
103:       System.out.println(e); 
104:     }
105:   }
106: 
107:   protected TickerPushClient ticker;
108:   protected BufferedReader in;
109: }