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

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

P2P Publish/Subscriber (buggy, aber für Präsentationszwecke geeignet)

File size: 13.3 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/icons/peer_inaktiv.png", "PeerToPeerBase/icons/peer_connecting.png", "PeerToPeerBase/icons/peer_online.png", "PeerToPeerBase/icons/peer_error.png")]
31    public class P2PPeer : IIOMisc
32    {
33        // to forward event from overlay/dht MessageReceived-Event from P2PBase
34        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
35
36        #region Variables
37
38        public P2PBase p2pBase;
39        /// <summary>
40        /// dirty workaround!!!
41        /// </summary>
42        private static bool bolFirstInitalisation = true;
43        private P2PPeerSettings settings;
44        private IP2PControl p2pSlave;
45
46        #endregion
47
48        public P2PPeer()
49        {
50            this.p2pBase = new P2PBase();
51            // to forward event from overlay/dht MessageReceived-Event from P2PBase
52            this.p2pBase.OnP2PMessageReceived += new P2PBase.P2PMessageReceived(p2pBase_OnP2PMessageReceived);
53            this.settings = new P2PPeerSettings(p2pBase);
54            this.settings.TaskPaneAttributeChanged += new TaskPaneAttributeChangedHandler(settings_TaskPaneAttributeChanged);
55            this.settings.OnPluginStatusChanged += new StatusChangedEventHandler(settings_OnPluginStatusChanged);
56        }
57
58        public event StatusChangedEventHandler OnPluginStatusChanged;
59        private void settings_OnPluginStatusChanged(IPlugin sender, StatusEventArgs args)
60        {
61            if (OnPluginStatusChanged != null) OnPluginStatusChanged(this, args);
62        }
63
64        // to forward event from overlay/dht MessageReceived-Event from P2PBase
65        private void p2pBase_OnP2PMessageReceived(byte[] byteSourceAddr, string sData)
66        {
67            if (OnPeerMessageReceived != null)
68                OnPeerMessageReceived(byteSourceAddr, sData);
69        }
70
71        void settings_TaskPaneAttributeChanged(ISettings settings, TaskPaneAttributeChangedEventArgs args)
72        {
73            //throw new NotImplementedException();
74        }
75
76        #region IPlugin Members
77
78        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
79
80        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
81
82        public ISettings Settings
83        {
84            set { this.settings = (P2PPeerSettings)value; }
85            get { return this.settings; }
86        }
87
88        public System.Windows.Controls.UserControl Presentation
89        {
90            get { return null; }
91        }
92
93        public System.Windows.Controls.UserControl QuickWatchPresentation
94        {
95            get { return null; }
96        }
97
98        public void PreExecution()
99        {
100            // nicht in PreExecute möglich, da schon beim Laden von CT2 gefeuert. Input-Data werden erst NACH PreExecute gesetzt!
101            if (bolFirstInitalisation)
102            {
103                bolFirstInitalisation = false;
104            }
105            else
106            {
107                if (!settings.PeerStarted)
108                {
109                    // starts peer in the settings class and enables/disables Controlbuttons
110                    this.settings.PeerStarted = true;
111                    GuiLogMessage("Successfully joined the P2P System", NotificationLevel.Info);
112                }
113            }
114        }
115
116        public void Execute()
117        {
118            // TODO: For future use copy functionality to Execute instead of PreExecute
119            //       so we don't need the workaround anymore!!!
120            //if (!settings.PeerStarted)
121            //{
122            //    // starts peer in the settings class and enables/disables Controlbuttons
123            //    this.settings.PeerStarted = true;
124            //}
125        }
126
127        public void process(IP2PControl sender)
128        {
129            GuiLogMessage("P2P Peer method 'process' is executed, because status of P2PSlave has changed!", NotificationLevel.Debug);
130        }
131
132        public void PostExecution()
133        {
134        }
135
136        public void Pause()
137        {
138        }
139
140        public void Stop()
141        {
142        }
143
144        public void Initialize()
145        {
146        }
147
148        public void Dispose()
149        {
150            //settings are already set to null in this Step...
151            //unsolved design problem in CT2...
152            //this.settings.PeerStopped = true;
153            if (this.p2pBase != null)
154            {
155                this.p2pBase.SynchStop();
156                this.p2pBase = null;
157            }
158        }
159
160        #endregion
161
162        #region INotifyPropertyChanged Members
163
164        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
165
166        public void OnPropertyChanged(string name)
167        {
168            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
169        }
170
171        public event PluginProgressChangedEventHandler OnPluginProcessChanged;
172
173        private void ProgressChanged(double value, double max)
174        {
175            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
176        }
177
178        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
179        {
180            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
181        }
182
183        #endregion
184
185        #region In and Output
186
187        [PropertyInfo(Direction.ControlSlave, "Master Peer", "One peer to rule them all", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
188        public IP2PControl P2PControlSlave
189        {
190            get
191            {
192                if (this.p2pSlave == null)
193                    // to commit the settings of the plugin to the IControl
194                    this.p2pSlave = new P2PPeerMaster(this);
195                return this.p2pSlave;
196            }
197            set
198            {
199                if (this.p2pSlave != null)
200                {
201                    this.p2pSlave.OnStatusChanged -= p2pSlave_OnStatusChanged;
202                }
203
204                this.p2pSlave.OnStatusChanged += new IControlStatusChangedEventHandler(p2pSlave_OnStatusChanged);
205                //Only when using asynchronous p2p-Start-method, add event handler for OnPeerJoinedCompletely
206                //this.p2pSlave.OnPeerJoinedCompletely += new PeerJoinedP2P(OnPeerJoinedCompletely);
207
208                if (this.p2pSlave != value)
209                {
210                    this.p2pSlave = (P2PPeerMaster)value;
211                    OnPropertyChanged("P2PControlSlave");
212                }
213            }
214        }
215
216        void p2pSlave_OnStatusChanged(IControl sender, bool readyForExecution)
217        {
218            if (readyForExecution)
219                this.process((IP2PControl)sender);
220        }
221
222        #endregion
223    }
224
225    public class P2PPeerMaster : IP2PControl
226    {
227        private P2PPeer p2pPeer;
228        private byte[] bytePeerID;
229        private string sPeerID;
230        private string sPeerName;
231
232        public P2PPeerMaster(P2PPeer p2pPeer)
233        {
234            this.p2pPeer = p2pPeer;
235            // to forward event from overlay/dht MessageReceived-Event from P2PBase
236            this.p2pPeer.OnPeerMessageReceived += new P2PBase.P2PMessageReceived(p2pPeer_OnPeerMessageReceived);
237            this.OnStatusChanged += new IControlStatusChangedEventHandler(P2PPeerMaster_OnStatusChanged);
238        }
239
240        #region Events and Event-Handling
241
242        // to forward event from overlay/dht MessageReceived-Event from P2PBase
243        public event P2PBase.P2PMessageReceived OnPeerReceivedMsg;
244        private void p2pPeer_OnPeerMessageReceived(byte[] byteSourceAddr, string sData)
245        {
246            if (OnPeerReceivedMsg != null)
247                OnPeerReceivedMsg(byteSourceAddr, sData);
248        }
249
250        public event IControlStatusChangedEventHandler OnStatusChanged;
251        private void P2PPeerMaster_OnStatusChanged(IControl sender, bool readyForExecution)
252        {
253            if (OnStatusChanged != null)
254                OnStatusChanged(sender, readyForExecution);
255        }
256
257        #endregion
258
259        #region IP2PControl Members
260
261        public bool DHTstore(string sKey, byte[] byteValue)
262        {
263            return this.p2pPeer.p2pBase.SynchStore(sKey, byteValue);
264        }
265
266        public bool DHTstore(string sKey, string sValue)
267        {
268            return this.p2pPeer.p2pBase.SynchStore(sKey, sValue);
269        }
270
271        public byte[] DHTload(string sKey)
272        {
273            return this.p2pPeer.p2pBase.SynchRetrieve(sKey);
274        }
275
276        public bool DHTremove(string sKey)
277        {
278            // derzeit liegt wohl in peerq@play ein Fehler in der Methode...
279            // erkennt den Übergabeparameter nicht an und wirft dann "ArgumentNotNullException"...
280            // Problem an M.Helling und S.Holzapfel von p@p weitergegeben...
281            return this.p2pPeer.p2pBase.SynchRemove(sKey);
282            //return false;
283        }
284
285        /// <summary>
286        /// This method only contacts the p2p system, if the peerID wasn't requested before
287        /// </summary>
288        /// <param name="sPeerName">returns the Peer Name</param>
289        /// <returns>returns the Peer ID</returns>
290        public byte[] GetPeerID(out string sPeerName)
291        {
292            if (this.bytePeerID == null)
293            {
294                this.bytePeerID = this.p2pPeer.p2pBase.GetPeerID(out this.sPeerName);
295                this.sPeerID = ConvertPeerId(bytePeerID);
296            }
297            sPeerName = this.sPeerName;
298            return this.bytePeerID;
299        }
300
301        public string ConvertPeerId(byte[] bytePeerId)
302        {
303            string sRet = String.Empty;
304            for (int i = 0; i < bytePeerId.Length; i++)
305            {
306                sRet += bytePeerId[i].ToString() + ":";
307            }
308            return sRet.Substring(0, sRet.Length - 1);
309        }
310
311        public void SendToPeer(string sData, byte[] byteDestinationPeerAddress)
312        {
313            this.p2pPeer.p2pBase.SendToPeer(sData, byteDestinationPeerAddress);
314        }
315
316        #endregion
317    }
318
319    //public class P2PPeerMaster : IP2PControl
320    //{
321    //    private P2PBase p2pBase;
322
323    //    public P2PPeerMaster(P2PBase p2pBase)
324    //    {
325    //        this.p2pBase = p2pBase;
326    //        this.OnPeerReceivedMsg += new P2PBase.P2PMessageReceived(P2PPeerMaster_OnPeerReceivedMsg);
327    //        this.OnStatusChanged += new IControlStatusChangedEventHandler(P2PPeerMaster_OnStatusChanged);
328    //    }
329
330    //    #region Events and Event-Handling
331
332    //    public event P2PBase.P2PMessageReceived OnPeerReceivedMsg;
333    //    private void P2PPeerMaster_OnPeerReceivedMsg(byte[] byteSourceAddr, string sData)
334    //    {
335    //        if (OnPeerReceivedMsg != null)
336    //            OnPeerReceivedMsg(byteSourceAddr, sData);
337    //    }
338
339    //    public event IControlStatusChangedEventHandler OnStatusChanged;
340    //    private void P2PPeerMaster_OnStatusChanged(IControl sender, bool readyForExecution)
341    //    {
342    //        if (OnStatusChanged != null)
343    //            OnStatusChanged(sender, readyForExecution);
344    //    }
345
346    //    #endregion
347
348    //    #region IP2PControl Members
349
350    //    public bool DHTstore(string sKey, string sValue)
351    //    {
352    //        return this.p2pBase.SynchStore(sKey, sValue);
353    //    }
354
355    //    public byte[] DHTload(string sKey)
356    //    {
357    //        return this.p2pBase.SynchRetrieve(sKey);
358    //    }
359
360    //    public bool DHTremove(string sKey)
361    //    {
362    //        // derzeit liegt wohl in peerq@play ein Fehler in der Methode...
363    //        // erkennt den Übergabeparameter nicht an und wirft dann "ArgumentNotNullException"...
364    //        // Problem an M.Helling und S.Holzapfel von p@p weitergegeben...
365    //        return this.p2pBase.SynchRemove(sKey);
366    //        //return false;
367    //    }
368
369    //    public byte[] GetPeerName()
370    //    {
371    //        return this.p2pBase.GetPeerName();
372    //    }
373
374    //    public void SendToPeer(string sData, byte[] byteDestinationPeerAddress)
375    //    {
376    //        this.p2pBase.SendToPeer(sData, byteDestinationPeerAddress);
377    //    }
378
379    //    #endregion
380    //}
381}
Note: See TracBrowser for help on using the repository browser.