source: branches/eraser6/EraserService/Eraser.Manager/Executor.cs @ 2624

Revision 2624, 7.7 KB checked in by lowjoel, 2 years ago (diff)

First set of changes to allow implementation as a service.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1/*
2 * $Id$
3 * Copyright 2008-2012 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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.IO;
26
27using System.ServiceModel;
28
29namespace Eraser.Manager
30{
31    /// <summary>
32    /// Executor base class. This class will manage the tasks currently scheduled
33    /// to be run and will run them when they are set to be run. This class is
34    /// abstract as they each will have their own ways of dealing with tasks.
35    /// </summary>
36    [ServiceContract(Namespace="http://eraser.sourceforge.net/", SessionMode=SessionMode.Required)]
37    public interface IExecutor : IDisposable
38    {
39        /// <summary>
40        /// Queues the task for execution.
41        /// </summary>
42        /// <param name="task">The task to queue.</param>
43        [OperationContract]
44        void QueueTask(Task task);
45
46        /// <summary>
47        /// Schedules the given task for execution.
48        /// </summary>
49        /// <param name="task">The task to schedule</param>
50        [OperationContract]
51        void ScheduleTask(Task task);
52
53        /// <summary>
54        /// Removes the given task from the execution queue.
55        /// </summary>
56        /// <remarks>If the task given runs a recurring schedule, the task will only
57        /// remove requested tasks and not the scheduled ones</remarks>
58        /// <param name="task">The task to cancel.</param>
59        [OperationContract]
60        void UnqueueTask(Task task);
61
62        /// <summary>
63        /// Queues all tasks in the task list which are meant for restart execution.
64        /// This is a separate function rather than just running them by default on
65        /// task load because creating a new instance and loading the task list
66        /// may just be a program restart and may not necessarily be a system
67        /// restart. Therefore this fuction has to be explicitly called by clients.
68        /// </summary>
69        [OperationContract]
70        void QueueRestartTasks();
71
72        /// <summary>
73        /// Retrieves the current task list for the executor.
74        /// </summary>
75        /// <returns>A list of tasks which the executor has registered.</returns>
76        [OperationContract]
77        ExecutorTasksCollection GetTasks();
78    }
79
80    public abstract class Executor : IExecutor, IDisposable
81    {
82        #region IDisposable members
83        ~Executor()
84        {
85            Dispose(false);
86        }
87
88        protected virtual void Dispose(bool disposing)
89        {
90        }
91
92        public void Dispose()
93        {
94            Dispose(true);
95            GC.SuppressFinalize(this);
96        }
97        #endregion
98
99        /// <summary>
100        /// Starts the execution of tasks queued.
101        /// </summary>
102        public abstract void Run();
103
104        /// <summary>
105        /// Queues the task for execution.
106        /// </summary>
107        /// <param name="task">The task to queue.</param>
108        public abstract void QueueTask(Task task);
109
110        /// <summary>
111        /// Schedules the given task for execution.
112        /// </summary>
113        /// <param name="task">The task to schedule</param>
114        public abstract void ScheduleTask(Task task);
115
116        /// <summary>
117        /// Removes the given task from the execution queue.
118        /// </summary>
119        /// <remarks>If the task given runs a recurring schedule, the task will only
120        /// remove requested tasks and not the scheduled ones</remarks>
121        /// <param name="task">The task to cancel.</param>
122        public abstract void UnqueueTask(Task task);
123
124        /// <summary>
125        /// Gets whether a task is currently queued for execution, outside of the
126        /// scheduled time.
127        /// </summary>
128        /// <param name="task">The task to query.</param>
129        /// <returns>True if the task is currently queued, false otherwise.</returns>
130        internal abstract bool IsTaskQueued(Task task);
131
132        /// <summary>
133        /// Queues all tasks in the task list which are meant for restart execution.
134        /// This is a separate function rather than just running them by default on
135        /// task load because creating a new instance and loading the task list
136        /// may just be a program restart and may not necessarily be a system
137        /// restart. Therefore this fuction has to be explicitly called by clients.
138        /// </summary>
139        public abstract void QueueRestartTasks();
140
141        /// <summary>
142        /// Retrieves the current task list for the executor.
143        /// </summary>
144        /// <returns>A list of tasks which the executor has registered.</returns>
145        public abstract ExecutorTasksCollection Tasks { get; }
146
147        /// <summary>
148        /// Retrieves the current task list for the executor.
149        /// </summary>
150        /// <returns>A list of tasks which the executor has registered.</returns>
151        public ExecutorTasksCollection GetTasks()
152        {
153            return Tasks;
154        }
155
156        /// <summary>
157        /// The task added event object.
158        /// </summary>
159        public EventHandler<TaskEventArgs> TaskAdded { get; set; }
160
161        /// <summary>
162        /// Helper function for the task added event.
163        /// </summary>
164        internal void OnTaskAdded(TaskEventArgs e)
165        {
166            if (TaskAdded != null)
167                TaskAdded(this, e);
168        }
169
170        /// <summary>
171        /// The task added event object.
172        /// </summary>
173        public EventHandler<TaskEventArgs> TaskDeleted { get; set; }
174
175        /// <summary>
176        /// Helper function for the task deleted event.
177        /// </summary>
178        internal void OnTaskDeleted(TaskEventArgs e)
179        {
180            if (TaskDeleted != null)
181                TaskDeleted(this, e);
182        }
183    }
184
185    public abstract class ExecutorTasksCollection : IList<Task>
186    {
187        /// <summary>
188        /// Constructor.
189        /// </summary>
190        /// <param name="executor">The <seealso cref="IExecutor"/> object owning
191        /// this task list.</param>
192        protected ExecutorTasksCollection(IExecutor executor)
193        {
194            Owner = executor;
195        }
196
197        #region IList<Task> Members
198        public abstract int IndexOf(Task item);
199        public abstract void Insert(int index, Task item);
200        public abstract void RemoveAt(int index);
201        public abstract Task this[int index] { get; set; }
202        #endregion
203
204        #region ICollection<Task> Members
205        public abstract void Add(Task item);
206        public abstract void Clear();
207        public abstract bool Contains(Task item);
208        public abstract void CopyTo(Task[] array, int arrayIndex);
209        public abstract int Count { get; }
210        public bool IsReadOnly { get { return false; } }
211        public abstract bool Remove(Task item);
212        #endregion
213
214        #region IEnumerable<Task> Members
215        public abstract IEnumerator<Task> GetEnumerator();
216        #endregion
217
218        #region IEnumerable Members
219        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
220        {
221            return GetEnumerator();
222        }
223        #endregion
224
225        /// <summary>
226        /// Saves a task list to the given stream.
227        /// </summary>
228        /// <param name="stream">The stream to save to.</param>
229        public abstract void SaveToStream(Stream stream);
230
231        /// <summary>
232        /// Saves the task list to file.
233        /// </summary>
234        /// <param name="file">The path to the task list.</param>
235        public virtual void SaveToFile(string file)
236        {
237            using (FileStream stream = new FileStream(file, FileMode.Create,
238                FileAccess.Write, FileShare.None))
239            {
240                SaveToStream(stream);
241            }
242        }
243
244        /// <summary>
245        /// Loads the task list from the given stream.
246        /// </summary>
247        /// <remarks>This will append the tasks in the given stream to the current list of
248        /// tasks instead of overwriting it.</remarks>
249        /// <param name="stream">The stream to save to.</param>
250        /// <exception cref="InvalidDataException">Thrown when the data in the stream is
251        /// invalid or unrecognised.</exception>
252        public abstract void LoadFromStream(Stream stream);
253
254        /// <summary>
255        /// The owner of this task list.
256        /// </summary>
257        protected IExecutor Owner { get; private set; }
258    }
259}
Note: See TracBrowser for help on using the repository browser.