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

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

P2P Integration (bisher teilweise noch buggy)
P2PBase, P2PLoad und P2PStore funktionieren bereits
P2P_DHTstore funktioniert noch nicht --> IControl-Versuch

File size: 6.7 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
27namespace Cryptool.Plugins.PeerToPeer
28{
29    [Author("Christian Arnold", "arnold@cryptool.org", "Uni Duisburg-Essen", "http://www.uni-due.de")]
30    [PluginInfo(false, "P2P_Peer", "Creates a new Peer", "", "PeerToPeerBase/ct2_p2p_load_medium.png")]
31    public class P2PPeer : IIOMisc
32    {
33        #region Variables
34
35        /// <summary>
36        /// dirty workaround!!!
37        /// </summary>
38        private static bool bolFirstInitalisation = true;
39        private P2PPeerSettings settings;
40        private P2PBase p2pBase;
41
42        #endregion
43
44        public P2PPeer()
45        {
46            this.p2pBase = new P2PBase();
47            this.settings = new P2PPeerSettings(p2pBase);
48        }
49
50        #region IPlugin Members
51
52        public event StatusChangedEventHandler OnPluginStatusChanged;
53
54        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
55
56        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
57
58        public ISettings Settings
59        {
60            set { this.settings = (P2PPeerSettings)value; }
61            get { return this.settings; }
62        }
63
64        public System.Windows.Controls.UserControl Presentation
65        {
66            get { return null; }
67        }
68
69        public System.Windows.Controls.UserControl QuickWatchPresentation
70        {
71            get { return null; }
72        }
73
74        public void PreExecution()
75        {
76            // nicht in PreExecute möglich, da schon beim Laden von CT2 gefeuert. Input-Data werden erst NACH PreExecute gesetzt!
77            if (bolFirstInitalisation)
78            {
79                bolFirstInitalisation = false;
80            }
81            else
82            {
83                this.p2pBase.Initialize(settings.P2PPeerName, settings.P2PWorldName);
84                this.p2pBase.SynchStart();
85            }
86        }
87
88        public void Execute()
89        {
90
91        }
92
93        public void process(IP2PControl sender)
94        {
95            GuiLogMessage("P2P Peer method 'process' is executed!", NotificationLevel.Debug);
96        }
97
98        public void PostExecution()
99        {
100        }
101
102        public void Pause()
103        {
104        }
105
106        public void Stop()
107        {
108        }
109
110        public void Initialize()
111        {
112        }
113
114        public void Dispose()
115        {
116            p2pBase.SynchStop();
117        }
118
119        #endregion
120
121        #region INotifyPropertyChanged Members
122
123        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
124
125        public void OnPropertyChanged(string name)
126        {
127            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
128        }
129
130        public event PluginProgressChangedEventHandler OnPluginProcessChanged;
131
132        private void ProgressChanged(double value, double max)
133        {
134            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
135        }
136
137        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
138        {
139            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
140        }
141
142        #endregion
143
144        #region In and Output
145
146        private IP2PControl p2pMaster;
147        [PropertyInfo(Direction.ControlMaster, "Master Peer", "One peer to rule them all", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
148        public IP2PControl P2PControlMaster
149        {
150            get
151            {
152                if (this.p2pMaster == null)
153                    // to commit the settings of the plugin to the IControl
154                    this.p2pMaster = new P2PPeerMaster(p2pBase);
155                return this.p2pMaster;
156            }
157            set
158            {
159                if (this.p2pMaster != null)
160                    this.p2pMaster.OnStatusChanged -= p2pMaster_OnStatusChanged;
161                //{
162                //    //Only when using asynchronous p2p-Start-method, remove event handler for OnPeerJoinedCompletely
163                //    this.p2pMaster.onPeerJoinedCompletely -= OnPeerJoinedCompletely;
164                //}
165                if (this.p2pMaster != value)
166                {
167                    this.p2pMaster.OnStatusChanged +=new IControlStatusChangedEventHandler(p2pMaster_OnStatusChanged);
168                    //Only when using asynchronous p2p-Start-method, add event handler for OnPeerJoinedCompletely
169                    //this.p2pMaster.OnPeerJoinedCompletely += new PeerJoinedP2P(OnPeerJoinedCompletely);
170                    this.p2pMaster = value;
171                    OnPropertyChanged("P2PControlMaster");
172                }
173            }
174        }
175
176        void p2pMaster_OnStatusChanged(IControl sender, bool readyForExecution)
177        {
178            if (readyForExecution)
179                this.process((IP2PControl)sender);
180        }
181
182        #endregion
183    }
184
185    public class P2PPeerMaster : IP2PControl
186    {
187        private P2PBase p2pBase;
188
189        public P2PPeerMaster(P2PBase p2pBase)
190        {
191            this.p2pBase = p2pBase;
192        }
193
194        #region IP2PControl Members
195
196        public bool DHTstore(string sKey, string sValue)
197        {
198            return this.p2pBase.SynchStore(sKey, sValue);
199        }
200
201        public byte[] DHTload(string sKey)
202        {
203            return this.p2pBase.SynchRetrieve(sKey);
204        }
205
206        public bool DHTremove(string sKey)
207        {
208            return this.p2pBase.SynchRemove(sKey);
209        }
210
211        public event PeerJoinedP2P OnPeerJoinedCompletely;
212
213        #endregion
214
215        #region IControl Members
216
217        public event IControlStatusChangedEventHandler OnStatusChanged;
218
219        #endregion
220    }
221}
Note: See TracBrowser for help on using the repository browser.