1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 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.
10 //
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.
15 //
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
24 //
25 
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"
37 #include "MuleColour.h"
38 #include "Statistics.h"
39 
40 #ifndef CLIENT_GUI
41 #include "kademlia/kademlia/Kademlia.h"
42 #endif
43 
44 
BEGIN_EVENT_TABLE(CKadDlg,wxPanel)45 BEGIN_EVENT_TABLE(CKadDlg, wxPanel)
46 	EVT_TEXT(ID_NODE_IP1, CKadDlg::OnFieldsChange)
47 	EVT_TEXT(ID_NODE_IP2, CKadDlg::OnFieldsChange)
48 	EVT_TEXT(ID_NODE_IP3, CKadDlg::OnFieldsChange)
49 	EVT_TEXT(ID_NODE_IP4, CKadDlg::OnFieldsChange)
50 	EVT_TEXT(ID_NODE_PORT, CKadDlg::OnFieldsChange)
51 
52 	EVT_TEXT_ENTER(IDC_NODESLISTURL ,CKadDlg::OnBnClickedUpdateNodeList)
53 
54 	EVT_BUTTON(ID_NODECONNECT, CKadDlg::OnBnClickedBootstrapClient)
55 	EVT_BUTTON(ID_KNOWNNODECONNECT, CKadDlg::OnBnClickedBootstrapKnown)
56 	EVT_BUTTON(ID_KADDISCONNECT, CKadDlg::OnBnClickedDisconnectKad)
57 	EVT_BUTTON(ID_UPDATEKADLIST, CKadDlg::OnBnClickedUpdateNodeList)
58 END_EVENT_TABLE()
59 
60 
61 
62 CKadDlg::CKadDlg(wxWindow* pParent)
63 	: wxPanel(pParent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("kadwnd") )
64 {
65 	m_kad_scope = NULL;
66 }
67 
68 
Init()69 void CKadDlg::Init()
70 {
71 	m_kad_scope = CastChild( wxT("kadScope"), COScopeCtrl );
72 	m_kad_scope->SetRanges(0.0, thePrefs::GetStatsMax());
73 	m_kad_scope->SetYUnits(wxT("Nodes"));
74 
75 #ifndef __WINDOWS__
76 	//
77 	// Get label with line breaks out of muuli.wdr, because generated code fails
78 	// to compile in Windows.
79 	//
80 	// In Windows, setting a button label with a newline fails (the newline is ignored).
81 	// Creating a button with such a label works however. :-/
82 	// So leave the label from the muuli (without line breaks) here,
83 	// so it can still be fixed in the translation.
84 	//
85 	wxButton* bootstrap = CastChild(ID_KNOWNNODECONNECT, wxButton);
86 	bootstrap->SetLabel(_("Bootstrap from \nknown clients"));
87 #endif
88 
89 	SetUpdatePeriod(thePrefs::GetTrafficOMeterInterval());
90 	SetGraphColors();
91 }
92 
93 
SetUpdatePeriod(int step)94 void CKadDlg::SetUpdatePeriod(int step)
95 {
96 	// this gets called after the value in Preferences/Statistics/Update delay has been changed
97 	if (step == 0) {
98 		m_kad_scope->Stop();
99 	} else {
100 		m_kad_scope->Reset(step);
101 	}
102 }
103 
104 
SetGraphColors()105 void CKadDlg::SetGraphColors()
106 {
107 	static const char aTrend[] = { 2,      1,        0        };
108 	static const int aRes[]    = { IDC_C0, IDC_C0_3, IDC_C0_2 };
109 
110 	m_kad_scope->SetBackgroundColor(CStatisticsDlg::getColors(0));
111 	m_kad_scope->SetGridColor(CStatisticsDlg::getColors(1));
112 
113 	for (size_t i = 0; i < 3; ++i) {
114 		m_kad_scope->SetPlotColor(CStatisticsDlg::getColors(12 + i), aTrend[i]);
115 
116 		CColorFrameCtrl* ctrl = CastChild(aRes[i], CColorFrameCtrl);
117 		ctrl->SetBackgroundBrushColour(CMuleColour(CStatisticsDlg::getColors(12 + i)));
118 		ctrl->SetFrameBrushColour(*wxBLACK);
119 	}
120 }
121 
122 
UpdateGraph(const GraphUpdateInfo & update)123 void CKadDlg::UpdateGraph(const GraphUpdateInfo& update)
124 {
125 	std::vector<float *> v(3);
126 	v[0] = const_cast<float *>(&update.kadnodes[0]);
127 	v[1] = const_cast<float *>(&update.kadnodes[1]);
128 	v[2] = const_cast<float *>(&update.kadnodes[2]);
129 	const std::vector<float *> &apfKad(v);
130 	unsigned nodeCount = static_cast<unsigned>(update.kadnodes[2]);
131 
132 	if (!IsShownOnScreen()) {
133 		m_kad_scope->DelayPoints();
134 	} else {
135 		// Check the current node-count to see if we should increase the graph height
136 		if (m_kad_scope->GetUpperLimit() < update.kadnodes[2]) {
137 			// Grow the limit by 50 sized increments.
138 			m_kad_scope->SetRanges(0.0, ((nodeCount + 49) / 50) * 50);
139 		}
140 
141 		m_kad_scope->AppendPoints(update.timestamp, apfKad);
142 	}
143 }
144 
145 
UpdateNodeCount(unsigned nodeCount)146 void CKadDlg::UpdateNodeCount(unsigned nodeCount)
147 {
148 	wxStaticText* label = CastChild( wxT("nodesListLabel"), wxStaticText );
149 	wxCHECK_RET(label, wxT("Failed to find kad-nodes label"));
150 
151 	label->SetLabel(CFormat(_("Nodes (%u)")) % nodeCount);
152 	label->GetParent()->Layout();
153 }
154 
155 
156 // Enables or disables the node connect button depending on the conents of the text fields
OnFieldsChange(wxCommandEvent & WXUNUSED (evt))157 void CKadDlg::OnFieldsChange(wxCommandEvent& WXUNUSED(evt))
158 {
159 	// These are the IDs of the search-fields
160 	int textfields[] = { ID_NODE_IP1, ID_NODE_IP2, ID_NODE_IP3, ID_NODE_IP4, ID_NODE_PORT};
161 
162 	bool enable = true;
163 	for ( uint16 i = 0; i < itemsof(textfields); i++ ) {
164 		enable &= !CastChild(textfields[i], wxTextCtrl)->GetValue().IsEmpty();
165 	}
166 
167 	// Enable the node connect button if all fields contain text
168 	FindWindowById(ID_NODECONNECT)->Enable( enable );
169 }
170 
171 
OnBnClickedBootstrapClient(wxCommandEvent & WXUNUSED (evt))172 void CKadDlg::OnBnClickedBootstrapClient(wxCommandEvent& WXUNUSED(evt))
173 {
174 	if (FindWindowById(ID_NODECONNECT)->IsEnabled()) {
175 		// Ip is reversed since StringIPtoUint32 returns anti-host and kad expects host order
176 		uint32 ip = StringIPtoUint32(
177 			dynamic_cast<wxTextCtrl*>(FindWindowById(ID_NODE_IP4))->GetValue() + wxT(".") +
178 			dynamic_cast<wxTextCtrl*>(FindWindowById(ID_NODE_IP3))->GetValue() + wxT(".") +
179 			dynamic_cast<wxTextCtrl*>(FindWindowById(ID_NODE_IP2))->GetValue() + wxT(".") +
180 			dynamic_cast<wxTextCtrl*>(FindWindowById(ID_NODE_IP1))->GetValue() );
181 
182 		if (ip == 0) {
183 			wxMessageBox(_("Invalid ip to bootstrap"), _("WARNING"), wxOK | wxICON_EXCLAMATION, this);
184 		} else {
185 			unsigned long port;
186 			if (dynamic_cast<wxTextCtrl*>(FindWindowById(ID_NODE_PORT))->GetValue().ToULong(&port)) {
187 				theApp->BootstrapKad(ip, port);
188 			} else {
189 				wxMessageBox(_("Invalid port to bootstrap"), _("WARNING"), wxOK | wxICON_EXCLAMATION, this);
190 			}
191 		}
192 	} else {
193 		wxMessageBox(_("Please fill all fields required"), _("Message"), wxOK | wxICON_INFORMATION, this);
194 	}
195 }
196 
197 
OnBnClickedBootstrapKnown(wxCommandEvent & WXUNUSED (evt))198 void CKadDlg::OnBnClickedBootstrapKnown(wxCommandEvent& WXUNUSED(evt))
199 {
200 	theApp->StartKad();
201 }
202 
203 
OnBnClickedDisconnectKad(wxCommandEvent & WXUNUSED (evt))204 void CKadDlg::OnBnClickedDisconnectKad(wxCommandEvent& WXUNUSED(evt))
205 {
206 	theApp->StopKad();
207 }
208 
209 
OnBnClickedUpdateNodeList(wxCommandEvent & WXUNUSED (evt))210 void CKadDlg::OnBnClickedUpdateNodeList(wxCommandEvent& WXUNUSED(evt))
211 {
212 	if ( wxMessageBox( wxString(_("Are you sure you want to download a new nodes.dat file?\n")) +
213 						_("Doing so will remove your current nodes and restart Kademlia connection.")
214 					, _("Continue?"), wxICON_EXCLAMATION | wxYES_NO, this) == wxYES ) {
215 		wxString strURL = dynamic_cast<wxTextCtrl*>(FindWindowById(IDC_NODESLISTURL))->GetValue();
216 
217 		thePrefs::SetKadNodesUrl(strURL);
218 		theApp->UpdateNotesDat(strURL);
219 	}
220 }
221 // File_checked_for_headers
222