import java.lang.*;
import java.io.*;
import java.net.*;

class Client {
   public static void main(String args[]) {
      try {
         float speed = (float)34.8;
         Socket skt = new Socket("192.168.1.100", 50007);
         BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));
         PrintWriter out = new PrintWriter(skt.getOutputStream(),true);
         String cmd = "!!type=speedupdate,speed="+speed+"##";

         RecvThread recvThread = new RecvThread();
         recvThread.SetSocket(in); 
         recvThread.start();
         out.println(cmd); 

         int i = 1;
         while (i == 1)
         {
             //sit here doing nothing while recv thread recvs...
         }   
         System.out.print("'\n");
         in.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
   public static void ParseCmd(String _cmd)
   {
      String cmdCode = _cmd.substring(_cmd.indexOf("=")+1,_cmd.indexOf(","));
      if (cmdCode.equals("update"))
      {
          int start = _cmd.indexOf(",") +1;
          int end = _cmd.length();
          ProcUpdate(_cmd.substring(start,end));
      } 
      else
      {
        System.out.println("Unknown cmd received\n");     
        System.out.println(_cmd);
      }
   }
   public static void ProcUpdate(String _updateCmd)
   {
      float bearing; 
      float speed; 
      int xCoord;
      int yCoord;
      int isAlive; 
      String id;


      System.out.println("ProcUpdate:");
      int start = _updateCmd.indexOf("=")+1;
      int end = _updateCmd.indexOf(",");
      int count = Integer.valueOf(_updateCmd.substring(start,end)).intValue();
      System.out.println("count: " + count);
      //Need some error checking below...
      for (int x = 0; x< count; x++)
      {
        _updateCmd = _updateCmd.substring(end+1,_updateCmd.length());
        start = _updateCmd.indexOf("=")+1;
        end = _updateCmd.indexOf(",");
        bearing = Float.valueOf(_updateCmd.substring(start,end)).floatValue();
        System.out.println("bearing: " + bearing);

        _updateCmd = _updateCmd.substring(end+1,_updateCmd.length());
        start = _updateCmd.indexOf("=")+1;
        end = _updateCmd.indexOf(",");
        speed = Float.valueOf(_updateCmd.substring(start,end)).floatValue();
        System.out.println("speed: " + speed);

        _updateCmd = _updateCmd.substring(end+1,_updateCmd.length());
        start = _updateCmd.indexOf("=")+1;
        end = _updateCmd.indexOf(",");
        xCoord = Integer.valueOf(_updateCmd.substring(start,end)).intValue();
        System.out.println("xCoord: " + xCoord);


        _updateCmd = _updateCmd.substring(end+1,_updateCmd.length());
        start = _updateCmd.indexOf("=")+1;
        end = _updateCmd.indexOf(",");
        yCoord = Integer.valueOf(_updateCmd.substring(start,end)).intValue();
        System.out.println("yCoord: " + yCoord);



        _updateCmd = _updateCmd.substring(end+1,_updateCmd.length());
        start = _updateCmd.indexOf("=")+1;
        end = _updateCmd.indexOf(",");
        isAlive = Integer.valueOf(_updateCmd.substring(start,end)).intValue();
        System.out.println("isAlivet: " + isAlive);
  

        _updateCmd = _updateCmd.substring(end+1,_updateCmd.length());
        start = _updateCmd.indexOf("=")+1;
        end = _updateCmd.indexOf(",");
        if (end == -1)
        { 
          end = _updateCmd.indexOf("#");
        }
        id = _updateCmd.substring(start,end);
        System.out.println("id: " + id);
      }
   }
   public static class RecvThread extends Thread
   {
     private BufferedReader in;
     public void run()
     {
        String cmd = "K";  //forced initialization...
        System.out.println("RECV THREAD STARTED\n");
        while (1==1)
        { 
           try
           {
             while (!in.ready()) {}
             cmd = in.readLine();
           }
           catch (Exception e)
           {
             System.out.println ("Tcp Read Error");
           } 
           ParseCmd(cmd);
        }  
     }
     public void SetSocket(BufferedReader _in)
     {
       in = _in;
    }

  }
}	