Ignore:
Timestamp:
Jan 12, 2010, 11:54:52 AM (12 years ago)
Author:
arnold
Message:

(Conflicted Workaround) New DLLs integrated and radical changes in the DHT functionality (DHT versioning system disabled, new DHTRemove, etc.). Furthermore modified P2P-Methods, so every address and data have to be byte[]

File:
1 edited

Legend:

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

    r1022 r1067  
    3636
    3737/* - Synchronous functions successfully tested (store, retrieve)
    38  * - !!! remove-Function is faulty - open field !!!
    3938 * - The DHT has an integrated versioning system. When a peer wants
    4039 *   to store data in an entry, which already holds data, the version
     
    5049 *
    5150 * TODO:
    52  * - dht.Remove-Method makes problems... "ArgumentNotNullException"
    53  *   event though the Parameter is correctly set to a valid value!
    54  *   --> forwarded to the p@p-Team
    5551 * - Testing asynchronous methods incl. EventHandlers
    5652 */
     
    7975        public event SystemLeft OnSystemLeft;
    8076
    81         public delegate void P2PMessageReceived(PeerId sourceAddr, string sData);
     77        public delegate void P2PMessageReceived(PeerId sourceAddr, byte[] data);
    8278        public event P2PMessageReceived OnP2PMessageReceived;
    8379
     
    10298
    10399        #region Variables
     100
     101        /// <summary>
     102        /// If you want to ignore the integrated Versioning System of PeersAtPlay,
     103        /// set this flag to true.
     104        /// The DHT has an integrated VERSIONING SYSTEM. When a peer wants
     105        /// to store data in an entry, which already holds data, the version
     106        /// number will be compared with the peers' version number. If the
     107        /// peer hasn't read/write the entry the last time, the storing instruction
     108        /// will be rejected. You must first read the actual data and than you can
     109        /// store your data in this entry...
     110        /// </summary>
     111        private const bool IGNORE_DHT_VERSIONING_SYSTEM = true;
    104112
    105113        private bool started = false;
     
    189197            #endregion
    190198
    191             this.dht.MessageReceived += new EventHandler<MessageReceived>(OnDHT_MessageReceived);
    192199            this.overlay.MessageReceived += new EventHandler<OverlayMessageEventArgs>(overlay_MessageReceived);
    193200            this.dht.SystemJoined += new EventHandler(OnDHT_SystemJoined);
    194201            this.dht.SystemLeft += new EventHandler(OnDHT_SystemLeft);
    195202
    196             //this.dht.Initialize(sUserName, sWorldName, this.overlay, this.bootstrapper, this.linkmanager, null);
    197203            this.dht.Initialize(sUserName, "", sWorldName, this.overlay, this.bootstrapper, this.linkmanager, null);
    198204        }
     
    234240        }
    235241
    236 
    237242        /// <summary>
    238243        /// Asynchronously starting the peer. When the given P2P world doesn't
     
    282287        public PeerId GetPeerID(byte[] byteId)
    283288        {
    284             return new PeerId(overlay.GetAddress(byteId));
     289            return new PeerId(this.overlay.GetAddress(byteId));
    285290        }
    286291
    287292        // overlay.LocalAddress = Overlay-Peer-Address/Names
    288         public void SendToPeer(string sData, byte[] byteDestinationPeerName)
    289         {
    290             ByteStack byteData = new ByteStack();
    291             byteData.PushUTF8String(sData);
    292 
    293             OverlayAddress destinationAddr = this.overlay.GetAddress(byteDestinationPeerName);
    294 
    295             // create own message receiver type... => P2PBase, so only this Application
    296             // receives Messages and not all active application on the SAME overlay!
     293        public void SendToPeer(byte[] data, byte[] destinationPeer)
     294        {
     295            // get stack size of the pap use-data and add own use data (for optimizing Stack size)
     296            int realStackSize = this.overlay.GetHeaderSize() + data.Length;
     297            ByteStack stackData = new ByteStack(realStackSize);
     298
     299            stackData.Push(data);
     300
     301            OverlayAddress destinationAddr = this.overlay.GetAddress(destinationPeer);
    297302            OverlayMessage overlayMsg = new OverlayMessage(MessageReceiverType.P2PBase,
    298                 this.overlay.LocalAddress, destinationAddr, byteData);
    299 
     303                this.overlay.LocalAddress, destinationAddr, stackData);
    300304            this.overlay.Send(overlayMsg);
    301305        }
    302306
    303         public void SendToPeer(PubSubMessageType msgType, byte[] byteDestinationPeerName)
    304         {
    305             SendToPeer(((int)msgType).ToString(), byteDestinationPeerName);
    306         }
    307 
    308         public void SendToPeer(string sData, PeerId destinationPeerId)
    309         {
    310             SendToPeer(sData, destinationPeerId.ToByteArray());
    311         }
    312 
    313         public void SendToPeer(PubSubMessageType msgType, PeerId destinationAddress)
    314         {
    315             SendToPeer(((int)msgType).ToString(), destinationAddress.ToByteArray());
    316         }
    317 
    318307        private void overlay_MessageReceived(object sender, OverlayMessageEventArgs e)
    319308        {
     
    321310            {
    322311                PeerId pid = new PeerId(e.Message.Source);
    323                 OnP2PMessageReceived(pid, e.Message.Data.PopUTF8String());
     312                OnP2PMessageReceived(pid, e.Message.Data.PopBytes(e.Message.Data.CurrentStackSize));
     313                //OnP2PMessageReceived(pid, e.Message.Data.PopUTF8String());
    324314            }
    325315        }
     
    345335        }
    346336
    347         private void OnDHT_MessageReceived(object sender, MessageReceived e)
    348         {
    349             if (OnP2PMessageReceived != null)
    350             {
    351                 PeerId pid = new PeerId(e.Source);
    352                 OnP2PMessageReceived(pid, e.Data.PopUTF8String());
    353             }
    354         }
    355 
    356337        #endregion
    357338
     
    384365        public void AsynchStore(string sKey, string sValue)
    385366        {
    386             this.dht.Store(OnAsynchStore_Completed, sKey, UTF8Encoding.UTF8.GetBytes(sValue));
     367            this.dht.Store(OnAsynchStore_Completed, sKey, UTF8Encoding.UTF8.GetBytes(sValue), IGNORE_DHT_VERSIONING_SYSTEM);
    387368        }
    388369
     
    406387        public void AsynchRemove(string sKey)
    407388        {
    408             this.dht.Remove(OnAsynchRemove_Completed, sKey);
     389            this.dht.Remove(OnAsynchRemove_Completed, sKey, IGNORE_DHT_VERSIONING_SYSTEM);
    409390        }
    410391        private void OnAsynchRemove_Completed(RemoveResult rr)
     
    425406        #region SynchStore incl.Callback and SecondTrialCallback
    426407
    427         /* The DHT has an integrated VERSIONING SYSTEM. When a peer wants
    428          * to store data in an entry, which already holds data, the version
    429          * number will be compared with the peers' version number. If the
    430          * peer hasn't read/write the entry the last time, the storing instruction
    431          * will be rejected. You must first read the actual data and than you can
    432          * store your data in this entry... */
    433408        /// <summary>
    434409        /// Stores a value in the DHT at the given key
     
    441416            AutoResetEvent are = new AutoResetEvent(false);
    442417            // this method returns always a GUID to distinguish between asynchronous actions
    443             Guid g = this.dht.Store(OnSynchStoreCompleted, sKey, byteData);
     418            Guid g = this.dht.Store(OnSynchStoreCompleted, sKey, byteData, IGNORE_DHT_VERSIONING_SYSTEM);
    444419
    445420            ResponseWait rw = new ResponseWait() { WaitHandle = are, key=sKey , value = byteData };
     
    472447                // if Status == Error, than the version of the value is out of date.
    473448                // There is a versioning system in the DHT. So you must retrieve the
    474                 // key and than store the new value --> that's it, but much traffic.
    475                 // to be fixed in a few weeks from M.Helling
     449                // key and than store the new value
    476450                if (sr.Status == OperationStatus.Failure)
    477451                {
     
    587561            AutoResetEvent are = new AutoResetEvent(false);
    588562            // this method returns always a GUID to distinguish between asynchronous actions
    589 
    590             // ROAD WORKS: This function throws an error (ArgumentNotNullException).
    591             //             I think that's an error in the p@p-environment --> forwarded to the p@p-Team
    592             Guid g = this.dht.Remove(OnSynchRemoveCompleted, sKey);
     563            Guid g = this.dht.Remove(OnSynchRemoveCompleted, sKey, IGNORE_DHT_VERSIONING_SYSTEM);
    593564
    594565            ResponseWait rw = new ResponseWait() { WaitHandle = are };
     
    684655        public static bool operator ==(PeerId left, PeerId right)
    685656        {
     657            // because it's possible that one parameter is null, catch this exception
     658            /* Begin add - Christian Arnold, 2009.12.16, must cast the parameters, otherwise --> recursion */
     659            if ((object)left == (object)right)
     660                return true;
     661
     662            if ((object)left == null || (object)right == null)
     663                return false;
     664            /* End add */
     665
    686666            if (left.hashCode != right.hashCode)
    687667                return false;
Note: See TracChangeset for help on using the changeset viewer.