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

public class TelemetryServerConnection {
  String host;
  int port;
  
  TelemetryServerConnection(String h, int p){
    host = h;
    port = p;
  }

  public Vector request(String who, String what, long when){
    StreamTokenizer st;
    Vector rdata = new Vector();
    long time;
    double value;

    try {
//    st = new StreamTokenizer((new URL("http://"+host+":"+port+"/marathon/?runner="+who+"&field="+what+"&time="+when)).openConnection().getInputStream());
    st = new StreamTokenizer((new URL("http://"+host+":"+port+"/marathon/"+who+"/"+what+"/"+when)).openConnection().getInputStream());
    } catch (Exception e) {
      System.out.println("Error!!! "+e);
      return new Vector();
    }
    st.parseNumbers();

    
    /* Get the time, y, x, value */
    while(true){
      time = -1;
      value = -1;
      try {st.nextToken();} catch (Exception e) { return (Vector) null; }
      if(st.ttype == StreamTokenizer.TT_EOF)
	return rdata;
      if(st.ttype == StreamTokenizer.TT_NUMBER)
	time = (long) st.nval;
      
      try {st.nextToken();} catch (Exception e) { return (Vector) null; }
      if(st.ttype == StreamTokenizer.TT_EOF)
	return rdata;
      if(st.ttype == StreamTokenizer.TT_NUMBER)
	value = st.nval;

      if((time >= 0) && (value >= 0)) {
	rdata.addElement(new RunnerData(time, value));
      }
    }
  }

}


