source: trunk/CrypPlugins/PeerToPeerWorker_NEW/P2PJobAdmin.cs @ 1433

Last change on this file since 1433 was 1433, checked in by Paul Lelgemann, 12 years ago

o Extracted common classes from PeerToPeerBase plugin into new PeerToPeer plugin as a preparation for the new P2P proxy
o Modified directory properties to ignore the CrypBuild directory

File size: 11.1 KB
Line 
1/* Copyright 2010 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
2
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6
7       http://www.apache.org/licenses/LICENSE-2.0
8
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14*/
15
16using System;
17using System.Collections.Generic;
18using System.Linq;
19using System.Text;
20using System.Threading;
21using Cryptool.PluginBase;
22using Cryptool.PluginBase.IO;
23using Cryptool.PluginBase.Control;
24using Cryptool.PluginBase.Miscellaneous;
25using System.ComponentModel;
26using Cryptool.Plugins.PeerToPeer.Jobs;
27using Cryptool.Plugins.PeerToPeer.Internal;
28
29namespace Cryptool.Plugins.PeerToPeer
30{
31    /// <summary>
32    /// This PlugIn only works, when its connected with a P2P_Peer object.
33    /// </summary>
34    [Author("Christian Arnold", "arnold@cryptool.org", "Uni Duisburg-Essen", "http://www.uni-due.de")]
35    [PluginInfo(true, "P2P_JobAdmin", "Creates a new Job-Administration-Peer, which receives and forwards jobs ans jobresults", "", "PeerToPeerJobAdmin/worker_medium_neutral.png", "PeerToPeerJobAdmin/worker_medium_working.png", "PeerToPeerJobAdmin/worker_medium_finished.png")]
36    public class P2PJobAdmin : IInput
37    {
38        private P2PJobAdminBase jobAdminBase;
39        private P2PJobAdminSettings settings;
40        private IControlWorker workerControl;
41        private IP2PControl p2pControl;
42
43        #region Constructor and setting stuff
44        public P2PJobAdmin()
45        {
46            this.settings = new P2PJobAdminSettings();
47            this.settings.PropertyChanged += new PropertyChangedEventHandler(settings_PropertyChanged);
48            this.settings.TaskPaneAttributeChanged += new TaskPaneAttributeChangedHandler(settings_TaskPaneAttributeChanged);
49            this.settings.OnPluginStatusChanged += new StatusChangedEventHandler(settings_OnPluginStatusChanged);
50        }
51
52        void settings_OnPluginStatusChanged(IPlugin sender, StatusEventArgs args)
53        {
54            if (OnPluginStatusChanged != null)
55                OnPluginStatusChanged(this, args);
56        }
57
58        void settings_TaskPaneAttributeChanged(ISettings settings, TaskPaneAttributeChangedEventArgs args)
59        {
60            // throw new NotImplementedException();
61        }
62
63        private void settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
64        {
65            if (e.PropertyName == "BtnUnregister")
66            {
67                if (this.jobAdminBase.Started)
68                {
69                    this.jobAdminBase.StopWorkerControl(PubSubMessageType.Unregister);
70                    GuiLogMessage("Worker unregistered from Publisher!", NotificationLevel.Info);
71                }
72                else
73                {
74                    GuiLogMessage("Worker isn't started, so this action isn't possible.", NotificationLevel.Info);
75                }
76            }
77            if (e.PropertyName == "BtnRegister")
78            {
79                this.jobAdminBase.StartWorkerControl(this.settings.TopicName, this.settings.CheckPublishersAvailability * 1000, this.settings.PublishersReplyTimespan * 1000);
80                GuiLogMessage("Worker registers with Publisher!", NotificationLevel.Info);
81            }
82        }
83        #endregion
84
85        #region In and Output
86
87        [PropertyInfo(Direction.ControlMaster, "Working Master", "Connect a WorkingMaster-PlugIn", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
88        public IControlWorker WorkerControl
89        {
90            get
91            {
92                return this.workerControl;
93            }
94            set
95            {
96                this.workerControl = value;
97                OnPropertyChanged("WorkerControl");
98            }
99        }
100
101        /// <summary>
102        /// Catches the completely configurated, initialized and joined P2P object from the P2PPeer-Slave-PlugIn.
103        /// </summary>
104        [PropertyInfo(Direction.ControlMaster, "P2P Master", "Input the P2P-Peer-PlugIn", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
105        public IP2PControl P2PControl
106        {
107            get
108            {
109                return this.p2pControl;
110            }
111            set
112            {
113                if (this.p2pControl != null)
114                {
115                    this.p2pControl.OnStatusChanged -= P2PControl_OnStatusChanged;
116                }
117                if (value != null)
118                {
119                    this.p2pControl = (P2PPeerMaster)value;
120                    this.p2pControl.OnStatusChanged += new IControlStatusChangedEventHandler(P2PControl_OnStatusChanged);
121                    OnPropertyChanged("P2PControl");
122                }
123                else
124                {
125                    this.p2pControl = null;
126                }
127            }
128        }
129
130        private void P2PControl_OnStatusChanged(IControl sender, bool readyForExecution)
131        {
132            //throw new NotImplementedException();
133        }
134
135        #endregion
136
137        #region Events
138
139        public event StatusChangedEventHandler OnPluginStatusChanged;
140
141        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
142
143        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
144
145        #endregion
146
147        #region Standard functionality
148
149        public ISettings Settings
150        {
151            set { this.settings = (P2PJobAdminSettings)value; }
152            get { return this.settings; }
153        }
154
155        public System.Windows.Controls.UserControl Presentation
156        {
157            get { return null; }
158        }
159
160        public System.Windows.Controls.UserControl QuickWatchPresentation
161        {
162            get { return null; }
163        }
164
165        //execution method is below this region
166
167        public void PreExecution()
168        {
169        }
170
171        public void PostExecution()
172        {
173            //throw new NotImplementedException();
174        }
175
176        public void Pause()
177        {
178        }
179
180        public void Stop()
181        {
182            if (this.jobAdminBase != null && this.jobAdminBase.Started)
183            {
184                this.jobAdminBase.StopWorkerControl(PubSubMessageType.Stop);
185            }
186        }
187
188        public void Initialize()
189        {
190        }
191
192        public void Dispose()
193        {
194            if (this.jobAdminBase != null && this.jobAdminBase.Started)
195            {
196                this.jobAdminBase.StopWorkerControl(PubSubMessageType.Stop);
197            }
198        }
199        #endregion
200
201        public void Execute()
202        {
203            // if no P2P Slave PlugIn is connected with this PlugIn --> No execution!
204            if (P2PControl == null)
205            {
206                GuiLogMessage("No P2P_Peer connected with this PlugIn!", NotificationLevel.Error);
207                return;
208            }
209            if (this.settings.TopicName != null && this.settings.PublishersReplyTimespan > 0 && this.settings.CheckPublishersAvailability > 0)
210            {
211                StartJobAdmin();
212            }
213            else
214            {
215                GuiLogMessage("The settings are empty. Operation isn't possible.", NotificationLevel.Error);
216            }
217        }
218
219        void jobAdminBase_OnGuiMessage(string sData, NotificationLevel notificationLevel)
220        {
221            GuiLogMessage(sData, notificationLevel);
222        }
223
224        void jobAdminBase_OnWorkerStopped()
225        {
226            this.settings.WorkerStatusChanged(P2PJobAdminSettings.WorkerStatus.Neutral);           
227        }
228
229        void jobAdminBase_OnSuccessfullyEnded()
230        {
231            this.settings.WorkerStatusChanged(P2PJobAdminSettings.WorkerStatus.Finished);
232        }
233
234        void jobAdminBase_OnStartWorking()
235        {
236            this.settings.WorkerStatusChanged(P2PJobAdminSettings.WorkerStatus.Working);
237        }
238
239        void jobAdminBase_OnCanceledWorking()
240        {
241            this.settings.WorkerStatusChanged(P2PJobAdminSettings.WorkerStatus.Neutral);
242        }
243
244        #region INotifyPropertyChanged Members
245
246        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
247
248        public void OnPropertyChanged(string name)
249        {
250            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
251        }
252
253        public event PluginProgressChangedEventHandler OnPluginProcessChanged;
254
255        private void ProgressChanged(double value, double max)
256        {
257            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
258        }
259
260        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
261        {
262            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
263        }
264
265        #endregion
266
267        public void StartJobAdmin()
268        {
269            if(this.settings.TopicName == null || this.settings.CheckPublishersAvailability <= 0 || this.settings.PublishersReplyTimespan <= 0)
270            {
271                GuiLogMessage("Please set all settings before you want to start the P2PJobAdmin",NotificationLevel.Warning);
272                return;
273            }
274            if (this.P2PControl == null || this.WorkerControl == null)
275            {
276                GuiLogMessage("Starting P2PJobAdmin isn't possible while P2PPeer and/or WorkerControl isn't connected with this PlugIn.", NotificationLevel.Error);
277                return;
278            }
279            if (this.jobAdminBase == null)
280            {
281                this.jobAdminBase = new P2PJobAdminBase(this.P2PControl, this.WorkerControl);
282                this.jobAdminBase.OnGuiMessage += new P2PSubscriberBase.GuiMessage(jobAdminBase_OnGuiMessage);
283                this.jobAdminBase.OnStartWorking += new P2PJobAdminBase.StartWorking(jobAdminBase_OnStartWorking);
284                this.jobAdminBase.OnSuccessfullyEnded += new P2PJobAdminBase.SuccessfullyEnded(jobAdminBase_OnSuccessfullyEnded);
285                this.jobAdminBase.OnCanceledWorking += new P2PJobAdminBase.CanceledWorking(jobAdminBase_OnCanceledWorking);
286                this.jobAdminBase.OnWorkerStopped += new P2PJobAdminBase.WorkerStopped(jobAdminBase_OnWorkerStopped);
287            }
288            if (!this.jobAdminBase.Started)
289            {
290                this.jobAdminBase.StartWorkerControl(this.settings.TopicName, this.settings.CheckPublishersAvailability * 1000,
291                    this.settings.PublishersReplyTimespan * 1000);
292            }
293            else
294            {
295                GuiLogMessage("P2PJobAdmin is already started.", NotificationLevel.Info);
296            }
297        }
298    }
299}
Note: See TracBrowser for help on using the repository browser.