source: trunk/CrypP2P/Internal/ConnectionManager.cs @ 2540

Last change on this file since 2540 was 2540, checked in by kopal, 11 years ago

added "Remember my password" button. So the user can decide if he wants to save his password on the machine or not

File size: 5.2 KB
Line 
1/*
2   Copyright 2010 Paul Lelgemann, University of Duisburg-Essen
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Threading;
19using System.Windows.Threading;
20using Cryptool.P2P.Worker;
21using Cryptool.PluginBase;
22using PeersAtPlay.CertificateLibrary.Certificates;
23using System.IO;
24
25namespace Cryptool.P2P.Internal
26{
27    public class ConnectionManager
28    {
29        #region Delegates
30
31        public delegate void P2PConnectionStateChangeEventHandler(object sender, bool newState);
32        public delegate void P2PTryConnectingStateChangeEventHandler(object sender, bool newState);
33
34        #endregion
35
36        #region Events
37
38        public event P2PConnectionStateChangeEventHandler OnP2PConnectionStateChangeOccurred;
39        public event P2PTryConnectingStateChangeEventHandler OnP2PTryConnectingStateChangeOccurred;
40
41        #endregion
42
43        private readonly object connectLock = new object();
44        private readonly P2PBase p2PBase;
45        private DateTime lastConnectionAttempt;
46
47        public ConnectionManager(P2PBase p2PBase)
48        {
49            this.p2PBase = p2PBase;
50        }
51
52        public bool Disconnected { get; private set; }
53
54        private bool isConnecting;
55        public bool IsConnecting { 
56            get { return isConnecting; } 
57            internal set
58            {
59                isConnecting = value;
60                if (OnP2PTryConnectingStateChangeOccurred != null)
61                {
62                    OnP2PTryConnectingStateChangeOccurred(this, isConnecting);
63                }
64            }
65        }
66       
67        public void Connect()
68        {
69            lock (connectLock)
70            {
71                Disconnected = false;
72                lastConnectionAttempt = DateTime.Now;
73               
74                if (p2PBase.IsConnected || IsConnecting)
75                {
76                    P2PManager.GuiLogMessage("Cannot connect, already connected or connecting.",
77                                             NotificationLevel.Warning);
78                    return;
79                }
80
81                if (!IsReadyToConnect())
82                {
83                    P2PManager.GuiLogMessage("Cannot connect, configuration is broken.", NotificationLevel.Warning);
84                    return;
85                }
86
87                IsConnecting = true;
88            }
89
90            P2PManager.GuiLogMessage("Dispatching connect request with ConnectionWorker.", NotificationLevel.Debug);
91            new ConnectionWorker(p2PBase, this).Start();
92        }
93
94        public void Disconnect()
95        {
96            lock (connectLock)
97            {
98                Disconnected = true;
99                if (!p2PBase.IsConnected || IsConnecting)
100                {
101                    P2PManager.GuiLogMessage("Cannot disconnect, no connection or connection attempt active.",
102                                             NotificationLevel.Warning);
103                    return;
104                }
105
106                IsConnecting = true;
107            }
108
109            P2PManager.GuiLogMessage("Dispatching disconnect request with ConnectionWorker.", NotificationLevel.Debug);
110            new ConnectionWorker(p2PBase, this).Start();
111        }
112
113        public bool IsReadyToConnect()
114        {
115            if (String.IsNullOrEmpty(P2PSettings.Default.PeerName))
116            {
117                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: username missing.", NotificationLevel.Info);
118                return false;
119            }
120
121            string password = null;
122            if (P2PSettings.Default.RememberPassword)
123            {
124                password = P2PBase.DecryptString(P2PSettings.Default.Password);
125            }
126            else
127            {
128                password = P2PBase.DecryptString(P2PBase.Password);
129            }
130
131            if (String.IsNullOrEmpty(password))
132            {
133                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: password missing.",
134                                         NotificationLevel.Info);
135                return false;
136            }
137
138            if (String.IsNullOrEmpty(P2PSettings.Default.WorldName))
139            {
140                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: world name missing.",
141                                         NotificationLevel.Info);
142                return false;
143            }
144
145            return true;
146        }
147
148        public void FireConnectionStatusChange()
149        {
150            if (OnP2PConnectionStateChangeOccurred != null)
151            {
152                OnP2PConnectionStateChangeOccurred(this, p2PBase.IsConnected);
153            }
154        }
155    }
156}
Note: See TracBrowser for help on using the repository browser.