source: trunk/CrypPlugins/PeerToPeerBase/P2PPeer.cs @ 813

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

P2P Classes updated (Remove, Base) and removed old P2P Classes (P2PStore, P2PLoad)

File size: 8.4 KB
Line 
1/* Copyright 2009 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.Control;
22using Cryptool.PluginBase;
23using Cryptool.PluginBase.Miscellaneous;
24using System.ComponentModel;
25using Cryptool.PluginBase.IO;
26
27/*
28 * TODO:
29 * - Events des Start- und Stop-Button aus Settings auffangen und entsprechend
30 *   Peer starten/stoppen. Natürlich auch Behandlung, daß nach START spätestens
31 *   der Peer gestartet wird...
32 */ 
33
34namespace Cryptool.Plugins.PeerToPeer
35{
36    [Author("Christian Arnold", "arnold@cryptool.org", "Uni Duisburg-Essen", "http://www.uni-due.de")]
37    [PluginInfo(false, "P2P_Peer", "Creates a new Peer", "", "PeerToPeerBase/ct2_p2p_system_icon_medium.png")]
38    public class P2PPeer : IIOMisc
39    {
40        #region Variables
41
42        /// <summary>
43        /// dirty workaround!!!
44        /// </summary>
45        private static bool bolFirstInitalisation = true;
46        private P2PPeerSettings settings;
47        private P2PBase p2pBase;
48
49        #endregion
50
51        public P2PPeer()
52        {
53            this.p2pBase = new P2PBase();
54            this.settings = new P2PPeerSettings(p2pBase);
55            this.settings.TaskPaneAttributeChanged += new TaskPaneAttributeChangedHandler(settings_TaskPaneAttributeChanged);
56        }
57
58        void settings_TaskPaneAttributeChanged(ISettings settings, TaskPaneAttributeChangedEventArgs args)
59        {
60            //throw new NotImplementedException();
61        }
62
63        #region IPlugin Members
64
65        public event StatusChangedEventHandler OnPluginStatusChanged;
66
67        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
68
69        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
70
71        public ISettings Settings
72        {
73            set { this.settings = (P2PPeerSettings)value; }
74            get { return this.settings; }
75        }
76
77        public System.Windows.Controls.UserControl Presentation
78        {
79            get { return null; }
80        }
81
82        public System.Windows.Controls.UserControl QuickWatchPresentation
83        {
84            get { return null; }
85        }
86
87        public void PreExecution()
88        {
89            // nicht in PreExecute möglich, da schon beim Laden von CT2 gefeuert. Input-Data werden erst NACH PreExecute gesetzt!
90            if (bolFirstInitalisation)
91            {
92                bolFirstInitalisation = false;
93            }
94            else
95            {
96                // TODO: When started on PreExecution hide Start-Button of Settings and set Stop-Button to visible!
97                if (!settings.PeerStarted)
98                {
99                    // starts peer in the settings class
100                    this.settings.PeerStarted = true;
101                    //this.p2pBase.InitializeAll(settings.P2PPeerName, settings.P2PWorldName, settings.P2PLinkMngrType, settings.P2PBSType, settings.P2POverlType, settings.P2PDhtType);
102                }
103
104                //this.p2pBase.Initialize(settings.P2PPeerName, settings.P2PWorldName);
105                // use the settings-Buttons "Start" and "Stop" to start and stop the P2P-System...
106                //this.p2pBase.SynchStart();
107            }
108        }
109
110        public void Execute()
111        {
112
113        }
114
115        public void process(IP2PControl sender)
116        {
117            GuiLogMessage("P2P Peer method 'process' is executed!", NotificationLevel.Debug);
118        }
119
120        public void PostExecution()
121        {
122        }
123
124        public void Pause()
125        {
126        }
127
128        public void Stop()
129        {
130        }
131
132        public void Initialize()
133        {
134        }
135
136        public void Dispose()
137        {
138            //settings are already set to null in this Step...
139            //unsolved design problem in CT2...
140            //this.settings.PeerStopped = true;
141            if (p2pBase != null)
142            {
143                p2pBase.SynchStop();
144                p2pBase = null;
145            }
146        }
147
148        #endregion
149
150        #region INotifyPropertyChanged Members
151
152        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
153
154        public void OnPropertyChanged(string name)
155        {
156            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
157        }
158
159        public event PluginProgressChangedEventHandler OnPluginProcessChanged;
160
161        private void ProgressChanged(double value, double max)
162        {
163            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
164        }
165
166        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
167        {
168            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
169        }
170
171        #endregion
172
173        #region In and Output
174
175        private IP2PControl p2pSlave;
176        [PropertyInfo(Direction.ControlSlave, "Master Peer", "One peer to rule them all", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
177        public IP2PControl P2PControlSlave
178        {
179            get
180            {
181                if (this.p2pSlave == null)
182                    // to commit the settings of the plugin to the IControl
183                    this.p2pSlave = new P2PPeerMaster(p2pBase);
184                return this.p2pSlave;
185            }
186            set
187            {
188                if (this.p2pSlave != null)
189                    this.p2pSlave.OnStatusChanged -= p2pSlave_OnStatusChanged;
190                //{
191                //    //Only when using asynchronous p2p-Start-method, remove event handler for OnPeerJoinedCompletely
192                //    this.p2pSlave.OnPeerJoinedCompletely -= OnPeerJoinedCompletely;
193                //}
194                if (this.p2pSlave != value)
195                {
196                    this.p2pSlave.OnStatusChanged +=new IControlStatusChangedEventHandler(p2pSlave_OnStatusChanged);
197                    //Only when using asynchronous p2p-Start-method, add event handler for OnPeerJoinedCompletely
198                    //this.p2pSlave.OnPeerJoinedCompletely += new PeerJoinedP2P(OnPeerJoinedCompletely);
199                    this.p2pSlave = value;
200                    OnPropertyChanged("P2PControlSlave");
201                }
202            }
203        }
204
205        void p2pSlave_OnStatusChanged(IControl sender, bool readyForExecution)
206        {
207            if (readyForExecution)
208                this.process((IP2PControl)sender);
209        }
210
211        #endregion
212    }
213
214    public class P2PPeerMaster : IP2PControl
215    {
216        private P2PBase p2pBase;
217
218        public P2PPeerMaster(P2PBase p2pBase)
219        {
220            this.p2pBase = p2pBase;
221        }
222
223        #region IP2PControl Members
224
225        public bool DHTstore(string sKey, string sValue)
226        {
227            return this.p2pBase.SynchStore(sKey, sValue);
228        }
229
230        public byte[] DHTload(string sKey)
231        {
232            return this.p2pBase.SynchRetrieve(sKey);
233        }
234
235        public bool DHTremove(string sKey)
236        {
237            // derzeit liegt wohl in peerq@play ein Fehler in der Methode...
238            // erkennt den Übergabeparameter nicht an und wirft dann "ArgumentNotNullException"...
239            // Problem an M.Helling und S.Holzapfel von p@p weitergegeben...
240            return this.p2pBase.SynchRemove(sKey);
241            //return false;
242        }
243
244        public string GetPeerName()
245        {
246            return this.p2pBase.GetPeerName();
247        }
248
249
250        //public event PeerJoinedP2P OnPeerJoinedCompletely;
251
252        #endregion
253
254        #region IControl Members
255
256        public event IControlStatusChangedEventHandler OnStatusChanged;
257
258        #endregion
259    }
260}
Note: See TracBrowser for help on using the repository browser.