]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/baseclient.cpp
disable console targets for WinCE
[wxWidgets.git] / samples / ipc / baseclient.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/ipc/baseclient.cpp
3 // Purpose: IPC sample: console client
4 // Author: Anders Larsen
5 // Most of the code was stolen from: samples/ipc/client.cpp
6 // (c) Julian Smart, Jurgen Doornik
7 // Created: 2007-11-08
8 // RCS-ID: $Id$
9 // Copyright: (c) 2007 Anders Larsen
10 // License: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #ifndef WX_PRECOMP
29 #include "wx/wx.h"
30 #endif
31
32 // Settings common to both executables: determines whether
33 // we're using TCP/IP or real DDE.
34 #include "ipcsetup.h"
35
36 #include "wx/timer.h"
37 #include "wx/datetime.h"
38
39 // ----------------------------------------------------------------------------
40 // wxWin macros
41 // ----------------------------------------------------------------------------
42
43
44 // Define a new application
45 class MyClient;
46 class MyConnection;
47
48 class MyApp: public wxApp
49 {
50 public:
51 virtual bool OnInit();
52 virtual int OnExit();
53
54 protected:
55 MyClient *m_client;
56 };
57
58 class MyConnection: public wxConnection
59 {
60 public:
61 MyConnection();
62 virtual ~MyConnection();
63 virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format);
64 virtual const void *Request(const wxString& item, size_t *size = NULL, wxIPCFormat format = wxIPC_TEXT);
65 virtual bool DoPoke(const wxString& item, const void* data, size_t size, wxIPCFormat format);
66 virtual bool OnAdvise(const wxString& topic, const wxString& item, const void *data, size_t size, wxIPCFormat format);
67 virtual bool OnDisconnect();
68
69 protected:
70 void Log(const wxString& command, const wxString& topic,
71 const wxString& item, const void *data, size_t size, wxIPCFormat format);
72 };
73
74 class MyClient: public wxClient, public wxTimer
75 {
76 public:
77 MyClient();
78 virtual ~MyClient();
79 bool Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic);
80 void Disconnect();
81 wxConnectionBase *OnMakeConnection();
82 bool IsConnected() { return m_connection != NULL; };
83 virtual void Notify();
84
85 protected:
86 MyConnection *m_connection;
87 int m_step;
88 };
89
90 // ============================================================================
91 // implementation
92 // ============================================================================
93
94 IMPLEMENT_APP(MyApp)
95
96 // ----------------------------------------------------------------------------
97 // MyApp
98 // ----------------------------------------------------------------------------
99
100 // The `main program' equivalent, creating the windows and returning the
101 // main frame
102 bool MyApp::OnInit()
103 {
104 if ( !wxApp::OnInit() )
105 return false;
106
107 delete wxLog::SetActiveTarget(new wxLogStderr);
108
109 // Create a new client
110 m_client = new MyClient;
111 bool retval = m_client->Connect("localhost", "4242", "IPC TEST");
112
113 wxLogMessage(_T("Client host=\"localhost\" port=\"4242\" topic=\"IPC TEST\" %s"),
114 retval ? _T("connected") : _T("failed to connect"));
115
116 return retval;
117 }
118
119 int MyApp::OnExit()
120 {
121 delete m_client;
122
123 return 0;
124 }
125
126 // ----------------------------------------------------------------------------
127 // MyClient
128 // ----------------------------------------------------------------------------
129
130 MyClient::MyClient() : wxClient()
131 {
132 m_connection = NULL;
133 m_step = 0;
134 }
135
136 bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
137 {
138 // suppress the log messages from MakeConnection()
139 wxLogNull nolog;
140
141 m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
142 if (m_connection)
143 Start(1000, false);
144 return m_connection != NULL;
145 }
146
147 wxConnectionBase *MyClient::OnMakeConnection()
148 {
149 return new MyConnection;
150 }
151
152 void MyClient::Disconnect()
153 {
154 if (m_connection)
155 {
156 m_connection->Disconnect();
157 delete m_connection;
158 m_connection = NULL;
159 wxLogMessage(_T("Client disconnected from server"));
160 }
161 wxGetApp().ExitMainLoop();
162 }
163
164 MyClient::~MyClient()
165 {
166 Disconnect();
167 }
168
169 void MyClient::Notify()
170 {
171 switch (m_step++)
172 {
173 case 0:
174 {
175 size_t size;
176 m_connection->Request(_T("Date"));
177 m_connection->Request(_T("Date+len"), &size);
178 m_connection->Request(_T("bytes[3]"), &size, wxIPC_PRIVATE);
179 break;
180 }
181 case 1:
182 {
183 wxString s = wxDateTime::Now().Format();
184 m_connection->Poke(_T("Date"), s);
185 s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
186 m_connection->Poke(_T("Date"), (const char *)s.c_str(), s.length() + 1);
187 char bytes[3];
188 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
189 m_connection->Poke(_T("bytes[3]"), bytes, 3, wxIPC_PRIVATE);
190 break;
191 }
192 case 2:
193 {
194 wxString s = _T("Date");
195 m_connection->Execute(s);
196 m_connection->Execute((const char *)s.c_str(), s.length() + 1);
197 #if wxUSE_DDE_FOR_IPC
198 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."));
199 #endif
200 char bytes[3];
201 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
202 m_connection->Execute(bytes, 3, wxIPC_PRIVATE);
203 break;
204 }
205 case 3:
206 wxLogMessage(_T("StartAdvise(\"something\")"));
207 m_connection->StartAdvise(_T("something"));
208 break;
209 case 10:
210 wxLogMessage(_T("StopAdvise(\"something\")"));
211 m_connection->StopAdvise(_T("something"));
212 break;
213 case 15:
214 Disconnect();
215 break;
216 }
217 }
218
219 // ----------------------------------------------------------------------------
220 // MyConnection
221 // ----------------------------------------------------------------------------
222
223 MyConnection::MyConnection()
224 {
225 }
226
227 MyConnection::~MyConnection()
228 {
229 }
230
231 void MyConnection::Log(const wxString& command, const wxString& topic,
232 const wxString& item, const void *data, size_t size, wxIPCFormat format)
233 {
234 wxString s;
235 if (topic.IsEmpty() && item.IsEmpty())
236 s.Printf(_T("%s("), command.c_str());
237 else if (topic.IsEmpty())
238 s.Printf(_T("%s(item=\"%s\","), command.c_str(), item.c_str());
239 else if (item.IsEmpty())
240 s.Printf(_T("%s(topic=\"%s\","), command.c_str(), topic.c_str());
241 else
242 s.Printf(_T("%s(topic=\"%s\",item=\"%s\","), command.c_str(), topic.c_str(), item.c_str());
243
244 switch (format)
245 {
246 case wxIPC_TEXT:
247 case wxIPC_UTF8TEXT:
248 #if !wxUSE_UNICODE || wxUSE_UNICODE_UTF8
249 wxLogMessage(_T("%s\"%s\",%d)"), s.c_str(), data, size);
250 #else
251 wxLogMessage(_T("%s\"%s\",%d)"), s.c_str(), wxConvUTF8.cMB2WC((const char*)data), size);
252 #endif
253 break;
254 case wxIPC_PRIVATE:
255 if (size == 3)
256 {
257 char *bytes = (char *)data;
258 wxLogMessage(_T("%s'%c%c%c',%d)"), s.c_str(), bytes[0], bytes[1], bytes[2], size);
259 }
260 else
261 wxLogMessage(_T("%s...,%d)"), s.c_str(), size);
262 break;
263 case wxIPC_INVALID:
264 wxLogMessage(_T("%s[invalid data],%d)"), s.c_str(), size);
265 break;
266 default:
267 wxLogMessage(_T("%s[unknown data],%d)"), s.c_str(), size);
268 break;
269 }
270 }
271
272 bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
273 size_t size, wxIPCFormat format)
274 {
275 Log(_T("OnAdvise"), topic, item, data, size, format);
276 return true;
277 }
278
279 bool MyConnection::OnDisconnect()
280 {
281 wxLogMessage(_T("OnDisconnect()"));
282 wxGetApp().ExitMainLoop();
283 return true;
284 }
285
286 bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
287 {
288 Log(_T("Execute"), wxEmptyString, wxEmptyString, data, size, format);
289 bool retval = wxConnection::DoExecute(data, size, format);
290 if (!retval)
291 wxLogMessage(_T("Execute failed!"));
292 return retval;
293 }
294
295 const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
296 {
297 const void *data = wxConnection::Request(item, size, format);
298 Log(_T("Request"), wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
299 return data;
300 }
301
302 bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
303 {
304 Log(_T("Poke"), wxEmptyString, item, data, size, format);
305 return wxConnection::DoPoke(item, data, size, format);
306 }