Upstream tarball 20080414
[amule.git] / src / KadDlg.cpp
blob90edf478ad7fcfba5a807387219f868b605ce5a3
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2008 Angel Vidal (Kry) ( kry@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "KadDlg.h"
27 #include "muuli_wdr.h"
28 #include "OScopeCtrl.h"
29 #include "OtherFunctions.h"
30 #include "HTTPDownload.h"
31 #include "Logger.h"
32 #include "amule.h"
33 #include "Preferences.h"
34 #include "StatisticsDlg.h"
35 #include "ColorFrameCtrl.h"
36 #include "amuleDlg.h"
39 #ifndef CLIENT_GUI
40 #include "kademlia/kademlia/Kademlia.h"
41 #endif
44 BEGIN_EVENT_TABLE(CKadDlg, wxPanel)
45 EVT_TEXT(ID_NODE_IP1, CKadDlg::OnFieldsChange)
46 EVT_TEXT(ID_NODE_IP2, CKadDlg::OnFieldsChange)
47 EVT_TEXT(ID_NODE_IP3, CKadDlg::OnFieldsChange)
48 EVT_TEXT(ID_NODE_IP4, CKadDlg::OnFieldsChange)
49 EVT_TEXT(ID_NODE_PORT, CKadDlg::OnFieldsChange)
51 EVT_TEXT_ENTER(IDC_NODESLISTURL ,CKadDlg::OnBnClickedUpdateNodeList)
53 EVT_BUTTON(ID_NODECONNECT, CKadDlg::OnBnClickedBootstrapClient)
54 EVT_BUTTON(ID_KNOWNNODECONNECT, CKadDlg::OnBnClickedBootstrapKnown)
55 EVT_BUTTON(ID_KADDISCONNECT, CKadDlg::OnBnClickedDisconnectKad)
56 EVT_BUTTON(ID_UPDATEKADLIST, CKadDlg::OnBnClickedUpdateNodeList)
57 END_EVENT_TABLE()
61 CKadDlg::CKadDlg(wxWindow* pParent)
62 : wxPanel(pParent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("kadwnd") )
64 m_kad_scope = NULL;
68 void CKadDlg::Init()
70 m_kad_scope = CastChild( wxT("kadScope"), COScopeCtrl );
71 m_kad_scope->SetRanges(0.0, thePrefs::GetStatsMax());
72 m_kad_scope->SetYUnits(wxT("Nodes"));
74 SetUpdatePeriod(thePrefs::GetTrafficOMeterInterval());
75 SetGraphColors();
79 void CKadDlg::SetUpdatePeriod(int step)
81 // this gets called after the value in Preferences/Statistics/Update delay has been changed
82 if (step == 0) {
83 m_kad_scope->Stop();
84 } else {
85 m_kad_scope->Reset(step);
90 void CKadDlg::SetGraphColors()
92 static const char aTrend[] = { 2, 1, 0 };
93 static const int aRes[] = { IDC_C0, IDC_C0_3, IDC_C0_2 };
95 m_kad_scope->SetBackgroundColor(CStatisticsDlg::getColors(0));
96 m_kad_scope->SetGridColor(CStatisticsDlg::getColors(1));
98 for (size_t i = 0; i < 3; ++i) {
99 m_kad_scope->SetPlotColor(CStatisticsDlg::getColors(12 + i), aTrend[i]);
101 CColorFrameCtrl* ctrl = CastChild(aRes[i], CColorFrameCtrl);
102 ctrl->SetBackgroundColor(CStatisticsDlg::getColors(12 + i));
103 ctrl->SetFrameColor((COLORREF)RGB(0,0,0));
108 void CKadDlg::UpdateGraph(const GraphUpdateInfo& update)
110 std::vector<float *> v(3);
111 v[0] = const_cast<float *>(&update.kadnodes[0]);
112 v[1] = const_cast<float *>(&update.kadnodes[1]);
113 v[2] = const_cast<float *>(&update.kadnodes[2]);
114 const std::vector<float *> &apfKad(v);
115 unsigned nodeCount = static_cast<unsigned>(update.kadnodes[2]);
117 if (!IsShownOnScreen()) {
118 m_kad_scope->DelayPoints();
119 } else {
120 // Check the current node-count to see if we should increase the graph height
121 if (m_kad_scope->GetUpperLimit() < update.kadnodes[2]) {
122 // Grow the limit by 50 sized increments.
123 m_kad_scope->SetRanges(0.0, ((nodeCount + 49) / 50) * 50);
126 m_kad_scope->AppendPoints(update.timestamp, apfKad);
129 wxStaticText* label = CastChild( wxT("nodesListLabel"), wxStaticText );
130 wxCHECK_RET(label, wxT("Failed to find kad-nodes label"));
132 label->SetLabel(wxString::Format(_("Nodes (%u)"), nodeCount));
133 label->GetParent()->Layout();
137 // Enables or disables the node connect button depending on the conents of the text fields
138 void CKadDlg::OnFieldsChange(wxCommandEvent& WXUNUSED(evt))
140 // These are the IDs of the search-fields
141 int textfields[] = { ID_NODE_IP1, ID_NODE_IP2, ID_NODE_IP3, ID_NODE_IP4, ID_NODE_PORT};
143 bool enable = true;
144 for ( uint16 i = 0; i < itemsof(textfields); i++ ) {
145 enable &= !CastChild(textfields[i], wxTextCtrl)->GetValue().IsEmpty();
148 // Enable the node connect button if all fields contain text
149 FindWindowById(ID_NODECONNECT)->Enable( enable );
153 void CKadDlg::OnBnClickedBootstrapClient(wxCommandEvent& WXUNUSED(evt))
155 if (FindWindowById(ID_NODECONNECT)->IsEnabled()) {
156 // Ip is reversed since StringIPtoUint32 returns anti-host and kad expects host order
157 uint32 ip = StringIPtoUint32(
158 ((wxTextCtrl*)FindWindowById( ID_NODE_IP4 ))->GetValue() +
159 wxT(".") +
160 ((wxTextCtrl*)FindWindowById( ID_NODE_IP3 ))->GetValue() +
161 wxT(".") +
162 ((wxTextCtrl*)FindWindowById( ID_NODE_IP2 ))->GetValue() +
163 wxT(".") +
164 ((wxTextCtrl*)FindWindowById( ID_NODE_IP1 ))->GetValue() );
166 if (ip == 0) {
167 wxMessageBox(_("Invalid ip to bootstrap"), _("Warning"), wxOK | wxICON_EXCLAMATION, this);
168 } else {
169 unsigned long port;
170 if (((wxTextCtrl*)FindWindowById( ID_NODE_PORT ))->GetValue().ToULong(&port)) {
171 theApp->BootstrapKad(ip, port);
172 } else {
173 wxMessageBox(_("Invalid port to bootstrap"), _("Warning"), wxOK | wxICON_EXCLAMATION, this);
176 } else {
177 wxMessageBox(_("Please fill all fields required"), _("Message"), wxOK | wxICON_INFORMATION, this);
182 void CKadDlg::OnBnClickedBootstrapKnown(wxCommandEvent& WXUNUSED(evt))
184 theApp->StartKad();
188 void CKadDlg::OnBnClickedDisconnectKad(wxCommandEvent& WXUNUSED(evt))
190 theApp->StopKad();
194 void CKadDlg::OnBnClickedUpdateNodeList(wxCommandEvent& WXUNUSED(evt))
196 if ( wxMessageBox( wxString(_("Are you sure you want to download a new nodes.dat file?\n")) +
197 _("Doing so will remove your current nodes and restart Kademlia connection.")
198 , _("Continue?"), wxICON_EXCLAMATION | wxYES_NO, this) == wxYES ) {
199 wxString strURL = ((wxTextCtrl*)FindWindowById( IDC_NODESLISTURL ))->GetValue();
201 thePrefs::SetKadNodesUrl(strURL);
202 theApp->UpdateNotesDat(strURL);
205 // File_checked_for_headers