]> git.saurik.com Git - wxWidgets.git/blame - samples/sockets/client.cpp
fix for dmc compilation of projects with more than one obj
[wxWidgets.git] / samples / sockets / client.cpp
CommitLineData
f85d901f
GRG
1/////////////////////////////////////////////////////////////////////////////
2// Name: client.cpp
3// Purpose: Client for wxSocket demo
4// Author: Guillermo Rodriguez Garcia <guille@iies.es>
5// Modified by:
6// Created: 1999/09/19
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Guillermo Rodriguez Garcia
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ==========================================================================
13// declarations
14// ==========================================================================
15
16// --------------------------------------------------------------------------
17// headers
18// --------------------------------------------------------------------------
e2a6f233 19
788233da 20#if defined(__GNUG__) && !defined(__APPLE__)
f85d901f
GRG
21# pragma implementation "client.cpp"
22# pragma interface "client.cpp"
f4ada568
GL
23#endif
24
f85d901f 25// For compilers that support precompilation, includes "wx/wx.h".
f4ada568
GL
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
f85d901f 29# pragma hdrstop
f4ada568
GL
30#endif
31
f85d901f 32// for all others, include the necessary headers
f4ada568 33#ifndef WX_PRECOMP
f85d901f 34# include "wx/wx.h"
09fb22cf
JS
35#endif
36
6097c3a2
GRG
37#include "wx/socket.h"
38#include "wx/url.h"
25ba2b89 39#include "wx/wfstream.h"
e2a6f233 40
f85d901f
GRG
41// --------------------------------------------------------------------------
42// resources
43// --------------------------------------------------------------------------
f4ada568 44
f85d901f 45// the application icon
618f2efa 46#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
f85d901f 47# include "mondrian.xpm"
e2a6f233
JS
48#endif
49
f85d901f
GRG
50// --------------------------------------------------------------------------
51// classes
52// --------------------------------------------------------------------------
f4ada568 53
f85d901f
GRG
54// Define a new application type
55class MyApp : public wxApp
56{
f4ada568 57public:
f85d901f 58 virtual bool OnInit();
f4ada568
GL
59};
60
f85d901f
GRG
61// Define a new frame type: this is going to be our main frame
62class MyFrame : public wxFrame
f4ada568
GL
63{
64public:
f85d901f
GRG
65 MyFrame();
66 ~MyFrame();
67
25ba2b89 68 // event handlers for File menu
f85d901f
GRG
69 void OnQuit(wxCommandEvent& event);
70 void OnAbout(wxCommandEvent& event);
25ba2b89
GRG
71
72 // event handlers for Socket menu
f85d901f
GRG
73 void OnOpenConnection(wxCommandEvent& event);
74 void OnTest1(wxCommandEvent& event);
75 void OnTest2(wxCommandEvent& event);
76 void OnTest3(wxCommandEvent& event);
77 void OnCloseConnection(wxCommandEvent& event);
25ba2b89
GRG
78
79 // event handlers for Protocols menu
80 void OnTestURL(wxCommandEvent& event);
81
e51b0130 82 // event handlers for DatagramSocket menu (stub)
6097c3a2 83 void OnDatagram(wxCommandEvent& event);
f85d901f 84
25ba2b89
GRG
85 // socket event handler
86 void OnSocketEvent(wxSocketEvent& event);
87
f85d901f
GRG
88 // convenience functions
89 void UpdateStatusBar();
90
91private:
92 wxSocketClient *m_sock;
f85d901f
GRG
93 wxTextCtrl *m_text;
94 wxMenu *m_menuFile;
95 wxMenu *m_menuSocket;
6097c3a2 96 wxMenu *m_menuDatagramSocket;
25ba2b89 97 wxMenu *m_menuProtocols;
f85d901f
GRG
98 wxMenuBar *m_menuBar;
99 bool m_busy;
100
101 // any class wishing to process wxWindows events must use this macro
102 DECLARE_EVENT_TABLE()
f4ada568
GL
103};
104
f85d901f
GRG
105// --------------------------------------------------------------------------
106// constants
107// --------------------------------------------------------------------------
f4ada568 108
f85d901f
GRG
109// IDs for the controls and the menu commands
110enum
f4ada568 111{
f85d901f
GRG
112 // menu items
113 CLIENT_QUIT = 1000,
114 CLIENT_ABOUT,
115 CLIENT_OPEN,
116 CLIENT_TEST1,
117 CLIENT_TEST2,
118 CLIENT_TEST3,
119 CLIENT_CLOSE,
25ba2b89 120 CLIENT_TESTURL,
6097c3a2 121 CLIENT_DGRAM,
f85d901f
GRG
122
123 // id for socket
124 SOCKET_ID
125};
f4ada568 126
f85d901f
GRG
127// --------------------------------------------------------------------------
128// event tables and other macros for wxWindows
129// --------------------------------------------------------------------------
f4ada568 130
f85d901f 131BEGIN_EVENT_TABLE(MyFrame, wxFrame)
25ba2b89
GRG
132 EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit)
133 EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout)
134 EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection)
135 EVT_MENU(CLIENT_TEST1, MyFrame::OnTest1)
136 EVT_MENU(CLIENT_TEST2, MyFrame::OnTest2)
137 EVT_MENU(CLIENT_TEST3, MyFrame::OnTest3)
138 EVT_MENU(CLIENT_CLOSE, MyFrame::OnCloseConnection)
139 EVT_MENU(CLIENT_DGRAM, MyFrame::OnDatagram)
140 EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL)
141 EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
f85d901f 142END_EVENT_TABLE()
f4ada568 143
f85d901f 144IMPLEMENT_APP(MyApp)
f4ada568 145
f85d901f
GRG
146// ==========================================================================
147// implementation
148// ==========================================================================
f4ada568 149
f85d901f
GRG
150// --------------------------------------------------------------------------
151// the application class
152// --------------------------------------------------------------------------
f4ada568 153
f85d901f
GRG
154bool MyApp::OnInit()
155{
156 // Create the main application window
157 MyFrame *frame = new MyFrame();
f4ada568 158
f85d901f 159 // Show it and tell the application that it's our main window
f4ada568 160 frame->Show(TRUE);
f85d901f
GRG
161 SetTopWindow(frame);
162
163 // success
27529614 164 return TRUE;
f4ada568
GL
165}
166
f85d901f
GRG
167// --------------------------------------------------------------------------
168// main frame
169// --------------------------------------------------------------------------
170
171// frame constructor
172MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1,
25ba2b89 173 _("wxSocket demo: Client"),
f85d901f 174 wxDefaultPosition, wxSize(300, 200))
f4ada568 175{
f85d901f
GRG
176 // Give the frame an icon
177 SetIcon(wxICON(mondrian));
178
179 // Make menus
180 m_menuFile = new wxMenu();
25ba2b89 181 m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
f85d901f 182 m_menuFile->AppendSeparator();
25ba2b89 183 m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client"));
f85d901f
GRG
184
185 m_menuSocket = new wxMenu();
25ba2b89 186 m_menuSocket->Append(CLIENT_OPEN, _("&Open session"), _("Connect to server"));
f85d901f 187 m_menuSocket->AppendSeparator();
25ba2b89
GRG
188 m_menuSocket->Append(CLIENT_TEST1, _("Test &1"), _("Test basic functionality"));
189 m_menuSocket->Append(CLIENT_TEST2, _("Test &2"), _("Test ReadMsg and WriteMsg"));
190 m_menuSocket->Append(CLIENT_TEST3, _("Test &3"), _("Test large data transfer"));
f85d901f 191 m_menuSocket->AppendSeparator();
25ba2b89 192 m_menuSocket->Append(CLIENT_CLOSE, _("&Close session"), _("Close connection"));
f85d901f 193
6097c3a2 194 m_menuDatagramSocket = new wxMenu();
25ba2b89
GRG
195 m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets"));
196
197 m_menuProtocols = new wxMenu();
198 m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL"));
6097c3a2 199
f85d901f
GRG
200 // Append menus to the menubar
201 m_menuBar = new wxMenuBar();
25ba2b89
GRG
202 m_menuBar->Append(m_menuFile, _("&File"));
203 m_menuBar->Append(m_menuSocket, _("&SocketClient"));
204 m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket"));
205 m_menuBar->Append(m_menuProtocols, _("&Protocols"));
f85d901f
GRG
206 SetMenuBar(m_menuBar);
207
208 // Status bar
f4ada568 209 CreateStatusBar(2);
f85d901f 210
bd4d918f
GRG
211 // Make a textctrl for logging
212 m_text = new wxTextCtrl(this, -1,
4693b20c 213 _("Welcome to wxSocket demo: Client\nClient ready\n"),
bd4d918f 214 wxDefaultPosition, wxDefaultSize,
f85d901f
GRG
215 wxTE_MULTILINE | wxTE_READONLY);
216
217 // Create the socket
218 m_sock = new wxSocketClient();
bd4d918f
GRG
219
220 // Setup the event handler and subscribe to most events
f85d901f
GRG
221 m_sock->SetEventHandler(*this, SOCKET_ID);
222 m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG |
223 wxSOCKET_INPUT_FLAG |
224 wxSOCKET_LOST_FLAG);
225 m_sock->Notify(TRUE);
226
227 m_busy = FALSE;
228 UpdateStatusBar();
f4ada568
GL
229}
230
231MyFrame::~MyFrame()
232{
bd4d918f 233 // No delayed deletion here, as the frame is dying anyway
f85d901f 234 delete m_sock;
f4ada568
GL
235}
236
f85d901f
GRG
237// event handlers
238
239void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
f4ada568 240{
f85d901f 241 // TRUE is to force the frame to close
f4ada568
GL
242 Close(TRUE);
243}
244
f85d901f
GRG
245void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
246{
4693b20c 247 wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"),
25ba2b89 248 _("About Client"),
f85d901f
GRG
249 wxOK | wxICON_INFORMATION, this);
250}
251
252void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event))
f4ada568
GL
253{
254 wxIPV4address addr;
f4ada568 255
f85d901f
GRG
256 m_menuSocket->Enable(CLIENT_OPEN, FALSE);
257 m_menuSocket->Enable(CLIENT_CLOSE, FALSE);
258
25ba2b89 259 // Ask user for server address
f85d901f 260 wxString hostname = wxGetTextFromUser(
25ba2b89
GRG
261 _("Enter the address of the wxSocket demo server:"),
262 _("Connect ..."),
263 _("localhost"));
f85d901f
GRG
264
265 addr.Hostname(hostname);
f4ada568 266 addr.Service(3000);
f4ada568 267
25ba2b89
GRG
268 // Mini-tutorial for Connect() :-)
269 // ---------------------------
270 //
271 // There are two ways to use Connect(): blocking and non-blocking,
272 // depending on the value passed as the 'wait' (2nd) parameter.
273 //
274 // Connect(addr, TRUE) will wait until the connection completes,
275 // returning TRUE on success and FALSE on failure. This call blocks
276 // the GUI (this might be changed in future releases to honour the
277 // wxSOCKET_BLOCK flag).
278 //
279 // Connect(addr, FALSE) will issue a nonblocking connection request
280 // and return immediately. If the return value is TRUE, then the
281 // connection has been already succesfully established. If it is
282 // FALSE, you must wait for the request to complete, either with
283 // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST
284 // events (please read the documentation).
285 //
286 // WaitOnConnect() itself never blocks the GUI (this might change
287 // in the future to honour the wxSOCKET_BLOCK flag). This call will
bd4d918f
GRG
288 // return FALSE on timeout, or TRUE if the connection request
289 // completes, which in turn might mean:
290 //
291 // a) That the connection was successfully established
292 // b) That the connection request failed (for example, because
293 // it was refused by the peer.
294 //
25ba2b89
GRG
295 // Use IsConnected() to distinguish between these two.
296 //
297 // So, in a brief, you should do one of the following things:
298 //
299 // For blocking Connect:
300 //
a85139a1 301 // bool success = client->Connect(addr, TRUE);
25ba2b89
GRG
302 //
303 // For nonblocking Connect:
304 //
a85139a1 305 // client->Connect(addr, FALSE);
bd4d918f 306 //
a85139a1
GRG
307 // bool waitmore = TRUE;
308 // while (! client->WaitOnConnect(seconds, millis) && waitmore )
bd4d918f
GRG
309 // {
310 // // possibly give some feedback to the user,
311 // // update waitmore if needed.
312 // }
a85139a1 313 // bool success = client->IsConnected();
25ba2b89
GRG
314 //
315 // And that's all :-)
e51b0130 316
25ba2b89
GRG
317 m_text->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n"));
318 m_sock->Connect(addr, FALSE);
319 m_sock->WaitOnConnect(10);
f85d901f
GRG
320
321 if (m_sock->IsConnected())
25ba2b89 322 m_text->AppendText(_("Succeeded ! Connection established\n"));
f85d901f
GRG
323 else
324 {
325 m_sock->Close();
25ba2b89
GRG
326 m_text->AppendText(_("Failed ! Unable to connect\n"));
327 wxMessageBox(_("Can't connect to the specified host"), _("Alert !"));
f85d901f
GRG
328 }
329
330 UpdateStatusBar();
f4ada568
GL
331}
332
f85d901f 333void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
f4ada568 334{
4693b20c
MB
335 const wxChar *buf1;
336 wxChar *buf2;
6097c3a2 337 unsigned char len;
f85d901f
GRG
338
339 // Disable socket menu entries (exception: Close Session)
340 m_busy = TRUE;
341 UpdateStatusBar();
342
25ba2b89 343 m_text->AppendText(_("\n=== Test 1 begins ===\n"));
f85d901f
GRG
344
345 // Tell the server which test we are running
f4d5e009 346 unsigned char c = 0xBE;
f85d901f
GRG
347 m_sock->Write(&c, 1);
348
349 // Send some data and read it back. We know the size of the
350 // buffer, so we can specify the exact number of bytes to be
6097c3a2
GRG
351 // sent or received and use the wxSOCKET_WAITALL flag. Also,
352 // we have disabled menu entries which could interfere with
353 // the test, so we can safely avoid the wxSOCKET_BLOCK flag.
f85d901f
GRG
354 //
355 // First we send a byte with the length of the string, then
356 // we send the string itself (do NOT try to send any integral
6097c3a2 357 // value larger than a byte "as is" across the network, or
f85d901f
GRG
358 // you might be in trouble! Ever heard about big and little
359 // endian computers?)
e51b0130 360
f85d901f
GRG
361 m_sock->SetFlags(wxSOCKET_WAITALL);
362
25ba2b89 363 buf1 = _("Test string (less than 256 chars!)");
4693b20c
MB
364 len = (wxStrlen(buf1) + 1) * sizeof(wxChar);
365 buf2 = new wxChar[wxStrlen(buf1) + 1];
f85d901f 366
25ba2b89 367 m_text->AppendText(_("Sending a test buffer to the server ..."));
f187448d 368 m_sock->Write(&len, 1);
f85d901f 369 m_sock->Write(buf1, len);
25ba2b89 370 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
f85d901f 371
25ba2b89 372 m_text->AppendText(_("Receiving the buffer back from server ..."));
f85d901f 373 m_sock->Read(buf2, len);
25ba2b89 374 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
f85d901f 375
25ba2b89 376 m_text->AppendText(_("Comparing the two buffers ..."));
f85d901f
GRG
377 if (memcmp(buf1, buf2, len) != 0)
378 {
25ba2b89
GRG
379 m_text->AppendText(_("failed!\n"));
380 m_text->AppendText(_("Test 1 failed !\n"));
f85d901f
GRG
381 }
382 else
383 {
25ba2b89
GRG
384 m_text->AppendText(_("done\n"));
385 m_text->AppendText(_("Test 1 passed !\n"));
f85d901f 386 }
25ba2b89 387 m_text->AppendText(_("=== Test 1 ends ===\n"));
f4ada568 388
f85d901f
GRG
389 delete[] buf2;
390 m_busy = FALSE;
391 UpdateStatusBar();
392}
f4ada568 393
f85d901f
GRG
394void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
395{
4693b20c
MB
396 const wxChar *msg1;
397 wxChar *msg2;
f85d901f
GRG
398 size_t len;
399
400 // Disable socket menu entries (exception: Close Session)
401 m_busy = TRUE;
402 UpdateStatusBar();
403
25ba2b89 404 m_text->AppendText(_("\n=== Test 2 begins ===\n"));
f85d901f
GRG
405
406 // Tell the server which test we are running
f4d5e009 407 unsigned char c = 0xCE;
f85d901f
GRG
408 m_sock->Write(&c, 1);
409
410 // Here we use ReadMsg and WriteMsg to send messages with
411 // a header with size information. Also, the reception is
412 // event triggered, so we test input events as well.
413 //
414 // We need to set no flags here (ReadMsg and WriteMsg are
415 // not affected by flags)
e51b0130 416
f85d901f
GRG
417 m_sock->SetFlags(wxSOCKET_WAITALL);
418
419 wxString s = wxGetTextFromUser(
25ba2b89
GRG
420 _("Enter an arbitrary string to send to the server:"),
421 _("Test 2 ..."),
422 _("Yes I like wxWindows!"));
f85d901f 423
4693b20c
MB
424 msg1 = s.c_str();
425 len = (wxStrlen(msg1) + 1) * sizeof(wxChar);
426 msg2 = new wxChar[wxStrlen(msg1) + 1];
f85d901f 427
25ba2b89 428 m_text->AppendText(_("Sending the string with WriteMsg ..."));
f85d901f 429 m_sock->WriteMsg(msg1, len);
25ba2b89
GRG
430 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
431 m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n"));
f85d901f
GRG
432
433 // Wait until data available (will also return if the connection is lost)
434 m_sock->WaitForRead(2);
435
436 if (m_sock->IsData())
437 {
25ba2b89 438 m_text->AppendText(_("Reading the string back with ReadMsg ..."));
f85d901f 439 m_sock->ReadMsg(msg2, len);
25ba2b89
GRG
440 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
441 m_text->AppendText(_("Comparing the two buffers ..."));
f85d901f
GRG
442 if (memcmp(msg1, msg2, len) != 0)
443 {
25ba2b89
GRG
444 m_text->AppendText(_("failed!\n"));
445 m_text->AppendText(_("Test 2 failed !\n"));
f85d901f
GRG
446 }
447 else
448 {
25ba2b89
GRG
449 m_text->AppendText(_("done\n"));
450 m_text->AppendText(_("Test 2 passed !\n"));
f85d901f 451 }
765e386b 452 }
f85d901f 453 else
25ba2b89 454 m_text->AppendText(_("Timeout ! Test 2 failed.\n"));
765e386b 455
25ba2b89 456 m_text->AppendText(_("=== Test 2 ends ===\n"));
765e386b 457
6097c3a2 458 delete[] msg2;
f85d901f
GRG
459 m_busy = FALSE;
460 UpdateStatusBar();
765e386b
GL
461}
462
f85d901f 463void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
f4ada568 464{
6097c3a2
GRG
465 char *buf1;
466 char *buf2;
467 unsigned char len;
468
469 // Disable socket menu entries (exception: Close Session)
470 m_busy = TRUE;
471 UpdateStatusBar();
472
25ba2b89 473 m_text->AppendText(_("\n=== Test 3 begins ===\n"));
6097c3a2
GRG
474
475 // Tell the server which test we are running
f4d5e009 476 unsigned char c = 0xDE;
6097c3a2
GRG
477 m_sock->Write(&c, 1);
478
479 // This test also is similar to the first one but it sends a
480 // large buffer so that wxSocket is actually forced to split
481 // it into pieces and take care of sending everything before
482 // returning.
e51b0130 483
6097c3a2
GRG
484 m_sock->SetFlags(wxSOCKET_WAITALL);
485
486 // Note that len is in kbytes here!
f4d5e009 487 len = 32;
6097c3a2
GRG
488 buf1 = new char[len * 1024];
489 buf2 = new char[len * 1024];
490
491 for (int i = 0; i < len * 1024; i ++)
492 buf1[i] = (char)(i % 256);
493
25ba2b89 494 m_text->AppendText(_("Sending a large buffer (32K) to the server ..."));
f187448d 495 m_sock->Write(&len, 1);
6097c3a2 496 m_sock->Write(buf1, len * 1024);
25ba2b89 497 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
6097c3a2 498
25ba2b89 499 m_text->AppendText(_("Receiving the buffer back from server ..."));
6097c3a2 500 m_sock->Read(buf2, len * 1024);
25ba2b89 501 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
6097c3a2 502
25ba2b89 503 m_text->AppendText(_("Comparing the two buffers ..."));
6097c3a2
GRG
504 if (memcmp(buf1, buf2, len) != 0)
505 {
25ba2b89
GRG
506 m_text->AppendText(_("failed!\n"));
507 m_text->AppendText(_("Test 3 failed !\n"));
6097c3a2
GRG
508 }
509 else
510 {
25ba2b89
GRG
511 m_text->AppendText(_("done\n"));
512 m_text->AppendText(_("Test 3 passed !\n"));
6097c3a2 513 }
25ba2b89 514 m_text->AppendText(_("=== Test 3 ends ===\n"));
6097c3a2
GRG
515
516 delete[] buf2;
517 m_busy = FALSE;
518 UpdateStatusBar();
f4ada568
GL
519}
520
f85d901f 521void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event))
f4ada568 522{
f85d901f
GRG
523 m_sock->Close();
524 UpdateStatusBar();
f4ada568
GL
525}
526
6097c3a2
GRG
527void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event))
528{
f187448d 529 m_text->AppendText(_("\n=== Datagram test begins ===\n"));
36bec0ac
GRG
530 m_text->AppendText(_("Sorry, not implemented\n"));
531 m_text->AppendText(_("=== Datagram test ends ===\n"));
6097c3a2
GRG
532}
533
25ba2b89
GRG
534void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event))
535{
536 // Note that we are creating a new socket here, so this
537 // won't mess with the client/server demo.
538
539 // Ask for the URL
540 m_text->AppendText(_("\n=== URL test begins ===\n"));
541 wxString urlname = wxGetTextFromUser(_("Enter an URL to get"),
542 _("URL:"),
8dfea369 543 _T("http://localhost"));
25ba2b89
GRG
544
545 // Parse the URL
546 wxURL url(urlname);
6f505cae
GRG
547 if (url.GetError() != wxURL_NOERR)
548 {
549 m_text->AppendText(_("Error: couldn't parse URL\n"));
550 m_text->AppendText(_("=== URL test ends ===\n"));
551 return;
552 }
25ba2b89
GRG
553
554 // Try to get the input stream (connects to the given URL)
555 m_text->AppendText(_("Trying to establish connection...\n"));
556 wxYield();
557 wxInputStream *data = url.GetInputStream();
558 if (!data)
559 {
560 m_text->AppendText(_("Error: couldn't read from URL\n"));
561 m_text->AppendText(_("=== URL test ends ===\n"));
562 return;
563 }
564
565 // Print the contents type and file size
566 wxString s;
4693b20c 567 s.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
25ba2b89
GRG
568 url.GetProtocol().GetContentType().c_str(),
569 data->GetSize());
570 m_text->AppendText(s);
571 wxYield();
572
573 // Get the data
2b5f62a0 574 wxFileOutputStream sout( wxT("test.url") );
6f505cae
GRG
575 if (!sout.Ok())
576 {
577 m_text->AppendText(_("Error: couldn't open file for output\n"));
578 m_text->AppendText(_("=== URL test ends ===\n"));
579 return;
580 }
581
25ba2b89
GRG
582 data->Read(sout);
583 m_text->AppendText(_("Results written to file: test.url\n"));
584 m_text->AppendText(_("Done.\n"));
585 m_text->AppendText(_("=== URL test ends ===\n"));
586
587 delete data;
588}
589
f85d901f 590void MyFrame::OnSocketEvent(wxSocketEvent& event)
f4ada568 591{
25ba2b89 592 wxString s = _("OnSocketEvent: ");
f85d901f 593
bc7e88ae 594 switch(event.GetSocketEvent())
f85d901f 595 {
25ba2b89
GRG
596 case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break;
597 case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break;
598 case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break;
599 default : s.Append(_("Unexpected event !\n")); break;
f85d901f 600 }
f4ada568 601
f85d901f
GRG
602 m_text->AppendText(s);
603 UpdateStatusBar();
f4ada568
GL
604}
605
f85d901f 606// convenience functions
a324a7bc 607
f85d901f 608void MyFrame::UpdateStatusBar()
a324a7bc 609{
f85d901f 610 wxString s;
a324a7bc 611
f85d901f
GRG
612 if (!m_sock->IsConnected())
613 {
25ba2b89 614 s.Printf(_("Not connected"));
a324a7bc 615 }
f85d901f
GRG
616 else
617 {
618 wxIPV4address addr;
a324a7bc 619
f85d901f 620 m_sock->GetPeer(addr);
25ba2b89 621 s.Printf(_("%s : %d"), (addr.Hostname()).c_str(), addr.Service());
f85d901f 622 }
f4ada568 623
f85d901f 624 SetStatusText(s, 1);
a737331d 625
f85d901f
GRG
626 m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy);
627 m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy);
628 m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy);
629 m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy);
630 m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected());
f4ada568 631}