import java.io.*;
import java.util.*;

/**
 * A class that reads in the daily list of dispatching 
 * tasks from a file and hands them off to a Dispatcher.
 *
 * Note: This version sleeps between dispatches rather than
 * running in a tight loop.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 3.0
 */
public class DailyDispatchHandler extends AbstractDispatchHandler
{
    /**
     * Construct a new DailyDispatchHandler
     *
     * @param d   The Dispatcher to use
     * @param f   The name of the dispatch file
     */
    public DailyDispatchHandler(Dispatcher d, String f) throws IOException
    {
        super(d, new BufferedReader(new FileReader(f)));
    }

    /**
     * Process an individual dispatch (required by 
     * AbstractDispatchHandler).
     *
     * @param line   The dispatch
     */
    public void processDispatch(String line)
    {
       int               wait;
       String            message;
       StringTokenizer   st;

       st = new StringTokenizer(line,"\t");
       wait = Integer.parseInt(st.nextToken());
       message = st.nextToken();

       try
       {
           // Sleep the appropriate amount of time 
           // before dispatching the vehicle.  Other
           // threads can execute while this one
           // is sleeping.
           //
           controlThread.sleep(wait);
           
           dispatcher.dispatch(message);
       }
       catch (InterruptedException ie)
       {
           // Don't dispatch
       }
    }
}
