package COM.xinit.demon.co.uk.3tier.tickertape;

import java.io.*;
import java.net.*;

import database.StockRec;

public class TickerReader
{
    public Socket socket;
    public DataInputStream in;
    public DataOutputStream out;
    int stockCount;

    public TickerReader(String hostname, int portnum)
    {
	try {
		socket = new Socket(hostname, portnum);
		socket.setSoTimeout(10000);

		in = new DataInputStream(socket.getInputStream());
		out = new DataOutputStream(socket.getOutputStream());

	} catch (UnknownHostException e) {
	    System.err.println("TickerReader.TickerReader(): hostname unknown: " + hostname);
	    return;
	} catch (IOException e) {
	    System.err.println("TickerReader: " + e.getMessage());
	    return;
	}
    }

    public String readData()
    {
	String symbol;
	StringBuffer quotes = new StringBuffer();
	float price;
	int i;

	if (socket == null)
	    return "No Data - null socket";

	try {

	    out.writeUTF("Bugger");
	    stockCount = in.readInt();

	    for ( i = 0; i < stockCount; i++)
	    {
		try {
		    symbol = in.readUTF();
		} catch (InterruptedIOException e)
		{
		    System.out.println("Feed has died");
		    return "Dead Feed - No Data present";
		}

		try {
		    price  = in.readFloat();
		} catch (InterruptedIOException e) {
		    System.out.println("Feed has died");
		    return "Dead Feed - No Data present";
		}

		quotes.append(symbol);
		quotes.append("  ");
		quotes.append((int)price);
		quotes.append(" ");
		quotes.append(MakeFraction.valueToFractionString(price));
		quotes.append("   ");
	    }
	    return quotes.toString();

	} catch (IOException e) {
	    System.err.println("TickerReader.readData(): " + e.getMessage());
	    return "Feed Dead - No Data";
	}
    }

    public void closePort()
    {
	if (socket == null)
	    return;

	try {
	    socket.close();
	} catch (IOException e) {
	    System.err.println("TickerReader.closePort(): " + e.getMessage());
	    return;
	}
    }
}



Last Updated