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

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

o Refactoring in CrypP2P
o AES: NullReference in AES-Plugin fixed
o Small fixes in KeySearcher, StorageKeyGenerator uses IV and truncates input data

File size: 3.6 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 Cryptool.P2P.Worker;
19using Cryptool.PluginBase;
20
21namespace Cryptool.P2P.Internal
22{
23    public class ConnectionManager
24    {
25        #region Delegates
26
27        public delegate void P2PConnectionStateChangeEventHandler(object sender, bool newState);
28
29        #endregion
30
31        private readonly object connectLock = new object();
32        private readonly P2PBase p2PBase;
33
34        public ConnectionManager(P2PBase p2PBase)
35        {
36            this.p2PBase = p2PBase;
37        }
38
39        public bool IsConnecting { get; internal set; }
40        public event P2PConnectionStateChangeEventHandler OnP2PConnectionStateChangeOccurred;
41
42        public void Connect()
43        {
44            lock (connectLock)
45            {
46                if (p2PBase.IsConnected || IsConnecting)
47                {
48                    P2PManager.GuiLogMessage("Cannot connect, already connected or connecting.",
49                                             NotificationLevel.Warning);
50                    return;
51                }
52
53                if (!IsReadyToConnect())
54                {
55                    P2PManager.GuiLogMessage("Cannot connect, configuration is broken.", NotificationLevel.Warning);
56                    return;
57                }
58
59                IsConnecting = true;
60            }
61
62            P2PManager.GuiLogMessage("Dispatching connect request with ConnectionWorker.", NotificationLevel.Debug);
63            new ConnectionWorker(p2PBase, this).Start();
64        }
65
66        public void Disconnect()
67        {
68            lock (connectLock)
69            {
70                if (!p2PBase.IsConnected || IsConnecting)
71                {
72                    P2PManager.GuiLogMessage("Cannot disconnect, no connection or connection attempt active.",
73                                             NotificationLevel.Warning);
74                    return;
75                }
76
77                IsConnecting = true;
78            }
79
80            P2PManager.GuiLogMessage("Dispatching disconnect request with ConnectionWorker.", NotificationLevel.Debug);
81            new ConnectionWorker(p2PBase, this).Start();
82        }
83
84        public bool IsReadyToConnect()
85        {
86            if (String.IsNullOrEmpty(P2PSettings.Default.PeerName))
87            {
88                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: username missing.", NotificationLevel.Error);
89                return false;
90            }
91
92            if (String.IsNullOrEmpty(P2PSettings.Default.PeerName))
93            {
94                P2PManager.GuiLogMessage("Peer-to-peer not fully configured: world name missing.",
95                                         NotificationLevel.Error);
96                return false;
97            }
98
99            return true;
100        }
101
102        public void FireConnectionStatusChange()
103        {
104            if (OnP2PConnectionStateChangeOccurred != null)
105            {
106                OnP2PConnectionStateChangeOccurred(this, p2PBase.IsConnected);
107            }
108        }
109    }
110}
Note: See TracBrowser for help on using the repository browser.