source: trunk/CrypPlugins/WorkspaceManager/Execution/ExecutionEngine.cs @ 1592

Last change on this file since 1592 was 1592, checked in by kopal, 12 years ago

Stop now calls all Stop() methods of plugins

File size: 7.8 KB
Line 
1/*                             
2   Copyright 2010 Nils Kopal, Viktor M.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21
22using WorkspaceManager.Model;
23using System.Threading;
24using System.Collections;
25using Cryptool.PluginBase;
26using System.Reflection;
27using Gears4Net;
28
29namespace WorkspaceManager.Execution
30{
31    /// <summary>
32    /// Engine to execute a model of the WorkspaceManager
33    /// </summary>
34    public class ExecutionEngine
35    {
36        private WorkspaceManager WorkspaceManagerEditor;
37        private Scheduler scheduler;
38        private WorkspaceModel workspaceModel;
39     
40        /// <summary>
41        /// Creates a new ExecutionEngine
42        /// </summary>
43        /// <param name="workspaceManagerEditor"></param>
44        public ExecutionEngine(WorkspaceManager workspaceManagerEditor)
45        {
46            WorkspaceManagerEditor = workspaceManagerEditor;
47        }
48
49        /// <summary>
50        /// Is this ExecutionEngine running?
51        /// </summary>
52        public bool IsRunning
53        {
54            get;
55            private set;
56        }
57
58        /// <summary>
59        /// Execute the current Model
60        /// </summary>
61        /// <param name="workspaceModel"></param>
62        public void Execute(WorkspaceModel workspaceModel)
63        {
64            this.workspaceModel = workspaceModel;
65
66            if (!IsRunning)
67            {
68                IsRunning = true;
69                scheduler = new WinFormsScheduler("Scheduler");
70                workspaceModel.resetStates();
71               
72                foreach (PluginModel pluginModel in workspaceModel.AllPluginModels)
73                {
74                    PluginProtocol pluginProtocol = new PluginProtocol(scheduler, pluginModel,this);
75                    pluginModel.PluginProtocol = pluginProtocol;
76                    scheduler.AddProtocol(pluginProtocol);
77                    pluginModel.checkExecutable(pluginProtocol);
78                    pluginProtocol.Start();
79                }
80            }
81        }     
82     
83        /// <summary>
84        /// Stop the execution
85        /// </summary>
86        public void Stop()
87        {
88            scheduler.Shutdown();           
89            foreach(PluginModel pluginModel in workspaceModel.AllPluginModels)
90            {
91                pluginModel.Plugin.Stop();
92            }
93            IsRunning = false;
94        }
95
96        /// <summary>
97        /// Pause the execution
98        /// </summary>
99        public void Pause()
100        {
101            //not implemented yet
102        }
103
104        /// <summary>
105        /// Use the logger of the WorkspaceManagerEditor
106        /// </summary>
107        /// <param name="message"></param>
108        /// <param name="level"></param>
109        public void GuiLogMessage(string message, NotificationLevel level)
110        {           
111            WorkspaceManagerEditor.GuiLogMessage(message, level);
112        }           
113    }
114
115    /// <summary>
116    /// Message send to scheduler for a Plugin to trigger the PreExecution
117    /// </summary>
118    public class MessagePreExecution : MessageBase
119    {
120        public PluginModel PluginModel;
121    }
122
123    /// <summary>
124    /// Message send to scheduler for a Plugin to trigger the Execution
125    /// </summary>
126    public class MessageExecution : MessageBase
127    {
128        public PluginModel PluginModel;
129    }
130
131    /// <summary>
132    /// Message send to scheduler for a Plugin to trigger the PostExecution
133    /// </summary>
134    public class MessagePostExecution : MessageBase
135    {
136        public PluginModel PluginModel;
137    }
138
139     /// <summary>
140    /// A Protocol for a PluginModel
141    /// </summary>
142    public class PluginProtocol : ProtocolBase
143    {
144        private PluginModel pluginModel;
145        private ExecutionEngine executionEngine;
146
147        /// <summary>
148        /// Create a new protocol. Each protocol requires a scheduler which provides
149        /// a thread for execution.
150        /// </summary>
151        /// <param name="scheduler"></param>
152        public PluginProtocol(Scheduler scheduler, PluginModel pluginModel,ExecutionEngine executionEngine)
153            : base(scheduler)
154        {
155            this.pluginModel = pluginModel;
156            this.executionEngine = executionEngine;
157        }
158
159        /// <summary>
160        /// The main function of the protocol
161        /// </summary>
162        /// <param name="stateMachine"></param>
163        /// <returns></returns>
164        public override System.Collections.Generic.IEnumerator<ReceiverBase> Execute(AbstractStateMachine stateMachine)
165        {
166            yield return Receive<MessagePreExecution>(null, this.HandlePreExecute);
167            MessageExecution msg_exec = new MessageExecution();
168            msg_exec.PluginModel = this.pluginModel;
169            this.BroadcastMessageReliably(msg_exec);
170            yield return Receive<MessageExecution>(null, this.HandleExecute);
171            MessagePostExecution msg_post = new MessagePostExecution();
172            msg_post.PluginModel = this.pluginModel;
173            this.BroadcastMessageReliably(msg_post);
174            yield return Receive<MessagePostExecution>(null, this.HandlePostExecute);
175        }
176
177        /// <summary>
178        /// Handler function for a message.
179        /// This handler must not block, because it executes inside the thread of the scheduler.
180        /// </summary>
181        /// <param name="msg"></param>
182        private void HandlePreExecute(MessagePreExecution msg)
183        {
184            executionEngine.GuiLogMessage("HandlePreExecute for \"" + msg.PluginModel.Name + "\"", NotificationLevel.Debug);
185            msg.PluginModel.Plugin.PreExecution();
186        }
187
188        /// <summary>
189        /// Handler function for a message.
190        /// This handler must not block, because it executes inside the thread of the scheduler.
191        /// </summary>
192        /// <param name="msg"></param>
193        private void HandleExecute(MessageExecution msg)
194        {
195            executionEngine.GuiLogMessage("HandleExecute for \"" + msg.PluginModel.Name + "\"", NotificationLevel.Debug);
196            //Fill the plugins Inputs with data
197            foreach (ConnectorModel connectorModel in pluginModel.InputConnectors)
198            {
199                if (connectorModel.HasData)
200                {
201                    PropertyInfo propertyInfo = pluginModel.Plugin.GetType().GetProperty(connectorModel.PropertyName);
202                    propertyInfo.SetValue(pluginModel.Plugin, connectorModel.Data, null);
203                    connectorModel.Data = null;
204                    connectorModel.HasData = false;
205                    connectorModel.InputConnection.Active = false;
206                }
207            }
208            msg.PluginModel.Plugin.Execute();
209        }
210
211        /// <summary>
212        /// Handler function for a message.
213        /// This handler must not block, because it executes inside the thread of the scheduler.
214        /// </summary>
215        /// <param name="msg"></param>
216        private void HandlePostExecute(MessagePostExecution msg)
217        {
218            executionEngine.GuiLogMessage("HandlePostExecute for \"" + msg.PluginModel.Name + "\"", NotificationLevel.Debug);
219            msg.PluginModel.Plugin.PostExecution();
220        }
221    }
222}
Note: See TracBrowser for help on using the repository browser.