source: trunk/CrypP2P/P2PManager.cs @ 1374

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

+ Added P2P connection to CrypP2P based on PeerToPeerBase plugin (using default settings)
+ Added MD5Collider to CoreDeveloper solution

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.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        #region DHT operations
112        // TODO add error handling, if P2P if not connected
113        public static bool Store(string key, byte[] data)
114        {
115            return INSTANCE.P2PBase.SynchStore(key, data);
116        }
117
118        public static byte[] Retrieve(string key)
119        {
120            return INSTANCE.P2PBase.SynchRetrieve(key);
121        }
122
123        public static bool Remove(string key)
124        {
125            return INSTANCE.P2PBase.SynchRemove(key);
126        }
127        #endregion DHT operations
128
129        // to forward event from overlay/dht MessageReceived-Event from P2PBase
130        private void p2pBase_OnP2PMessageReceived(PeerId sourceAddr, byte[] data)
131        {
132            if (OnPeerMessageReceived != null)
133                OnPeerMessageReceived(sourceAddr, data);
134        }
135
136        #region Framework methods
137        void HandleDisconnectByApplicationShutdown(object sender, EventArgs e)
138        {
139            if (P2PConnected())
140            {
141                new ConnectionWorker(P2PBase, P2PSettings, P2PButton).Start();
142            }
143        }
144
145        public void GuiLogMessage(string message, NotificationLevel logLevel)
146        {
147            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, null, new GuiLogEventArgs(message, null, logLevel));
148        }
149        #endregion Framework methods
150    }
151}
Note: See TracBrowser for help on using the repository browser.