Ignore:
Timestamp:
May 31, 2010, 7:22:34 PM (12 years ago)
Author:
Paul Lelgemann
Message:

o CrypP2P: refactored P2PBase, removed race condition and unused code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypP2P/P2PManager.cs

    r1537 r1545  
    3434        {
    3535            P2PBase = new P2PBase();
    36             P2PSettings = new P2PSettings();
    37             P2PBase.AllowLoggingToMonitor = P2PSettings.Log2Monitor;
    3836
    3937            // Register events
    4038
    4139            // to forward event from overlay/dht MessageReceived-Event from P2PBase
    42             P2PBase.OnP2PMessageReceived += p2pBase_OnP2PMessageReceived;
     40            P2PBase.OnP2PMessageReceived += OnP2PMessageReceived;
    4341
    4442            // Register exit event to terminate P2P connection without loosing data
     
    4947        #endregion
    5048
    51         # region Constants
    52 
    53         public const string P2PDisconnectImageUri = "images/peer2peer-disconnect.png";
    54         public const string P2PConnectImageUri = "images/peer2peer-connect.png";
    55 
    56         # endregion
    57 
    5849        #region Variables
    5950
    60         public P2PBase P2PBase { get; set; }
    61         public P2PSettings P2PSettings { get; set; }
     51        public P2PBase P2PBase { get; private set; }
    6252        public bool IsP2PConnecting { get; internal set; }
    6353
     
    7161        public static event P2PConnectionStateChangeEventHandler OnP2PConnectionStateChangeOccurred;
    7262
    73         // to forward event from overlay/dht MessageReceived-Event from P2PBase
    7463        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
    7564
     
    8069            if (OnP2PConnectionStateChangeOccurred != null)
    8170            {
    82                 OnP2PConnectionStateChangeOccurred(this, P2PConnected());
    83             }
    84         }
    85 
    86         public bool P2PConnected()
     71                OnP2PConnectionStateChangeOccurred(this, IsP2PConnected());
     72            }
     73        }
     74
     75        public bool IsP2PConnected()
    8776        {
    8877            return P2PBase.Started;
     
    9180        public string UserInfo()
    9281        {
    93             if (!P2PConnected())
     82            if (!IsP2PConnected())
    9483            {
    9584                return null;
     
    9786
    9887            string userName;
    99             var userInfo = P2PBase.GetPeerID(out userName);
     88            var userInfo = P2PBase.GetPeerId(out userName);
    10089            return userInfo + " (" + userName + ")";
    10190        }
     
    10392        public void HandleConnectOnStartup()
    10493        {
    105             if (P2PSettings.ConnectOnStartup && IsReadyToConnect())
     94            if (P2PSettings.Default.ConnectOnStartup && IsReadyToConnect())
    10695            {
    10796                GuiLogMessage("Connect on startup enabled. Establishing connection...", NotificationLevel.Info);
    108                 new ConnectionWorker(P2PBase, P2PSettings).Start();
     97                new ConnectionWorker(P2PBase).Start();
    10998            }
    11099        }
     
    112101        private bool IsReadyToConnect()
    113102        {
    114             if (String.IsNullOrEmpty(P2PSettings.PeerName))
     103            if (String.IsNullOrEmpty(P2PSettings.Default.PeerName))
    115104            {
    116105                GuiLogMessage("Peer-to-peer not fully configured: username missing.", NotificationLevel.Error);
     
    118107            }
    119108
    120             if (String.IsNullOrEmpty(P2PSettings.WorldName))
     109            if (String.IsNullOrEmpty(P2PSettings.Default.PeerName))
    121110            {
    122111                GuiLogMessage("Peer-to-peer not fully configured: world name missing.", NotificationLevel.Error);
     
    129118        public PeerId GetPeerId(out string userName)
    130119        {
    131             return P2PBase.GetPeerID(out userName);
     120            return P2PBase.GetPeerId(out userName);
    132121        }
    133122
    134123        // to forward event from overlay/dht MessageReceived-Event from P2PBase
    135         private void p2pBase_OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
     124        private void OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
    136125        {
    137126            if (OnPeerMessageReceived != null)
     
    143132        private void HandleDisconnectByApplicationShutdown(object sender, EventArgs e)
    144133        {
    145             if (P2PConnected())
    146             {
    147                 new ConnectionWorker(P2PBase, P2PSettings).Start();
     134            if (IsP2PConnected())
     135            {
     136                new ConnectionWorker(P2PBase).Start();
    148137            }
    149138        }
     
    158147        #region DHT operations
    159148
     149        /// <summary>
     150        /// Stores the given data in the DHT. This method will block until a response has been received.
     151        ///
     152        /// The underlying DHT is versionend. Store attempts will fail, if the latest version has not been retrieved before.
     153        /// </summary>
     154        /// <param name="key">key to write</param>
     155        /// <param name="data">data to write</param>
     156        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
     157        /// <returns>true if the store attempt was successful, false otherwise</returns>
    160158        public static bool Store(string key, byte[] data)
    161159        {
    162             if (!Instance.P2PConnected())
     160            if (!Instance.IsP2PConnected())
    163161                throw new NotConnectedException();
    164162
     
    166164        }
    167165
     166        /// <summary>
     167        /// Stores the given data in the DHT. This method will block until a response has been received.
     168        ///
     169        /// The underlying DHT is versionend. Store attempts will fail, if the latest version has not been retrieved before.
     170        /// </summary>
     171        /// <param name="key">key to write</param>
     172        /// <param name="data">data to write</param>
     173        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
     174        /// <returns>true if the store attempt was successful, false otherwise</returns>
    168175        public static bool Store(string key, string data)
    169176        {
    170             if (!Instance.P2PConnected())
     177            if (!Instance.IsP2PConnected())
    171178                throw new NotConnectedException();
    172179           
     
    174181        }
    175182
     183        /// <summary>
     184        /// Retrieves the latest version of a given in key from the DHT.
     185        /// </summary>
     186        /// <param name="key">key to retrieve</param>
     187        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
     188        /// <returns>byte array containing the data</returns>
    176189        public static byte[] Retrieve(string key)
    177190        {
    178             if (!Instance.P2PConnected())
     191            if (!Instance.IsP2PConnected())
    179192                throw new NotConnectedException();
    180193           
     
    182195        }
    183196
     197        /// <summary>
     198        /// Removes a key and its data from the DHT.
     199        ///
     200        /// The underlying DHT is versionend. Remove attempts will fail, if the latest version has not been retrieved before.
     201        /// </summary>
     202        /// <param name="key">key to remove</param>
     203        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
     204        /// <returns>bool determining wether the attempt was successful</returns>
    184205        public static bool Remove(string key)
    185206        {
    186             if (!Instance.P2PConnected())
     207            if (!Instance.IsP2PConnected())
    187208                throw new NotConnectedException();
    188209
Note: See TracChangeset for help on using the changeset viewer.