Ignore:
Timestamp:
May 2, 2010, 1:43:33 PM (12 years ago)
Author:
Paul Lelgemann
Message:

+ Added P2P connection to CrypP2P based on PeerToPeerBase plugin (using default settings)
+ Added MD5Collider to CoreDeveloper solution

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypP2P/P2PManager.cs

    r1364 r1374  
    2222using Cryptool.PluginBase;
    2323using Cryptool.PluginBase.Miscellaneous;
     24using System.Windows.Forms;
     25using Cryptool.P2P.Worker;
     26using DevComponents.WpfRibbon;
     27using Cryptool.P2P.Helper;
     28using Cryptool.P2P.Internal;
    2429
    2530namespace Cryptool.P2P
     
    2934        #region Singleton
    3035        static readonly P2PManager INSTANCE = new P2PManager();
    31         static P2PManager() {}
    32         P2PManager() {}
     36        private P2PManager() { }
    3337
    3438        public static P2PManager Instance
     
    4751
    4852        #region Private variables
    49         private bool isP2PNetworkConnected = false;
     53        private P2PBase P2PBase { get; set; }
     54        private P2PSettings P2PSettings { get; set; }
     55        private ButtonDropDown P2PButton { get; set; }
    5056        #endregion
    5157
    52         public void initialize()
     58        #region Events
     59        public static event Cryptool.PluginBase.GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
     60
     61        // to forward event from overlay/dht MessageReceived-Event from P2PBase
     62        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
     63        #endregion Events
     64
     65        /// <summary>
     66        /// Initialises variables and important environment settings
     67        /// regarding certificates.
     68        /// </summary>
     69        /// <param name="p2pButton">Button that can change the connection state
     70        /// and displays it by showing different images.</param>
     71        public void Initialize(ButtonDropDown p2pButton)
    5372        {
    54             // TODO P2P init code
     73            this.P2PButton = p2pButton;
     74            this.P2PBase = new P2PBase();
     75            this.P2PSettings = new P2PSettings();
     76            this.P2PBase.AllowLoggingToMonitor = this.P2PSettings.Log2Monitor;
     77
     78            // Validate certificats
     79            if (!PAPCertificate.CheckAndInstallPAPCertificates())
     80            {
     81                GuiLogMessage("Certificates not validated, P2P might not be working!", NotificationLevel.Error);
     82                return;
     83            }
     84
     85            // Register events
     86
     87            // to forward event from overlay/dht MessageReceived-Event from P2PBase
     88            this.P2PBase.OnP2PMessageReceived += new P2PBase.P2PMessageReceived(p2pBase_OnP2PMessageReceived);
     89
     90            // Register exit event to terminate P2P connection without loosing data
     91            // TODO check if this is correct, should be - but handler is not called (and application does not shut down), probably unrelated to this problem
     92            Application.ApplicationExit += new EventHandler(HandleDisconnectByApplicationShutdown);
    5593        }
    5694
    57         public void setConnectionState(bool newState)
     95        /// <summary>
     96        /// Changes the current connection state to the P2P network.
     97        /// If there is currently no connection, it will try to connect.
     98        /// If a connection is present, it will disconnect.
     99        /// The actual work will be done asynchronous.
     100        /// </summary>
     101        public void ToggleConnectionState()
    58102        {
    59             // TODO implement logic for handling connects/disconnects
    60             isP2PNetworkConnected = newState;
     103            new ConnectionWorker(P2PBase, P2PSettings, P2PButton).Start();
    61104        }
    62105
    63         public bool getP2PConnectionState()
     106        public bool P2PConnected()
    64107        {
    65             return isP2PNetworkConnected;
     108            return P2PBase.Started;
    66109        }
     110
     111        #region DHT operations
     112        // TODO add error handling, if P2P if not connected
     113        public static bool Store(string key, byte[] data)
     114        {
     115            return INSTANCE.P2PBase.SynchStore(key, data);
     116        }
     117
     118        public static byte[] Retrieve(string key)
     119        {
     120            return INSTANCE.P2PBase.SynchRetrieve(key);
     121        }
     122
     123        public static bool Remove(string key)
     124        {
     125            return INSTANCE.P2PBase.SynchRemove(key);
     126        }
     127        #endregion DHT operations
     128
     129        // to forward event from overlay/dht MessageReceived-Event from P2PBase
     130        private void p2pBase_OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
     131        {
     132            if (OnPeerMessageReceived != null)
     133                OnPeerMessageReceived(sourceAddr, data);
     134        }
     135
     136        #region Framework methods
     137        void HandleDisconnectByApplicationShutdown(object sender, EventArgs e)
     138        {
     139            if (P2PConnected())
     140            {
     141                new ConnectionWorker(P2PBase, P2PSettings, P2PButton).Start();
     142            }
     143        }
     144
     145        public void GuiLogMessage(string message, NotificationLevel logLevel)
     146        {
     147            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, null, new GuiLogEventArgs(message, null, logLevel));
     148        }
     149        #endregion Framework methods
    67150    }
    68151}
Note: See TracChangeset for help on using the changeset viewer.