package tutor; import java.net.*; import java.io.*; import java.util.*; import java.applet.*; /** * This class manages a set of tasks which need to be * carried out. The manager doesnt actually need to know * what the task is, just if it has started, or finished, * if it was successful or if it failed. * * A {@linkplain tutor.SoundDownload} is an example of * a {@linkplain tutor.Task}. In this case, the task * of the sound download, is to download a sound file from * the Internet. * * @see: RecordSet, SoundDownload, Record etc */ public class TaskManager extends Object implements Runnable { //-------------------------------------------- /** A set of {@linkplain tutor.Tasks}s which need * to be carried out, or have been carried out */ private Vector tasks; //-------------------------------------------- /** This flag gets set when a new language sound file is * successfully downloaded */ private boolean hasFreshTasks; //-------------------------------------------- private Thread taskThread; //-------------------------------------------- /** a flag to allow the downloading thread to exit */ private boolean isWorking; //-------------------------------------------- /** an interval in milliseconds to pause between tasks */ private long taskPause; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- private Applet appletReference; //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** a constructor which doesnt do much */ public TaskManager() { this.hasFreshTasks = false; this.tasks = new Vector(); this.isWorking = false; } //-- constr: () //-------------------------------------------- public TaskManager(Task[] ttTasks) { this(); for (int ii = 0; ii < ttTasks.length; ii++) { this.tasks.addElement(ttTasks[ii]); } } //-- constr: (tasks) //-------------------------------------------- /** adds a task (a sound to download) to be managed */ public void addTask(Task ssTask) { this.tasks.addElement(ssTask); } //-------------------------------------------- /** clears the list of tasks */ public void clearTasks() { this.tasks.clear(); } //-------------------------------------------- public long getTaskPause() { return this.taskPause; } //-------------------------------------------- /** sets the pause between the execution of each task. * If the task involves downloading from a server, * for example, it may be courteous to pause between each * task */ public void setTaskPause(long lPause) { this.taskPause = lPause; } //-------------------------------------------- public void run() { this.download(); } //-------------------------------------------- /** starts the downloading task thread */ public void startWorking() { this.isWorking = true; this.taskThread = new Thread(this); this.taskThread.start(); } //-------------------------------------------- /** stops the downloading process by setting a flag variable */ public void stopWorking() { this.isWorking = false; } //-------------------------------------------- /** begin the process of downloading the sounds */ public void download() { this.startTime = new Date(); this.isWorking = true; Task sdCurrent; Enumeration ii = this.tasks.elements(); while (ii.hasMoreElements() && this.isWorking) { //-- TODO: add the pause code sdCurrent = (Task)ii.nextElement(); sdCurrent.begin(); if (sdCurrent.wasSuccessful()) { this.hasFreshTasks = true; } } //-- while this.finishTime = new Date(); } //-- method: startWorking() //-------------------------------------------- /** informs how long the manager has been working, milliseconds */ public long getWorkTime() { if (this.startTime == null) { return 0; } if (this.finishTime == null) { Date dNow = new Date(); return dNow.getTime() - this.startTime.getTime(); } return this.finishTime.getTime() - this.startTime.getTime(); } //-- method getWorkTime //-------------------------------------------- /** informs how long the manager has been working in seconds */ public float getWorkTimeInSeconds() { if (this.startTime == null) { return 0; } return (float)this.getWorkTime()/1000; } //-------------------------------------------- public int completedTasks() { int iCompletedTasks = 0; Enumeration ii; Task dCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { dCurrentTask = (Task)ii.nextElement(); if (dCurrentTask.hasFinished()) { iCompletedTasks++; } } //-- while return iCompletedTasks; } //-- m: completedTasks //-------------------------------------------- /** tell the number of completed tasks, successful or failed */ public int countCompletedTasks() { int iGoodTasks = 0; Enumeration ii; Task dCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { dCurrentTask = (Task)ii.nextElement(); if (dCurrentTask.hasFinished()) { iGoodTasks++; } } //-- while return iGoodTasks; } //-------------------------------------------- /** tell the number of successful tasks */ public int countGoodTasks() { int iGoodTasks = 0; Enumeration ii; Task dCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { dCurrentTask = (Task)ii.nextElement(); if (dCurrentTask.hasFinished() && dCurrentTask.wasSuccessful()) { iGoodTasks++; } } //-- while return iGoodTasks; } //-------------------------------------------- /** tell the number of unsuccessful tasks */ public int countBadTasks() { int iBadTasks = 0; Enumeration ii; Task dCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { dCurrentTask = (Task)ii.nextElement(); if (dCurrentTask.hasFinished() && !dCurrentTask.wasSuccessful()) { iBadTasks++; } } //-- while return iBadTasks; } //-------------------------------------------- /** allows another class to know if this class has * download new sound files and if it is worth * calling the getFreshTasks method */ public boolean hasFreshTasks() { return this.hasFreshTasks; } //-------------------------------------------- /** retrieves the successful downloads */ public Vector getGoodTasks() { //-- TODO } //-------------------------------------------- /** retrieves the unsuccessful downloads */ public Vector getBadTasks() { } //-------------------------------------------- /** * returns those downloads which have been retrieved since * the last call to this method. */ public Vector getFreshTasks() { Vector aaReturn = new Vector(); Enumeration ii = this.freshTasks.elements(); while (ii.hasMoreElements()) { aaReturn.addElement(ii.nextElement()); } this.freshTasks.removeAllElements(); this.hasFreshTasks = false; return aaReturn; } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(); return this.toString(); } //-------------------------------------------- public String printFailedTasks() { StringBuffer sReturn = new StringBuffer(); Enumeration ii; Task dCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { //sReturn.append("* "); dCurrentTask = (Task)ii.nextElement(); if (dCurrentTask.hasFinished() && !dCurrentTask.wasSuccessful()) { sReturn.append(dCurrentTask.toString()); sReturn.append(NEWLINE); } } //-- while return sReturn.toString(); } //-------------------------------------------- public String printSuccessfulTasks() { StringBuffer sReturn = new StringBuffer(); Enumeration ii; Task dCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { //sReturn.append("* "); dCurrentTask = (Task)ii.nextElement(); if (dCurrentTask.hasFinished() && dCurrentTask.wasSuccessful()) { sReturn.append(dCurrentTask.toString()); sReturn.append(NEWLINE); } } //-- while return sReturn.toString(); } //-------------------------------------------- /** shows all tasks, or sound downloads which the manager * has carried out, or will carry out */ public String printAllTasks() { StringBuffer sReturn = new StringBuffer(); Enumeration ii; ii = this.tasks.elements(); while (ii.hasMoreElements()) { //sReturn.append("* "); //sdCurrentTask = (Task)ii.nextElement(); sReturn.append((Task)ii.nextElement()); sReturn.append(NEWLINE); } //-- while return sReturn.toString(); } //-------------------------------------------- /** shows those tasks (that is {@linkplain tutor.Task}s) * which the download manager has already completed, */ public String printCompletedTasks() { StringBuffer sReturn = new StringBuffer(); Enumeration ii; Task tCurrentTask; ii = this.tasks.elements(); while (ii.hasMoreElements()) { tCurrentTask = (Task)ii.nextElement(); if (tCurrentTask.hasFinished()) { sReturn.append("* "); sReturn.append(tCurrentTask.print()); sReturn.append(NEWLINE + NEWLINE); } } //-- while return sReturn.toString(); } //-------------------------------------------- /** displays a concise summary. */ public String toString() { StringBuffer sbReturn = new StringBuffer(); Task sdCurrentTask; sbReturn.append("Task Manager: "); sbReturn.append("("); sbReturn.append(this.tasks.size()); sbReturn.append(" tasks, "); sbReturn.append("completed:" + this.countCompletedTasks()); sbReturn.append(", successful:" + this.countGoodTasks()); sbReturn.append(", failed:" + this.countBadTasks()); sbReturn.append(", time taken:"); sbReturn.append(this.getWorkTimeInSeconds()); sbReturn.append(" secs"); sbReturn.append(")"); if (this.startTime != null) { sbReturn.append(" started at:"); sbReturn.append(this.startTime); } if (this.finishTime != null) { sbReturn.append(" -finished-"); } else { sbReturn.append(" -not started-"); } return sbReturn.toString(); } //-- method: toString //-------------------------------------------- /** * prints information about all the attempts to download resources. * * @see "the toString() and printFullReport() methods" * @return a string which is suitable for displaying somewhere */ public String printReport() { StringBuffer sbReturn = new StringBuffer(); Task sdCurrentTask; sbReturn.append("- Task Manager-"); sbReturn.append(NEWLINE); sbReturn.append("number of tasks :"); sbReturn.append(this.tasks.size()); sbReturn.append(NEWLINE); sbReturn.append("completed tasks :"); sbReturn.append(this.countCompletedTasks()); sbReturn.append(NEWLINE); sbReturn.append("successful loads :"); sbReturn.append(this.countGoodTasks()); sbReturn.append(NEWLINE); sbReturn.append("failed loads :"); sbReturn.append(this.countBadTasks()); sbReturn.append(NEWLINE); if (this.startTime == null) { sbReturn.append("start time :"); sbReturn.append(" -not started-"); } else { sbReturn.append("work time :"); sbReturn.append((float)this.getWorkTime()/1000); sbReturn.append(" secs"); sbReturn.append(NEWLINE); sbReturn.append("start time :"); sbReturn.append(this.startTime); } sbReturn.append(NEWLINE); if (this.finishTime == null) { sbReturn.append("finish time :"); sbReturn.append(" -not finished-"); } else { sbReturn.append("finish time :" + this.finishTime); } sbReturn.append(NEWLINE); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-- method: printReport //-------------------------------------------- /** allows the display of information about all the attempts * to download resources from the internet. */ public String printLongReport() { StringBuffer sbReturn = new StringBuffer(); Task sdCurrentTask; sbReturn.append("** Task Manager**"); sbReturn.append(NEWLINE); sbReturn.append("Total Successful Tasks :"); sbReturn.append(this.countGoodTasks()); sbReturn.append(NEWLINE); sbReturn.append("Total Failed Tasks :"); sbReturn.append(this.countBadTasks()); sbReturn.append(NEWLINE); sbReturn.append(NEWLINE); sbReturn.append("--successful loads--"); sbReturn.append(NEWLINE); Enumeration ii; ii = this.tasks.elements(); while (ii.hasMoreElements()) { sbReturn.append(NEWLINE); sdCurrentTask = (Task)ii.nextElement(); if (sdCurrentTask.wasSuccessful()) { sbReturn.append(sdCurrentTask.printReport()); } } //-- while sbReturn.append("--failed loads--"); sbReturn.append(NEWLINE); ii = this.tasks.elements(); while (ii.hasMoreElements()) { sbReturn.append(NEWLINE); sdCurrentTask = (Task)ii.nextElement(); if (!sdCurrentTask.wasSuccessful()) { sbReturn.append(sdCurrentTask.printReport()); } } //-- while return sbReturn.toString(); } //-- method: printFullReport //-------------------------------------------- /** provides a command loop to test the sound download manager */ public static void main(String[] args) throws Exception { StringBuffer sMessage = new StringBuffer(""); sMessage.append("usage: java TaskManager [datafile] \n\n"); sMessage.append(" manages the downloading of sounds from \n"); sMessage.append(" the Internet. See http://bumble.sf.net/lengua/tutor \n"); if (args.length == 0) { System.out.println(sMessage); System.exit(-1); } StringBuffer sOutput = new StringBuffer(); RecordSet rData; rData = new RecordSet(args[0]); TaskManager manager = new TaskManager(rData); Vector soundTasks = manager.getGoodTasks(); //-- make a simple command loop //-- BufferedReader brUserInput = new BufferedReader( new InputStreamReader(System.in)); String sCommand = ""; sMessage.setLength(0); System.out.println("testing the download manager..."); System.out.println("commands:"); System.out.println(" h - show this help message"); System.out.println(" q - exit the program"); System.out.println(" r - show recordset data"); System.out.println(" d - start downloading "); System.out.println(" D - stop downloading "); System.out.println(" t - show all tasks "); System.out.println(" c - show completed tasks"); System.out.println(" s - show concise work summary"); System.out.println(" ss - show longer work summary"); System.out.println(" f - show downloads which failed"); System.out.println(" "); //---------------------------------- //-- the command loop //-- while (!sCommand.equals("q")) { //------------------------------------- //-- show all tasks if (sCommand.equals("r")) { System.out.println(rData.printReport()); } //------------------------------------- //-- show all tasks if (sCommand.equals("t")) { System.out.println(manager.printAllTasks()); } //------------------------------------- if (sCommand.equals("c")) { System.out.println(manager.printCompletedTasks()); } //------------------------------------- //-- start downloading if (sCommand.equals("d")) { System.out.println("starting to download..."); manager.startWorking(); } //------------------------------------- //-- stop downloading if (sCommand.equals("D")) { System.out.println("stopping to download..."); manager.stopWorking(); } //------------------------------------- //-- concise work summary if (sCommand.equals("s")) { System.out.println(manager.toString()); } //------------------------------------- //-- longer work summary if (sCommand.equals("ss")) { System.out.println(manager.printReport()); } //------------------------------------- //-- show failed downloads if (sCommand.equals("f")) { System.out.println(manager.printFailedTasks()); } //------------------------------------- //-- a help message if (sCommand.equals("h")) { System.out.println("testing the download manager..."); System.out.println("commands:"); System.out.println(" h - show this help message"); System.out.println(" q - exit the program"); System.out.println(" r - show recordset data"); System.out.println(" d - start the downloading "); System.out.println(" D - stop the downloading "); System.out.println(" t - show all tasks "); System.out.println(" c - show completed tasks"); System.out.println(" s - show concise work summary"); System.out.println(" ss - show longer work summary"); System.out.println(" f - show downloads which failed"); System.out.println(" "); } System.out.print(">"); sCommand = brUserInput.readLine(); } //-- while System.out.println("method: .printReport()"); System.out.println(manager.printReport()); System.out.println("method: .toString()"); System.out.println(manager.toString()); } //-- main() } //-- TaskManager class