Upstream tarball 9193
[amule.git] / src / ServerWnd.cpp
blob7ff3dcbe0621c2d073096488554a7b8e17b20472
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) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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
27 #include "muuli_wdr.h" // Needed for ID_ADDTOLIST
28 #include "ServerWnd.h" // Interface declarations.
29 #include "Server.h" // Needed for CServer
30 #include "ServerList.h" // Needed for CServerList
31 #include "ServerListCtrl.h" // Needed for CServerListCtrl
32 #include "Preferences.h" // Needed for CPreferences
33 #include "ServerConnect.h"
34 #include "amuleDlg.h" // Needed for CamuleDlg
35 #include "amule.h" // Needed for theApp
36 #include "Logger.h"
38 #include "kademlia/kademlia/Kademlia.h"
39 #include "kademlia/kademlia/UDPFirewallTester.h"
40 #include "kademlia/kademlia/Indexed.h"
41 #include "ClientList.h"
42 #include "updownclient.h"
44 BEGIN_EVENT_TABLE(CServerWnd,wxPanel)
45 EVT_BUTTON(ID_ADDTOLIST,CServerWnd::OnBnClickedAddserver)
46 EVT_BUTTON(IDC_ED2KDISCONNECT,CServerWnd::OnBnClickedED2KDisconnect)
47 EVT_BUTTON(ID_UPDATELIST,CServerWnd::OnBnClickedUpdateservermetfromurl)
48 EVT_TEXT_ENTER(IDC_SERVERLISTURL,CServerWnd::OnBnClickedUpdateservermetfromurl)
49 EVT_BUTTON(ID_BTN_RESET, CServerWnd::OnBnClickedResetLog)
50 EVT_BUTTON(ID_BTN_RESET_SERVER, CServerWnd::OnBnClickedResetServerLog)
51 EVT_SPLITTER_SASH_POS_CHANGED(ID_SRV_SPLITTER,CServerWnd::OnSashPositionChanged)
52 END_EVENT_TABLE()
55 CServerWnd::CServerWnd(wxWindow* pParent /*=NULL*/, int splitter_pos)
56 : wxPanel(pParent, -1)
58 wxSizer* sizer = serverListDlg(this,TRUE);
60 // init serverlist
61 // no use now. too early.
63 serverlistctrl = CastChild( ID_SERVERLIST, CServerListCtrl );
65 CastChild( ID_SRV_SPLITTER, wxSplitterWindow )->SetSashPosition(splitter_pos, true);
66 CastChild( ID_SRV_SPLITTER, wxSplitterWindow )->SetSashGravity(0.5f);
67 CastChild( IDC_NODESLISTURL, wxTextCtrl )->SetValue(thePrefs::GetKadNodesUrl());
68 CastChild( IDC_SERVERLISTURL, wxTextCtrl )->SetValue(thePrefs::GetEd2kServersUrl());
70 // Insert two columns, currently without a header
71 wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
72 wxASSERT(ED2KInfoList);
73 ED2KInfoList->InsertColumn(0, wxEmptyString);
74 ED2KInfoList->InsertColumn(1, wxEmptyString);
76 wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
77 wxASSERT(KadInfoList);
78 KadInfoList->InsertColumn(0, wxEmptyString);
79 KadInfoList->InsertColumn(1, wxEmptyString);
81 sizer->Show(this,TRUE);
85 CServerWnd::~CServerWnd()
87 thePrefs::SetEd2kServersUrl(CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue());
88 thePrefs::SetKadNodesUrl(CastChild( IDC_NODESLISTURL, wxTextCtrl )->GetValue());
92 void CServerWnd::UpdateServerMetFromURL(const wxString& strURL)
94 thePrefs::SetEd2kServersUrl(strURL);
95 theApp->serverlist->UpdateServerMetFromURL(strURL);
99 void CServerWnd::OnBnClickedAddserver(wxCommandEvent& WXUNUSED(evt))
101 wxString servername = CastChild( IDC_SERVERNAME, wxTextCtrl )->GetValue();
102 wxString serveraddr = CastChild( IDC_IPADDRESS, wxTextCtrl )->GetValue();
103 long port = StrToULong( CastChild( IDC_SPORT, wxTextCtrl )->GetValue() );
105 if ( serveraddr.IsEmpty() ) {
106 AddLogLineM( true, _("Server not added: No IP or hostname specified."));
107 return;
110 if ( port <= 0 || port > 65535 ) {
111 AddLogLineM( true, _("Server not added: Invalid server-port specified."));
112 return;
115 CServer* toadd = new CServer( port, serveraddr );
116 toadd->SetListName( servername.IsEmpty() ? serveraddr : servername );
118 if ( theApp->AddServer( toadd, true ) ) {
119 CastChild( IDC_SERVERNAME, wxTextCtrl )->Clear();
120 CastChild( IDC_IPADDRESS, wxTextCtrl )->Clear();
121 CastChild( IDC_SPORT, wxTextCtrl )->Clear();
122 } else {
123 CServer* update = theApp->serverlist->GetServerByAddress(toadd->GetAddress(), toadd->GetPort());
124 // See note on CServerList::AddServer
125 if (update == NULL && toadd->GetIP() != 0) {
126 update = theApp->serverlist->GetServerByIPTCP(toadd->GetIP(), toadd->GetPort());
129 if ( update ) {
130 update->SetListName(toadd->GetListName());
131 serverlistctrl->RefreshServer(update);
133 delete toadd;
136 theApp->serverlist->SaveServerMet();
140 void CServerWnd::OnBnClickedUpdateservermetfromurl(wxCommandEvent& WXUNUSED(evt))
142 wxString strURL = CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue();
143 UpdateServerMetFromURL(strURL);
147 void CServerWnd::OnBnClickedResetLog(wxCommandEvent& WXUNUSED(evt))
149 theApp->GetLog(true); // Reset it.
153 void CServerWnd::OnBnClickedResetServerLog(wxCommandEvent& WXUNUSED(evt))
155 theApp->GetServerLog(true); // Reset it
159 void CServerWnd::UpdateED2KInfo()
161 wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
163 ED2KInfoList->DeleteAllItems();
164 ED2KInfoList->InsertItem(0, _("eD2k Status:"));
166 if (theApp->IsConnectedED2K()) {
167 ED2KInfoList->SetItem(0, 1, _("Connected"));
169 // Connection data
171 ED2KInfoList->InsertItem(1, _("IP:Port"));
172 ED2KInfoList->SetItem(1, 1, theApp->serverconnect->IsLowID() ?
173 wxString(_("LowID")) : Uint32_16toStringIP_Port( theApp->GetED2KID(), thePrefs::GetPort()));
175 ED2KInfoList->InsertItem(2, _("ID"));
176 // No need to test the server connect, it's already true
177 ED2KInfoList->SetItem(2, 1, wxString::Format(wxT("%u"), theApp->GetED2KID()));
179 ED2KInfoList->InsertItem(3, wxEmptyString);
181 if (theApp->serverconnect->IsLowID()) {
182 ED2KInfoList->SetItem(1, 1, _("Server")); // LowID, unknown ip
183 ED2KInfoList->SetItem(3, 1, _("LowID"));
184 } else {
185 ED2KInfoList->SetItem(1, 1, Uint32_16toStringIP_Port(theApp->GetED2KID(), thePrefs::GetPort()));
186 ED2KInfoList->SetItem(3, 1, _("HighID"));
189 } else {
190 // No data
191 ED2KInfoList->SetItem(0, 1, _("Not Connected"));
194 // Fit the width of the columns
195 ED2KInfoList->SetColumnWidth(0, -1);
196 ED2KInfoList->SetColumnWidth(1, -1);
199 void CServerWnd::UpdateKadInfo()
201 wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
203 int next_row = 0;
205 KadInfoList->DeleteAllItems();
207 KadInfoList->InsertItem(next_row, _("Kademlia Status:"));
209 if (theApp->IsKadRunning()) {
210 KadInfoList->SetItem(next_row++, 1, _("Running"));
212 // Connection data
213 KadInfoList->InsertItem(next_row, _("Status:"));
214 KadInfoList->SetItem(next_row++, 1, theApp->IsConnectedKad() ? _("Connected"): _("Disconnected"));
215 if (theApp->IsConnectedKad()) {
216 KadInfoList->InsertItem(next_row, _("Connection State:"));
217 KadInfoList->SetItem(next_row++, 1, theApp->IsFirewalledKad() ?
218 CFormat(_("Firewalled - open TCP port %d in your router or firewall")) % thePrefs::GetPort() : _("OK"));
219 #ifndef CLIENT_GUI
220 KadInfoList->InsertItem(next_row, _("UDP Connection State:"));
221 bool UDPFirewalled = Kademlia::CUDPFirewallTester::IsFirewalledUDP(true);
222 KadInfoList->SetItem(next_row++, 1, UDPFirewalled ?
223 CFormat(_("Firewalled - open UDP port %d in your router or firewall")) % thePrefs::GetUDPPort() : _("OK"));
225 if (theApp->IsFirewalledKad() || UDPFirewalled) {
226 KadInfoList->InsertItem(next_row, _("Firewalled state: "));
227 wxString BuddyState;
228 switch ( theApp->clientlist->GetBuddyStatus() )
230 case Disconnected:
231 if (!theApp->IsFirewalledKad()) {
232 BuddyState = _("No buddy required - TCP port open");
233 } else if (!UDPFirewalled) {
234 BuddyState = _("No buddy required - UDP port open");
235 } else {
236 BuddyState = _("No buddy");
238 break;
239 case Connecting:
240 BuddyState = _("Connecting to buddy");
241 break;
242 case Connected:
243 BuddyState = CFormat(_("Connected to buddy at %s")) % Uint32_16toStringIP_Port(theApp->clientlist->GetBuddy()->GetIP(), theApp->clientlist->GetBuddy()->GetUDPPort());
244 break;
246 KadInfoList->SetItem(next_row++, 1, BuddyState);
249 KadInfoList->InsertItem(next_row, _("IP address:"));
250 KadInfoList->SetItem(next_row++, 1, Uint32toStringIP(wxUINT32_SWAP_ALWAYS(Kademlia::CKademlia::GetPrefs()->GetIPAddress()))); // WTF ?
252 // Index info
253 KadInfoList->InsertItem(next_row, _("Indexed sources:"));
254 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % Kademlia::CKademlia::GetIndexed()->m_totalIndexSource);
255 KadInfoList->InsertItem(next_row, _("Indexed keywords:"));
256 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % Kademlia::CKademlia::GetIndexed()->m_totalIndexKeyword);
257 KadInfoList->InsertItem(next_row, _("Indexed notes:"));
258 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % Kademlia::CKademlia::GetIndexed()->m_totalIndexNotes);
259 KadInfoList->InsertItem(next_row, _("Indexed load:"));
260 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % Kademlia::CKademlia::GetIndexed()->m_totalIndexLoad);
262 uint32 KademliaUsers = Kademlia::CKademlia::GetKademliaUsers();
263 uint32 KademliaFiles = Kademlia::CKademlia::GetKademliaFiles();
264 #else
265 uint32 KademliaUsers = theStats::GetKadUsers();
266 uint32 KademliaFiles = theStats::GetKadFiles();
267 //#warning TODO: Buddy state on remote GUI
268 /* Maybe Averages too, but that would be redundant
269 they are already on the status bar */
270 #endif
271 KadInfoList->InsertItem(next_row, _("Average Users:"));
272 KadInfoList->SetItem(next_row, 1, CastItoIShort(KademliaUsers));
273 ++next_row;
274 KadInfoList->InsertItem(next_row, _("Average Files:"));
275 KadInfoList->SetItem(next_row, 1, CastItoIShort(KademliaFiles));
279 } else {
280 // No data
281 KadInfoList->SetItem(next_row, 1, _("Not running"));
284 // Fit the width of the columns
285 KadInfoList->SetColumnWidth(0, -1);
286 KadInfoList->SetColumnWidth(1, -1);
289 void CServerWnd::OnSashPositionChanged(wxSplitterEvent& WXUNUSED(evt))
291 if (theApp->amuledlg) {
292 theApp->amuledlg->m_srv_split_pos = CastChild( wxT("SrvSplitterWnd"), wxSplitterWindow )->GetSashPosition();
296 void CServerWnd::OnBnClickedED2KDisconnect(wxCommandEvent& WXUNUSED(evt))
298 if (theApp->serverconnect->IsConnecting()) {
299 theApp->serverconnect->StopConnectionTry();
300 } else {
301 theApp->serverconnect->Disconnect();
304 // File_checked_for_headers