* Purpose: wxSocket: client demo
* Author: LAVAUX Guilhem
* Created: June 1997
- * Updated:
+ * CVS ID: $Id$
* Copyright: (c) 1997, LAVAUX Guilhem
*/
#include "wx/url.h"
#include "wx/protocol/http.h"
#include "wx/thread.h"
+#include "wx/progdlg.h"
#if defined(__WXMOTIF__) || defined(__WXGTK__)
#include "mondrian.xpm"
void OnExecCloseConnection(wxCommandEvent& evt);
void UpdateStatus();
+ void Download(wxInputStream *input);
+
DECLARE_EVENT_TABLE()
};
public:
MyFrame *frame;
- void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); }
+ void OnNotify(GSocketEventFlags WXUNUSED(flags)) { frame->UpdateStatus(); }
};
// ID for the menu quit command
wxFrame(NULL, -1, "wxSocket client demo",
wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
{
- // Init all
- wxSocketHandler::Master();
-
sock = new MyClient();
sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
- wxSocketHandler::Master().Register(sock);
sock->frame = this;
- sock->SetNotify(wxSocketBase::REQ_LOST);
+ sock->SetNotify(GSOCK_LOST_FLAG);
CreateStatusBar(2);
UpdateStatus();
}
if (sock->IsConnected())
sock->Close();
-/*
wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
"Connect ...", "localhost");
-*/
- wxString hname = "localhost";
addr.Hostname(hname);
addr.Service(3000);
sock->SetNotify(0);
sock->Connect(addr, TRUE);
+ sock->SetFlags(wxSocketBase::NONE);
if (!sock->IsConnected())
wxMessageBox("Can't connect to the specified host", "Alert !");
void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
{
- if (sock)
- sock->Close();
+ sock->Close();
UpdateStatus();
}
delete dlgbox;
}
+
+void MyFrame::Download(wxInputStream *input)
+{
+ wxProgressDialog progress("Downloading ...", "0% downloaded");
+ wxBufferedInputStream buf_in(*input);
+ wxFileOutputStream f_out("test.url");
+
+ size_t file_size = input->StreamSize();
+ size_t downloaded;
+ int BUFSIZE = (file_size > 100) ? (file_size / 100) : file_size;
+ int bytes_read = BUFSIZE;
+ wxString message;
+ int percents;
+
+ char *buf;
+
+// TODO: Support for streams which don't support StreamSize
+
+ buf = new char[BUFSIZE];
+
+ downloaded = 0;
+ bytes_read = BUFSIZE;
+ while (downloaded < file_size && bytes_read != 0) {
+ bytes_read = buf_in.Read(buf, BUFSIZE).LastRead();
+ f_out.Write(buf, bytes_read);
+ downloaded += bytes_read;
+
+ percents = downloaded * 100 / file_size;
+
+ message = _T("");
+ message << percents << _T("% downloaded");
+ progress.Update(percents, message);
+ }
+
+ delete[] buf;
+}
+
void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
{
wxString urlname = wxGetTextFromUser("Enter an URL to get",
wxURL url(urlname);
wxInputStream *datas = url.GetInputStream();
- if (!datas)
- wxMessageBox("Error in getting data from the URL.", "Alert !");
- else {
- wxFileOutputStream *str_out = new wxFileOutputStream("test.url");
- str_out->Write(*datas);
+ if (!datas) {
+ wxString error;
+ error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError());
+ wxMessageBox(error, "Alert !");
+ } else {
+ Download(datas);
- wxMessageBox("Success !! Click on OK to see the text.", "OK");
delete datas;
- delete str_out;
}
}