source: trunk/CrypP2P/P2PManager.cs @ 1458

Last change on this file since 1458 was 1458, checked in by Paul Lelgemann, 12 years ago
  • Removed unused references and using statement in CrypP2P, PeerToPeer, PeerToPeerBaseProxy

+ Skeleton for P2PEditor

File size: 5.3 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;
24using DevComponents.WpfRibbon;
25
26namespace Cryptool.P2P
27{
28    public sealed class P2PManager
29    {
30        #region Singleton
31
32        public static readonly P2PManager Instance = new P2PManager();
33
34        private P2PManager()
35        {
36        }
37
38        #endregion
39
40        # region Constants
41
42        public const string P2PDisconnectImageUri = "images/peer2peer-disconnect.png";
43        public const string P2PConnectImageUri = "images/peer2peer-connect.png";
44
45        # endregion
46
47        #region Private variables
48
49        public P2PBase P2PBase { get; set; }
50        private P2PSettings P2PSettings { get; set; }
51        private ButtonDropDown P2PButton { get; set; }
52
53        #endregion
54
55        #region Events
56
57        public static event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
58
59        // to forward event from overlay/dht MessageReceived-Event from P2PBase
60        public event P2PBase.P2PMessageReceived OnPeerMessageReceived;
61
62        #endregion Events
63
64        /// <summary>
65        /// Initialises variables and important environment settings
66        /// regarding certificates.
67        /// </summary>
68        /// <param name="p2PButton">Button that can change the connection state
69        /// and displays it by showing different images.</param>
70        public void Initialize(ButtonDropDown p2PButton)
71        {
72            P2PButton = p2PButton;
73            P2PBase = new P2PBase();
74            P2PSettings = new P2PSettings();
75            P2PBase.AllowLoggingToMonitor = P2PSettings.Log2Monitor;
76
77            // Register events
78
79            // to forward event from overlay/dht MessageReceived-Event from P2PBase
80            P2PBase.OnP2PMessageReceived += p2pBase_OnP2PMessageReceived;
81
82            // Register exit event to terminate P2P connection without loosing data
83            // TODO check if this is correct, should be - but handler is not called (and application does not shut down), probably unrelated to this problem
84            Application.ApplicationExit += HandleDisconnectByApplicationShutdown;
85        }
86
87        /// <summary>
88        /// Changes the current connection state to the P2P network.
89        /// If there is currently no connection, it will try to connect.
90        /// If a connection is present, it will disconnect.
91        /// The actual work will be done asynchronous.
92        /// </summary>
93        public void ToggleConnectionState()
94        {
95            new ConnectionWorker(P2PBase, P2PSettings, P2PButton).Start();
96        }
97
98        public bool P2PConnected()
99        {
100            return P2PBase.Started;
101        }
102
103        public string UserInfo()
104        {
105            string userName;
106            PeerId userInfo = P2PBase.GetPeerID(out userName);
107            return userInfo + " (" + userName + ")";
108        }
109
110        public PeerId GetPeerId(out string userName)
111        {
112            return P2PBase.GetPeerID(out userName);
113        }
114
115        // to forward event from overlay/dht MessageReceived-Event from P2PBase
116        private void p2pBase_OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
117        {
118            if (OnPeerMessageReceived != null)
119                OnPeerMessageReceived(sourceAddr, data);
120        }
121
122        #region Framework methods
123
124        private void HandleDisconnectByApplicationShutdown(object sender, EventArgs e)
125        {
126            if (P2PConnected())
127            {
128                new ConnectionWorker(P2PBase, P2PSettings, P2PButton).Start();
129            }
130        }
131
132        public void GuiLogMessage(string message, NotificationLevel logLevel)
133        {
134            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, null, new GuiLogEventArgs(message, null, logLevel));
135        }
136
137        #endregion Framework methods
138
139        #region DHT operations
140
141        // TODO add error handling, if P2P is not connected
142        public static bool Store(string key, byte[] data)
143        {
144            return Instance.P2PBase.SynchStore(key, data);
145        }
146
147        public static bool Store(string key, string data)
148        {
149            return Instance.P2PBase.SynchStore(key, data);
150        }
151
152        public static byte[] Retrieve(string key)
153        {
154            return Instance.P2PBase.SynchRetrieve(key);
155        }
156
157        public static bool Remove(string key)
158        {
159            return Instance.P2PBase.SynchRemove(key);
160        }
161
162        #endregion DHT operations
163    }
164}
Note: See TracBrowser for help on using the repository browser.