Ignore:
Timestamp:
May 21, 2010, 4:00:55 PM (12 years ago)
Author:
Paul Lelgemann
Message:
  • Removed unused references and using statement in CrypP2P, PeerToPeer, PeerToPeerBaseProxy

+ Skeleton for P2PEditor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/PeerToPeerBaseProxy/P2PPeer.cs

    r1443 r1458  
    1616
    1717using System;
    18 using System.Collections.Generic;
    19 using System.Linq;
     18using System.ComponentModel;
    2019using System.Text;
    2120using System.Threading;
    22 using Cryptool.PluginBase.Control;
    23 using Cryptool.PluginBase;
    24 using Cryptool.PluginBase.Miscellaneous;
    25 using System.ComponentModel;
    26 using Cryptool.PluginBase.IO;
    27 using Cryptool.Plugins.PeerToPeer.Internal;
     21using System.Windows.Controls;
    2822using Cryptool.P2P;
    2923using Cryptool.P2P.Internal;
     24using Cryptool.PluginBase;
     25using Cryptool.PluginBase.Control;
     26using Cryptool.PluginBase.IO;
     27using Cryptool.PluginBase.Miscellaneous;
     28using Cryptool.Plugins.PeerToPeer.Internal;
    3029
    3130namespace Cryptool.Plugins.PeerToPeerProxy
    3231{
    3332    [Author("Paul Lelgemann", "lelgemann@cryptool.org", "Uni Duisburg-Essen", "http://www.uni-due.de")]
    34     [PluginInfo(false, "P2P_Peer_Proxy", "Creates a new Peer. Uses the CrypTool2 built-in P2P network and can be used as a replacement for P2P_Peer.", "", "PeerToPeerBaseProxy/icons/peer_inactive.png", "PeerToPeerBaseProxy/icons/peer_connecting.png", "PeerToPeerBaseProxy/icons/peer_online.png", "PeerToPeerBaseProxy/icons/peer_error.png")]
     33    [PluginInfo(false, "P2P_Peer_Proxy",
     34        "Creates a new Peer. Uses the CrypTool2 built-in P2P network and can be used as a replacement for P2P_Peer.", ""
     35        , "PeerToPeerBaseProxy/icons/peer_inactive.png", "PeerToPeerBaseProxy/icons/peer_connecting.png",
     36        "PeerToPeerBaseProxy/icons/peer_online.png", "PeerToPeerBaseProxy/icons/peer_error.png")]
    3537    public class P2PPeer : IIOMisc
    3638    {
     39        #region IIOMisc Members
     40
     41        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
     42
     43        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
     44
     45        public event PropertyChangedEventHandler PropertyChanged;
     46
     47        #endregion
     48
    3749        // to forward event from overlay/dht MessageReceived-Event from P2PBase
    3850        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
    3951
     52        public void OnPropertyChanged(string name)
     53        {
     54            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
     55        }
     56
     57        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
     58        {
     59            // for evaluation issues only
     60            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this,
     61                                       new GuiLogEventArgs(p + "(" + DebugToFile.GetTimeStamp() + ")", this,
     62                                                           notificationLevel));
     63            //EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
     64        }
     65
     66        public void LogInternalState()
     67        {
     68            P2PManager.Instance.P2PBase.LogInternalState();
     69        }
     70
     71        #region In and Output
     72
     73        [PropertyInfo(Direction.ControlSlave, "Master Peer", "One peer to rule them all", "", true, false,
     74            DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
     75        public IP2PControl P2PControlSlave
     76        {
     77            get { return _p2PSlave ?? (_p2PSlave = new P2PPeerMaster(this)); }
     78        }
     79
     80        #endregion
     81
     82        #region Start and Stop Peer
     83
     84        /// <summary>
     85        /// Status flag for starting and stopping peer only once.
     86        /// </summary>
     87        public bool PeerStarted()
     88        {
     89            return P2PManager.Instance.P2PConnected();
     90        }
     91
     92        public void StartPeer()
     93        {
     94            _settings.PeerStatusChanged(P2PPeerSettings.PeerStatus.Connecting);
     95            P2PManager.Instance.P2PBase.OnP2PMessageReceived += P2PBaseOnP2PMessageReceived;
     96
     97            if (P2PManager.Instance.P2PConnected())
     98            {
     99                GuiLogMessage("P2P connected.", NotificationLevel.Info);
     100                _settings.PeerStatusChanged(P2PPeerSettings.PeerStatus.Online);
     101            }
     102            else
     103            {
     104                GuiLogMessage("P2P network must be configured and connecting using the world button.",
     105                              NotificationLevel.Error);
     106                _settings.PeerStatusChanged(P2PPeerSettings.PeerStatus.Error);
     107            }
     108        }
     109
     110        public void StopPeer()
     111        {
     112            GuiLogMessage("Peer cannot be stopped, it is running in CrypTool!", NotificationLevel.Info);
     113        }
     114
     115        #endregion Start and Stop Peer
     116
    40117        #region Variables
    41118
    42         private P2PPeerSettings settings;
    43         private IP2PControl p2pSlave;
     119        private IP2PControl _p2PSlave;
     120        private P2PPeerSettings _settings;
    44121
    45122        #endregion
     
    49126        public P2PPeer()
    50127        {
    51             this.settings = new P2PPeerSettings(this);
    52             this.settings.TaskPaneAttributeChanged += new TaskPaneAttributeChangedHandler(settings_TaskPaneAttributeChanged);
    53             this.settings.OnPluginStatusChanged += new StatusChangedEventHandler(settings_OnPluginStatusChanged);
     128            _settings = new P2PPeerSettings(this);
     129            _settings.TaskPaneAttributeChanged += SettingsTaskPaneAttributeChanged;
     130            _settings.OnPluginStatusChanged += SettingsOnPluginStatusChanged;
    54131        }
    55132
    56133        public event StatusChangedEventHandler OnPluginStatusChanged;
    57         private void settings_OnPluginStatusChanged(IPlugin sender, StatusEventArgs args)
    58         {
    59             if (OnPluginStatusChanged != null)
    60                 OnPluginStatusChanged(this, args);
    61         }
    62 
    63         // to forward event from overlay/dht MessageReceived-Event from P2PBase
    64         private void p2pBase_OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
    65         {
    66             if (OnPeerMessageReceived != null)
    67                 OnPeerMessageReceived(sourceAddr, data);
    68         }
    69 
    70         void settings_TaskPaneAttributeChanged(ISettings settings, TaskPaneAttributeChangedEventArgs args)
    71         {
    72             //throw new NotImplementedException();
    73         }
    74134
    75135        public ISettings Settings
    76136        {
    77             set { this.settings = (P2PPeerSettings)value; }
    78             get { return this.settings; }
    79         }
    80 
    81         public System.Windows.Controls.UserControl Presentation
     137            set { _settings = (P2PPeerSettings) value; }
     138            get { return _settings; }
     139        }
     140
     141        public UserControl Presentation
    82142        {
    83143            get { return null; }
    84144        }
    85145
    86         public System.Windows.Controls.UserControl QuickWatchPresentation
     146        public UserControl QuickWatchPresentation
    87147        {
    88148            get { return null; }
     
    122182        }
    123183
    124         #endregion
    125 
    126         #region IPlugin Members
    127 
    128         public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
    129 
    130         public event PluginProgressChangedEventHandler OnPluginProgressChanged;
    131 
    132         #endregion
    133 
    134         #region INotifyPropertyChanged Members
    135 
    136         public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    137 
    138         public void OnPropertyChanged(string name)
    139         {
    140             EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
    141         }
    142 
    143         public event PluginProgressChangedEventHandler OnPluginProcessChanged;
    144 
    145         private void ProgressChanged(double value, double max)
    146         {
    147             EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
    148         }
    149 
    150         private void GuiLogMessage(string p, NotificationLevel notificationLevel)
    151         {
    152             // for evaluation issues only
    153             EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p + "(" + DebugToFile.GetTimeStamp() + ")", this, notificationLevel));
    154             //EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
    155         }
    156 
    157         #endregion
    158 
    159         #region In and Output
    160 
    161         [PropertyInfo(Direction.ControlSlave, "Master Peer", "One peer to rule them all", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    162         public IP2PControl P2PControlSlave
    163         {
    164             get
    165             {
    166                 if (this.p2pSlave == null)
    167                     // to commit the settings of the plugin to the IControl
    168                     this.p2pSlave = new P2PPeerMaster(this);
    169                 return this.p2pSlave;
    170             }
    171         }
    172 
    173         #endregion
    174 
    175         #region Start and Stop Peer
    176         /// <summary>
    177         /// Status flag for starting and stopping peer only once.
    178         /// </summary>
    179         public bool PeerStarted()
    180         {
    181             return P2PManager.Instance.P2PConnected();
    182         }
    183 
    184         public void StartPeer()
    185         {
    186             this.settings.PeerStatusChanged(P2PPeerSettings.PeerStatus.Connecting);
    187             P2PManager.Instance.P2PBase.OnP2PMessageReceived += new P2PBase.P2PMessageReceived(p2pBase_OnP2PMessageReceived);
    188 
    189             if (P2PManager.Instance.P2PConnected())
    190             {
    191                 GuiLogMessage("P2P connected.", NotificationLevel.Info);
    192                 this.settings.PeerStatusChanged(P2PPeerSettings.PeerStatus.Online);
    193             }
    194             else
    195             {
    196                 GuiLogMessage("P2P network must be configured and connecting using the world button.", NotificationLevel.Error);
    197                 this.settings.PeerStatusChanged(P2PPeerSettings.PeerStatus.Error);
    198             }
    199         }
    200 
    201         public void StopPeer()
    202         {
    203 
    204             GuiLogMessage("Peer cannot be stopped, it is running in CrypTool!", NotificationLevel.Info);
    205         }
    206         #endregion Start and Stop Peer
    207 
    208         public void LogInternalState()
    209         {
    210             P2PManager.Instance.P2PBase.LogInternalState();
    211         }
     184        private void SettingsOnPluginStatusChanged(IPlugin sender, StatusEventArgs args)
     185        {
     186            if (OnPluginStatusChanged != null)
     187                OnPluginStatusChanged(this, args);
     188        }
     189
     190        // to forward event from overlay/dht MessageReceived-Event from P2PBase
     191        private void P2PBaseOnP2PMessageReceived(PeerId sourceAddr, byte[] data)
     192        {
     193            if (OnPeerMessageReceived != null)
     194                OnPeerMessageReceived(sourceAddr, data);
     195        }
     196
     197        private static void SettingsTaskPaneAttributeChanged(ISettings settings, TaskPaneAttributeChangedEventArgs args)
     198        {
     199            //throw new NotImplementedException();
     200        }
     201
     202        #endregion
    212203    }
    213204
     
    215206    public class P2PPeerMaster : IP2PControl
    216207    {
    217         private AutoResetEvent systemJoined;
    218         private P2PPeer p2pPeer;
    219         private PeerId peerID;
    220         private string sPeerName;
     208        private readonly Encoding _enc = Encoding.UTF8;
     209        private readonly P2PPeer _p2PPeer;
     210        private readonly AutoResetEvent _systemJoined;
     211        private PeerId _peerId;
     212        private string _sPeerName;
    221213        // used for every encoding stuff
    222         private Encoding enc = UTF8Encoding.UTF8;
    223 
    224         public P2PPeerMaster(P2PPeer p2pPeer)
    225         {
    226             this.p2pPeer = p2pPeer;
    227             this.systemJoined = new AutoResetEvent(false);
    228 
    229             P2PManager.Instance.P2PBase.OnSystemJoined += new P2PBase.SystemJoined(p2pBase_OnSystemJoined);
    230             P2PManager.Instance.OnPeerMessageReceived += new P2PBase.P2PMessageReceived(p2pPeer_OnPeerMessageReceived);
    231             this.OnStatusChanged += new IControlStatusChangedEventHandler(P2PPeerMaster_OnStatusChanged);
     214
     215        public P2PPeerMaster(P2PPeer p2PPeer)
     216        {
     217            _p2PPeer = p2PPeer;
     218            _systemJoined = new AutoResetEvent(false);
     219
     220            P2PManager.Instance.P2PBase.OnSystemJoined += P2PBaseOnSystemJoined;
     221            P2PManager.Instance.OnPeerMessageReceived += P2PPeerOnPeerMessageReceived;
     222            OnStatusChanged += P2PPeerMaster_OnStatusChanged;
    232223        }
    233224
    234225        #region Events and Event-Handling
    235        
    236         private void p2pBase_OnSystemJoined()
    237         {
    238             systemJoined.Set();
    239         }
    240226
    241227        // to forward event from overlay MessageReceived-Event from P2PBase
     
    243229        public event P2PPayloadMessageReceived OnPayloadMessageReceived;
    244230        public event P2PSystemMessageReceived OnSystemMessageReceived;
    245         private void p2pPeer_OnPeerMessageReceived(PeerId sourceAddr, byte[] data)
     231
     232        public event IControlStatusChangedEventHandler OnStatusChanged;
     233
     234        private void P2PBaseOnSystemJoined()
     235        {
     236            _systemJoined.Set();
     237        }
     238
     239        private void P2PPeerOnPeerMessageReceived(PeerId sourceAddr, byte[] data)
    246240        {
    247241            switch (GetMessageType(data[0])) //analyses the first byte of data (index, which represents the MessageType)
     
    250244                    if (data.Length == 2)
    251245                    {
    252                         if(OnSystemMessageReceived != null)
     246                        if (OnSystemMessageReceived != null)
    253247                            OnSystemMessageReceived(sourceAddr, GetPubSubType(data[1]));
    254248                    }
    255249                    else
    256250                    {
    257                         throw (new Exception("Data seems to be from type 'PubSub', but is to long for it... Data: '" + enc.GetString(data) + "'"));
     251                        throw (new Exception("Data seems to be from type 'PubSub', but is to long for it... Data: '" +
     252                                             _enc.GetString(data) + "'"));
    258253                    }
    259254                    break;
    260255                case P2PMessageIndex.Payload:
    261                     if(OnPayloadMessageReceived != null)
     256                    if (OnPayloadMessageReceived != null)
    262257                        OnPayloadMessageReceived(sourceAddr, GetMessagePayload(data));
    263258                    break;
     
    268263        }
    269264
    270         public event IControlStatusChangedEventHandler OnStatusChanged;
    271265        private void P2PPeerMaster_OnStatusChanged(IControl sender, bool readyForExecution)
    272266        {
     
    277271        #endregion
    278272
     273        #region IP2PControl Members
     274
    279275        public bool PeerStarted()
    280276        {
    281             return this.p2pPeer.PeerStarted();
    282         }
    283 
    284         /// <summary>
    285         /// workaround method. If the PAP functions are used, but the PAP system isn't
    286         /// started yet. This could happen because of the plugin hierarchy and
    287         /// when a p2p-using plugin uses PAP functions in the PreExecution method,
    288         /// this could run into a race condition (peer plugin not computed by the CT-system
    289         /// yet, but p2p-using plugin is already executed)
    290         /// </summary>
    291         /// <returns></returns>
    292         private bool SystemJoinedCompletely()
    293         {
    294             return P2PManager.Instance.P2PConnected();
    295         }
    296 
    297         #region IP2PControl Members
     277            return _p2PPeer.PeerStarted();
     278        }
    298279
    299280        public bool DHTstore(string sKey, byte[] byteValue)
     
    334315            if (SystemJoinedCompletely())
    335316            {
    336                 if (this.peerID == null)
     317                if (_peerId == null)
    337318                {
    338                     this.peerID = P2PManager.Instance.P2PBase.GetPeerID(out this.sPeerName);
     319                    _peerId = P2PManager.Instance.P2PBase.GetPeerID(out _sPeerName);
    339320                }
    340                 sPeerName = this.sPeerName;
    341                 return this.peerID;
     321                sPeerName = _sPeerName;
     322                return _peerId;
    342323            }
    343             sPeerName = this.sPeerName;
     324            sPeerName = _sPeerName;
    344325            return null;
    345326        }
     
    348329        {
    349330            return P2PManager.Instance.P2PBase.GetPeerID(byteId);
    350         }
    351 
    352         private void SendReadilyMessage(byte[] data, PeerId destinationAddress)
    353         {
    354             if (SystemJoinedCompletely())
    355                 P2PManager.Instance.P2PBase.SendToPeer(data, destinationAddress.ToByteArray());
    356331        }
    357332
     
    368343            SendReadilyMessage(data, destinationAddress);
    369344        }
     345
    370346        public void SendToPeer(PubSubMessageType msgType, PeerId destinationAddress)
    371347        {
     
    373349            SendReadilyMessage(data, destinationAddress);
    374350        }
     351
     352        #endregion
    375353
    376354        #region Communication protocol
     
    382360        /// <param name="msgIndex">type of message (system message, simple payload for a special use case, etc.)</param>
    383361        /// <returns>the message, which is processable by the ct2/p2p system</returns>
    384         private byte[] GenerateMessage(byte[] payload, P2PMessageIndex msgIndex)
     362        private static byte[] GenerateMessage(byte[] payload, P2PMessageIndex msgIndex)
    385363        {
    386364            // first byte is the index, if it is payload or Publish/Subscriber stuff
    387             byte[] retByte = new byte[1 + payload.Length];
    388             retByte[0] = (byte)msgIndex;
     365            var retByte = new byte[1 + payload.Length];
     366            retByte[0] = (byte) msgIndex;
    389367            payload.CopyTo(retByte, 1);
    390368            return retByte;
     
    399377        private byte[] GenerateMessage(string sPayload, P2PMessageIndex msgIndex)
    400378        {
    401             return GenerateMessage(enc.GetBytes(sPayload), msgIndex);
     379            return GenerateMessage(_enc.GetBytes(sPayload), msgIndex);
    402380        }
    403381
     
    406384        /// </summary>
    407385        /// <param name="pubSubData">PubSubMessageType</param>
    408         /// <returns>the message, which is processable by the ct2/p2p system<</returns>
    409         private byte[] GenerateMessage(PubSubMessageType pubSubData)
    410         {
    411             byte[] bytePubSubData = new byte[] { (byte)pubSubData };
     386        /// <returns>the message, which is processable by the ct2/p2p system</returns>
     387        private static byte[] GenerateMessage(PubSubMessageType pubSubData)
     388        {
     389            var bytePubSubData = new[] {(byte) pubSubData};
    412390            return GenerateMessage(bytePubSubData, P2PMessageIndex.PubSub);
    413391        }
     
    418396        /// <param name="msgType">the FIRST byte of a raw message, received by the system</param>
    419397        /// <returns>the message type</returns>
    420         private P2PMessageIndex GetMessageType(byte msgType)
    421         {
    422             try
    423             {
    424                 return (P2PMessageIndex)msgType;
    425             }
    426             catch (Exception ex)
    427             {
    428                 throw (ex);
    429             }
    430         }
    431 
    432         /// <summary>
    433         /// returns the message type, e.g. PubSub or Payload message (to accelarate this process, only assign first byte of the whole array message)
    434         /// </summary>
    435         /// <param name="message">the whole message as an byte array</param>
    436         /// <returns>the message type</returns>
    437         private P2PMessageIndex GetMessageType(byte[] message)
    438         {
    439             try
    440             {
    441                 return (P2PMessageIndex)message[0];
    442             }
    443             catch (Exception ex)
    444             {
    445                 throw (ex);
    446             }
     398        private static P2PMessageIndex GetMessageType(byte msgType)
     399        {
     400            return (P2PMessageIndex) msgType;
    447401        }
    448402
     
    452406        /// <param name="message">the raw message, received by the system, as an byte array (with the first index byte!!!)</param>
    453407        /// <returns>only the payload part of the message</returns>
    454         private byte[] GetMessagePayload(byte[] message)
     408        private static byte[] GetMessagePayload(byte[] message)
    455409        {
    456410            if (message.Length > 1)
    457411            {
    458                 byte[] retMsg = new byte[message.Length - 1];
     412                var retMsg = new byte[message.Length - 1];
    459413                // workaround because CopyTo doesn't work...
    460414                //for (int i = 0; i < message.Length-1; i++)
     
    470424        #endregion
    471425
     426        /// <summary>
     427        /// workaround method. If the PAP functions are used, but the PAP system isn't
     428        /// started yet. This could happen because of the plugin hierarchy and
     429        /// when a p2p-using plugin uses PAP functions in the PreExecution method,
     430        /// this could run into a race condition (peer plugin not computed by the CT-system
     431        /// yet, but p2p-using plugin is already executed)
     432        /// </summary>
     433        /// <returns></returns>
     434        private static bool SystemJoinedCompletely()
     435        {
     436            return P2PManager.Instance.P2PConnected();
     437        }
     438
     439        private static void SendReadilyMessage(byte[] data, PeerId destinationAddress)
     440        {
     441            if (SystemJoinedCompletely())
     442                P2PManager.Instance.P2PBase.SendToPeer(data, destinationAddress.ToByteArray());
     443        }
    472444
    473445        /// <summary>
    474446        /// Converts a string to the PubSubMessageType if possible. Otherwise return null.
    475447        /// </summary>
    476         /// <param name="sData">Data</param>
     448        /// <param name="data">Data</param>
    477449        /// <returns>PubSubMessageType if possible. Otherwise null.</returns>
    478         private PubSubMessageType GetPubSubType(byte data)
     450        private static PubSubMessageType GetPubSubType(byte data)
    479451        {
    480452            // Convert one byte data to PublishSubscribeMessageType-Enum
    481             try
    482             {
    483                 return (PubSubMessageType)data;
    484             }
    485             catch (Exception ex)
    486             {
    487                 throw(ex);
    488             }
    489         }
    490 
    491         #endregion
     453            return (PubSubMessageType) data;
     454        }
    492455    }
    493456}
Note: See TracChangeset for help on using the changeset viewer.