import java.awt.*;
import java.io.*;
import java.util.*;

import internet.*;

/**
 * A class for receiving position reports using UDP
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class UDPPositionReceiver extends    AbstractPositionSubject
                                 implements MessageListener
{
    private UDPMessageReceiver   receiver;


    /**
     * Explicit Value Constructor
     *
     * @param port  The UDP port to listen to
     */
    public UDPPositionReceiver(int port) throws IOException
    {
       super();

       receiver = new UDPMessageReceiver(port);
       receiver.start(); // It should also be stopped!
       receiver.addMessageListener(this);
    }
			    

    /**
     * Handle text messages containing position reports
     *
     * @param line   The position messag
     */
    public void handleMessage(String line)
    {
       int              x, y;
       String           token;
       StringTokenizer  st;




       try 
       {
          st = new StringTokenizer(line,",");

          token = st.nextToken();
          x = Integer.valueOf(token.trim()).intValue();
	    
          token = st.nextToken();
          y = Integer.valueOf(token.trim()).intValue();
	    
          notifyListeners(new Point(x,y));
       } 
       catch (NoSuchElementException nsee) 
       {
          // Bad position
       }
       catch (NumberFormatException nfe) 
       {
          // Bad position
       }
    }
}
