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

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

o CrypP2P: Code cleanup
o P2PEditor: job list will be reloaded in a BackgroundWorker

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