import java.io.*;
import java.net.*;
import java.util.*;

import internet.*;

/**
 * An application that reads position reports from a file
 * and transmits them using UDP
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class UDPPositionServer
{

    public static void main(String[] args)
    {
       BufferedReader        in;
       byte[]                data;
       InetAddress           ipAddress;
       int                   i, port;
       String[]              track;
       UDPMessageSender      sender;


       track = new String[396];
       try 
       {
          in = new BufferedReader(
             new FileReader("track.txt"));

          for (i=0; i<396; i++) 
          {
             track[i] = in.readLine();
          }

          ipAddress = InetAddress.getByName(args[0]);
          port      = Integer.valueOf(args[1]).intValue();
          sender = new UDPMessageSender(ipAddress, port);
          sender.start(); // It should also be stopped!

          System.out.println("Reporting to "+args[0]+
                             " on port "+port+"\n");

          i = 0;
          while (true)
          {
		
             try
             {
                Thread.sleep(1000);

                sender.send(track[i]);

                i++;
                if (i >= track.length-1) i=0;
             } 
             catch (InterruptedException ie) 
             {
                // Do nothing
             }
          }
       }
       catch (UnknownHostException uhe)
       {
          System.err.println("Unknown host: "+uhe);
          System.exit(1);
       }
       catch (IOException ioes)
       {
          System.err.println("Unable to open socket: "+
                             ioes);
          System.exit(1);

       }
    }

}
