source: trunk/CrypPlugins/KeySearcher/P2P/Helper/ConnectionHelper.cs @ 1707

Last change on this file since 1707 was 1707, checked in by Paul Lelgemann, 11 years ago

o KeySearcher: more precise statistics in the new visualization of distributed search

File size: 1.9 KB
Line 
1using System.Threading;
2using Cryptool.P2P;
3using Cryptool.PluginBase;
4
5namespace KeySearcher.P2P.Helper
6{
7    public class ConnectionHelper
8    {
9        private readonly KeySearcher keySearcher;
10        private readonly KeySearcherSettings settings;
11        private AutoResetEvent connectResetEvent;
12
13        public ConnectionHelper(KeySearcher keySearcher, KeySearcherSettings settings)
14        {
15            this.keySearcher = keySearcher;
16            this.settings = settings;
17        }
18
19        public void ValidateConnectionToPeerToPeerSystem()
20        {
21            if (P2PManager.IsConnected)
22            {
23                return;
24            }
25
26            if (settings.AutoconnectPeerToPeer)
27            {
28                HandleAutoconnect();
29            }
30            else
31            {
32                keySearcher.GuiLogMessage("P2P network not connected and autoconnect disabled. Cannot compute job.",
33                                          NotificationLevel.Error);
34            }
35        }
36
37        private void HandleAutoconnect()
38        {
39            P2PManager.ConnectionManager.OnP2PConnectionStateChangeOccurred += HandleConnectionStateChange;
40            connectResetEvent = new AutoResetEvent(false);
41
42            P2PManager.Connect();
43
44            connectResetEvent.WaitOne();
45
46            if (P2PManager.IsConnected)
47            {
48                keySearcher.GuiLogMessage("P2P network was connected due to plugin setting.",
49                                          NotificationLevel.Info);
50            }
51            else
52            {
53                keySearcher.GuiLogMessage("P2P network could not be connected.",
54                                          NotificationLevel.Error);
55            }
56        }
57
58        void HandleConnectionStateChange(object sender, bool newState)
59        {
60            connectResetEvent.Set();
61        }
62    }
63}
Note: See TracBrowser for help on using the repository browser.