source: trunk/CrypP2P/P2PManager.cs @ 1537

Last change on this file since 1537 was 1537, checked in by Paul Lelgemann, 12 years ago

o (probably) fixed bug with a locked P2P instance when storing large values
+ CrypP2P: Added event for connection state changes

File size: 6.1 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.Windows.Forms;
19using Cryptool.P2P.Internal;
20using Cryptool.P2P.Worker;
21using Cryptool.PluginBase;
22using Cryptool.PluginBase.Miscellaneous;
23using Cryptool.Plugins.PeerToPeer.Internal;
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            P2PSettings = new P2PSettings();
37            P2PBase.AllowLoggingToMonitor = P2PSettings.Log2Monitor;
38
39            // Register events
40
41            // to forward event from overlay/dht MessageReceived-Event from P2PBase
42            P2PBase.OnP2PMessageReceived += p2pBase_OnP2PMessageReceived;
43
44            // Register exit event to terminate P2P connection without loosing data
45            // TODO check if this is correct, should be - but handler is not called (and application does not shut down), probably unrelated to this problem
46            Application.ApplicationExit += HandleDisconnectByApplicationShutdown;
47        }
48
49        #endregion
50
51        # region Constants
52
53        public const string P2PDisconnectImageUri = "images/peer2peer-disconnect.png";
54        public const string P2PConnectImageUri = "images/peer2peer-connect.png";
55
56        # endregion
57
58        #region Variables
59
60        public P2PBase P2PBase { get; set; }
61        public P2PSettings P2PSettings { get; set; }
62        public bool IsP2PConnecting { get; internal set; }
63
64        #endregion
65
66        #region Events
67
68        public static event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
69
70        public delegate void P2PConnectionStateChangeEventHandler(object sender, bool newState);
71        public static event P2PConnectionStateChangeEventHandler OnP2PConnectionStateChangeOccurred;
72
73        // to forward event from overlay/dht MessageReceived-Event from P2PBase
74        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
75
76        #endregion Events
77
78        internal void FireConnectionStatusChange()
79        {
80            if (OnP2PConnectionStateChangeOccurred != null)
81            {
82                OnP2PConnectionStateChangeOccurred(this, P2PConnected());
83            }
84        }
85
86        public bool P2PConnected()
87        {
88            return P2PBase.Started;
89        }
90
91        public string UserInfo()
92        {
93            if (!P2PConnected())
94            {
95                return null;
96            }
97
98            string userName;
99            var userInfo = P2PBase.GetPeerID(out userName);
100            return userInfo + " (" + userName + ")";
101        }
102
103        public void HandleConnectOnStartup()
104        {
105            if (P2PSettings.ConnectOnStartup && IsReadyToConnect())
106            {
107                GuiLogMessage("Connect on startup enabled. Establishing connection...", NotificationLevel.Info);
108                new ConnectionWorker(P2PBase, P2PSettings).Start();
109            }
110        }
111
112        private bool IsReadyToConnect()
113        {
114            if (String.IsNullOrEmpty(P2PSettings.PeerName))
115            {
116                GuiLogMessage("Peer-to-peer not fully configured: username missing.", NotificationLevel.Error);
117                return false;
118            }
119
120            if (String.IsNullOrEmpty(P2PSettings.WorldName))
121            {
122                GuiLogMessage("Peer-to-peer not fully configured: world name missing.", NotificationLevel.Error);
123                return false;
124            }
125
126            return true;
127        }
128
129        public PeerId GetPeerId(out string userName)
130        {
131            return P2PBase.GetPeerID(out userName);
132        }
133
134        // to forward event from overlay/dht MessageReceived-Event from P2PBase
135        private void p2pBase_OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
136        {
137            if (OnPeerMessageReceived != null)
138                OnPeerMessageReceived(sourceAddr, data);
139        }
140
141        #region Framework methods
142
143        private void HandleDisconnectByApplicationShutdown(object sender, EventArgs e)
144        {
145            if (P2PConnected())
146            {
147                new ConnectionWorker(P2PBase, P2PSettings).Start();
148            }
149        }
150
151        public void GuiLogMessage(string message, NotificationLevel logLevel)
152        {
153            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, null, new GuiLogEventArgs(message, null, logLevel));
154        }
155
156        #endregion Framework methods
157
158        #region DHT operations
159
160        public static bool Store(string key, byte[] data)
161        {
162            if (!Instance.P2PConnected())
163                throw new NotConnectedException();
164
165            return Instance.P2PBase.SynchStore(key, data);
166        }
167
168        public static bool Store(string key, string data)
169        {
170            if (!Instance.P2PConnected())
171                throw new NotConnectedException();
172           
173            return Instance.P2PBase.SynchStore(key, data);
174        }
175
176        public static byte[] Retrieve(string key)
177        {
178            if (!Instance.P2PConnected())
179                throw new NotConnectedException();
180           
181            return Instance.P2PBase.SynchRetrieve(key);
182        }
183
184        public static bool Remove(string key)
185        {
186            if (!Instance.P2PConnected())
187                throw new NotConnectedException();
188
189            return Instance.P2PBase.SynchRemove(key);
190        }
191
192        #endregion DHT operations
193    }
194
195   
196}
Note: See TracBrowser for help on using the repository browser.