Ignore:
Timestamp:
Dec 8, 2009, 3:31:08 PM (12 years ago)
Author:
arnold
Message:

Beta Version of Distributed KeySearcher with P2PManager und P2PWorker.

Location:
trunk/CrypPlugins/PeerToPeerSubscriber
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/PeerToPeerSubscriber/P2PSubscriber.cs

    r872 r971  
    124124                if (this.p2pSubscriber != null)
    125125                {
    126                     this.p2pSubscriber.Stop();
     126                    this.p2pSubscriber.Stop(PubSubMessageType.Unregister);
    127127                    GuiLogMessage("Subscriber unregistered from Publisher!", NotificationLevel.Info);
    128128                }
     
    141141                if (this.p2pSubscriber != null)
    142142                {
    143                     this.p2pSubscriber.SolutionFound();
     143                    this.p2pSubscriber.SolutionFound("");
    144144                    GuiLogMessage("Solution found message sent to Publisher.", NotificationLevel.Info);
    145145                }
     
    185185        {
    186186            if(this.p2pSubscriber != null)
    187                 this.p2pSubscriber.Stop();
     187                this.p2pSubscriber.Stop(PubSubMessageType.Stop);
    188188        }
    189189
  • trunk/CrypPlugins/PeerToPeerSubscriber/P2PSubscriberBase.cs

    r928 r971  
    2323        public delegate void TextArrivedFromPublisher(string sData, PeerId pid);
    2424        public event TextArrivedFromPublisher OnTextArrivedFromPublisher;
     25        public delegate void ReceivedStopFromPublisher(PubSubMessageType stopType, string sData);
     26        /// <summary>
     27        /// fired when Manager sent "stop" message to the worker.
     28        /// </summary>
     29        public event ReceivedStopFromPublisher OnReceivedStopMessageFromPublisher;
    2530
    2631        #region Variables
     
    8590        /* BEGIN: Only for experimental cases */
    8691
    87         public void SolutionFound()
     92        public void SolutionFound(string sSolutionData)
    8893        {
    8994            SendMessage(actualPublisher, PubSubMessageType.Solution);
     95            this.p2pControl.SendToPeer(sSolutionData, actualPublisher.byteId);
    9096        }
    9197        /* END: Only for experimental cases */
     
    129135            if (sourceAddr.stringId != actualPublisher.stringId)
    130136            {
    131                 OnGuiMessage("RECEIVED message from third party peer (not the publisher!): " + sData.Trim() + ", ID: " + sourceAddr.stringId, NotificationLevel.Info);
     137                GuiLogging("RECEIVED message from third party peer (not the publisher!): " + sData.Trim() + ", ID: " + sourceAddr.stringId, NotificationLevel.Info);
    132138                return;
    133139            }
     
    138144            {
    139145                case PubSubMessageType.RegisteringAccepted:
    140                     OnGuiMessage("REGISTERING ACCEPTED received from publisher!", NotificationLevel.Info);
     146                    GuiLogging("REGISTERING ACCEPTED received from publisher!", NotificationLevel.Info);
    141147                    if (this.timeoutForPublishersRegAccept != null)
    142148                    {
     
    147153                case PubSubMessageType.Ping:
    148154                    SendMessage(sourceAddr, PubSubMessageType.Pong);
    149                     OnGuiMessage("REPLIED to a ping message from " + sourceAddr, NotificationLevel.Info);
     155                    GuiLogging("REPLIED to a ping message from " + sourceAddr, NotificationLevel.Info);
    150156                    break;
    151157                case PubSubMessageType.Register:
    152158                case PubSubMessageType.Unregister:
    153                     OnGuiMessage(msgType.ToString().ToUpper() + " received from PUBLISHER.", NotificationLevel.Warning);
     159                    GuiLogging(msgType.ToString().ToUpper() + " received from PUBLISHER.", NotificationLevel.Warning);
    154160                    // continuously try to get a unregister and than re-register with publisher
    155                     Stop();
     161                    Stop(msgType);
    156162                    Register(this.sTopic,this.checkPublishersAvailability, this.publisherReplyTimespan);
    157163                    break;
    158164                case PubSubMessageType.Solution:
    159                     Stop();
    160                     OnGuiMessage("Another Subscriber had found the solution!", NotificationLevel.Info);
     165                    Stop(msgType);
     166                    GuiLogging("Another Subscriber had found the solution!", NotificationLevel.Info);
    161167                    break;
    162168                case PubSubMessageType.Stop:
    163                     Stop();
    164                     OnGuiMessage("STOP received from publisher. Subscriber is stopped!", NotificationLevel.Warning);
     169                    Stop(msgType);
     170                    GuiLogging("STOP received from publisher. Subscriber is stopped!", NotificationLevel.Warning);
    165171                    break;
    166172                case PubSubMessageType.Pong:
     
    172178                    break;
    173179                // if the received Data couldn't be casted to enum,
    174                 // it must be text-data
     180                // it must be some data
    175181                case PubSubMessageType.NULL:
    176                     OnGuiMessage("RECEIVED: Message from '" + sourceAddr.stringId
    177                     + "' with data: '" + sData + "'", NotificationLevel.Info);
    178 
    179                     if (OnTextArrivedFromPublisher != null)
    180                         OnTextArrivedFromPublisher(sData, sourceAddr);
     182                    // functionality swapped for better inheritance
     183                    HandleIncomingData(sourceAddr, sData);
    181184
    182185                    break;
     
    186189                    break;
    187190            }
     191        }
     192
     193        /// <summary>
     194        /// Incoming data will be printed in the information field and the OnTextArrivedEvent will be thrown
     195        /// </summary>
     196        /// <param name="senderId"></param>
     197        /// <param name="sData"></param>
     198        protected virtual void HandleIncomingData(PeerId senderId, string sData)
     199        {
     200            GuiLogging("RECEIVED: Message from '" + senderId.stringId
     201                    + "' with data: '" + sData + "'", NotificationLevel.Info);
     202
     203            if (OnTextArrivedFromPublisher != null)
     204                OnTextArrivedFromPublisher(sData, senderId);
    188205        }
    189206
     
    209226                    break;
    210227                case PubSubMessageType.Solution:
    211                     Stop();
     228                    // when i send Solution to the Stop method, we will run into a recursive loop between SendMessage and Stop!
     229                    Stop(PubSubMessageType.NULL);
    212230                    break;
    213231                default:
    214                     OnGuiMessage("No Message sent, because MessageType wasn't supported: " + msgType.ToString(), NotificationLevel.Warning);
     232                    GuiLogging("No Message sent, because MessageType wasn't supported: " + msgType.ToString(), NotificationLevel.Warning);
    215233                    return;
    216234            }
     
    219237            // don't show every single alive message
    220238            if (msgType != PubSubMessageType.Alive)
    221                 OnGuiMessage(msgType.ToString() + " message sent to Publisher", NotificationLevel.Info);
     239                GuiLogging(msgType.ToString() + " message sent to Publisher", NotificationLevel.Info);
    222240        }
    223241
     
    232250                SendMessage(pubId, PubSubMessageType.Register);
    233251            else
    234                 OnGuiMessage("No publisher for registering found.", NotificationLevel.Info);
     252                GuiLogging("No publisher for registering found.", NotificationLevel.Info);
    235253        }
    236254
     
    258276            if (byteISettings == null)
    259277            {
    260                 OnGuiMessage("Can't find settings from Publisher for the Subscriber.", NotificationLevel.Error);
     278                GuiLogging("Can't find settings from Publisher for the Subscriber.", NotificationLevel.Error);
    261279                return null;
    262280            }
     
    264282
    265283            string sPubId = this.p2pControl.ConvertIdToString(bytePubId);
    266             OnGuiMessage("RECEIVED: Publishers' peer name '" + sPubId + "', Alive-Msg-Interval: " + sendAliveMessageInterval / 1000 + " sec!", NotificationLevel.Info);
     284            GuiLogging("RECEIVED: Publishers' peer name '" + sPubId + "', Alive-Msg-Interval: " + sendAliveMessageInterval / 1000 + " sec!", NotificationLevel.Info);
    267285
    268286            pid = new PeerId(sPubId, bytePubId);
     
    289307            if (newPubId == null)
    290308            {
    291                 OnGuiMessage("Publisher wasn't found in DHT or settings didn't stored on the right way.", NotificationLevel.Warning);
     309                GuiLogging("Publisher wasn't found in DHT or settings didn't stored on the right way.", NotificationLevel.Warning);
    292310                return;
    293311            }
     
    295313            {
    296314                //Handle case, when publisher changed or isn't active at present (don't reply on response)
    297                 OnGuiMessage("CHANGED: Publisher from '" + actualPublisher.stringId
     315                GuiLogging("CHANGED: Publisher from '" + actualPublisher.stringId
    298316                    + "' to '" + newPubId.stringId + "'!", NotificationLevel.Info);
    299317                actualPublisher = newPubId;
     
    319337        private void OnTimeoutRegisteringAccepted(object state)
    320338        {
    321             OnGuiMessage("TIMEOUT: Waiting for registering accepted message from publisher!", NotificationLevel.Warning);
     339            GuiLogging("TIMEOUT: Waiting for registering accepted message from publisher!", NotificationLevel.Warning);
    322340            // TODO: anything
    323341        }
     
    329347        private void OnTimeoutPublishersPong(object state)
    330348        {
    331             OnGuiMessage("Publisher didn't answer on Ping in the given time span!", NotificationLevel.Warning);
     349            GuiLogging("Publisher didn't answer on Ping in the given time span!", NotificationLevel.Warning);
    332350            timeoutForPublishersPong.Dispose();
    333351            timeoutForPublishersPong = null;
     
    341359        /// unregister message will be send to the publisher
    342360        /// </summary>
    343         public void Stop()
     361        public void Stop(PubSubMessageType msgType)
    344362        {
    345363            this.bolStopped = true;
    346             if (actualPublisher != null)
    347                 SendMessage(actualPublisher, PubSubMessageType.Unregister);
     364            if (actualPublisher != null && msgType != PubSubMessageType.NULL)
     365                SendMessage(actualPublisher, msgType);
    348366
    349367            #region stopping all timers, if they are still active
     
    377395            this.Started = false;
    378396        }
     397
     398        protected void GuiLogging(string sText, NotificationLevel notLev)
     399        {
     400            if (OnGuiMessage != null)
     401                OnGuiMessage(sText, notLev);
     402        }
    379403    }
    380404}
Note: See TracChangeset for help on using the changeset viewer.