source: trunk/CrypPlugins/PeerToPeerSubscriber/P2PSubscriber.cs @ 2280

Last change on this file since 2280 was 1433, checked in by Paul Lelgemann, 12 years ago

o Extracted common classes from PeerToPeerBase plugin into new PeerToPeer plugin as a preparation for the new P2P proxy
o Modified directory properties to ignore the CrypBuild directory

File size: 9.1 KB
Line 
1/* Copyright 2009 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
2
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6
7       http://www.apache.org/licenses/LICENSE-2.0
8
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14*/
15
16using System;
17using System.Collections.Generic;
18using System.Linq;
19using System.Text;
20using System.Threading;
21using Cryptool.PluginBase;
22using Cryptool.PluginBase.IO;
23using Cryptool.PluginBase.Control;
24using Cryptool.PluginBase.Miscellaneous;
25using System.ComponentModel;
26using Cryptool.Plugins.PeerToPeer.Internal;
27
28namespace Cryptool.Plugins.PeerToPeer
29{
30    /// <summary>
31    /// This PlugIn only works, when its connected with a P2P_Peer object.
32    /// </summary>
33    [Author("Christian Arnold", "arnold@cryptool.org", "Uni Duisburg-Essen", "http://www.uni-due.de")]
34    [PluginInfo(true, "P2P_Subscriber", "Creates a new Subscribing-Peer", "", "PeerToPeerSubscriber/ct2_p2p_sub_medium.png")]
35    public class P2PSubscriber : IInput
36    {
37        private P2PSubscriberSettings settings;
38        private IP2PControl p2pControl;
39        private P2PSubscriberBase p2pSubscriber;
40
41        #region In and Output
42
43        /// <summary>
44        /// Catches the completely configurated, initialized and joined P2P object from the P2PPeer-Slave-PlugIn.
45        /// </summary>
46        [PropertyInfo(Direction.ControlMaster, "P2P Slave", "Input the P2P-Peer-PlugIn", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
47        public IP2PControl P2PControl
48        {
49            get
50            {
51                return this.p2pControl;
52            }
53            set
54            {
55                if (this.p2pControl != null)
56                {
57                    this.p2pControl.OnStatusChanged -= P2PControl_OnStatusChanged;
58                }
59                if (value != null)
60                {
61                    this.p2pControl = (P2PPeerMaster)value;
62                    this.p2pControl.OnStatusChanged += new IControlStatusChangedEventHandler(P2PControl_OnStatusChanged);
63                    OnPropertyChanged("P2PControl");
64                }
65                else
66                {
67                    this.p2pControl = null;
68                }
69            }
70        }
71
72        private string sOutputvalue;
73        [PropertyInfo(Direction.OutputData, "Data from subscribed Publisher", "When you're subscribed to an alive Publisher, receive published data here", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
74        public string Outputvalue
75        {
76            get
77            {
78                return this.sOutputvalue;
79            }
80            set
81            {
82                if (value != this.sOutputvalue)
83                {
84                    this.sOutputvalue = value;
85                    OnPropertyChanged("Outputvalue");
86                }
87            }
88        }
89
90        #endregion
91
92        private void P2PControl_OnStatusChanged(IControl sender, bool readyForExecution)
93        {
94            //throw new NotImplementedException();
95        }
96
97        #region Events
98
99        public event StatusChangedEventHandler OnPluginStatusChanged;
100
101        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
102
103        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
104
105        #endregion
106
107        #region Standard functionality
108
109        public P2PSubscriber()
110        {
111            this.settings = new P2PSubscriberSettings(this);
112            this.settings.PropertyChanged += new PropertyChangedEventHandler(settings_PropertyChanged);
113            this.settings.TaskPaneAttributeChanged += new TaskPaneAttributeChangedHandler(settings_TaskPaneAttributeChanged);
114        }
115
116        void settings_TaskPaneAttributeChanged(ISettings settings, TaskPaneAttributeChangedEventArgs args)
117        {
118            // throw new NotImplementedException();
119        }
120
121        private void settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
122        {
123            if (e.PropertyName == "BtnUnregister")
124            {
125                if (this.p2pSubscriber != null)
126                {
127                    this.p2pSubscriber.Stop(PubSubMessageType.Unregister);
128                    GuiLogMessage("Subscriber unregistered from Publisher!", NotificationLevel.Info);
129                }
130                else
131                {
132                    GuiLogMessage("Publisher isn't initialized, so this action isn't possible.", NotificationLevel.Info);
133                }
134            }
135            if (e.PropertyName == "BtnRegister")
136            {
137                StartSubscriber();
138                GuiLogMessage("Subscriber registers with Publisher!", NotificationLevel.Info);
139            }
140            if (e.PropertyName == "BtnSolutionFound")
141            {
142                if (this.p2pSubscriber != null)
143                {
144                    this.p2pSubscriber.SolutionFound(new byte[]{0});
145                    GuiLogMessage("Solution found message sent to Publisher.", NotificationLevel.Info);
146                }
147                else
148                {
149                    GuiLogMessage("Publisher isn't initialized, so this action isn't possible.", NotificationLevel.Info);
150                }
151            }
152        }
153
154        public ISettings Settings
155        {
156            set { this.settings = (P2PSubscriberSettings)value; }
157            get { return this.settings; }
158        }
159
160        public System.Windows.Controls.UserControl Presentation
161        {
162            get { return null; }
163        }
164
165        public System.Windows.Controls.UserControl QuickWatchPresentation
166        {
167            get { return null; }
168        }
169
170        //execution method is below this region
171
172        public void PreExecution()
173        {
174        }
175
176        public void PostExecution()
177        {
178            //throw new NotImplementedException();
179        }
180
181        public void Pause()
182        {
183        }
184
185        public void Stop()
186        {
187            if(this.p2pSubscriber != null)
188                this.p2pSubscriber.Stop(PubSubMessageType.Stop);
189        }
190
191        public void Initialize()
192        {
193        }
194
195        public void Dispose()
196        {
197        }
198        #endregion
199
200        public void Execute()
201        {
202            // if no P2P Slave PlugIn is connected with this PlugIn --> No execution!
203            if (P2PControl == null)
204            {
205                GuiLogMessage("No P2P_Peer connected with this PlugIn!", NotificationLevel.Error);
206                return;
207            }
208            if (this.settings.TopicName != null)
209            {
210                StartSubscriber();
211            }
212            else
213            {
214                GuiLogMessage("The settings are empty. Operation isn't possible.", NotificationLevel.Error);
215            }
216        }
217
218        private void StartSubscriber()
219        {
220            if (this.p2pSubscriber == null)
221            {
222                this.p2pSubscriber = new P2PSubscriberBase(this.P2PControl);
223                this.p2pSubscriber.OnGuiMessage += new P2PSubscriberBase.GuiMessage(p2pSubscriber_OnGuiMessage);
224                this.p2pSubscriber.OnTextArrivedFromPublisher += new P2PSubscriberBase.TextArrivedFromPublisher(p2pSubscriber_OnTextArrivedFromPublisher);
225            }
226            this.p2pSubscriber.Start(this.settings.TopicName, (long)(this.settings.CheckPublishersAvailability * 1000),
227                    (long)(this.settings.PublishersReplyTimespan * 1000));
228        }
229
230        void p2pSubscriber_OnTextArrivedFromPublisher(byte[] data, PeerId pid)
231        {
232            this.Outputvalue = UTF8Encoding.UTF8.GetString(data);
233        }
234
235        void p2pSubscriber_OnGuiMessage(string sData, NotificationLevel notificationLevel)
236        {
237            GuiLogMessage(sData, notificationLevel);
238        }
239
240        #region INotifyPropertyChanged Members
241
242        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
243
244        public void OnPropertyChanged(string name)
245        {
246            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
247        }
248
249        public event PluginProgressChangedEventHandler OnPluginProcessChanged;
250
251        private void ProgressChanged(double value, double max)
252        {
253            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
254        }
255
256        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
257        {
258            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
259        }
260
261        #endregion
262    }
263}
Note: See TracBrowser for help on using the repository browser.