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.)

Location:
trunk/CrypPlugins/PeerToPeerPublisher
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/PeerToPeerPublisher/P2PPublisher.cs

    r1068 r1107  
    8787                this.p2pPublisher.Stop(PubSubMessageType.Solution);
    8888                GuiLogMessage("TEST: Emulate Solution-Found-message", NotificationLevel.Info);
     89            }
     90            if (e.PropertyName == "BtnSerDeser")
     91            {
     92                this.p2pPublisher.TestSerialization();
    8993            }
    9094        }
  • trunk/CrypPlugins/PeerToPeerPublisher/P2PPublisherBase.cs

    r1098 r1107  
    66using Cryptool.PluginBase.Control;
    77using System.Threading;
     8using System.IO;
    89
    910/*
     
    2627        private Timer timerWaitingForAliveMsg;
    2728        private PeerId ownPeerId;
     29        /// <summary>
     30        /// interval in milliseconds!!! Divide with 1000 to preserve seconds
     31        /// </summary>
    2832        private long aliveMessageInterval;
    2933
     
    204208            } // end switch
    205209            if (timerWaitingForAliveMsg == null)
    206                 timerWaitingForAliveMsg = new Timer(OnWaitingForAliveMsg, null, this.aliveMessageInterval * 1000,
    207                     this.aliveMessageInterval * 1000);
     210                timerWaitingForAliveMsg = new Timer(OnWaitingForAliveMsg, null, this.aliveMessageInterval,
     211                    this.aliveMessageInterval);
    208212
    209213            if (msgType != PubSubMessageType.Unregister)
     
    271275
    272276        #endregion
     277
     278        // Only for testing the (De-)Serialization of SubscribersManagement
     279        public void TestSerialization()
     280        {
     281            /* Get all Subs and serialize them manual */
     282            List<PeerId> originalSubList = this.peerManagement.GetAllSubscribers();
     283
     284            /* Serialize and deserialize active subs automatically */
     285            byte[] byResult = this.peerManagement.Serialize();
     286            List<PeerId> pid = this.peerManagement.Deserialize(byResult, ref this.p2pControl);
     287
     288            /* Comparing the deserialized list with the original SubList */
     289            bool result = true;
     290
     291            if (pid.Count != originalSubList.Count)
     292            {
     293                result = false;
     294            }
     295            else
     296            {
     297                int f = 0;
     298                foreach (PeerId originalPeer in originalSubList)
     299                {
     300                    if (originalPeer != pid[f])
     301                    {
     302                        result = false;
     303                        break;
     304                    }
     305                    f++;
     306                }
     307            }
     308            GuiLogging("Result of serialization/deserialization: " + result, NotificationLevel.Debug);
     309        }
    273310    }
    274311}
  • trunk/CrypPlugins/PeerToPeerPublisher/P2PPublisherSettings.cs

    r1068 r1107  
    8585            OnPropertyChanged("BtnSolutionFound");
    8686        }
     87
     88        [TaskPane("Ser/Deser Test", "Click here to test serialization/deserialization of PeerManagementList!", "Serialization Test", 0, true, DisplayLevel.Beginner, ControlType.Button)]
     89        public void BtnSerDeser()
     90        {
     91            OnPropertyChanged("BtnSerDeser");
     92        }
    8793        /* FOR TESTING ISSUES */
    8894
  • trunk/CrypPlugins/PeerToPeerPublisher/SubscribersManagement.cs

    r1098 r1107  
    44using System.Text;
    55using System.Collections;
     6using System.IO;
     7using Cryptool.PluginBase.Control;
    68
    79namespace Cryptool.Plugins.PeerToPeer
     
    2830        /// <summary>
    2931        /// Timespan in which subscriber gets marked secondChance first and twice is removed from Subscriber list.
    30         /// Latency of 2 seconds will be added because of network latency.
     32        /// Latency of 3 seconds will be added because of network latency.
    3133        /// </summary>
    3234        public long ExpirationTime
    3335        {
    34             get { return this.expirationTime * 2000;  }
     36            get { return this.expirationTime + 3000;  }
    3537            set { this.expirationTime = value; }
    3638        }
     
    162164            this.secondChanceList = null;
    163165        }
     166
     167        private Encoding enc = Encoding.UTF8;
     168        /// <summary>
     169        /// serializes only the active subscribers list,
     170        /// either the second chance list or the timestamps
     171        /// are nonrelevant, because after Deserializing
     172        /// this stuff, the availablity is obsolete and have
     173        /// to be additionally checked
     174        /// </summary>
     175        /// <returns></returns>
     176        public virtual byte[] Serialize()
     177        {
     178            byte[] ret = null;
     179            lock (this.checkList)
     180            {
     181                if (this.checkList == null || this.checkList.Count == 0)
     182                {
     183                    return null;
     184                }
     185                List<PeerId> lstActivePeers = this.checkList.Keys.ToList<PeerId>();
     186                using (MemoryStream memStream = new MemoryStream())
     187                {
     188                    // first write dataset count to list
     189                    memStream.WriteByte(Convert.ToByte(lstActivePeers.Count));
     190                    // than write every peer id as an byte array - first byte is the length of the id
     191                    foreach (PeerId pid in lstActivePeers)
     192                    {
     193                        byte[] byPid = pid.ToByteArray();
     194                        byte[] enhancedByte = new byte[byPid.Length + 1];
     195                        //additional store PeerId length to ease reconstructing
     196                        enhancedByte[0] = Convert.ToByte(byPid.Length);
     197                        Buffer.BlockCopy(byPid, 0, enhancedByte, 1, byPid.Length);
     198                        memStream.Write(enhancedByte, 0, enhancedByte.Length);
     199                    }
     200                    ret = memStream.ToArray();
     201                }
     202            }
     203            return ret;
     204        }
     205
     206        /// <summary>
     207        /// Deserializes a Subscriber/Worker list and reconstructs the PeerIds. Returns
     208        /// the deserialized PeerId, so you can ping the peers to check whether they are
     209        /// available anymore.
     210        /// </summary>
     211        /// <param name="serializedPeerIds">the already serialized peerId byte-array</param>
     212        /// <param name="p2pControl">a reference to the p2pControl to convert deserialized byte arrays to valid PeerIds</param>
     213        /// <returns></returns>
     214        public virtual List<PeerId> Deserialize(byte[] serializedPeerIds, ref IP2PControl p2pControl)
     215        {
     216            if (serializedPeerIds == null || serializedPeerIds.Length < 2)
     217            {
     218                throw (new Exception("Invalid byte[] input - deserialization not possible"));
     219            }
     220            List<PeerId> deserializedPeers = new List<PeerId>();
     221
     222            MemoryStream memStream = new MemoryStream(serializedPeerIds);
     223            try
     224            {
     225                int peerIdAmount = Convert.ToInt32(memStream.ReadByte());
     226                int peerIdLen, readResult;
     227                PeerId pid;
     228                for (int i = 0; i < peerIdAmount; i++)
     229                {
     230                    peerIdLen = Convert.ToInt32(memStream.ReadByte());
     231                    byte[] byPeerId = new byte[peerIdLen];
     232                    readResult = memStream.Read(byPeerId, 0, byPeerId.Length);
     233                    if (readResult == 0)
     234                        throw (new Exception("Deserialization process of the byte[] was canceled, because byte[] didn't achieve to the conventions."));
     235                    // create a new PeerId Object and add it to the list
     236                    pid = p2pControl.GetPeerID(byPeerId);
     237                    //deserializedPeers.Add(byPeerId);
     238                    deserializedPeers.Add(pid);
     239                }
     240            }
     241            catch (Exception ex)
     242            {
     243                memStream.Flush();
     244                memStream.Close();
     245                memStream.Dispose();
     246                deserializedPeers.Clear();
     247                deserializedPeers = null;
     248                throw new Exception("Deserialization process of byte[] was canceled, because byte[] didn't achieve to the conventions.", ex);
     249            }
     250            return deserializedPeers;
     251        }
    164252    }
    165253}
Note: See TracChangeset for help on using the changeset viewer.