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

Vulminous changes and updates (for example: enabling replacement of the publisher, WPF QuickWatchPresentation in P2PManager, etc.)

Location:
trunk/CrypPlugins/PeerToPeerPublisher
Files:
4 edited

Legend:

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

    r872 r1068  
    5454        private void settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
    5555        {
     56            if (this.p2pPublisher == null || !this.p2pPublisher.Started)
     57                return;
     58
     59
    5660            // storing settings for subscribers in the DHT, so they can load them there
    5761            if (e.PropertyName == "SendAliveMessageInterval")
    5862            {
    59                 this.p2pControl.DHTstore(settings.TopicName + "Settings",
     63                this.p2pControl.DHTstore(settings.TopicName + "AliveMsg",
    6064                    System.BitConverter.GetBytes(this.settings.SendAliveMessageInterval * 1000));
    6165            }
     
    184188                this.p2pPublisher.Start(this.settings.TopicName, (long)this.settings.SendAliveMessageInterval);
    185189            }
     190            if (this.p2pPublisher != null && !this.p2pPublisher.Started) // when Workspace has stopped and has been started again
     191            {
     192                this.p2pPublisher.Start(this.settings.TopicName, (long)this.settings.SendAliveMessageInterval);
     193            }
    186194        }
    187195
  • trunk/CrypPlugins/PeerToPeerPublisher/P2PPublisherBase.cs

    r1022 r1068  
    99/*
    1010 * TODO:
    11  * - Removing excluded in Stop-Method, because it throws an exception... --> to be fixed by M.Helling (PAP)
    1211 * - FUTURE: dual data management of subscriber list (on local peer and in DHT)
    1312 */
     
    2221        #region Variables
    2322
     23        private const string DHT_ALIVE_POSTFIX = "AliveMsg";
     24
    2425        protected IP2PControl p2pControl;
    2526        protected SubscriberManagement peerManagement;
    2627        private string topic = String.Empty;
    27         private string sDHTSettingsPostfix = "Settings";       
    2828        private Timer timerWaitingForAliveMsg;
    2929        private PeerId ownPeerId;
     
    4545        {
    4646            this.p2pControl = p2pControl;
    47             this.p2pControl.OnPeerReceivedMsg +=new P2PBase.P2PMessageReceived(MessageReceived);
    4847        }
    4948
     
    6463        public bool Start(string sTopic, long aliveMessageInterval)
    6564        {
     65            this.p2pControl.OnPayloadMessageReceived += new P2PPayloadMessageReceived(p2pControl_OnPayloadMessageReceived);
     66            this.p2pControl.OnSystemMessageReceived += new P2PSystemMessageReceived(p2pControl_OnSystemMessageReceived);
     67
    6668            /* BEGIN: CHECKING WHETHER THERE HAS ALREADY EXIST ANOTHER PUBLISHER */
    6769            this.topic = sTopic;
     
    9395
    9496            bool bolTopicStored = this.p2pControl.DHTstore(sTopic, myPeerId.ToByteArray());
    95             bool bolSettingsStored = this.p2pControl.DHTstore(sTopic + this.sDHTSettingsPostfix,
     97            bool bolSettingsStored = this.p2pControl.DHTstore(sTopic + DHT_ALIVE_POSTFIX,
    9698                System.BitConverter.GetBytes(this.aliveMessageInterval));
    9799
    98100            if (!bolTopicStored || !bolSettingsStored)
    99101            {
    100                 GuiLogging("Storing Publishers ID or Publishers Settings wasn't possible.", NotificationLevel.Error);
     102                GuiLogging("Storing Publishers ID and/or Publishers Settings wasn't possible.", NotificationLevel.Error);
    101103                return false;
    102104            }
     
    133135        public virtual void Stop(PubSubMessageType msgType)
    134136        {
    135             // send unregister message to all subscribers
    136             int i = SendInternalMsg(msgType);
    137             GuiLogging("Unregister messages were sent to " + i.ToString() + " subscribers!", NotificationLevel.Info);
    138 
    139             //Still an error in dhtRemove... so at present ignored... - partially freezing the workspace forever...
    140             /*
    141              * if someone wants to call the Stop-method, but the peer is already down,
    142              * this will run into a problem (CrypWin.exe lives on), because of the
    143              * necessary workaround in P2PPeerMaster (Initializes Peer, if it isn't
    144              * initialized yet). This workaround was necessary, because of the gradual
    145              * execution of the PlugIns, so that the case could occur, that the Peer isn't
    146              * initialized by the PlugIn-method, but its Master-PlugIns want to use its
    147              * functions right now
    148              */
    149137            if (this.p2pControl != null && this.p2pControl.PeerStarted())
    150138            {
     
    153141
    154142                bool removeTopic = this.p2pControl.DHTremove(this.topic);
    155                 bool removeSettings = this.p2pControl.DHTremove(this.topic + this.sDHTSettingsPostfix);
     143                bool removeSettings = this.p2pControl.DHTremove(this.topic + DHT_ALIVE_POSTFIX);
    156144                string removeInfo = String.Empty;
    157145                if (removeTopic && removeSettings)
     
    165153                else
    166154                    GuiLogging("Neither Topic nor settings were removed from DHT.", NotificationLevel.Debug);
     155
     156                // send unregister message to all subscribers
     157                int i = SendInternalMsg(msgType);
     158                GuiLogging("Unregister messages were sent to " + i.ToString() + " subscribers!", NotificationLevel.Info);
    167159            }
    168160
     
    175167            }
    176168
     169            GuiLogging("Deregister message-received-events", NotificationLevel.Debug);
     170            this.p2pControl.OnPayloadMessageReceived -= p2pControl_OnPayloadMessageReceived;
     171            this.p2pControl.OnSystemMessageReceived -= p2pControl_OnSystemMessageReceived;
     172
    177173            GuiLogging("Publisher completely stopped!", NotificationLevel.Info);
    178174
     
    184180        #region private Methods (MessageReceived, SendInternalMsg, OnWaitingForAliveMsg, peerManagement_OnSubscriberRemoved)
    185181
    186         protected virtual void MessageReceived(PeerId sourceAddr, string sData)
    187         {
    188             PubSubMessageType msgType = this.p2pControl.GetMsgType(sData);
    189 
    190             if (msgType != PubSubMessageType.NULL)
    191             {
    192                 switch (msgType)
    193                 {
    194                     case PubSubMessageType.Register:
    195                         if (this.peerManagement.Add(sourceAddr))
    196                         {
    197                             GuiLogging("REGISTERED: Peer with ID " + sourceAddr, NotificationLevel.Info);
    198                             this.p2pControl.SendToPeer(PubSubMessageType.RegisteringAccepted, sourceAddr);
    199                         }
    200                         else
    201                         {
    202                             GuiLogging("ALREADY REGISTERED peer with ID " + sourceAddr, NotificationLevel.Info);
    203                         }
    204                         break;
    205                     case PubSubMessageType.Unregister:
    206                         if (this.peerManagement.Remove(sourceAddr))
    207                             GuiLogging("REMOVED peer " + sourceAddr + " because it had sent an unregister message", NotificationLevel.Info);
    208                         else
    209                             GuiLogging("ALREADY REMOVED or had not registered anytime. ID " + sourceAddr, NotificationLevel.Info);
    210                         break;
    211                     case PubSubMessageType.Alive:
    212                     case PubSubMessageType.Pong:
    213                         if (this.peerManagement.Update(sourceAddr))
    214                         {
    215                             GuiLogging("RECEIVED: " + msgType.ToString() + " Message from " + sourceAddr, NotificationLevel.Debug);
    216                         }
    217                         else
    218                         {
    219                             GuiLogging("UPDATE FAILED for " + sourceAddr + " because it hasn't registered first. " + msgType.ToString(), NotificationLevel.Info);
    220                         }
    221                         break;
    222                     case PubSubMessageType.Ping:
    223                         this.p2pControl.SendToPeer(PubSubMessageType.Pong, sourceAddr);
    224                         GuiLogging("REPLIED to a ping message from peer " + sourceAddr, NotificationLevel.Debug);
    225                         break;
    226                     case PubSubMessageType.Solution:
    227                         // Send solution msg to all subscriber peers and delete subList
    228                         Stop(msgType);
    229                         break;
    230                     case PubSubMessageType.Stop: //ignore this case. No subscriber can't command the Publisher to stop!
    231                         break;
    232                     default:
    233                         throw (new NotImplementedException());
    234                 } // end switch
    235                 if (timerWaitingForAliveMsg == null)
    236                     timerWaitingForAliveMsg = new Timer(OnWaitingForAliveMsg, null, this.aliveMessageInterval * 1000,
    237                         this.aliveMessageInterval * 1000);
    238             }
    239             // Received Data aren't PubSubMessageTypes or rather no action-relevant messages
    240             else
    241             {
    242                 GuiLogging("RECEIVED message from non subscribed peer: " + sData.Trim() + ", ID: " + sourceAddr, NotificationLevel.Debug);
     182        protected virtual void p2pControl_OnSystemMessageReceived(PeerId sender, PubSubMessageType msgType)
     183        {
     184            switch (msgType)
     185            {
     186                case PubSubMessageType.Register:
     187                    if (this.peerManagement.Add(sender))
     188                    {
     189                        GuiLogging("REGISTERED: Peer with ID " + sender, NotificationLevel.Info);
     190                        this.p2pControl.SendToPeer(PubSubMessageType.RegisteringAccepted, sender);
     191                    }
     192                    else
     193                    {
     194                        GuiLogging("ALREADY REGISTERED peer with ID " + sender, NotificationLevel.Info);
     195                    }
     196                    break;
     197                case PubSubMessageType.Unregister:
     198                    if (!this.peerManagement.Remove(sender))
     199                        GuiLogging("ALREADY REMOVED or had not registered anytime. ID " + sender, NotificationLevel.Info);
     200                    break;
     201                case PubSubMessageType.Alive:
     202                case PubSubMessageType.Pong:
     203                    break;
     204                case PubSubMessageType.Ping:
     205                    this.p2pControl.SendToPeer(PubSubMessageType.Pong, sender);
     206                    GuiLogging("REPLIED to a ping message from peer " + sender, NotificationLevel.Debug);
     207                    break;
     208                case PubSubMessageType.Solution:
     209                    // Send solution msg to all subscriber peers and delete subList
     210                    Stop(msgType);
     211                    break;
     212                case PubSubMessageType.Stop: //ignore this case. No subscriber can't command the Publisher to stop!
     213                    break;
     214                default:
     215                    throw (new NotImplementedException());
     216            } // end switch
     217            if (timerWaitingForAliveMsg == null)
     218                timerWaitingForAliveMsg = new Timer(OnWaitingForAliveMsg, null, this.aliveMessageInterval * 1000,
     219                    this.aliveMessageInterval * 1000);
     220
     221            if (msgType != PubSubMessageType.Unregister)
     222            {
     223                if(this.peerManagement.Update(sender))
     224                    GuiLogging("UPDATED Peer " + sender + " successfully.", NotificationLevel.Debug);
     225                else
     226                    GuiLogging("UPDATING Peer " + sender + " failed.", NotificationLevel.Debug);
     227            }
     228        }
     229
     230        protected virtual void p2pControl_OnPayloadMessageReceived(PeerId sender, byte[] data)
     231        {
     232            GuiLogging("RECEIVED message from non subscribed peer: " + UTF8Encoding.UTF8.GetString(data) + ", ID: " + sender, NotificationLevel.Debug);
     233            // if sender is already registered, update its entry in either case
     234            if (this.peerManagement.Update(sender))
     235            {
     236                GuiLogging("UPDATED Peer " + sender + " successfully.", NotificationLevel.Debug);
    243237            }
    244238        }
  • trunk/CrypPlugins/PeerToPeerPublisher/P2PPublisherSettings.cs

    r862 r1068  
    8787        /* FOR TESTING ISSUES */
    8888
    89         private int sendAliveMessageInterval = 20;
     89        private int sendAliveMessageInterval = 60;
    9090        [TaskPane("Alive Message Interval (in seconds)","In which interval do you wish to receive Alive-Messages from your Subscribers?"
    9191            ,"Subscriber Properties",0,false,DisplayLevel.Beginner,ControlType.NumericUpDown, ValidationType.RangeInteger, 10, 3600)]
  • trunk/CrypPlugins/PeerToPeerPublisher/SubscribersManagement.cs

    r1022 r1068  
    55using System.Collections;
    66
    7 /*
    8  * Everything works fine.
    9  * @CheckVitality: Don't know what is better. Run through two loops and save copying dict or copying dict and save a second loop...
    10  */
    11 
    127namespace Cryptool.Plugins.PeerToPeer
    138{
    14     public class PeerValue
    15     {
    16         public PeerId peerId;
    17         public DateTime dateTime;
    18 
    19         public PeerValue(PeerId pid, DateTime dt)
    20         {
    21             this.peerId = pid;
    22             this.dateTime = dt;
    23         }
    24     }
    25 
    269    public class SubscriberManagement
    2710    {
     
    3619        /// contains all active subscribers
    3720        /// </summary>
    38         private Dictionary<PeerId, PeerValue> checkList;
    39 
    40 
    41         /*TESTING*/
    42         protected HashSet<PeerId> activeSubsList;
    43 
    44 
     21        private Dictionary<PeerId, DateTime> checkList;
    4522        /// <summary>
    4623        /// when a peer is in this list, it will be deleted on the next vitality check
     
    6542        public SubscriberManagement(long expirationTime)
    6643        {
    67             this.activeSubsList = new HashSet<PeerId>();
    68 
    6944            this.dateTimeNow = new DateTime();
    70             this.checkList = new Dictionary<PeerId, PeerValue>();
     45            this.checkList = new Dictionary<PeerId, DateTime>();
    7146            this.secondChanceList = new HashSet<PeerId>();
    7247            this.ExpirationTime = expirationTime;
     
    8661                lock (this.checkList)
    8762                {
    88                     this.checkList.Add(subscriberId, new PeerValue(subscriberId, this.dateTimeNow));
    89                     this.activeSubsList.Add(subscriberId);
     63                    this.checkList.Add(subscriberId, this.dateTimeNow);
    9064                }
    9165                return true;
     
    10680            if (this.checkList.ContainsKey(subscriberId))
    10781            {
    108                 this.checkList[subscriberId].dateTime = this.dateTimeNow;
     82                this.checkList[subscriberId] = this.dateTimeNow;
    10983                // remove subscriber from this list, because it's updated now and hence alive!
    11084                if (this.secondChanceList.Contains(subscriberId))
     
    137111                    result = true;
    138112                }
    139                 if (this.activeSubsList.Contains(subId))
    140                 {
    141                     this.activeSubsList.Remove(subId);
    142                     result = true;
    143                 }
    144113
    145114                if (result && OnSubscriberRemoved != null)
     
    148117            return result;
    149118        }
    150 
    151         /* see alternative method */
    152119
    153120        /// <summary>
     
    164131            lock (this.checkList)
    165132            {
    166                 foreach (KeyValuePair<PeerId, PeerValue> entry in this.checkList)
     133                foreach (KeyValuePair<PeerId, DateTime> entry in this.checkList)
    167134                {
    168                     DateTime valueWithExpirationTime = entry.Value.dateTime.AddMilliseconds(ExpirationTime);
     135                    DateTime valueWithExpirationTime = entry.Value.AddMilliseconds(ExpirationTime);
    169136
    170137                    // if time is expired AND the ID is already in the secondChanceList --> Add to remove list
    171138                    if (this.dateTimeNow > valueWithExpirationTime && secondChanceList.Contains(entry.Key))
    172139                    {
    173                         removeSubscribersFromDict.Add(entry.Value.peerId);
     140                        removeSubscribersFromDict.Add(entry.Key);
    174141                    }
    175142                    else if (this.dateTimeNow > valueWithExpirationTime) //otherwise give a second chance
     
    189156        }
    190157
    191         /* alternative method, works with a copy of checkList, saves a foreach-loop, but produces load for copying */
    192         /*
    193         /// <summary>
    194         /// Removes all Subscribers which are long-rated outdated (expiration time is considered).
    195         /// The recently outdated subscribers will be added to the returned second chance list.
    196         /// </summary>
    197         /// <returns>all recently outdated subscribers</returns>
    198         public List<PeerId> CheckVitality()
    199         {
    200             this.dateTimeNow = DateTime.Now;
    201 
    202             lock (this.checkList)
    203             {
    204                 //don't know what is better. Run through two loops and save copying dict or copying dict and save a second loop...
    205                 Dictionary<string, PeerValue> checkListCopy = this.checkList;
    206                 foreach (KeyValuePair<string, PeerValue> entry in this.checkListCopy)
    207                 {
    208                     DateTime valueWithExpirationTime = entry.Value.dateTime.AddMilliseconds(ExpirationTime);
    209 
    210                     // if time is expired AND the ID is already in the secondChanceList --> Add to remove list
    211                     if (this.dateTimeNow > valueWithExpirationTime && secondChanceList.ContainsKey(entry.Key))
    212                     {
    213                         this.secondChanceList.Remove(removeSub.stringId);
    214                         // remove entry from ORIGINAL dictionary!
    215                         this.checkList.Remove(removeSub.stringId);
    216                         this.activeSubsList.Remove(removeSub.stringId);
    217 
    218                         if (OnSubscriberRemoved != null)
    219                             OnSubscriberRemoved(removeSub);
    220                     }
    221                     else if (this.dateTimeNow > valueWithExpirationTime) //otherwise give a second chance
    222                     {
    223                         this.secondChanceList.Add(entry.Key, entry.Value.peerId);
    224                     }
    225                 }
    226             } //end lock(this.checkList)
    227 
    228             return this.secondChanceList.Values.ToList<PeerId>();
    229         }
    230         */
    231 
    232158        public List<PeerId> GetAllSubscribers()
    233159        {
    234             return this.activeSubsList.ToList<PeerId>();
     160            return this.checkList.Keys.ToList<PeerId>();
    235161        }
    236162
     
    241167            this.secondChanceList.Clear();
    242168            this.secondChanceList = null;
    243             this.activeSubsList.Clear();
    244             this.activeSubsList = null;
    245169        }
    246170    }
Note: See TracChangeset for help on using the changeset viewer.