source: trunk/CrypP2P/P2PManager.cs @ 1438

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