]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/client.cpp
Unicode compilation fix
[wxWidgets.git] / samples / ipc / client.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: client.cpp
3 // Purpose: DDE sample: client
4 // Author: Julian Smart
5 // Modified by: Jurgen Doornik
6 // Created: 25/01/99
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
33 #include "ipcsetup.h"
34
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
36 #include "mondrian.xpm"
37 #endif
38
39 #include "wx/datetime.h"
40 #include "client.h"
41
42 // ----------------------------------------------------------------------------
43 // wxWin macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_APP(MyApp)
47
48 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
49 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
50 EVT_CLOSE( MyFrame::OnClose )
51 EVT_BUTTON( ID_START, MyFrame::OnStart )
52 EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServername )
53 EVT_CHOICE( ID_HOSTNAME, MyFrame::OnHostname )
54 EVT_CHOICE( ID_TOPIC, MyFrame::OnTopic )
55 EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect )
56 EVT_BUTTON( ID_STARTADVISE, MyFrame::OnStartAdvise )
57 EVT_BUTTON( ID_STOPADVISE, MyFrame::OnStopAdvise )
58 EVT_BUTTON( ID_POKE, MyFrame::OnPoke )
59 EVT_BUTTON( ID_EXECUTE, MyFrame::OnExecute )
60 EVT_BUTTON( ID_REQUEST, MyFrame::OnRequest )
61 END_EVENT_TABLE()
62
63 // ----------------------------------------------------------------------------
64 // globals
65 // ----------------------------------------------------------------------------
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // MyApp
73 // ----------------------------------------------------------------------------
74
75 // The `main program' equivalent, creating the windows and returning the
76 // main frame
77 bool MyApp::OnInit()
78 {
79 // Create the main frame window
80 m_frame = new MyFrame(NULL, _T("Client"));
81 m_frame->Show(true);
82
83 return true;
84 }
85
86 int MyApp::OnExit()
87 {
88
89 return 0;
90 }
91
92 // Define my frame constructor
93 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
94 : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300))
95 {
96 // Give it an icon
97 SetIcon(wxICON(mondrian));
98
99 // Make a menubar
100 wxMenu *file_menu = new wxMenu;
101
102 file_menu->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
103
104 wxMenuBar *menu_bar = new wxMenuBar;
105
106 menu_bar->Append(file_menu, _T("&File"));
107
108 // Associate the menu bar with the frame
109 SetMenuBar(menu_bar);
110
111 // set a dialog background
112 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
113
114 // add the controls to the frame
115 wxString strs4[] =
116 {
117 IPC_SERVICE, _T("...")
118 };
119 wxString strs5[] =
120 {
121 IPC_HOST, _T("...")
122 };
123 wxString strs6[] =
124 {
125 IPC_TOPIC, _T("...")
126 };
127
128 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
129
130 wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
131
132 wxGridSizer *item2 = new wxGridSizer( 4, 0, 0 );
133
134 wxButton *item3 = new wxButton( this, ID_START, wxT("Connect to server"), wxDefaultPosition, wxDefaultSize, 0 );
135 item2->Add( item3, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
136
137 wxChoice *item5 = new wxChoice( this, ID_HOSTNAME, wxDefaultPosition, wxSize(100,-1), 2, strs5, 0 );
138 item2->Add( item5, 0, wxALIGN_CENTER|wxALL, 5 );
139
140 wxChoice *item4 = new wxChoice( this, ID_SERVERNAME, wxDefaultPosition, wxSize(100,-1), 2, strs4, 0 );
141 item2->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
142
143 wxChoice *item6 = new wxChoice( this, ID_TOPIC, wxDefaultPosition, wxSize(100,-1), 2, strs6, 0 );
144 item2->Add( item6, 0, wxALIGN_CENTER|wxALL, 5 );
145
146 wxButton *item7 = new wxButton( this, ID_DISCONNECT, wxT("Disconnect "), wxDefaultPosition, wxDefaultSize, 0 );
147 item2->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
148
149 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
150
151 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
152
153 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
154
155 wxButton *item8 = new wxButton( this, ID_STARTADVISE, wxT("StartAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
156 item2->Add( item8, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
157
158 wxButton *item9 = new wxButton( this, ID_STOPADVISE, wxT("StopAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
159 item2->Add( item9, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
160
161 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
162
163 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
164
165 wxButton *item10 = new wxButton( this, ID_EXECUTE, wxT("Execute"), wxDefaultPosition, wxDefaultSize, 0 );
166 item2->Add( item10, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
167
168 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
169
170 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
171
172 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
173
174 wxButton *item11 = new wxButton( this, ID_POKE, wxT("Poke"), wxDefaultPosition, wxDefaultSize, 0 );
175 item2->Add( item11, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
176
177 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
178
179 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
180
181 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
182
183 wxButton *item12 = new wxButton( this, ID_REQUEST, wxT("Request"), wxDefaultPosition, wxDefaultSize, 0 );
184 item2->Add( item12, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
185
186 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
187
188 item1->Add( item2, 1, wxALIGN_CENTER|wxALL, 5 );
189
190 item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
191
192 wxStaticBox *item14 = new wxStaticBox( this, -1, wxT("Client log") );
193 wxStaticBoxSizer *item13 = new wxStaticBoxSizer( item14, wxVERTICAL );
194
195 wxTextCtrl *item15 = new wxTextCtrl( this, ID_LOG, wxT(""), wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE );
196 item13->Add( item15, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
197
198 item0->Add( item13, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
199
200 this->SetSizer( item0 );
201 item0->SetSizeHints( this );
202
203 // status
204 m_client = NULL;
205 GetServername()->SetSelection(0);
206 GetHostname()->SetSelection(0);
207 GetTopic()->SetSelection(0);
208 wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog());
209 delete wxLog::SetActiveTarget(logWindow);
210 wxLogMessage(_T("Click on Connect to connect to the server"));
211 Enable();
212 }
213
214 void MyFrame::Enable()
215 {
216 GetStart()->Enable(m_client == NULL);
217 GetServername()->Enable(m_client == NULL);
218 GetHostname()->Enable(m_client == NULL);
219 GetTopic()->Enable(m_client == NULL);
220 GetDisconnect()->Enable(m_client && m_client->IsConnected() != NULL);
221 GetStartAdvise()->Enable(m_client && m_client->IsConnected() != NULL);
222 GetStopAdvise()->Enable(m_client && m_client->IsConnected() != NULL);
223 GetExecute()->Enable(m_client && m_client->IsConnected() != NULL);
224 GetPoke()->Enable(m_client && m_client->IsConnected() != NULL);
225 GetRequest()->Enable(m_client && m_client->IsConnected() != NULL);
226 }
227
228 void MyFrame::OnClose(wxCloseEvent& event)
229 {
230 if (m_client)
231 {
232 delete m_client;
233 m_client = NULL;
234 }
235 event.Skip();
236 }
237
238 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
239 {
240 Close();
241 }
242
243 void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
244 {
245 // Connect to the client
246 wxString servername = GetServername()->GetStringSelection();
247 wxString hostname = GetHostname()->GetStringSelection();
248 wxString topic = GetTopic()->GetStringSelection();
249
250 m_client = new MyClient;
251 bool retval = m_client->Connect(hostname, servername, topic);
252
253 wxLogMessage(_T("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
254 hostname.c_str(), servername.c_str(), topic.c_str(),
255 retval ? _T("connected") : _T("failed to connect"));
256
257 if (!retval)
258 {
259 delete m_client;
260 m_client = NULL;
261 }
262 Enable();
263 }
264
265 void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) )
266 {
267 if (GetServername()->GetStringSelection() == _T("..."))
268 {
269 wxString s = wxGetTextFromUser(_T("Specify the name of the server"),
270 _T("Server Name"), _(""), this);
271 if (!s.IsEmpty() && s != IPC_SERVICE)
272 {
273 GetServername()->Insert(s, 0);
274 GetServername()->SetSelection(0);
275 }
276 }
277 }
278
279 void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) )
280 {
281 if (GetHostname()->GetStringSelection() == _T("..."))
282 {
283 wxString s = wxGetTextFromUser(_T("Specify the name of the host (ignored under DDE)"),
284 _T("Host Name"), _(""), this);
285 if (!s.IsEmpty() && s != IPC_HOST)
286 {
287 GetHostname()->Insert(s, 0);
288 GetHostname()->SetSelection(0);
289 }
290 }
291 }
292
293 void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) )
294 {
295 if (GetTopic()->GetStringSelection() == _T("..."))
296 {
297 wxString s = wxGetTextFromUser(_T("Specify the name of the topic"),
298 _T("Topic Name"), _(""), this);
299 if (!s.IsEmpty() && s != IPC_TOPIC)
300 {
301 GetTopic()->Insert(s, 0);
302 GetTopic()->SetSelection(0);
303 }
304 }
305 }
306
307 void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
308 {
309 Disconnect();
310 }
311
312 void MyFrame::Disconnect()
313 {
314 delete m_client;
315 m_client = NULL;
316 Enable();
317 }
318
319 void MyFrame::OnStartAdvise(wxCommandEvent& WXUNUSED(event))
320 {
321 m_client->GetConnection()->StartAdvise(_T("something"));
322 }
323
324 void MyFrame::OnStopAdvise(wxCommandEvent& WXUNUSED(event))
325 {
326 m_client->GetConnection()->StopAdvise(_T("something"));
327 }
328
329 void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
330 {
331 if (m_client->IsConnected())
332 {
333 wxString s = _T("Date");
334
335 m_client->GetConnection()->Execute((wxChar *)s.c_str());
336 m_client->GetConnection()->Execute((wxChar *)s.c_str(), (s.Length() + 1) * sizeof(wxChar));
337 #if wxUSE_DDE_FOR_IPC
338 wxLogMessage(_T("DDE Execute can only be used to send text strings, not arbitrary data.\nThe type argument will be ignored, text truncated, converted to Unicode and null terminated."));
339 #endif
340 char bytes[3];
341 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
342 m_client->GetConnection()->Execute((wxChar *)bytes, 3, wxIPC_PRIVATE);
343 }
344 }
345
346 void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
347 {
348 if (m_client->IsConnected())
349 {
350 wxString s = wxDateTime::Now().Format();
351 m_client->GetConnection()->Poke(_T("Date"), (wxChar *)s.c_str());
352 s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
353 m_client->GetConnection()->Poke(_T("Date"), (wxChar *)s.c_str(), (s.Length() + 1) * sizeof(wxChar));
354 char bytes[3];
355 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
356 m_client->GetConnection()->Poke(_T("bytes[3]"), (wxChar *)bytes, 3, wxIPC_PRIVATE);
357 }
358 }
359
360 void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
361 {
362 if (m_client->IsConnected())
363 {
364 int size;
365 m_client->GetConnection()->Request(_T("Date"));
366 m_client->GetConnection()->Request(_T("Date+len"), &size);
367 m_client->GetConnection()->Request(_T("bytes[3]"), &size, wxIPC_PRIVATE);
368 }
369 }
370
371 // ----------------------------------------------------------------------------
372 // MyClient
373 // ----------------------------------------------------------------------------
374 MyClient::MyClient() : wxClient()
375 {
376 m_connection = NULL;
377 }
378
379 bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
380 {
381 // suppress the log messages from MakeConnection()
382 wxLogNull nolog;
383
384 m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
385 return m_connection != NULL;
386 }
387
388 wxConnectionBase *MyClient::OnMakeConnection()
389 {
390 return new MyConnection;
391 }
392
393 void MyClient::Disconnect()
394 {
395 if (m_connection)
396 {
397 m_connection->Disconnect();
398 delete m_connection;
399 m_connection = NULL;
400 wxGetApp().GetFrame()->Enable();
401 wxLogMessage(_T("Client disconnected from server"));
402 }
403 }
404
405 MyClient::~MyClient()
406 {
407 Disconnect();
408 }
409
410 // ----------------------------------------------------------------------------
411 // MyConnection
412 // ----------------------------------------------------------------------------
413
414 void MyConnection::Log(const wxString& command, const wxString& topic,
415 const wxString& item, wxChar *data, int size, wxIPCFormat format)
416 {
417 wxString s;
418 if (topic.IsEmpty() && item.IsEmpty())
419 s.Printf(_T("%s("), command.c_str());
420 else if (topic.IsEmpty())
421 s.Printf(_T("%s(item=\"%s\","), command.c_str(), item.c_str());
422 else if (item.IsEmpty())
423 s.Printf(_T("%s(topic=\"%s\","), command.c_str(), topic.c_str());
424 else
425 s.Printf(_T("%s(topic=\"%s\",item=\"%s\","), command.c_str(), topic.c_str(), item.c_str());
426
427 if (format == wxIPC_TEXT || format == wxIPC_UNICODETEXT)
428 wxLogMessage(_T("%s\"%s\",%d)"), s.c_str(), data, size);
429 else if (format == wxIPC_PRIVATE)
430 {
431 if (size == 3)
432 {
433 char *bytes = (char *)data;
434 wxLogMessage(_T("%s'%c%c%c',%d)"), s.c_str(), bytes[0], bytes[1], bytes[2], size);
435 }
436 else
437 wxLogMessage(_T("%s...,%d)"), s.c_str(), size);
438 }
439 else if (format == wxIPC_INVALID)
440 wxLogMessage(_T("%s[invalid data],%d)"), s.c_str(), size);
441 }
442
443 bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, wxChar *data,
444 int size, wxIPCFormat format)
445 {
446 Log(_T("OnAdvise"), topic, item, data, size, format);
447 return true;
448 }
449
450 bool MyConnection::OnDisconnect()
451 {
452 wxLogMessage(_T("OnDisconnect()"));
453 wxGetApp().GetFrame()->Disconnect();
454 return true;
455 }
456
457 bool MyConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
458 {
459 Log(_T("Execute"), _T(""), _T(""), (wxChar *)data, size, format);
460 bool retval = wxConnection::Execute(data, size, format);
461 if (!retval)
462 wxLogMessage(_T("Execute failed!"));
463 return retval;
464 }
465
466 wxChar *MyConnection::Request(const wxString& item, int *size, wxIPCFormat format)
467 {
468 wxChar *data = wxConnection::Request(item, size, format);
469 Log(_T("Request"), _T(""), item, data, size ? *size : -1, format);
470 return data;
471 }
472
473 bool MyConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format)
474 {
475 Log(_T("Poke"), _T(""), item, data, size, format);
476 return wxConnection::Poke(item, data, size, format);
477 }
478