The Winamp Remote Control suite
a remote control client and plugin for Winamp 2.x, 5.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ServersF.cpp
Go to the documentation of this file.
1 /*
2 winamp remote control suite ŠPatrick Michael Martin 2000, 2001, 2002
3 
4 Copyright (C) 2000,2001,2002 Patrick M. Martin
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 Patrick M. Martin may be reached by email at patrickmmartin@gmail.com.
21 */
22 
23 #include <vcl.h>
24 #pragma hdrstop
25 
26 #include <time.h>
27 #include <stdlib.h> // max
28 
29 #include "ServersF.h" // servers
30 #include "messageF.h" // message form
31 #include "RFC1060U.h" // ports
32 #include "IPAddressResolver.h" // IP utility class
33 
34 // winamp IPC declarations
35 #include "waint.h"
36 #include "remotestrs.h"
37 
38 #include "ServerEnumerator.h" // class to enumerate servers
39 #include "CursorGuard.h"
40 
41 #pragma package(smart_init)
42 #pragma resource "*.dfm"
43 
45 
46 __fastcall TfrmServers::TfrmServers(TComponent* Owner)
47  : TForm(Owner)
48 {
49 }
50 
51 void __fastcall TfrmServers::FormCreate(TObject *)
52 {
53  lvServers->Items->Clear();
54  // sets up the standard icons from Windows
55  imgInformation->Picture->Icon->Handle = LoadIcon(NULL, IDI_ASTERISK);
56  imgWarning->Picture->Icon->Handle = LoadIcon(NULL, IDI_EXCLAMATION);
57  imgError->Picture->Icon->Handle = LoadIcon(NULL, IDI_HAND);
58 
59  imlEvents->AddIcon(imgInformation->Picture->Icon);
60  imlEvents->AddIcon(imgWarning->Picture->Icon);
61  imlEvents->AddIcon(imgError->Picture->Icon);
62 
63 }
64 
65 void __fastcall TfrmServers::FormClose(TObject *, TCloseAction &)
66 {
67  if (ModalResult == mrOk)
68  {
69  EndPoint = ebEndPoint->Text;
70  if (lvServers->Selected)
71  Address = lvServers->Selected->Caption;
72  }
73 }
74 
75 void __fastcall TfrmServers::btnLocateClick(TObject *)
76 {
77 
79 
80  btnLocate->Enabled = false;
81 
82  try
83  {
84  pbServers->Position = 0;
85  pbServers->Max = 100;
86  lvMessages->Items->Clear();
87 
89  se.OnMessage = doNetworkMessage;
90  se.OnServer = doNetworkServer;
91  se.OnProgress = doNetworkProgress;
92  se.enumerateServers();
93 
94  addLocal();
95 
96  pbServers->Position = 100;
97 
98 
99  btnTest->Enabled = true;
100  }
101  __finally
102  {
103  btnLocate->Enabled = true;
104  pbServers->Position = 0;
105  }
106 }
107 
108 
109 
110 void __fastcall TfrmServers::lvServersClick(TObject *)
111 {
112  btnOK->Enabled =(lvServers->Selected != NULL );
113 }
114 
115 
116 void __fastcall TfrmServers::StartTest(TObject *)
117 {
118 
119  AbortTest = false;
120 
121  // validate the selected port
122  CheckPort();
123  // now this is OK
124 
125  lvMessages->Items->Clear();
126  btnLocate->Enabled = false;
127  btnOK->Enabled = false;
128  btnCancel->Enabled = false;
129 
130  btnTest->Caption = sStop;
131  btnTest->OnClick = StopTest;
132 
134 
135  try
136  {
137 
138  ServerTester st;
139  st.endPoint = ebEndPoint->Text;
140  map<AnsiString, ServerInfo> servers;
141  getServers(servers);
142  st.testServers(servers);
143 
144  }
145  __finally
146  {
147  pbServers->Position = 0;
148  btnLocate->Enabled = true;
149  btnOK->Enabled = false;
150  btnCancel->Enabled = true;
151  btnTest->OnClick = StartTest;
152  btnTest->Caption = sTest;
153  }
154 
155 }
156 
157 
158 
159 void __fastcall TfrmServers::StopTest(TObject *)
160 {
161 
162  AbortTest = true;
163 
164 }
165 
166 
167 void __fastcall TfrmServers::CheckPort(void)
168 {
169 
170 // assumes the endpoint is an int, which needs amending
171 
172 /*
173  int port;
174 
175  try
176  {
177  port = ebEndPoint->Text.ToInt();
178  if ((port < 0) || (port > 65535))
179  {
180  throw EConvertError(AnsiString().sprintf(sPortOutOfRangeFmt.c_str(), port).c_str());
181  }
182  }
183  catch (EConvertError &E)
184  {
185  MessageBox(this->Handle, E.Message.c_str(), sEndpointInvalid.c_str(), MB_OK | MB_ICONERROR);
186  throw EAbort("abort operation");
187  }
188  bool usedport = false;
189  int i;
190 
191  for (i = 0 ; i < RFC1060portscount ; i++){
192  if (RFC1060ports[i].port == port){
193  usedport = true;
194  break;
195  }
196  }
197 
198  if (usedport &&
199  Application->MessageBox((AnsiString().sprintf(sWellKnownPortFmt.c_str(),
200  port , RFC1060ports[i].service)).c_str(),
201  sAlertWellKnownPort.c_str(),
202  MB_ICONEXCLAMATION + MB_OKCANCEL + MB_DEFBUTTON2) != IDOK) throw EAbort("user cancellled bogus port");
203 */
204 }
205 
206 
207 void __fastcall TfrmServers::btnOKClick(TObject *)
208 {
209  CheckPort();
210  ModalResult = mrOk;
211 }
212 
213 void __fastcall TfrmServers::GetServerIp(TObject *)
214 {
215 
216  AnsiString HostName;
217 
219  lvMessages->Items->Clear();
220  if (lvServers->Selected)
221  {
222  try
223  {
224 
225  string lRemoteName = lvServers->Selected->Caption.c_str();
226  WinampRemote::Net::IPAddressResolver ipr(lRemoteName);
227  //TODO - some stuff here with the results
228 
229  // GetIPAddress(lvServers->Selected->Caption.c_str(), HostName, Addresses, Aliases);
230 
231  doNetworkMessage(sIPLookup + lvServers->Selected->Caption, 1);
232  doNetworkMessage(sAuthoritativeName + HostName, 1);
233 
234  // if (Addresses->Count > 0 )
235  // {
236  // doNetworkMessage(sAddressesRetrieved, 1);
237  // for (int i = 0; i < Addresses->Count ; i ++)
238  // {
239  // doNetworkMessage(sIPAddress + Addresses->Strings[i], 1);
240  // }
241  // }
242  // else
243  {
244  doNetworkMessage(sAddressesNotRetrieved, 2);
245  }
246 
247  // if (Aliases->Count > 0 )
248  // {
249  // doNetworkMessage(sAliasesRetrieved, 1);
250  // for (int i = 0; i < Aliases->Count ; i ++)
251  // {
252  // doNetworkMessage(AnsiString(sIPAlias) + Aliases->Strings[i], 1);
253  // }
254  // }
255  // else
256  {
257  doNetworkMessage(sAliasesNotRetrieved, 2);
258  }
259 
260  }
261  catch (...)
262  {
263  doNetworkMessage(sGetIPAddressFailed /* + E.Message */, 3);
264  }
265  }
266 }
267 
268 
269 void __fastcall TfrmServers::spltMessagesCanResize(TObject * , int &NewSize, bool &Accept)
270 {
271  Accept = (NewSize > 30);
272 }
273 
274 
275 void TfrmServers::addLocal()
276 {
277  char szComputerName[MAX_COMPUTERNAME_LENGTH+1] = {0};
278  DWORD dwSize;
279 
280  dwSize = sizeof(szComputerName);
281  Win32Check(GetComputerName(szComputerName, &dwSize));
282  AnsiString ComputerName = szComputerName;
283  AnsiString localhost = "127.0.0.1";
284  doNetworkServer(ComputerName.c_str(), sLocalMachine.c_str(), localhost);
285 
286 }
287 
288 void TfrmServers::doNetworkServer(const AnsiString& RemoteName, const AnsiString& Comment, const AnsiString& ip)
289 {
290  TListItem * ListItem = findServerItem(RemoteName);
291 
292  ListItem->Caption = RemoteName;
293  ListItem->SubItems->Clear();
294  ListItem->SubItems->Add(Comment);
295  ListItem->SubItems->Add(ip);
296  ListItem->SubItems->Add(sServerUntested);
297  ListItem->ImageIndex = 0;
298 
299 }
300 
301 void TfrmServers::doNetworkMessage(const AnsiString& Message, const int Level)
302 {
303  addProcessMessage(Message, Level);
304 }
305 
306 void TfrmServers::doNetworkProgress(const float progress)
307 {
308  pbServers->Position = progress * pbServers->Max;
309 }
310 
311 
312 void TfrmServers::doTestEvent(const AnsiString& remoteName,
313  const AnsiString& data,
314  const int level)
315 {
316  addProcessMessage(remoteName + ":" + data, level);
317  TListItem * ListItem = findServerItem(remoteName);
318  ListItem->SubItems->Strings[2] = data;
319 }
320 
321 void TfrmServers::doTestResult(const AnsiString& remoteName,
322  const bool success,
323  bool& abort)
324 {
325  TListItem * ListItem = findServerItem(remoteName);
326  ListItem->ImageIndex = (success)?2:1;
327  Application->ProcessMessages();
328  abort = AbortTest;
329 }
330 
331 void TfrmServers::addProcessMessage(const AnsiString & Message, const int Level)
332 {
333  TListItem *ListItem = lvMessages->Items->Add();
334  ListItem->Caption = Message;
335  ListItem->ImageIndex = Level - 1;
336  lvMessages->Selected = ListItem;
337  lvMessages->Update();
338 }
339 
340 TListItem* TfrmServers::findServerItem(const AnsiString& RemoteName)
341 {
342  TListItem * ListItem;
343  for (int i = 0; (i < lvServers->Items->Count) ; i++)
344  {
345  ListItem = lvServers->Items->Item[i];
346  if (ListItem->Caption == RemoteName)
347  return ListItem;
348  }
349 
350  return lvServers->Items->Add();
351 }
352 
353 void TfrmServers::getServers(map<AnsiString, ServerInfo>& servers)
354 {
355  if (lvServers->Selected)
356  {
357  servers[lvServers->Selected->Caption].status = SI_UNTESTED;
358  return;
359  }
360 
361  for (int i = 0; (i < lvServers->Items->Count); i++)
362  {
363  servers[lvServers->Items->Item[i]->Caption].status = SI_UNTESTED;
364  }
365 }
366