From 1e4080f02e3b624be72914c9d25f4e12b163d2ac Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 30 Dec 2008 13:51:02 +0000 Subject: [PATCH] prettify and simplify the URL test; use a URL more likely to run a web server than localhost by default git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57665 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/sockets/client.cpp | 101 ++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/samples/sockets/client.cpp b/samples/sockets/client.cpp index 9147a03045..e0604c063c 100644 --- a/samples/sockets/client.cpp +++ b/samples/sockets/client.cpp @@ -31,7 +31,9 @@ #include "wx/socket.h" #include "wx/url.h" -#include "wx/wfstream.h" +#include "wx/sstream.h" +#include "wx/scopeguard.h" +#include // -------------------------------------------------------------------------- // resources @@ -212,7 +214,8 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY, #if wxUSE_URL m_menuProtocols = new wxMenu(); - m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL")); + m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL\tCtrl-U"), + _("Get data from the specified URL")); #endif // Append menus to the menubar @@ -235,6 +238,7 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY, _("Welcome to wxSocket demo: Client\nClient ready\n"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY); + delete wxLog::SetActiveTarget(new wxLogTextCtrl(m_text)); // Create the socket m_sock = new wxSocketClient(); @@ -577,62 +581,55 @@ void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event)) { - // Note that we are creating a new socket here, so this - // won't mess with the client/server demo. - - // Ask for the URL - m_text->AppendText(_("\n=== URL test begins ===\n")); - wxString urlname = wxGetTextFromUser(_("Enter an URL to get"), - _("URL:"), - _T("http://localhost")); - - // Parse the URL - wxURL url(urlname); - if (url.GetError() != wxURL_NOERR) - { - m_text->AppendText(_("Error: couldn't parse URL\n")); - m_text->AppendText(_("=== URL test ends ===\n")); - return; - } - - // Try to get the input stream (connects to the given URL) - m_text->AppendText(_("Trying to establish connection...\n")); - wxYield(); - wxInputStream *data = url.GetInputStream(); - if (!data) - { - m_text->AppendText(_("Error: couldn't read from URL\n")); - m_text->AppendText(_("=== URL test ends ===\n")); - return; - } + // Ask for the URL + static wxString s_urlname("http://www.google.com/"); + wxString urlname = wxGetTextFromUser + ( + _("Enter an URL to get"), + _("URL:"), + s_urlname + ); + if ( urlname.empty() ) + return; // cancelled by user + + s_urlname = urlname; + + + wxLogMessage("=== URL test begins ==="); + wxON_BLOCK_EXIT1( wxLogMessage, "=== URL test ends ===" ); + + // Parse the URL + wxURL url(urlname); + if ( url.GetError() != wxURL_NOERR ) + { + wxLogError("Failed to parse URL \"%s\"", urlname); + return; + } - // Print the contents type and file size - wxString s; - s.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"), - url.GetProtocol().GetContentType().c_str(), - data->GetSize()); - m_text->AppendText(s); - wxYield(); + // Try to get the input stream (connects to the given URL) + wxLogMessage("Establishing connection to \"%s\"...", urlname); + const std::auto_ptr data(url.GetInputStream()); + if ( !data.get() ) + { + wxLogError("Failed to retrieve URL \"%s\"", urlname); + return; + } - // Get the data - wxFile fileTest(wxT("test.url"), wxFile::write); - wxFileOutputStream sout(fileTest); - if (!sout.Ok()) - { - m_text->AppendText(_("Error: couldn't open file for output\n")); - m_text->AppendText(_("=== URL test ends ===\n")); - return; - } + // Print the contents type and file size + wxLogMessage("Contents type: %s\nFile size: %i\nStarting to download...", + url.GetProtocol().GetContentType(), + data->GetSize()); - data->Read(sout); - m_text->AppendText(_("Results written to file: test.url\n")); - m_text->AppendText(_("Done.\n")); - m_text->AppendText(_("=== URL test ends ===\n")); + // Get the data + wxStringOutputStream sout; + if ( data->Read(sout).GetLastError() != wxSTREAM_EOF ) + wxLogError("Error reading the input stream."); - delete data; + wxLogMessage("Text retrieved from URL \"%s\" follows:\n%s", + urlname, sout.GetString()); } -#endif +#endif // wxUSE_URL void MyFrame::OnSocketEvent(wxSocketEvent& event) { -- 2.45.2