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

Last change on this file since 2438 was 2435, checked in by Sven Rech, 11 years ago

completely new p2p reconnection (now inside CrypWin)

File size: 4.9 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            if (String.IsNullOrEmpty(P2PSettings.Default.Password))
122            {
123                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: password missing.",
124                                         NotificationLevel.Info);
125                return false;
126            }
127
128            if (String.IsNullOrEmpty(P2PSettings.Default.WorldName))
129            {
130                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: world name missing.",
131                                         NotificationLevel.Info);
132                return false;
133            }
134
135            return true;
136        }
137
138        public void FireConnectionStatusChange()
139        {
140            if (OnP2PConnectionStateChangeOccurred != null)
141            {
142                OnP2PConnectionStateChangeOccurred(this, p2PBase.IsConnected);
143            }
144        }
145    }
146}
Note: See TracBrowser for help on using the repository browser.