source: trunk/CrypP2P/P2PManager.cs @ 2438

Last change on this file since 2438 was 2348, checked in by kopal, 11 years ago
  • removed old certificate stuff
  • p2p editor now checks if certificate exists and can be loaded BEFORE trying to connect to network
  • p2p password is now encrypted using the current windows user
  • some design changes of p2p editor
File size: 9.8 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.Helper;
19using Cryptool.P2P.Internal;
20using Cryptool.PluginBase;
21using Cryptool.PluginBase.Miscellaneous;
22using PeersAtPlay.P2PStorage.DHT;
23using PeersAtPlay.Util.Threading;
24
25namespace Cryptool.P2P
26{
27    public sealed class P2PManager
28    {
29        #region Singleton
30
31        public static readonly P2PManager Instance = new P2PManager();
32
33        private P2PManager()
34        {
35            P2PBase = new P2PBase();
36            ConnectionManager = new ConnectionManager(P2PBase);
37
38            SettingsHelper.ValidateSettings();
39        }
40
41        #endregion
42
43        #region Variables
44
45        public static ConnectionManager ConnectionManager { get; private set; }
46        public static P2PBase P2PBase { get; private set; }
47        public static bool IsAutoconnectConsoleOptionSet { get; set; }
48
49        #endregion
50
51        #region Events
52
53        public static event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
54
55        #endregion Events
56
57        #region CrypWin helper methods
58
59        public static void HandleConnectOnStartup()
60        {
61            var isAutoconnectConfiguredOrRequested = P2PSettings.Default.ConnectOnStartup || IsAutoconnectConsoleOptionSet;
62            var isReadyToConnect = ConnectionManager.IsReadyToConnect();
63
64            if (isAutoconnectConfiguredOrRequested && isReadyToConnect)
65            {
66                GuiLogMessage("Connect on startup enabled. Establishing connection...", NotificationLevel.Info);
67                ConnectionManager.Connect();
68            }
69        }
70
71        public static void HandleDisconnectOnShutdown()
72        {
73            Disconnect();
74        }
75
76        #endregion
77
78        #region Framework methods
79
80        public static void GuiLogMessage(string message, NotificationLevel logLevel)
81        {
82            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, null, new GuiLogEventArgs(message, null, logLevel));
83        }
84
85        #endregion Framework methods
86
87        #region DHT operations (blocking)
88
89        /// <summary>
90        /// Stores the given data in the DHT. This method will block until a response has been received.
91        ///
92        /// The underlying DHT is versionend. Store attempts will fail, if the latest version has not been retrieved before.
93        /// </summary>
94        /// <param name="key">key to write</param>
95        /// <param name="data">data to write</param>
96        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
97        /// <returns>true if the store attempt was successful, false otherwise</returns>
98        public static RequestResult Store(string key, byte[] data)
99        {
100            if (!IsConnected)
101                throw new NotConnectedException();
102
103            return P2PBase.SynchStore(key, data);
104        }
105
106        /// <summary>
107        /// Stores the given data in the DHT. This method will block until a response has been received.
108        ///
109        /// The underlying DHT is versionend. Store attempts will fail, if the latest version has not been retrieved before.
110        /// </summary>
111        /// <param name="key">key to write</param>
112        /// <param name="data">data to write</param>
113        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
114        /// <returns>true if the store attempt was successful, false otherwise</returns>
115        public static RequestResult Store(string key, string data)
116        {
117            if (!IsConnected)
118                throw new NotConnectedException();
119           
120            return P2PBase.SynchStore(key, data);
121        }
122
123        /// <summary>
124        /// Retrieves the latest version of a given in key from the DHT. This method will block until a response has been received.
125        /// </summary>
126        /// <param name="key">key to retrieve</param>
127        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
128        /// <returns>byte array containing the data</returns>
129        public static RequestResult Retrieve(string key)
130        {
131            if (!IsConnected)
132                throw new NotConnectedException();
133           
134            return P2PBase.SynchRetrieve(key);
135        }
136
137        /// <summary>
138        /// Removes a key and its data from the DHT. This method will block until a response has been received.
139        ///
140        /// The underlying DHT is versionend. Remove attempts will fail, if the latest version has not been retrieved before.
141        /// </summary>
142        /// <param name="key">key to remove</param>
143        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
144        /// <returns>bool determining wether the attempt was successful</returns>
145        public static RequestResult Remove(string key)
146        {
147            if (!IsConnected)
148                throw new NotConnectedException();
149
150            return P2PBase.SynchRemove(key);
151        }
152
153        #endregion DHT operations (blocking)
154
155        #region DHT operations (non-blocking)
156
157        /// <summary>
158        /// Stores the given data in the DHT.
159        ///
160        /// The underlying DHT is versionend. Store attempts will fail, if the latest version has not been retrieved before.
161        /// </summary>
162        /// <param name="callback">Callback for asynchronous call</param>
163        /// <param name="key">key to write</param>
164        /// <param name="data">data to write</param>
165        /// <param name="asyncState">Arbitrary data, which will be included in the callback parameter</param>
166        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
167        /// <returns>Guid identifying the request</returns>
168        public static Guid Store(AsyncCallback<StoreResult> callback, string key, byte[] data, object asyncState)
169        {
170            if (!IsConnected)
171                throw new NotConnectedException();
172
173            return P2PBase.VersionedDht.Store(callback, key, data, asyncState);
174        }
175
176        /// <summary>
177        /// Retrieves the latest version of a given in key from the DHT.
178        /// </summary>
179        /// <param name="callback">Callback for asynchronous call</param>
180        /// <param name="key">key to retrieve</param>
181        /// <param name="asyncState">Arbitrary data, which will be included in the callback parameter</param>
182        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
183        /// <returns>Guid identifying the request</returns>
184        public static Guid Retrieve(AsyncCallback<RetrieveResult> callback, string key, object asyncState)
185        {
186            if (!IsConnected)
187                throw new NotConnectedException();
188
189            return P2PBase.Dht.Retrieve(callback, key, asyncState);
190        }
191
192        /// <summary>
193        /// Removes a key and its data from the DHT.
194        ///
195        /// The underlying DHT is versionend. Remove attempts will fail, if the latest version has not been retrieved before.
196        /// </summary>
197        /// <param name="callback">Callback for asynchronous call</param>
198        /// <param name="key">key to remove</param>
199        /// <param name="asyncState">Arbitrary data, which will be included in the callback parameter</param>
200        /// <exception cref="NotConnectedException">Will be thrown if the P2P system is not connected</exception>
201        /// <returns>Guid identifying the request</returns>
202        public static Guid Remove(AsyncCallback<RemoveResult> callback, string key, object asyncState)
203        {
204            if (!IsConnected)
205                throw new NotConnectedException();
206
207            return P2PBase.VersionedDht.Remove(callback, key, asyncState);
208        }
209
210        #endregion DHT operations (non-blocking)
211
212        #region Connection methods
213
214        /// <summary>
215        /// Connect to the peer-to-peer network.
216        /// <see cref="ConnectionManager">OnP2PConnectionStateChangeOccurred will be fired when the connection state changes.</see>
217        /// </summary>
218        public static void Connect()
219        {
220            ConnectionManager.Connect();
221        }
222
223        /// <summary>
224        /// Disconnect from the peer-to-peer network.
225        /// <see cref="ConnectionManager">OnP2PConnectionStateChangeOccurred will be fired when the connection state changes.</see>
226        /// </summary>
227        public static void Disconnect()
228        {
229            ConnectionManager.Disconnect();
230        }
231
232        /// <summary>
233        /// Boolean which indicates, if the peer-to-peer network is currently connected and not in a connect/disconnect attempt.
234        /// </summary>
235        public static bool IsConnected
236        {
237            get { return P2PBase.IsConnected && !IsConnecting; }
238        }
239
240        /// <summary>
241        /// Boolean which indicates, if the peer-to-peer network is currently in a connect/disconnect attempt.
242        /// </summary>
243        public static bool IsConnecting
244        {
245            get { return ConnectionManager.IsConnecting; }
246        }
247
248        #endregion
249    }
250}
Note: See TracBrowser for help on using the repository browser.