source: trunk/CrypPlugins/KeySearcher_IControl/KeySearcher_IControl.cs @ 1137

Last change on this file since 1137 was 1137, checked in by arnold, 12 years ago

Completely redesigned Manager-JobAdmin-Worker-infrastructure to distribute Jobs with a Peer-to-Peer infrastructure to remote CT2-Workspaces.

To test this infrastructure, open 2 instances of CT and load P2P_Manager_NEW_DES.cte and in the other instance P2P_Worker_NEW.cte.
HINT: Working with remote peers isn't possible every time, because the so called "SuperNode", which is necessary for relaying, sometimes goes down. But testing this infrastructure on different computers in the same network should work every time.

File size: 7.6 KB
Line 
1/*
2   Copyright 2009 Christian Arnold, Universität Duisburg-Essen
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;
21using Cryptool.PluginBase.IO;
22using Cryptool.PluginBase;
23using System.ComponentModel;
24using Cryptool.PluginBase.Miscellaneous;
25using System.Windows.Controls;
26using Cryptool.PluginBase.Analysis;
27// KeyPattern kicked out of this project and will be sourced from the namespace KeySearcher
28using KeySearcher;
29using Cryptool.PluginBase.Control;
30using Cryptool.Plugins.PeerToPeer;
31using Cryptool.Plugins.PeerToPeer.Jobs;
32using System.Threading;
33
34namespace Cryptool.Plugins.KeySearcher_IControl
35{
36    [Author("Christian Arnold", "arnold@cryptool.org", "Universität Duisburg-Essen, Fachgebiet Verteilte Systeme", "http://www.vs.uni-due.de")]
37    [PluginInfo(false, "Peer-based KeySearcher", "Peer-driven KeySearcher. KeySearcher gets the search pattern from a managing peer.", null, "KeySearcher_IControl/P2PKeySearcher.png")]
38    public class KeySearcher_IControl : KeySearcher.KeySearcher, IAnalysisMisc
39    {
40        //only change: mandatory = false!!!
41        [PropertyInfo(Direction.InputData, "Encrypted Data", "Encrypted data out of an Encryption PlugIn", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, "")]
42        public override byte[] EncryptedData
43        {
44            get
45            {
46                return base.EncryptedData;
47            }
48            set
49            {
50                base.EncryptedData = value;
51            }
52        }
53
54        //only change: mandatory = false!!!
55        [PropertyInfo(Direction.InputData, "Initialization Vector", "Initialization vector with which the data were encrypted", "",false,false, DisplayLevel.Beginner,QuickWatchFormat.Hex,"")]
56        public override byte[] InitVector
57        {
58            get
59            {
60                return base.InitVector;
61            }
62            set
63            {
64                base.InitVector = value;
65            }
66        }
67
68        #region IKeySearcherControl Members
69
70        private IControlWorker controlWorker;
71        [PropertyInfo(Direction.ControlSlave, "Master Worker", "For distributed job processing", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
72        public IControlWorker ControlWorker
73        {
74            get
75            {
76                if (this.controlWorker == null)
77                {
78                    // to commit the settings of the plugin to the IControl
79                    this.controlWorker = new KeySearcherMaster(this);
80                }
81                return this.controlWorker;
82            }
83        }
84
85        #endregion
86    }
87
88    public class KeySearcherMaster : IControlWorker
89    {
90        #region processing and result variables and properties
91       
92        private BigInteger JobId;
93        private DateTime dtStartProcessing;
94        private DateTime dtEndProcessing;
95
96        #endregion
97
98        private KeySearcher_IControl keySearcher;
99
100        public KeySearcherMaster(KeySearcher_IControl keysearcher)
101        {
102            this.keySearcher = keysearcher;
103            this.keySearcher.OnBruteforcingEnded += new KeySearcher.KeySearcher.BruteforcingEnded(keySearcher_OnBruteforcingEnded);
104        }
105
106        public IControlEncryption GetEncyptionControl()
107        {
108            return this.keySearcher.ControlMaster;
109        }
110
111        public IControlCost GetCostControl()
112        {
113            return this.keySearcher.CostMaster;
114        }
115
116        void keySearcher_OnBruteforcingEnded(LinkedList<KeySearcher.KeySearcher.ValueKey> top10List)
117        {
118            this.dtEndProcessing = DateTime.Now;
119            // Create a new JobResult
120            TimeSpan processingTime = this.dtEndProcessing.Subtract(this.dtStartProcessing);
121            KeyPatternJobResult jobResult = 
122                new KeyPatternJobResult(this.JobId, top10List, processingTime);
123
124            GuiLogging("Ended bruteforcing JobId '" + this.JobId.ToString() + "' in " 
125                + processingTime.TotalMinutes.ToString() + " minutes",NotificationLevel.Info);
126
127            // if registered, sending the serialized Job Result
128            if (OnProcessingSuccessfullyEnded != null)
129            {
130                OnProcessingSuccessfullyEnded(this.JobId, jobResult.Serialize());
131            }
132        }
133
134        #region IControl Members
135
136        public event IControlStatusChangedEventHandler OnStatusChanged;
137
138        #endregion
139
140        #region IControlWorker Members
141        public event ProcessingSuccessfullyEnded OnProcessingSuccessfullyEnded;
142        public event ProcessingCanceled OnProcessingCanceled;
143        public event InfoText OnInfoTextReceived;
144
145        string sTopicName = String.Empty;
146        public string TopicName
147        {
148            get { return this.sTopicName; }
149            set { this.sTopicName = value; }
150        }
151
152        public bool StartProcessing(byte[] job, out BigInteger jobId)
153        {
154            jobId = 0; //out parameter
155            if (job != null)
156            {
157                KeyPatternJobPart jobPart = new KeyPatternJobPart(job);
158                if (jobPart != null)
159                {
160                    this.JobId = jobPart.JobId;
161                    // fill out parameter
162                    jobId = this.JobId;
163                    this.dtStartProcessing = DateTime.Now;
164
165                    GuiLogging("Deserializing job with id '" + jobPart.JobId + "' was successful. Start bruteforcing the KeyPattern '" + jobPart.Pattern.WildcardKey + "'", NotificationLevel.Info);
166
167                    // call bruteforcing method in a thread, so this method didn't block the flow
168                    Thread bruteforcingThread = new Thread(StartBruteforcing);
169                    bruteforcingThread.Start(jobPart);
170
171                    return true;
172                }
173                else
174                {
175                    GuiLogging("The received job byte[] wasn't null, but couldn't be deserialized!", NotificationLevel.Warning);
176                }
177            }
178            else
179            {
180                GuiLogging("Received job byte[] was null. Nothing to do.", NotificationLevel.Warning);
181            }
182            return false;
183        }
184
185        private void StartBruteforcing(object what)
186        {
187            if (what is KeyPatternJobPart)
188            {
189                KeyPatternJobPart jobPart = what as KeyPatternJobPart;
190                this.keySearcher.BruteforcePattern(jobPart.Pattern, jobPart.EncryptData, jobPart.InitVector,
191                            this.keySearcher.ControlMaster, this.keySearcher.CostMaster);
192            }
193            else
194                throw(new Exception("Bruteforcing object wasn't from Type 'KeyPatternJobPart'!"));
195        }
196
197        public void StopProcessing()
198        {
199            this.keySearcher.Stop();
200        }
201
202        private void GuiLogging(string sText, NotificationLevel notLevel)
203        {
204            if (OnInfoTextReceived != null)
205                OnInfoTextReceived(sText, notLevel);
206        }
207
208        #endregion
209    }
210}
Note: See TracBrowser for help on using the repository browser.