source: trunk/CrypP2P/P2PManager.cs @ 1378

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

o Fixed solutions and broken plugins, which had missing/broken dependencies
+ Added debug output after connecting about our local peer to CrypP2P
+ Prepared PeerToPeer plugin for later use

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