Ignore:
Timestamp:
Jan 26, 2010, 1:03:56 PM (12 years ago)
Author:
arnold
Message:

P2PManager and P2PWorker Updates (NAT Traversal after installing certificates possible, etc.)

File:
1 edited

Legend:

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

    r1098 r1107  
    2020using PeersAtPlay.P2PStorage.DHT;
    2121using PeersAtPlay.P2PStorage.FullMeshDHT;
    22 using PeersAtPlay.P2PLink.SimpleSnalNG;
    2322using PeersAtPlay.P2POverlay.Bootstrapper;
    2423using PeersAtPlay.P2POverlay;
     
    2928using System.Threading;
    3029using Cryptool.PluginBase.Control;
    31 using Cryptool.PluginBase;
    32 using Cryptool.PluginBase.Miscellaneous;
    3330using System.ComponentModel;
    34 using Cryptool.PluginBase.IO;
    3531using PeersAtPlay;
     32using PeersAtPlay.Util.Logging;
    3633
    3734/* - Synchronous functions successfully tested (store, retrieve)
     
    9996        #region Variables
    10097
     98        private bool allowLoggingToMonitor;
     99        /// <summary>
     100        /// If true, all kinds of actions will be logged in the PeersAtPlay LogMonitor.
     101        /// </summary>
     102        public bool AllowLoggingToMonitor
     103        {
     104            get { return this.allowLoggingToMonitor; }
     105            set { this.allowLoggingToMonitor = value; }
     106        }
     107
     108        private const bool ALLOW_LOGGING_TO_MONITOR = true;
     109
    101110        private bool started = false;
    102111        /// <summary>
     
    107116            get { return this.started; }
    108117            private set { this.started = value; }
     118        }
     119
     120        private bool useNatTraversal = true;
     121        /// <summary>
     122        /// Initialization value = true!
     123        /// When you want to use NAT-Traversal (tunneling the P2P
     124        /// connection through NATs and Firewalls), you have to
     125        /// set this flag to true, before initializing this peer
     126        /// ATTENTION: At present you have to install two certificates
     127        /// manually on each workstation, where you want to use P2P@CT2,
     128        /// and set a static PeerName (pap0001) and P2PWorldName (TestBruteforcingWorld)
     129        /// TODO: Automatize the certificate stuff and implement a chance
     130        /// to choose own PeerNames and P2PWorldNames
     131        /// </summary>
     132        public bool UseNatTraversal
     133        {
     134            get { return this.useNatTraversal; }
     135            set { this.useNatTraversal = value; }
    109136        }
    110137
     
    137164        /// </summary>
    138165        /// <param name="sUserName">Choose an individual name for the user</param>
    139         /// <param name="sWorldName">fundamental: two peers are only in the SAME P2P system, when they initialized the SAME WORLD!</param>
     166        /// <param name="sWorldName">fundamental: two peers are only in the SAME
     167        /// P2P system, when they initialized the SAME WORLD!</param>
     168        /// <param name="bolUseNatTraversal">When you want to use NAT-Traversal #
     169        /// (tunneling the P2P connection through NATs and Firewalls), you have to
     170        /// set this flag to true</param>
    140171        /// <param name="linkManagerType"></param>
    141172        /// <param name="bsType"></param>
    142173        /// <param name="overlayType"></param>
    143174        /// <param name="dhtType"></param>
    144         public void Initialize(string sUserName, string sWorldName, P2PLinkManagerType linkManagerType, P2PBootstrapperType bsType, P2POverlayType overlayType, P2PDHTType dhtType)
    145         {
     175        public void Initialize(string sUserName, string sWorldName, bool bolUseNatTraversal, P2PLinkManagerType linkManagerType, P2PBootstrapperType bsType, P2POverlayType overlayType, P2PDHTType dhtType)
     176        {
     177            this.UseNatTraversal = bolUseNatTraversal;
     178
    146179            #region Setting LinkManager, Bootstrapper, Overlay and DHT to the specified types
    147180            switch (linkManagerType)
     
    149182                case P2PLinkManagerType.Snal:
    150183                    //snal = secure network abstraction layer
    151                     this.linkmanager = new Snal();
     184                    if (UseNatTraversal)
     185                    {
     186                        if (bsType == P2PBootstrapperType.LocalMachineBootstrapper)
     187                            throw (new Exception("It's mindless to activate NAT traversal, but use the LocalMachineBootstrapper."));
     188
     189                        LogToMonitor("Init LinkMgr: Using NAT Traversal stuff");
     190                        // NAT-Traversal stuff needs a different Snal-Version
     191                        this.linkmanager = new PeersAtPlay.P2PLink.SnalNG.Snal();
     192
     193                        ((PeersAtPlay.P2PLink.SnalNG.Snal)this.linkmanager).Settings.ConnectInternal = false;
     194                        ((PeersAtPlay.P2PLink.SnalNG.Snal)this.linkmanager).Settings.LocalReceivingPort = 0;
     195                        ((PeersAtPlay.P2PLink.SnalNG.Snal)this.linkmanager).Settings.UseLocalAddressDetection = false;
     196                    }
     197                    else
     198                    {
     199                        this.linkmanager = new PeersAtPlay.P2PLink.SimpleSnalNG.Snal();
     200                    }
    152201                    break;
    153202                default:
     
    161210                    break;
    162211                case P2PBootstrapperType.IrcBootstrapper:
     212                    if (UseNatTraversal)
     213                    {
     214                        LogToMonitor("Init Bootstrapper: Using NAT Traversal stuff");
     215                        PeersAtPlay.P2POverlay.Bootstrapper.IrcBootstrapper.Settings.DelaySymmetricResponse = true;
     216                        PeersAtPlay.P2POverlay.Bootstrapper.IrcBootstrapper.Settings.IncludeSymmetricInResponse = false;
     217                        PeersAtPlay.P2POverlay.Bootstrapper.IrcBootstrapper.Settings.SymmetricResponseDelay = 6000;
     218                    }
     219                    else
     220                    {
     221                        PeersAtPlay.P2POverlay.Bootstrapper.IrcBootstrapper.Settings.IncludeSymmetricInResponse = true;
     222                        PeersAtPlay.P2POverlay.Bootstrapper.IrcBootstrapper.Settings.DelaySymmetricResponse = false;
     223                    }
    163224                    this.bootstrapper = new IrcBootstrapper();
    164225                    break;
     
    275336        public PeerId GetPeerID(byte[] byteId)
    276337        {
     338            LogToMonitor("GetPeerID: Converting byte[] to PeerId-Object");
    277339            return new PeerId(this.overlay.GetAddress(byteId));
    278340        }
     
    298360            {
    299361                PeerId pid = new PeerId(e.Message.Source);
    300                 OnP2PMessageReceived(pid, e.Message.Data.PopBytes(e.Message.Data.CurrentStackSize));
     362                /* You have to fire this event asynchronous, because the main
     363                 * thread will be stopped in this wrapper class for synchronizing
     364                 * the asynchronous stuff (AutoResetEvent) --> so this could run
     365                 * into a deadlock, when you fire this event synchronous (normal Invoke)
     366                 * ATTENTION: This could change the invocation order!!! In my case
     367                              no problem, but maybe in future cases... */
     368                OnP2PMessageReceived.BeginInvoke(pid, e.Message.Data.PopBytes(e.Message.Data.CurrentStackSize),null,null);
    301369                //OnP2PMessageReceived(pid, e.Message.Data.PopUTF8String());
    302370            }
     
    321389            this.systemLeft.Set();
    322390            Started = false;
     391
     392            LogToMonitor("OnDHT_SystemLeft has nulled the dht and setted the systemLeft Waithandle");
    323393        }
    324394
     
    404474        public bool SynchStore(string sKey, byte[] byteData)
    405475        {
     476            LogToMonitor("Begin: SynchStore. Key: " + sKey + ", Data: " + Encoding.UTF8.GetString(byteData));
    406477            AutoResetEvent are = new AutoResetEvent(false);
    407478            // this method returns always a GUID to distinguish between asynchronous actions
    408479            Guid g = this.dht.Store(OnSynchStoreCompleted, sKey, byteData);
    409             //Guid g = this.dht.Store(OnSynchStoreCompleted, sKey, byteData, IGNORE_DHT_VERSIONING_SYSTEM);
    410480
    411481            ResponseWait rw = new ResponseWait() { WaitHandle = are, key=sKey , value = byteData };
     
    414484            //blocking till response
    415485            are.WaitOne();
     486
     487            LogToMonitor("End: SynchStore. Key: " + sKey + ". Success: " + rw.success.ToString());
     488
    416489            return rw.success;
    417490        }
     
    502575        public byte[] SynchRetrieve(string sKey)
    503576        {
     577            LogToMonitor("ThreadId (P2PBase SynchRetrieve): " + Thread.CurrentThread.ManagedThreadId.ToString());
     578
    504579            AutoResetEvent are = new AutoResetEvent(false);
    505580            // this method returns always a GUID to distinguish between asynchronous actions
     581
     582            LogToMonitor("Begin: SynchRetrieve. Key: " + sKey);
     583             
    506584            Guid g = this.dht.Retrieve(OnSynchRetrieveCompleted, sKey);
    507585           
     
    511589            // blocking till response
    512590            are.WaitOne();
     591
     592            LogToMonitor("End: SynchRetrieve. Key: " + sKey + ". Success: " + rw.success.ToString());
     593
    513594            //Rückgabe der Daten
    514595            return rw.Message;
     
    521602        private void OnSynchRetrieveCompleted(RetrieveResult rr)
    522603        {
     604            LogToMonitor(rr.Guid.ToString());
     605           
    523606            ResponseWait rw;
     607
     608            LogToMonitor("ThreadId (P2PBase retrieve callback): " + Thread.CurrentThread.ManagedThreadId.ToString());
    524609
    525610            if (this.waitDict.TryGetValue(rr.Guid, out rw))
     
    550635        public bool SynchRemove(string sKey)
    551636        {
     637            LogToMonitor("Begin SynchRemove. Key: " + sKey);
     638
    552639            AutoResetEvent are = new AutoResetEvent(false);
    553640            // this method returns always a GUID to distinguish between asynchronous actions
     
    560647            // blocking till response
    561648            are.WaitOne();
     649
     650            LogToMonitor("Ended SynchRemove. Key: " + sKey + ". Success: " + rw.success.ToString());
     651
    562652            return rw.success;
    563653        }
     
    598688            }
    599689        }
     690
     691        public void LogToMonitor(string sTextToLog)
     692        {
     693            if(AllowLoggingToMonitor)
     694                Log.Debug(sTextToLog);
     695        }
     696
    600697    }
    601698
Note: See TracChangeset for help on using the changeset viewer.