| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2009 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| 25 | using System.IO; |
|---|
| 26 | |
|---|
| 27 | namespace Eraser.Manager |
|---|
| 28 | { |
|---|
| 29 | /// <summary> |
|---|
| 30 | /// Executor base class. This class will manage the tasks currently scheduled |
|---|
| 31 | /// to be run and will run them when they are set to be run. This class is |
|---|
| 32 | /// abstract as they each will have their own ways of dealing with tasks. |
|---|
| 33 | /// </summary> |
|---|
| 34 | public abstract class Executor : IDisposable |
|---|
| 35 | { |
|---|
| 36 | #region IDisposable members |
|---|
| 37 | ~Executor() |
|---|
| 38 | { |
|---|
| 39 | Dispose(false); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | protected virtual void Dispose(bool disposing) |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public void Dispose() |
|---|
| 47 | { |
|---|
| 48 | Dispose(true); |
|---|
| 49 | GC.SuppressFinalize(this); |
|---|
| 50 | } |
|---|
| 51 | #endregion |
|---|
| 52 | |
|---|
| 53 | /// <summary> |
|---|
| 54 | /// Starts the execution of tasks queued. |
|---|
| 55 | /// </summary> |
|---|
| 56 | public abstract void Run(); |
|---|
| 57 | |
|---|
| 58 | /// <summary> |
|---|
| 59 | /// Queues the task for execution. |
|---|
| 60 | /// </summary> |
|---|
| 61 | /// <param name="task">The task to queue.</param> |
|---|
| 62 | public abstract void QueueTask(Task task); |
|---|
| 63 | |
|---|
| 64 | /// <summary> |
|---|
| 65 | /// Schedules the given task for execution. |
|---|
| 66 | /// </summary> |
|---|
| 67 | /// <param name="task">The task to schedule</param> |
|---|
| 68 | public abstract void ScheduleTask(Task task); |
|---|
| 69 | |
|---|
| 70 | /// <summary> |
|---|
| 71 | /// Removes the given task from the execution queue. |
|---|
| 72 | /// </summary> |
|---|
| 73 | /// <remarks>If the task given runs a recurring schedule, the task will only |
|---|
| 74 | /// remove requested tasks and not the scheduled ones</remarks> |
|---|
| 75 | /// <param name="task">The task to cancel.</param> |
|---|
| 76 | public abstract void UnqueueTask(Task task); |
|---|
| 77 | |
|---|
| 78 | /// <summary> |
|---|
| 79 | /// Gets whether a task is currently queued for execution, outside of the |
|---|
| 80 | /// scheduled time. |
|---|
| 81 | /// </summary> |
|---|
| 82 | /// <param name="task">The task to query.</param> |
|---|
| 83 | /// <returns>True if the task is currently queued, false otherwise.</returns> |
|---|
| 84 | internal abstract bool IsTaskQueued(Task task); |
|---|
| 85 | |
|---|
| 86 | /// <summary> |
|---|
| 87 | /// Queues all tasks in the task list which are meant for restart execution. |
|---|
| 88 | /// This is a separate function rather than just running them by default on |
|---|
| 89 | /// task load because creating a new instance and loading the task list |
|---|
| 90 | /// may just be a program restart and may not necessarily be a system |
|---|
| 91 | /// restart. Therefore this fuction has to be explicitly called by clients. |
|---|
| 92 | /// </summary> |
|---|
| 93 | public abstract void QueueRestartTasks(); |
|---|
| 94 | |
|---|
| 95 | /// <summary> |
|---|
| 96 | /// Retrieves the current task list for the executor. |
|---|
| 97 | /// </summary> |
|---|
| 98 | /// <returns>A list of tasks which the executor has registered.</returns> |
|---|
| 99 | public abstract ExecutorTasksCollection Tasks { get; } |
|---|
| 100 | |
|---|
| 101 | /// <summary> |
|---|
| 102 | /// The task added event object. |
|---|
| 103 | /// </summary> |
|---|
| 104 | public EventHandler<TaskEventArgs> TaskAdded { get; set; } |
|---|
| 105 | |
|---|
| 106 | /// <summary> |
|---|
| 107 | /// Helper function for the task added event. |
|---|
| 108 | /// </summary> |
|---|
| 109 | internal void OnTaskAdded(TaskEventArgs e) |
|---|
| 110 | { |
|---|
| 111 | if (TaskAdded != null) |
|---|
| 112 | TaskAdded(this, e); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /// <summary> |
|---|
| 116 | /// The task added event object. |
|---|
| 117 | /// </summary> |
|---|
| 118 | public EventHandler<TaskEventArgs> TaskDeleted { get; set; } |
|---|
| 119 | |
|---|
| 120 | /// <summary> |
|---|
| 121 | /// Helper function for the task deleted event. |
|---|
| 122 | /// </summary> |
|---|
| 123 | internal void OnTaskDeleted(TaskEventArgs e) |
|---|
| 124 | { |
|---|
| 125 | if (TaskDeleted != null) |
|---|
| 126 | TaskDeleted(this, e); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /// <summary> |
|---|
| 130 | /// The task processing event object. |
|---|
| 131 | /// </summary> |
|---|
| 132 | public EventHandler<TaskEventArgs> TaskProcessing { get; set; } |
|---|
| 133 | |
|---|
| 134 | /// <summary> |
|---|
| 135 | /// Helper function for the Task processing event. |
|---|
| 136 | /// </summary> |
|---|
| 137 | protected void OnTaskProcessing(TaskEventArgs e) |
|---|
| 138 | { |
|---|
| 139 | if (TaskProcessing != null) |
|---|
| 140 | TaskProcessing(this, e); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /// <summary> |
|---|
| 144 | /// The task processed event object. |
|---|
| 145 | /// </summary> |
|---|
| 146 | public EventHandler<TaskEventArgs> TaskProcessed { get; set; } |
|---|
| 147 | |
|---|
| 148 | /// <summary> |
|---|
| 149 | /// Helper function for the Task processed event. |
|---|
| 150 | /// </summary> |
|---|
| 151 | protected void OnTaskProcessed(TaskEventArgs e) |
|---|
| 152 | { |
|---|
| 153 | if (TaskProcessed != null) |
|---|
| 154 | TaskProcessed(this, e); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | public abstract class ExecutorTasksCollection : IList<Task>, ICollection<Task>, |
|---|
| 159 | IEnumerable<Task> |
|---|
| 160 | { |
|---|
| 161 | /// <summary> |
|---|
| 162 | /// Constructor. |
|---|
| 163 | /// </summary> |
|---|
| 164 | /// <param name="executor">The <seealso cref="Executor"/> object owning |
|---|
| 165 | /// this task list.</param> |
|---|
| 166 | protected ExecutorTasksCollection(Executor executor) |
|---|
| 167 | { |
|---|
| 168 | Owner = executor; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | #region IList<Task> Members |
|---|
| 172 | public abstract int IndexOf(Task item); |
|---|
| 173 | public abstract void Insert(int index, Task item); |
|---|
| 174 | public abstract void RemoveAt(int index); |
|---|
| 175 | public abstract Task this[int index] { get; set; } |
|---|
| 176 | #endregion |
|---|
| 177 | |
|---|
| 178 | #region ICollection<Task> Members |
|---|
| 179 | public abstract void Add(Task item); |
|---|
| 180 | public abstract void Clear(); |
|---|
| 181 | public abstract bool Contains(Task item); |
|---|
| 182 | public abstract void CopyTo(Task[] array, int arrayIndex); |
|---|
| 183 | public abstract int Count { get; } |
|---|
| 184 | public bool IsReadOnly { get { return false; } } |
|---|
| 185 | public abstract bool Remove(Task item); |
|---|
| 186 | #endregion |
|---|
| 187 | |
|---|
| 188 | #region IEnumerable<Task> Members |
|---|
| 189 | public abstract IEnumerator<Task> GetEnumerator(); |
|---|
| 190 | #endregion |
|---|
| 191 | |
|---|
| 192 | #region IEnumerable Members |
|---|
| 193 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|---|
| 194 | { |
|---|
| 195 | return GetEnumerator(); |
|---|
| 196 | } |
|---|
| 197 | #endregion |
|---|
| 198 | |
|---|
| 199 | /// <param name="stream">The stream to save to.</param> |
|---|
| 200 | public abstract void SaveToStream(Stream stream); |
|---|
| 201 | |
|---|
| 202 | /// <summary> |
|---|
| 203 | /// Loads the task list from the given stream. |
|---|
| 204 | /// </summary> |
|---|
| 205 | /// <remarks>This will append the tasks in the given stream to the current list of |
|---|
| 206 | /// tasks instead of overwriting it.</remarks> |
|---|
| 207 | /// <param name="stream">The stream to save to.</param> |
|---|
| 208 | public abstract void LoadFromStream(Stream stream); |
|---|
| 209 | |
|---|
| 210 | /// <summary> |
|---|
| 211 | /// The owner of this task list. |
|---|
| 212 | /// </summary> |
|---|
| 213 | protected Executor Owner { get; private set; } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|