]>
Commit | Line | Data |
---|---|---|
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" |
f4ada568 | 31 | |
e2a6f233 JS |
32 | #if defined(__WXMOTIF__) || defined(__WXGTK__) |
33 | #include "mondrian.xpm" | |
34 | #endif | |
35 | ||
f4ada568 GL |
36 | // Define a new application type |
37 | class MyApp: public wxApp | |
38 | { public: | |
39 | virtual bool OnInit(void); | |
40 | }; | |
41 | ||
42 | class MyClient; | |
43 | ||
44 | // Define a new frame type | |
45 | class MyFrame: public wxFrame | |
46 | { | |
47 | DECLARE_CLASS(MyFrame) | |
48 | public: | |
49 | MyClient *sock; | |
50 | ||
51 | MyFrame(void); | |
52 | virtual ~MyFrame(); | |
53 | void OnCloseTest(wxCommandEvent& evt); | |
54 | void OnExecTest1(wxCommandEvent& evt); | |
55 | void OnExecUrlTest(wxCommandEvent& evt); | |
56 | void OnQuitApp(wxCommandEvent& evt); | |
57 | void OnExecOpenConnection(wxCommandEvent& evt); | |
58 | void OnExecCloseConnection(wxCommandEvent& evt); | |
59 | void UpdateStatus(); | |
60 | ||
61 | DECLARE_EVENT_TABLE() | |
62 | }; | |
63 | ||
64 | ||
65 | IMPLEMENT_CLASS(MyFrame, wxFrame) | |
66 | ||
67 | /* | |
68 | * Define a new derived SocketClient | |
69 | */ | |
70 | class MyClient: public wxSocketClient | |
71 | { | |
72 | public: | |
73 | MyFrame *frame; | |
74 | ||
75 | void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); } | |
76 | }; | |
77 | ||
78 | // ID for the menu quit command | |
db131261 RR |
79 | const int SKDEMO_QUIT = 101; |
80 | const int SKDEMO_CONNECT = 102; | |
81 | const int SKDEMO_TEST1 = 103; | |
82 | const int SKDEMO_TEST2 = 104; | |
83 | const int SKDEMO_CLOSE = 105; | |
84 | const int SKDEMO_TEST3 = 106; | |
85 | const int ID_TEST_CLOSE = 107; | |
f4ada568 GL |
86 | |
87 | IMPLEMENT_APP(MyApp) | |
88 | ||
89 | /* | |
90 | * `Main program' equivalent, creating windows and returning main app frame | |
91 | */ | |
92 | bool MyApp::OnInit(void) | |
93 | { | |
94 | // Create the main frame window | |
95 | MyFrame *frame = new MyFrame(); | |
96 | ||
97 | // Give it an icon | |
e2a6f233 | 98 | frame->SetIcon(wxICON(mondrian)); |
f4ada568 GL |
99 | |
100 | // Make a menubar | |
101 | wxMenu *file_menu = new wxMenu(); | |
102 | ||
103 | file_menu->Append(SKDEMO_QUIT, "Exit"); | |
104 | wxMenuBar *menu_bar = new wxMenuBar; | |
105 | menu_bar->Append(file_menu, "File"); | |
106 | ||
107 | wxMenu *socket_menu = new wxMenu(); | |
108 | socket_menu->Append(SKDEMO_CONNECT, "Open session"); | |
109 | socket_menu->AppendSeparator(); | |
110 | socket_menu->Append(SKDEMO_TEST1, "Start test 1"); | |
f4ada568 GL |
111 | socket_menu->AppendSeparator(); |
112 | socket_menu->Append(SKDEMO_CLOSE, "Close session"); | |
113 | socket_menu->AppendSeparator(); | |
114 | socket_menu->Append(SKDEMO_TEST3, "Start URL test"); | |
115 | ||
116 | menu_bar->Append(socket_menu, "Socket"); | |
117 | ||
118 | frame->SetMenuBar(menu_bar); | |
119 | ||
120 | // Make a panel with a message | |
e2a6f233 | 121 | (void)new wxPanel(frame, -1, wxPoint(0, 0), wxSize(300, 100)); |
f4ada568 GL |
122 | |
123 | // Show the frame | |
124 | frame->Show(TRUE); | |
125 | ||
126 | // Return the main frame window | |
27529614 | 127 | return TRUE; |
f4ada568 GL |
128 | } |
129 | ||
130 | /* | |
131 | * MyFrame Constructor | |
132 | */ | |
133 | MyFrame::MyFrame(): | |
134 | wxFrame(NULL, -1, "wxSocket client demo", | |
135 | wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE) | |
136 | { | |
137 | // Init all | |
138 | wxSocketHandler::Master(); | |
139 | ||
140 | sock = new MyClient(); | |
9111db68 | 141 | sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED)); |
f4ada568 GL |
142 | wxSocketHandler::Master().Register(sock); |
143 | sock->frame = this; | |
144 | sock->SetNotify(wxSocketBase::REQ_LOST); | |
145 | CreateStatusBar(2); | |
146 | UpdateStatus(); | |
147 | } | |
148 | ||
149 | MyFrame::~MyFrame() | |
150 | { | |
151 | delete sock; | |
152 | } | |
153 | ||
154 | void MyFrame::OnQuitApp(wxCommandEvent& WXUNUSED(evt)) | |
155 | { | |
156 | Close(TRUE); | |
157 | } | |
158 | ||
159 | void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt)) | |
160 | { | |
161 | wxIPV4address addr; | |
162 | ||
163 | if (sock->IsConnected()) | |
164 | sock->Close(); | |
165 | ||
166 | wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server", | |
167 | "Connect ...", "localhost"); | |
168 | addr.Hostname(hname); | |
169 | addr.Service(3000); | |
170 | sock->SetNotify(0); | |
171 | sock->Connect(addr, TRUE); | |
172 | if (!sock->IsConnected()) | |
173 | wxMessageBox("Can't connect to the specified host", "Alert !"); | |
174 | ||
175 | UpdateStatus(); | |
176 | } | |
177 | ||
178 | void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt)) | |
179 | { | |
180 | if (sock) | |
181 | sock->Close(); | |
182 | UpdateStatus(); | |
183 | } | |
184 | ||
185 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
186 | EVT_BUTTON(ID_TEST_CLOSE, MyFrame::OnCloseTest) | |
187 | EVT_MENU(SKDEMO_TEST1, MyFrame::OnExecTest1) | |
188 | EVT_MENU(SKDEMO_TEST3, MyFrame::OnExecUrlTest) | |
189 | EVT_MENU(SKDEMO_QUIT, MyFrame::OnQuitApp) | |
190 | EVT_MENU(SKDEMO_CONNECT, MyFrame::OnExecOpenConnection) | |
191 | EVT_MENU(SKDEMO_CLOSE, MyFrame::OnExecCloseConnection) | |
192 | END_EVENT_TABLE() | |
193 | ||
194 | void MyFrame::OnCloseTest(wxCommandEvent& evt) | |
195 | { | |
196 | wxButton *button = (wxButton *)evt.GetEventObject(); | |
197 | wxDialog *dlg = (wxDialog *)button->GetParent(); | |
198 | ||
199 | dlg->EndModal(0); | |
200 | } | |
201 | ||
202 | void MyFrame::UpdateStatus() | |
203 | { | |
204 | if (!sock->IsConnected()) { | |
205 | SetStatusText("Not connected", 0); | |
206 | SetStatusText("", 1); | |
207 | } else { | |
208 | wxIPV4address addr; | |
a737331d | 209 | wxChar s[100]; |
f4ada568 GL |
210 | |
211 | sock->GetPeer(addr); | |
a737331d | 212 | wxSprintf(s, _T("Connected to %s"), WXSTRINGCAST addr.Hostname()); |
f4ada568 | 213 | SetStatusText(s, 0); |
a737331d | 214 | wxSprintf(s, _T("Service: %d"), addr.Service()); |
f4ada568 GL |
215 | SetStatusText(s, 1); |
216 | } | |
217 | } | |
218 | ||
219 | void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt)) | |
220 | { | |
221 | if (!sock->IsConnected()) | |
222 | return; | |
223 | ||
db131261 | 224 | wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250)); |
f4ada568 GL |
225 | wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "", |
226 | wxPoint(0, 0), wxSize(400, 200), | |
227 | wxTE_MULTILINE); | |
228 | (void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close", | |
db131261 | 229 | wxPoint(100, 210), wxSize(100, -1)); |
a737331d | 230 | wxChar *buf, *buf2; |
f4ada568 GL |
231 | |
232 | dlgbox->Layout(); | |
233 | dlgbox->Show(TRUE); | |
234 | ||
235 | text_win->WriteText("Initializing test 1 ...\n"); | |
236 | ||
db131261 RR |
237 | wxYield(); |
238 | ||
f4ada568 | 239 | /* Init */ |
a737331d GL |
240 | buf = copystring(_T("Hi ! Hi ! Hi !\n")); |
241 | buf2 = new wxChar[wxStrlen(buf)+1]; | |
f4ada568 | 242 | char c = 0xbe; |
a737331d | 243 | sock->Write(&c, 1); |
f4ada568 GL |
244 | |
245 | /* No 1 */ | |
246 | text_win->WriteText("Sending some byte to the server ..."); | |
a737331d GL |
247 | wxYield(); |
248 | sock->Write((char *)buf, wxStrlen(buf)+1); | |
f4ada568 | 249 | text_win->WriteText("done\n"); |
a737331d | 250 | wxYield(); |
f4ada568 | 251 | text_win->WriteText("Receiving some byte from the server ..."); |
a737331d GL |
252 | wxYield(); |
253 | sock->Read((char *)buf2, wxStrlen(buf)+1); | |
f4ada568 | 254 | text_win->WriteText("done\n"); |
a737331d | 255 | wxYield(); |
f4ada568 GL |
256 | |
257 | text_win->WriteText("Comparing the two buffers ..."); | |
a737331d | 258 | if (memcmp(buf, buf2, wxStrlen(buf)+1) != 0) { |
f4ada568 GL |
259 | text_win->WriteText("Fail\n"); |
260 | sock->Close(); | |
261 | UpdateStatus(); | |
262 | } else | |
263 | text_win->WriteText("done\nTest 1 passed !\n"); | |
264 | ||
265 | dlgbox->Layout(); | |
266 | dlgbox->ShowModal(); | |
267 | ||
268 | delete [] buf; | |
269 | delete [] buf2; | |
270 | delete text_win; | |
271 | delete dlgbox; | |
272 | } | |
273 | ||
274 | void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt)) | |
275 | { | |
856d2e52 GL |
276 | wxString urlname = wxGetTextFromUser("Enter an URL to get", |
277 | "URL:", "http://localhost"); | |
f4ada568 GL |
278 | |
279 | wxURL url(urlname); | |
280 | wxInputStream *datas = url.GetInputStream(); | |
281 | ||
062c4861 GL |
282 | if (!datas) { |
283 | wxString error; | |
284 | error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError()); | |
285 | wxMessageBox(error, "Alert !"); | |
286 | } else { | |
a737331d GL |
287 | wxFileOutputStream *str_out = new wxFileOutputStream("test.url"); |
288 | str_out->Write(*datas); | |
289 | ||
062c4861 | 290 | wxMessageBox(_T("Success !! Click on OK to see the text."), "OK"); |
f4ada568 | 291 | delete datas; |
a737331d | 292 | delete str_out; |
f4ada568 GL |
293 | } |
294 | } |