source: trunk/CrypP2P/Worker/ConnectionWorker.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: 3.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.Linq;
20using System.Text;
21using DevComponents.WpfRibbon;
22using System.Windows.Controls;
23using System.Windows.Media.Imaging;
24using System.Windows.Media;
25using Cryptool.PluginBase;
26using Cryptool.P2P.Internal;
27using Cryptool.P2P.Helper;
28
29namespace Cryptool.P2P.Worker
30{
31    class ConnectionWorker : WorkerBase
32    {
33        P2PBase p2pBase;
34        P2PSettings p2pSettings;
35        ButtonDropDown p2pButton;
36
37        public ConnectionWorker(P2PBase p2pBase, P2PSettings p2pSettings, ButtonDropDown p2pButton)
38        {
39            this.p2pBase = p2pBase;
40            this.p2pSettings = p2pSettings;
41            this.p2pButton = p2pButton;
42        }
43
44        protected override void PerformWork(object sender, System.ComponentModel.DoWorkEventArgs e)
45        {
46            //p2pButton.IsEnabled = false;
47
48            if (!p2pBase.Started)
49            {
50                P2PManager.Instance.GuiLogMessage("Connecting to P2P network...", NotificationLevel.Info);
51
52                // Validate certificats
53                if (!PAPCertificate.CheckAndInstallPAPCertificates())
54                {
55                    P2PManager.Instance.GuiLogMessage("Certificates not validated, P2P might not be working!", NotificationLevel.Error);
56                    return;
57                }
58
59                p2pBase.Initialize(p2pSettings);
60                p2pBase.SynchStart();
61            }
62            else
63            {
64                P2PManager.Instance.GuiLogMessage("Disconnecting from P2P network...", NotificationLevel.Info);
65                p2pBase.SynchStop();
66            }
67        }
68
69        protected override void WorkComplete(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
70        {
71            // Update image according to new state
72            Image newImage = new Image();
73
74            if (P2PManager.Instance.P2PConnected())
75            {
76                P2PManager.Instance.GuiLogMessage("Connection to P2P network established.", NotificationLevel.Info);
77                P2PManager.Instance.GuiLogMessage("P2P user info: " + P2PManager.Instance.UserInfo(), NotificationLevel.Debug);
78                newImage.Source = new BitmapImage(new Uri(P2PManager.P2PDisconnectImageURI, UriKind.RelativeOrAbsolute));
79            }
80            else
81            {
82                P2PManager.Instance.GuiLogMessage("Connection to P2P network terminated.", NotificationLevel.Info);
83                newImage.Source = new BitmapImage(new Uri(P2PManager.P2PConnectImageURI, UriKind.RelativeOrAbsolute));
84            }
85
86            newImage.Stretch = Stretch.Uniform;
87            newImage.Width = 40;
88            newImage.Height = 40;
89            p2pButton.Image = newImage;
90
91            p2pButton.IsEnabled = true;
92        }
93
94        protected override void PrePerformWork()
95        {
96            p2pButton.IsEnabled = false;
97        }
98    }
99}
Note: See TracBrowser for help on using the repository browser.