]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/client.cpp
Added the forgotten file txtstrm.tex
[wxWidgets.git] / samples / wxsocket / client.cpp
CommitLineData
f4ada568
GL
1/*
2 * File: client.cpp
3 * Purpose: wxSocket: client demo
e2a6f233 4 * Author: LAVAUX Guilhem
f4ada568 5 * Created: June 1997
062c4861 6 * CVS ID: $Id$
e2a6f233 7 * Copyright: (c) 1997, LAVAUX Guilhem
f4ada568 8 */
e2a6f233 9
f4ada568
GL
10#ifdef __GNUG__
11#pragma implementation
12#pragma interface
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
23#include "wx/wx.h"
24#endif
e2a6f233 25
a737331d 26#include "wx/wfstream.h"
f4ada568
GL
27#include "wx/socket.h"
28#include "wx/url.h"
29#include "wx/protocol/http.h"
6b3eb77a 30#include "wx/thread.h"
a324a7bc 31#include "wx/progdlg.h"
f4ada568 32
e2a6f233
JS
33#if defined(__WXMOTIF__) || defined(__WXGTK__)
34#include "mondrian.xpm"
35#endif
36
f4ada568
GL
37// Define a new application type
38class MyApp: public wxApp
39{ public:
40 virtual bool OnInit(void);
41};
42
43class MyClient;
44
45// Define a new frame type
46class MyFrame: public wxFrame
47{
48 DECLARE_CLASS(MyFrame)
49public:
50 MyClient *sock;
51
52 MyFrame(void);
53 virtual ~MyFrame();
54 void OnCloseTest(wxCommandEvent& evt);
55 void OnExecTest1(wxCommandEvent& evt);
56 void OnExecUrlTest(wxCommandEvent& evt);
57 void OnQuitApp(wxCommandEvent& evt);
58 void OnExecOpenConnection(wxCommandEvent& evt);
59 void OnExecCloseConnection(wxCommandEvent& evt);
60 void UpdateStatus();
61
a324a7bc
GL
62 void Download(wxInputStream *input);
63
f4ada568
GL
64 DECLARE_EVENT_TABLE()
65};
66
67
68IMPLEMENT_CLASS(MyFrame, wxFrame)
69
70/*
71 * Define a new derived SocketClient
72 */
73class MyClient: public wxSocketClient
74{
75public:
76 MyFrame *frame;
77
a324a7bc 78 void OnNotify(GSocketEventFlags WXUNUSED(flags)) { frame->UpdateStatus(); }
f4ada568
GL
79};
80
81// ID for the menu quit command
db131261
RR
82const int SKDEMO_QUIT = 101;
83const int SKDEMO_CONNECT = 102;
84const int SKDEMO_TEST1 = 103;
85const int SKDEMO_TEST2 = 104;
86const int SKDEMO_CLOSE = 105;
87const int SKDEMO_TEST3 = 106;
88const int ID_TEST_CLOSE = 107;
f4ada568
GL
89
90IMPLEMENT_APP(MyApp)
91
92/*
93 * `Main program' equivalent, creating windows and returning main app frame
94 */
95bool MyApp::OnInit(void)
96{
97 // Create the main frame window
98 MyFrame *frame = new MyFrame();
99
100 // Give it an icon
e2a6f233 101 frame->SetIcon(wxICON(mondrian));
f4ada568
GL
102
103 // Make a menubar
104 wxMenu *file_menu = new wxMenu();
105
106 file_menu->Append(SKDEMO_QUIT, "Exit");
107 wxMenuBar *menu_bar = new wxMenuBar;
108 menu_bar->Append(file_menu, "File");
109
110 wxMenu *socket_menu = new wxMenu();
111 socket_menu->Append(SKDEMO_CONNECT, "Open session");
112 socket_menu->AppendSeparator();
113 socket_menu->Append(SKDEMO_TEST1, "Start test 1");
f4ada568
GL
114 socket_menu->AppendSeparator();
115 socket_menu->Append(SKDEMO_CLOSE, "Close session");
116 socket_menu->AppendSeparator();
117 socket_menu->Append(SKDEMO_TEST3, "Start URL test");
118
119 menu_bar->Append(socket_menu, "Socket");
120
121 frame->SetMenuBar(menu_bar);
122
123 // Make a panel with a message
e2a6f233 124 (void)new wxPanel(frame, -1, wxPoint(0, 0), wxSize(300, 100));
f4ada568
GL
125
126 // Show the frame
127 frame->Show(TRUE);
128
129 // Return the main frame window
27529614 130 return TRUE;
f4ada568
GL
131}
132
133/*
134 * MyFrame Constructor
135 */
136MyFrame::MyFrame():
137 wxFrame(NULL, -1, "wxSocket client demo",
138 wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
139{
f4ada568 140 sock = new MyClient();
9111db68 141 sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
f4ada568 142 sock->frame = this;
a324a7bc 143 sock->SetNotify(GSOCK_LOST_FLAG);
f4ada568
GL
144 CreateStatusBar(2);
145 UpdateStatus();
146}
147
148MyFrame::~MyFrame()
149{
150 delete sock;
151}
152
153void MyFrame::OnQuitApp(wxCommandEvent& WXUNUSED(evt))
154{
155 Close(TRUE);
156}
157
158void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
159{
160 wxIPV4address addr;
161
162 if (sock->IsConnected())
163 sock->Close();
164
165 wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
166 "Connect ...", "localhost");
167 addr.Hostname(hname);
168 addr.Service(3000);
169 sock->SetNotify(0);
170 sock->Connect(addr, TRUE);
a324a7bc 171 sock->SetFlags(wxSocketBase::NONE);
f4ada568
GL
172 if (!sock->IsConnected())
173 wxMessageBox("Can't connect to the specified host", "Alert !");
174
175 UpdateStatus();
176}
177
178void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
179{
a324a7bc 180 sock->Close();
f4ada568
GL
181 UpdateStatus();
182}
183
184BEGIN_EVENT_TABLE(MyFrame, wxFrame)
185 EVT_BUTTON(ID_TEST_CLOSE, MyFrame::OnCloseTest)
186 EVT_MENU(SKDEMO_TEST1, MyFrame::OnExecTest1)
187 EVT_MENU(SKDEMO_TEST3, MyFrame::OnExecUrlTest)
188 EVT_MENU(SKDEMO_QUIT, MyFrame::OnQuitApp)
189 EVT_MENU(SKDEMO_CONNECT, MyFrame::OnExecOpenConnection)
190 EVT_MENU(SKDEMO_CLOSE, MyFrame::OnExecCloseConnection)
191END_EVENT_TABLE()
192
193void MyFrame::OnCloseTest(wxCommandEvent& evt)
194{
195 wxButton *button = (wxButton *)evt.GetEventObject();
196 wxDialog *dlg = (wxDialog *)button->GetParent();
197
198 dlg->EndModal(0);
199}
200
201void MyFrame::UpdateStatus()
202{
203 if (!sock->IsConnected()) {
204 SetStatusText("Not connected", 0);
205 SetStatusText("", 1);
206 } else {
207 wxIPV4address addr;
a737331d 208 wxChar s[100];
f4ada568
GL
209
210 sock->GetPeer(addr);
a737331d 211 wxSprintf(s, _T("Connected to %s"), WXSTRINGCAST addr.Hostname());
f4ada568 212 SetStatusText(s, 0);
a737331d 213 wxSprintf(s, _T("Service: %d"), addr.Service());
f4ada568
GL
214 SetStatusText(s, 1);
215 }
216}
217
218void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
219{
220 if (!sock->IsConnected())
221 return;
222
db131261 223 wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250));
f4ada568
GL
224 wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "",
225 wxPoint(0, 0), wxSize(400, 200),
226 wxTE_MULTILINE);
227 (void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close",
db131261 228 wxPoint(100, 210), wxSize(100, -1));
a737331d 229 wxChar *buf, *buf2;
f4ada568
GL
230
231 dlgbox->Layout();
232 dlgbox->Show(TRUE);
233
234 text_win->WriteText("Initializing test 1 ...\n");
235
db131261
RR
236 wxYield();
237
f4ada568 238 /* Init */
a737331d
GL
239 buf = copystring(_T("Hi ! Hi ! Hi !\n"));
240 buf2 = new wxChar[wxStrlen(buf)+1];
f4ada568 241 char c = 0xbe;
a737331d 242 sock->Write(&c, 1);
f4ada568
GL
243
244 /* No 1 */
245 text_win->WriteText("Sending some byte to the server ...");
a737331d
GL
246 wxYield();
247 sock->Write((char *)buf, wxStrlen(buf)+1);
f4ada568 248 text_win->WriteText("done\n");
a737331d 249 wxYield();
f4ada568 250 text_win->WriteText("Receiving some byte from the server ...");
a737331d
GL
251 wxYield();
252 sock->Read((char *)buf2, wxStrlen(buf)+1);
f4ada568 253 text_win->WriteText("done\n");
a737331d 254 wxYield();
f4ada568
GL
255
256 text_win->WriteText("Comparing the two buffers ...");
a737331d 257 if (memcmp(buf, buf2, wxStrlen(buf)+1) != 0) {
f4ada568
GL
258 text_win->WriteText("Fail\n");
259 sock->Close();
260 UpdateStatus();
261 } else
262 text_win->WriteText("done\nTest 1 passed !\n");
263
264 dlgbox->Layout();
265 dlgbox->ShowModal();
266
267 delete [] buf;
268 delete [] buf2;
269 delete text_win;
270 delete dlgbox;
271}
272
a324a7bc
GL
273
274void MyFrame::Download(wxInputStream *input)
275{
276 wxProgressDialog progress("Downloading ...", "0% downloaded");
277 wxBufferedInputStream buf_in(*input);
278 wxFileOutputStream f_out("test.url");
279
280 size_t file_size = input->StreamSize();
281 size_t downloaded;
282 int BUFSIZE = (file_size > 100) ? (file_size / 100) : file_size;
283 int bytes_read = BUFSIZE;
284 wxString message;
285 int percents;
286
287 char *buf;
288
289// TODO: Support for streams which don't support StreamSize
290
291 buf = new char[BUFSIZE];
292
293 downloaded = 0;
294 bytes_read = BUFSIZE;
295 while (downloaded < file_size && bytes_read != 0) {
296 bytes_read = buf_in.Read(buf, BUFSIZE).LastRead();
297 f_out.Write(buf, bytes_read);
298 downloaded += bytes_read;
299
300 percents = downloaded * 100 / file_size;
301
302 message = _T("");
303 message << percents << _T("% downloaded");
304 progress.Update(percents, message);
305 }
306
307 delete[] buf;
308}
309
f4ada568
GL
310void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
311{
856d2e52
GL
312 wxString urlname = wxGetTextFromUser("Enter an URL to get",
313 "URL:", "http://localhost");
f4ada568
GL
314
315 wxURL url(urlname);
316 wxInputStream *datas = url.GetInputStream();
317
062c4861
GL
318 if (!datas) {
319 wxString error;
320 error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError());
321 wxMessageBox(error, "Alert !");
322 } else {
a324a7bc 323 Download(datas);
a737331d 324
f4ada568
GL
325 delete datas;
326 }
327}