]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/wxsocket/client.cpp
Makefile for mingw/gcc-2.95
[wxWidgets.git] / samples / wxsocket / client.cpp
index 7f1131330a76acda15db80f763daea5c14c6c1da..4d14e9cb941f00b3a3094e1e4ea813ba02f8465c 100644 (file)
@@ -1,12 +1,12 @@
 /*
  * File:       client.cpp
  * Purpose:    wxSocket: client demo
- * Author:     LAVAUX Guilhem (from minimal.cc)
+ * Author:     LAVAUX Guilhem
  * Created:    June 1997
- * Updated:    
- * Copyright:  (c) 1993, AIAI, University of Edinburgh
- *             (C) 1997, LAVAUX Guilhem
+ * CVS ID:     $Id$
+ * Copyright:  (c) 1997, LAVAUX Guilhem
  */
+
 #ifdef __GNUG__
 #pragma implementation
 #pragma interface
 #ifndef WX_PRECOMP
 #include "wx/wx.h"
 #endif
+
+#include "wx/wfstream.h"
 #include "wx/socket.h"
 #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"
+#endif
 
 // Define a new application type
 class MyApp: public wxApp
@@ -40,6 +48,7 @@ class MyFrame: public wxFrame
   DECLARE_CLASS(MyFrame)
 public:
   MyClient *sock;
+  int m_good;
 
   MyFrame(void);
   virtual ~MyFrame();
@@ -49,8 +58,11 @@ public:
   void OnQuitApp(wxCommandEvent& evt);
   void OnExecOpenConnection(wxCommandEvent& evt);
   void OnExecCloseConnection(wxCommandEvent& evt);
+  void OnSocketEvent(wxSocketEvent& evt);
   void UpdateStatus();
 
+  void Download(wxInputStream *input);
+
   DECLARE_EVENT_TABLE()
 };
 
@@ -65,17 +77,18 @@ class MyClient: public wxSocketClient
 public:
   MyFrame *frame;
 
-  void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); }
+  void OnNotify(GSocketEventFlags WXUNUSED(flags)) { frame->UpdateStatus(); }
 };
 
 // ID for the menu quit command
-const SKDEMO_QUIT    = 101;
-const SKDEMO_CONNECT = 102;
-const SKDEMO_TEST1   = 103;
-const SKDEMO_TEST2   = 104;
-const SKDEMO_CLOSE   = 105;
-const SKDEMO_TEST3   = 106;
-const ID_TEST_CLOSE  = 107;
+const int SKDEMO_QUIT    = 101;
+const int SKDEMO_CONNECT = 102;
+const int SKDEMO_TEST1   = 103;
+const int SKDEMO_TEST2   = 104;
+const int SKDEMO_CLOSE   = 105;
+const int SKDEMO_TEST3   = 106;
+const int ID_TEST_CLOSE  = 107;
+const int SKDEMO_SCK     = 108;
 
 IMPLEMENT_APP(MyApp)
 
@@ -88,12 +101,7 @@ bool MyApp::OnInit(void)
   MyFrame *frame = new MyFrame();
 
   // Give it an icon
-#ifdef wx_msw
-  frame->SetIcon(new wxIcon("mondrian"));
-#endif
-#ifdef wx_x
-  frame->SetIcon(new wxIcon("mondrian.xbm"));
-#endif
+  frame->SetIcon(wxICON(mondrian));
 
   // Make a menubar
   wxMenu *file_menu = new wxMenu();
@@ -116,7 +124,7 @@ bool MyApp::OnInit(void)
   frame->SetMenuBar(menu_bar);
 
   // Make a panel with a message
-  (void)new wxPanel(frame, 0, 0, 300, 100);
+  (void)new wxPanel(frame, -1, wxPoint(0, 0), wxSize(300, 100));
 
   // Show the frame
   frame->Show(TRUE);
@@ -132,14 +140,10 @@ MyFrame::MyFrame():
   wxFrame(NULL, -1, "wxSocket client demo",
           wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
 {
-  // Init all
-  wxSocketHandler::Master();
-
   sock = new MyClient();
-  sock->SetFlags(wxSocketBase::WAITALL);
-  wxSocketHandler::Master().Register(sock);
+  sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
   sock->frame = this;
-  sock->SetNotify(wxSocketBase::REQ_LOST);
+  sock->SetNotify(wxSOCKET_LOST_FLAG);
   CreateStatusBar(2);
   UpdateStatus();
 }
@@ -165,8 +169,9 @@ void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
                                   "Connect ...", "localhost");
   addr.Hostname(hname);
   addr.Service(3000);
-  sock->SetNotify(0);
-  sock->Connect(addr, TRUE);
+  sock->Connect(addr, FALSE);
+  sock->WaitOnConnect(10);
+  sock->SetFlags(wxSocketBase::NONE);
   if (!sock->IsConnected())
     wxMessageBox("Can't connect to the specified host", "Alert !");
 
@@ -175,8 +180,7 @@ void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
 
 void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
 {
-  if (sock)
-    sock->Close();
+  sock->Close();
   UpdateStatus();
 }
 
@@ -187,8 +191,23 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
   EVT_MENU(SKDEMO_QUIT, MyFrame::OnQuitApp)
   EVT_MENU(SKDEMO_CONNECT, MyFrame::OnExecOpenConnection)
   EVT_MENU(SKDEMO_CLOSE, MyFrame::OnExecCloseConnection)
+  EVT_SOCKET(SKDEMO_SCK, MyFrame::OnSocketEvent)
 END_EVENT_TABLE()
 
+class MyFrameSocketTimer: public wxTimer {
+ public:
+  void Notify() {
+    *m_var = 0;
+  }
+
+  int *m_var;
+};
+
+void MyFrame::OnSocketEvent(wxSocketEvent& evt)
+{
+  m_good = 1;
+}
+
 void MyFrame::OnCloseTest(wxCommandEvent& evt)
 {
   wxButton *button = (wxButton *)evt.GetEventObject();
@@ -204,12 +223,12 @@ void MyFrame::UpdateStatus()
     SetStatusText("", 1);
   } else {
     wxIPV4address addr;
-    char s[100];
+    wxChar s[100];
 
     sock->GetPeer(addr);
-    sprintf(s, "Connected to %s", (const char *)addr.Hostname());
+    wxSprintf(s, _T("Connected to %s"), WXSTRINGCAST addr.Hostname());
     SetStatusText(s, 0);
-    sprintf(s, "Service: %d", addr.Service());
+    wxSprintf(s, _T("Service: %d"), addr.Service());
     SetStatusText(s, 1);
   } 
 }
@@ -219,40 +238,83 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
   if (!sock->IsConnected())
     return;
 
-  wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(410, 270));
+  wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 280));
   wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "",
                                         wxPoint(0, 0), wxSize(400, 200),
                                        wxTE_MULTILINE);
   (void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close",
-                     wxPoint(100, 210), wxSize(100, 40));
-  char *buf, *buf2;
+                     wxPoint(100, 210), wxSize(100, -1));
+  wxChar *buf, *buf2;
 
   dlgbox->Layout();
   dlgbox->Show(TRUE);
 
   text_win->WriteText("Initializing test 1 ...\n");
   
+  wxYield();
+  
   /* Init */
-  buf = copystring("Salut ! Salut ! Salut ! Salut Toto\n");
-  buf2 = new char[strlen(buf)+1];
+  buf = copystring(_T("Hi ! Hi ! Hi !\n"));
+  buf2 = new wxChar[wxStrlen(buf)+1];
   char c = 0xbe;
-  sock->WriteMsg(&c, 1);
+  sock->Write(&c, 1);
 
   /* No 1 */
   text_win->WriteText("Sending some byte to the server ...");
-  sock->Write(buf, strlen(buf)+1);
+  wxYield();
+  sock->Write((char *)buf, wxStrlen(buf)+1);
   text_win->WriteText("done\n");
+  wxYield();
   text_win->WriteText("Receiving some byte from the server ...");
-  sock->Read(buf2, strlen(buf)+1);
+  wxYield();
+  sock->Read((char *)buf2, wxStrlen(buf)+1);
   text_win->WriteText("done\n");
+  wxYield();
   
   text_win->WriteText("Comparing the two buffers ...");
-  if (memcmp(buf, buf2, strlen(buf)+1) != 0) {
+  if (memcmp(buf, buf2, wxStrlen(buf)+1) != 0) {
     text_win->WriteText("Fail\n");
     sock->Close();
     UpdateStatus();
   } else
-    text_win->WriteText("done\nTest 1 passed !\n");
+    text_win->WriteText("done\nTest 1A passed !\n");
+
+  /* No 2 */
+  sock->SetEventHandler(*this, SKDEMO_SCK);
+  sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
+  sock->Notify(TRUE);
+  text_win->WriteText("Test 1B: sending bytes to the server\n");
+  if (!sock->IsData())
+    text_win->WriteText("No data to read yet (this is OK)\n");
+
+  wxYield();
+  sock->Write((char *)buf, wxStrlen(buf)+1);
+  text_win->WriteText("Waiting for incoming bytes (timeout = 2 sec) ...");
+  wxYield();
+  
+  m_good = 2;
+
+  MyFrameSocketTimer timer;
+
+  timer.m_var = &m_good; 
+  timer.Start(2000, TRUE);
+
+  while (m_good == 2)
+   wxYield();
+
+  if (!m_good) {
+    text_win->WriteText("timeout ! Failed.\n");
+    sock->Close();
+    UpdateStatus();
+  } else
+    text_win->WriteText("event ! (no timeout).\n");
+
+  if (sock->IsData())
+    text_win->WriteText("Data is available, as expected...\n");
+
+  sock->Read((char *)buf2, wxStrlen(buf)+1);
+
+  text_win->WriteText("Success!\n");
 
   dlgbox->Layout();
   dlgbox->ShowModal();
@@ -263,6 +325,48 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
   delete dlgbox;
 }
 
+
+void MyFrame::Download(wxInputStream *input)
+{
+  wxProgressDialog progress("Downloading ...", "0% downloaded");
+  wxFileOutputStream f_out("test.url");
+  size_t downloaded;
+  int BUFSIZE, bytes_read;
+  size_t file_size;
+  wxString message;
+  int percents;
+
+  char *buf;
+
+  if (input->GetSize() == (size_t)-1) {
+    file_size = (size_t)-1;
+    bytes_read = BUFSIZE = 10240;
+  } else {
+    file_size = input->GetSize();
+    if (file_size > 10240)
+      bytes_read = BUFSIZE = file_size / 1024;
+    else
+      bytes_read = BUFSIZE = 1024;
+  }
+  buf = new char[BUFSIZE];
+
+  downloaded = 0;
+  bytes_read = BUFSIZE;
+  while (downloaded < file_size && bytes_read != 0) {
+    bytes_read = input->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",
@@ -271,10 +375,13 @@ void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
   wxURL url(urlname);
   wxInputStream *datas = url.GetInputStream();
 
-  if (!datas)
-    wxMessageBox("Error in getting data from the URL.", "Alert !");
-  else {
-    wxMessageBox("Success !! Click on OK to see the text.", "OK");
+  if (!datas) {
+    wxString error;
+    error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError());
+    wxMessageBox(error, "Alert !");
+  } else {
+    Download(datas);
+
     delete datas;
   }
 }