Changeset 836


Ignore:
Timestamp:
Nov 10, 2009, 1:02:33 PM (12 years ago)
Author:
arnold
Message:

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

Location:
trunk
Files:
25 added
3 deleted
46 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/PeerToPeerBase/IP2PControl.cs

    r813 r836  
    33using System.Linq;
    44using System.Text;
     5using Cryptool.Plugins.PeerToPeer;
    56
    67namespace Cryptool.PluginBase.Control
     
    910    public enum P2PLinkManagerType
    1011    {
    11         Snal
     12        Snal = 0
    1213    }
    1314
    1415    public enum P2PBootstrapperType
    1516    {
    16         LocalMachineBootstrapper,
    17         IrcBootstrapper
     17        LocalMachineBootstrapper = 0,
     18        IrcBootstrapper = 1
    1819    }
    1920
    2021    public enum P2POverlayType
    2122    {
    22         FullMeshOverlay
     23        FullMeshOverlay = 0
    2324    }
    2425
    2526    public enum P2PDHTType
    2627    {
    27         FullMeshDHT
     28        FullMeshDHT = 0
     29    }
     30
     31    /// <summary>
     32    /// Message types for Publish/Subscriber systems
     33    /// </summary>
     34    public enum PubSubMessageType
     35    {
     36        Register = 0,
     37        Alive = 1,
     38        Ping = 2,
     39        Pong = 3
    2840    }
    2941    #endregion
     
    3143    public interface IP2PControl : IControl
    3244    {
     45        bool DHTstore(string sKey, byte[] byteValue);
    3346        bool DHTstore(string sKey, string sValue);
    3447        byte[] DHTload(string sKey);
    3548        bool DHTremove(string sKey);
    3649
    37         string GetPeerName();
     50        byte[] GetPeerID(out string sPeerName);
    3851
    39         /// <summary>
    40         /// delegate is defined in Cryptool.PluginBase.Delegates.cs
    41         /// </summary>
    42         //event PeerJoinedP2P OnPeerJoinedCompletely;
     52        string ConvertPeerId(byte[] bytePeerId);
     53
     54        void SendToPeer(string sData, byte[] sDestinationPeerAddress);
     55
     56        event P2PBase.P2PMessageReceived OnPeerReceivedMsg;
    4357    }
    4458}
  • trunk/CrypPlugins/PeerToPeerBase/P2PPeer.cs

    r813 r836  
    2525using Cryptool.PluginBase.IO;
    2626
    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 
    3427namespace Cryptool.Plugins.PeerToPeer
    3528{
    3629    [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")]
     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")]
    3831    public class P2PPeer : IIOMisc
    3932    {
     33        // to forward event from overlay/dht MessageReceived-Event from P2PBase
     34        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
     35
    4036        #region Variables
    4137
     38        public P2PBase p2pBase;
    4239        /// <summary>
    4340        /// dirty workaround!!!
     
    4542        private static bool bolFirstInitalisation = true;
    4643        private P2PPeerSettings settings;
    47         private P2PBase p2pBase;
     44        private IP2PControl p2pSlave;
    4845
    4946        #endregion
     
    5249        {
    5350            this.p2pBase = new P2PBase();
     51            // to forward event from overlay/dht MessageReceived-Event from P2PBase
     52            this.p2pBase.OnP2PMessageReceived += new P2PBase.P2PMessageReceived(p2pBase_OnP2PMessageReceived);
    5453            this.settings = new P2PPeerSettings(p2pBase);
    5554            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);
    5669        }
    5770
     
    6275
    6376        #region IPlugin Members
    64 
    65         public event StatusChangedEventHandler OnPluginStatusChanged;
    6677
    6778        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
     
    94105            else
    95106            {
    96                 // TODO: When started on PreExecution hide Start-Button of Settings and set Stop-Button to visible!
    97107                if (!settings.PeerStarted)
    98108                {
    99                     // starts peer in the settings class
     109                    // starts peer in the settings class and enables/disables Controlbuttons
    100110                    this.settings.PeerStarted = true;
    101                     //this.p2pBase.InitializeAll(settings.P2PPeerName, settings.P2PWorldName, settings.P2PLinkMngrType, settings.P2PBSType, settings.P2POverlType, settings.P2PDhtType);
     111                    GuiLogMessage("Successfully joined the P2P System", NotificationLevel.Info);
    102112                }
    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();
    107113            }
    108114        }
     
    110116        public void Execute()
    111117        {
    112 
     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            //}
    113125        }
    114126
    115127        public void process(IP2PControl sender)
    116128        {
    117             GuiLogMessage("P2P Peer method 'process' is executed!", NotificationLevel.Debug);
     129            GuiLogMessage("P2P Peer method 'process' is executed, because status of P2PSlave has changed!", NotificationLevel.Debug);
    118130        }
    119131
     
    139151            //unsolved design problem in CT2...
    140152            //this.settings.PeerStopped = true;
    141             if (p2pBase != null)
    142             {
    143                 p2pBase.SynchStop();
    144                 p2pBase = null;
     153            if (this.p2pBase != null)
     154            {
     155                this.p2pBase.SynchStop();
     156                this.p2pBase = null;
    145157            }
    146158        }
     
    173185        #region In and Output
    174186
    175         private IP2PControl p2pSlave;
    176187        [PropertyInfo(Direction.ControlSlave, "Master Peer", "One peer to rule them all", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    177188        public IP2PControl P2PControlSlave
     
    181192                if (this.p2pSlave == null)
    182193                    // to commit the settings of the plugin to the IControl
    183                     this.p2pSlave = new P2PPeerMaster(p2pBase);
     194                    this.p2pSlave = new P2PPeerMaster(this);
    184195                return this.p2pSlave;
    185196            }
     
    187198            {
    188199                if (this.p2pSlave != null)
     200                {
    189201                    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                 //}
     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
    194208                if (this.p2pSlave != value)
    195209                {
    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;
     210                    this.p2pSlave = (P2PPeerMaster)value;
    200211                    OnPropertyChanged("P2PControlSlave");
    201212                }
     
    214225    public class P2PPeerMaster : IP2PControl
    215226    {
    216         private P2PBase p2pBase;
    217 
    218         public P2PPeerMaster(P2PBase p2pBase)
    219         {
    220             this.p2pBase = p2pBase;
    221         }
     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
    222258
    223259        #region IP2PControl Members
    224260
     261        public bool DHTstore(string sKey, byte[] byteValue)
     262        {
     263            return this.p2pPeer.p2pBase.SynchStore(sKey, byteValue);
     264        }
     265
    225266        public bool DHTstore(string sKey, string sValue)
    226267        {
    227             return this.p2pBase.SynchStore(sKey, sValue);
     268            return this.p2pPeer.p2pBase.SynchStore(sKey, sValue);
    228269        }
    229270
    230271        public byte[] DHTload(string sKey)
    231272        {
    232             return this.p2pBase.SynchRetrieve(sKey);
     273            return this.p2pPeer.p2pBase.SynchRetrieve(sKey);
    233274        }
    234275
     
    238279            // erkennt den Übergabeparameter nicht an und wirft dann "ArgumentNotNullException"...
    239280            // Problem an M.Helling und S.Holzapfel von p@p weitergegeben...
    240             return this.p2pBase.SynchRemove(sKey);
     281            return this.p2pPeer.p2pBase.SynchRemove(sKey);
    241282            //return false;
    242283        }
    243284
    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;
     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        }
    257315
    258316        #endregion
    259317    }
     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    //}
    260381}
  • trunk/CrypPlugins/PeerToPeerBase/P2PPeerSettings.cs

    r813 r836  
    88using System.Windows;
    99
    10 /*
    11  * TODO:
    12  * - Standardwerte in P2P-Settings setzen
    13  * - Start- und Stop-Button fürs Beenden und Starten des Peers auch außerhalb des Starts des PlugIns
    14  *
    15  * FRAGE:
    16  * - Wie kann ich statt StringArrays die Enum-Werte als Parameter wählen?
    17  */
    18 
    1910namespace Cryptool.Plugins.PeerToPeer
    2011{
     
    4536        #region taskPane
    4637
    47         // Parse String-Value to a valid Enum-Value
    48         private static T StringToEnum<T>(string sName)
    49         {
    50             return (T)Enum.Parse(typeof(T), sName);
    51         }
    52 
    5338        public P2PPeerSettings (P2PBase p2pBase)
    5439            {
     
    5641                TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("BtnStop", Visibility.Hidden)));
    5742            this.p2pBase = p2pBase;
     43            ChangePluginIcon(PeerStatus.NotConnected);
    5844            }
    5945
     
    6753            PeerStarted = !this.peerStarted;
    6854        }
    69 
    70         //private int iTest = 2;
    71         //[PropertySaveOrder(4)]
    72         //[ContextMenu("CaptionText","ContextToolTip",4,DisplayLevel.Beginner,ContextMenuControlType.ComboBox,new int[]{1,2},"Welt","Hallo")]
    73         //[TaskPane("Testfunktion","",null,4,false,DisplayLevel.Beginner,ControlType.ComboBox,new string[]{"Welt","Hallo"})]
    74         //public int Test
    75         //{
    76         //    get {return this.iTest;}
    77         //    set
    78         //    {
    79         //        if (value != this.iTest)
    80         //            this.iTest = value;
    81         //    }
    82         //}
    8355
    8456        private bool peerStarted = false;
     
    9567                    if (P2PPeerName != null && P2PWorldName != null)
    9668                    {
    97                         this.p2pBase.InitializeAll(P2PPeerName, P2PWorldName, P2PLinkMngrType, P2PBSType, P2POverlType, P2PDhtType);
     69                        ChangePluginIcon(PeerStatus.Connecting);
     70                        this.p2pBase.Initialize(P2PPeerName, P2PWorldName, (P2PLinkManagerType)P2PLinkMngrType,
     71                            (P2PBootstrapperType)P2PBSType, (P2POverlayType)P2POverlType, (P2PDHTType)P2PDhtType);
    9872                        this.p2pBase.SynchStart();
    9973                        TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("BtnStart", Visibility.Collapsed)));
    10074                        TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("BtnStop", Visibility.Visible)));
     75                        ChangePluginIcon(PeerStatus.Online);
    10176                    }
    10277                    else
    10378                    {
     79                        ChangePluginIcon(PeerStatus.Error);
    10480                        // can not initialize Peer, because P2PUserName and/or P2PWorldName are missing
    10581                        throw (new Exception("You must set P2PPeerName and/or P2PWorldName, otherwise starting the peer isn't possible"));
     
    10985                {
    11086                    this.peerStarted = value;
    111                     //don't use the PeerStopped-Property, because you will run into a recursive loop!!!
     87                    //use the private Var instead of the PeerStopped-Property, because you will run into a recursive loop!!!
    11288                    this.peerStopped = !value;
    11389                    OnPropertyChanged("PeerStarted");
     
    134110                if (this.peerStarted)
    135111                {
     112                    ChangePluginIcon(PeerStatus.Connecting);
    136113                    this.p2pBase.SynchStop();
    137114                    TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("BtnStart", Visibility.Visible)));
    138115                    TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("BtnStop", Visibility.Collapsed)));
     116                    ChangePluginIcon(PeerStatus.NotConnected);
    139117                }
    140118                if (value != this.peerStopped)
     
    193171        private P2PLinkManagerType p2pLinkManagerType = P2PLinkManagerType.Snal;
    194172        [TaskPane("LinkManager-Type", "Select the LinkManager-Type", "P2P Settings", 2, false, DisplayLevel.Beginner, ControlType.ComboBox, new string[] { "Snal" })]
    195         public P2PLinkManagerType P2PLinkMngrType
    196         {
    197             get { return this.p2pLinkManagerType; }
    198             set
    199             {
    200                 if (value != this.p2pLinkManagerType)
    201                 {
    202                     this.p2pLinkManagerType = StringToEnum<P2PLinkManagerType>(value.ToString());
     173        public int P2PLinkMngrType
     174        {
     175            get { return (int)this.p2pLinkManagerType; }
     176            set
     177            {
     178                if ((P2PLinkManagerType)value != this.p2pLinkManagerType)
     179                {
     180                    this.p2pLinkManagerType = (P2PLinkManagerType)value;
    203181                    OnPropertyChanged("P2PLinkManagerType");
    204182                    HasChanges = true;
     
    207185        }
    208186
    209         //private P2PBootstrapperType p2pBSType = P2PBootstrapperType.LocalMachineBootstrapper;
    210         private P2PBootstrapperType p2pBSType;
     187        private P2PBootstrapperType p2pBSType = P2PBootstrapperType.LocalMachineBootstrapper;
    211188        [TaskPane("Bootstrapper-Type", "Select the Bootstrapper-Type", "P2P Settings", 3, false, DisplayLevel.Beginner, ControlType.ComboBox, new string[] { "LocalMachineBootstrapper", "IrcBootstrapper" })]
    212         public P2PBootstrapperType P2PBSType
    213         {
    214             get { return this.p2pBSType; }
    215             set
    216             {
    217                 if (value != this.p2pBSType)
    218                 {
    219                     this.p2pBSType = StringToEnum<P2PBootstrapperType>(value.ToString());
     189        public int P2PBSType
     190        {
     191            get { return (int)this.p2pBSType; }
     192            set
     193            {
     194                if ((P2PBootstrapperType)value != this.p2pBSType)
     195                {
     196                    this.p2pBSType = (P2PBootstrapperType)value;
    220197                    OnPropertyChanged("P2PBSType");
    221198                    HasChanges = true;
     
    226203        private P2POverlayType p2pOverlayType = P2POverlayType.FullMeshOverlay;
    227204        [TaskPane("Overlay-Type", "Select the Overlay-Type", "P2P Settings", 4, false, DisplayLevel.Beginner, ControlType.ComboBox, new string[] { "FullMeshOverlay" })]
    228         public P2POverlayType P2POverlType
    229         {
    230             get { return this.p2pOverlayType; }
    231             set
    232             {
    233                 if (value != this.p2pOverlayType)
    234                 {
    235                     this.p2pOverlayType = StringToEnum<P2POverlayType>(value.ToString());
     205        public int P2POverlType
     206        {
     207            get { return (int)this.p2pOverlayType; }
     208            set
     209            {
     210                if ((P2POverlayType)value != this.p2pOverlayType)
     211                {
     212                    this.p2pOverlayType = (P2POverlayType)value;
    236213                    OnPropertyChanged("P2POverlType");
    237214                    HasChanges = true;
     
    242219        private P2PDHTType p2pDhtType = P2PDHTType.FullMeshDHT;
    243220        [TaskPane("DHT-Type", "Select the DHT-Type", "P2P Settings", 5, false, DisplayLevel.Beginner, ControlType.ComboBox, new string[] { "FullMeshDHT" })]
    244         public P2PDHTType P2PDhtType
    245         {
    246             get { return this.p2pDhtType;  }
    247             set
    248             {
    249                 if (value != this.p2pDhtType)
    250                 {
    251                     this.p2pDhtType = StringToEnum<P2PDHTType>(value.ToString());
     221        public int P2PDhtType
     222        {
     223            get { return (int)this.p2pDhtType;  }
     224            set
     225            {
     226                if ((P2PDHTType)value != this.p2pDhtType)
     227                {
     228                    this.p2pDhtType = (P2PDHTType)value;
    252229                    OnPropertyChanged("P2PDhtType");
    253230                    HasChanges = true;
     
    269246
    270247        #endregion
     248
     249        // Index depends on icon-position in P2PPeer-Class properties
     250        private enum PeerStatus
     251        {
     252            Connecting = 1,
     253            Online = 2,
     254            Error = 3,
     255            NotConnected = 0
     256        }
     257
     258        public event StatusChangedEventHandler OnPluginStatusChanged;
     259        private void ChangePluginIcon(PeerStatus peerStatus)
     260        {
     261            if (OnPluginStatusChanged != null) OnPluginStatusChanged(null,
     262                new StatusEventArgs(StatusChangedMode.ImageUpdate, (int)peerStatus));
     263        }
    271264    }
    272265}
  • trunk/CrypPlugins/PeerToPeerBase/PeerToPeerBase.cs

    r813 r836  
    3333using System.ComponentModel;
    3434using Cryptool.PluginBase.IO;
     35using PeersAtPlay;
    3536
    3637/*
    3738 * Synchronous functions successfully tested (store, retrieve)
    38  * !!! remove-Function is faulty !!!
    39  * Standard connection for test issues is LocalMachineBootstrapper.
    40  * IrcBootstrapper also works, but it's to lame for testing issues!
     39 * !!! remove-Function is faulty - open field !!!
    4140 *
    4241 * TODO:
    43  * - Add enums (BS,LinkManager,Overlay) to class - make it public to use in cross-cutting classes
    4442 * - dht.Remove-Method makes problems... "ArgumentNotNullException"
    4543 *   event though the Parameter is correctly set to a valid value!
     
    4947namespace Cryptool.Plugins.PeerToPeer
    5048{
     49    /* Advantages of this wrapper class:
     50     * - The PeerAtPlay-Libraries are only referenced in this project
     51     *   --> so they're easy to update
     52     * - PeerAtPlay only works with asynchronous methods, so this class
     53     *   "synchronizes" this methods.
     54     * - The PeerToPeer-Layers are unimportant for CT2-Developers, so this
     55     *   issue is obfuscated by this wrapper class
     56     */
    5157    /// <summary>
    5258    /// Wrapper class to integrate peer@play environment into CrypTool.
     
    6369        public event SystemLeft OnSystemLeft;
    6470
    65         public delegate void P2PMessageReceived(string sMsg);
     71        public delegate void P2PMessageReceived(byte[] byteSourceAddr, string sData);
    6672        public event P2PMessageReceived OnP2PMessageReceived;
    6773
    6874        /// <summary>
    69         /// returns true if key-value-pair was successfully stored in the DHT
     75        /// returns true if key-value-pair is successfully stored in the DHT
    7076        /// </summary>
    7177        /// <param name="result"></param>
     
    120126        /// <param name="overlayType"></param>
    121127        /// <param name="dhtType"></param>
    122         public void InitializeAll(string sUserName, string sWorldName, P2PLinkManagerType linkManagerType, P2PBootstrapperType bsType, P2POverlayType overlayType, P2PDHTType dhtType)
     128        public void Initialize(string sUserName, string sWorldName, P2PLinkManagerType linkManagerType, P2PBootstrapperType bsType, P2POverlayType overlayType, P2PDHTType dhtType)
    123129        {
    124130            #region Setting LinkManager, Bootstrapper, Overlay and DHT to the specified types
     
    131137                default:
    132138                    throw (new NotImplementedException());
    133                     break;
    134139            }
    135140            switch (bsType)
     
    144149                default:
    145150                    throw (new NotImplementedException());
    146                     break;
    147151            }
    148152            switch (overlayType)
     
    154158                default:
    155159                    throw (new NotImplementedException());
    156                     break;
    157160            }
    158161            switch (dhtType)
     
    163166                default:
    164167                    throw (new NotImplementedException());
    165                     break;
    166168            }
    167169            #endregion
     
    170172
    171173            this.dht.MessageReceived += new EventHandler<MessageReceived>(OnDHT_MessageReceived);
    172             this.dht.SystemJoined += new EventHandler(OnDHT_SystemJoined);
    173             this.dht.SystemLeft += new EventHandler(OnDHT_SystemLeft);
    174         }
    175 
    176         /// <summary>
    177         /// Initializing is the first step to build a new or access an existing p2p network
    178         /// </summary>
    179         /// <param name="sUserName">Choose an individual name for the user</param>
    180         /// <param name="sWorldName">fundamental: two peers are only in the SAME P2P system, when they initialized the SAME WORLD!</param>
    181         public void Initialize(string sUserName, string sWorldName)
    182         {
    183             //snal = secure network abstraction layer
    184             this.linkmanager = new Snal();
    185             //LocalMachineBootstrapper = only local connection (runs only on one machine)
    186             //for more machines use this line:
    187            
    188             this.bootstrapper = new IrcBootstrapper();
    189             //this.bootstrapper = new LocalMachineBootstrapper();
    190 
    191             // changing overlay example: this.overlay = new ChordOverlay();
    192             this.overlay = new FullMeshOverlay();
    193             //changing overlay example: this.overlay = new ExampleDHT();
    194             this.dht = new FullMeshDHT();
    195 
    196             this.dht.Initialize(sUserName, sWorldName, this.overlay, this.bootstrapper, this.linkmanager, null);
    197 
    198             this.dht.MessageReceived += new EventHandler<MessageReceived>(OnDHT_MessageReceived);
     174            this.overlay.MessageReceived += new EventHandler<OverlayMessageEventArgs>(overlay_MessageReceived);
    199175            this.dht.SystemJoined += new EventHandler(OnDHT_SystemJoined);
    200176            this.dht.SystemLeft += new EventHandler(OnDHT_SystemLeft);
     
    220196        }
    221197
     198        /*TESTING AREA - COMPLETELY STOP THE WHOLE P2P SYSTEM*/
    222199        /// <summary>
    223200        /// Disjoins the peer from the system. The P2P system survive while one peer is still in the network.
     
    230207                this.dht.BeginStop(null);
    231208                //wait till systemLeft Event is invoked
     209                this.overlay.BeginStop(null);
     210                this.linkmanager.BeginStop(null);
     211                this.bootstrapper.Dispose();
    232212                this.systemLeft.WaitOne();
    233213            }
     
    265245        #endregion
    266246
    267         // not tested and not sure that this function return the right PeerName...
    268247        /// <summary>
    269248        /// Get PeerName of the actual peer
    270249        /// </summary>
    271         /// <returns>Peer name of the actual peer</returns>
    272         public string GetPeerName()
    273         {
    274             return this.overlay.LocalAddress.ToString();
     250        /// <param name="sPeerName">out: additional peer information UserName on LinkManager</param>
     251        /// <returns>PeerID as an byte array (suitable for correct addressing on the overlay)</returns>
     252        public byte[] GetPeerID(out string sPeerName)
     253        {
     254            sPeerName = this.linkmanager.UserName;
     255            return this.overlay.LocalAddress.ToByteArray();
     256        }
     257
     258        // overlay.LocalAddress = Overlay-Peer-Address/Names
     259        public void SendToPeer(string sData, byte[] byteDestinationPeerName)
     260        {
     261            ByteStack byteData = new ByteStack();
     262            byteData.PushUTF8String(sData);
     263
     264            OverlayAddress destinationAddr = this.overlay.GetAddress(byteDestinationPeerName);
     265
     266            // create own message receiver type... => P2PBase, so only this Application
     267            // receives Messages and not all active application on the SAME overlay!
     268            OverlayMessage overlayMsg = new OverlayMessage(MessageReceiverType.P2PBase,
     269                this.overlay.LocalAddress,destinationAddr, byteData);
     270            this.overlay.Send(overlayMsg);
    275271        }
    276272
     
    293289        }
    294290
     291        private void overlay_MessageReceived(object sender, OverlayMessageEventArgs e)
     292        {
     293            if (OnP2PMessageReceived != null)
     294                OnP2PMessageReceived(e.Message.Source.ToByteArray(), e.Message.Data.PopUTF8String());
     295        }
     296
    295297        private void OnDHT_MessageReceived(object sender, MessageReceived e)
    296298        {
    297299            if (OnP2PMessageReceived != null)
    298                 OnP2PMessageReceived("Source: " + e.Source + ", Data:" + e.Data);
     300                OnP2PMessageReceived(e.Source.ToByteArray(), e.Data.PopUTF8String());
    299301        }
    300302
     
    374376        /// </summary>
    375377        /// <param name="sKey">Key of DHT Entry</param>
    376         /// <param name="sValue">Value of DHT Entry</param>
     378        /// <param name="byteData">Value of DHT Entry</param>
    377379        /// <returns>True, when storing is completed!</returns>
    378         public bool SynchStore(string sKey, string sValue)
     380        public bool SynchStore(string sKey, byte[] byteData)
    379381        {
    380382            AutoResetEvent are = new AutoResetEvent(false);
    381383            // this method returns always a GUID to distinguish between asynchronous actions
    382             Guid g = this.dht.Store(OnSynchStoreCompleted, sKey, UTF8Encoding.UTF8.GetBytes(sValue));
     384            Guid g = this.dht.Store(OnSynchStoreCompleted, sKey, byteData);
    383385
    384386            ResponseWait rw = new ResponseWait() { WaitHandle = are };
     
    388390            are.WaitOne();
    389391            return true;
     392        }
     393
     394        /// <summary>
     395        /// Stores a value in the DHT at the given key
     396        /// </summary>
     397        /// <param name="sKey">Key of DHT Entry</param>
     398        /// <param name="sValue">Value of DHT Entry</param>
     399        /// <returns>True, when storing is completed!</returns>
     400        public bool SynchStore(string sKey, string sData)
     401        {
     402            return SynchStore(sKey, UTF8Encoding.UTF8.GetBytes(sData));
    390403        }
    391404
     
    442455            if (this.waitDict.TryGetValue(sr.Guid, out rw))
    443456            {
     457                // if Status == Error, than the version of the value is out of date.
     458                // There is a versioning system in the DHT. So you must retrieve the
     459                // key and than store the new value --> that's it, but much traffic.
     460                // to be fixed in a few weeks from M.Helling
    444461                rw.Message = UTF8Encoding.UTF8.GetBytes(sr.Status.ToString());
    445462
  • trunk/CrypPlugins/PeerToPeerBase/PeerToPeerBase.csproj

    r794 r836  
    113113  </ItemGroup>
    114114  <ItemGroup>
    115     <Resource Include="ct2_p2p_system_icon_medium.png" />
    116     <Resource Include="ct2_p2p_system_icon_small.png" />
    117     <Resource Include="ct2_p2p_system_icon_tall.png" />
     115    <Resource Include="icons\peer_connecting.png" />
     116    <Resource Include="icons\peer_error.png" />
     117    <Resource Include="icons\peer_online.png" />
     118  </ItemGroup>
     119  <ItemGroup>
     120    <Resource Include="icons\peer_inaktiv.png" />
    118121  </ItemGroup>
    119122  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Note: See TracChangeset for help on using the changeset viewer.