]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/baseserver.cpp
eliminate possibility of configure enabling gnomeprint with GTK3
[wxWidgets.git] / samples / ipc / baseserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/ipc/baseserver.cpp
3 // Purpose: IPC sample: console server
4 // Author: Anders Larsen
5 // Most of the code was stolen from: samples/ipc/server.cpp
6 // (c) Julian Smart, Jurgen Doornik
7 // Created: 2007-11-08
8 // RCS-ID: $Id$
9 // Copyright: (c) 2007 Anders Larsen
10 // Licence: 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 "connection.h"
37
38 #include "wx/timer.h"
39 #include "wx/datetime.h"
40
41 // ----------------------------------------------------------------------------
42 // local classes
43 // ----------------------------------------------------------------------------
44
45 // a simple connection class testing and logging various operations
46 class MyConnection : public MyConnectionBase, public wxTimer
47 {
48 public:
49 virtual bool Disconnect() { return wxConnection::Disconnect(); }
50 virtual bool OnExecute(const wxString& topic,
51 const void *data,
52 size_t size,
53 wxIPCFormat format);
54 virtual const void *OnRequest(const wxString& topic,
55 const wxString& item,
56 size_t *size,
57 wxIPCFormat format);
58 virtual bool OnPoke(const wxString& topic,
59 const wxString& item,
60 const void *data,
61 size_t size,
62 wxIPCFormat format);
63 virtual bool OnStartAdvise(const wxString& topic, const wxString& item);
64 virtual bool OnStopAdvise(const wxString& topic, const wxString& item);
65 virtual bool DoAdvise(const wxString& item,
66 const void *data,
67 size_t size,
68 wxIPCFormat format);
69 virtual bool OnDisconnect();
70 virtual void Notify();
71
72 private:
73 wxString m_sAdvise;
74
75 wxString m_sRequestDate;
76 char m_achRequestBytes[3];
77 };
78
79 // a connection used for benchmarking some IPC operations by
80 // tests/benchmarks/ipcclient.cpp
81 class BenchConnection : public wxConnection
82 {
83 public:
84 BenchConnection() { m_advise = false; }
85
86 virtual bool OnPoke(const wxString& topic,
87 const wxString& item,
88 const void *data,
89 size_t size,
90 wxIPCFormat format);
91 virtual bool OnStartAdvise(const wxString& topic, const wxString& item);
92 virtual bool OnStopAdvise(const wxString& topic, const wxString& item);
93
94 private:
95 // return true if this is the supported topic+item combination, log an
96 // error message otherwise
97 bool IsSupportedTopicAndItem(const wxString& operation,
98 const wxString& topic,
99 const wxString& item) const;
100
101 // the item which can be manipulated by the client via Poke() calls
102 wxString m_item;
103
104 // should we notify the client about changes to m_item?
105 bool m_advise;
106
107 wxDECLARE_NO_COPY_CLASS(BenchConnection);
108 };
109
110 // a simple server accepting connections to IPC_TOPIC and IPC_BENCHMARK_TOPIC
111 class MyServer : public wxServer
112 {
113 public:
114 MyServer();
115 virtual ~MyServer();
116 void Disconnect();
117 bool IsConnected() { return m_connection != NULL; };
118
119 virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
120
121 private:
122 wxConnection *m_connection;
123 };
124
125 // Define a new application
126 class MyApp : public wxApp
127 {
128 public:
129 virtual bool OnInit();
130
131 protected:
132 MyServer m_server;
133 };
134
135 DECLARE_APP(MyApp)
136
137 // ============================================================================
138 // implementation
139 // ============================================================================
140
141 IMPLEMENT_APP_CONSOLE(MyApp)
142
143 // ----------------------------------------------------------------------------
144 // MyApp
145 // ----------------------------------------------------------------------------
146
147 bool MyApp::OnInit()
148 {
149 if ( !wxApp::OnInit() )
150 return false;
151
152 delete wxLog::SetActiveTarget(new wxLogStderr);
153
154 const char * const kind =
155 #if wxUSE_DDE_FOR_IPC
156 "DDE"
157 #else
158 "TCP"
159 #endif
160 ;
161
162 // Create a new server
163 if ( !m_server.Create(IPC_SERVICE) )
164 {
165 wxLogMessage("%s server failed to start on %s", kind, IPC_SERVICE);
166 return false;
167 }
168
169 wxLogMessage("%s server started on %s", kind, IPC_SERVICE);
170 return true;
171 }
172
173 // ----------------------------------------------------------------------------
174 // MyServer
175 // ----------------------------------------------------------------------------
176
177 MyServer::MyServer()
178 {
179 m_connection = NULL;
180 }
181
182 MyServer::~MyServer()
183 {
184 Disconnect();
185 }
186
187 wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
188 {
189 wxLogMessage("OnAcceptConnection(\"%s\")", topic);
190
191 if ( topic == IPC_TOPIC )
192 {
193 m_connection = new MyConnection;
194 }
195 else if ( topic == IPC_BENCHMARK_TOPIC )
196 {
197 m_connection = new BenchConnection;
198 }
199 else // unknown topic
200 {
201 wxLogMessage("Unknown topic");
202 return NULL;
203 }
204
205 wxLogMessage("Connection accepted");
206 return m_connection;
207 }
208
209 void MyServer::Disconnect()
210 {
211 if ( m_connection )
212 {
213 m_connection->Disconnect();
214 wxDELETE(m_connection);
215 wxLogMessage("Disconnected client");
216 }
217 }
218
219 // ----------------------------------------------------------------------------
220 // MyConnection
221 // ----------------------------------------------------------------------------
222
223 bool
224 MyConnection::OnExecute(const wxString& topic,
225 const void *data,
226 size_t size,
227 wxIPCFormat format)
228 {
229 Log("OnExecute", topic, "", data, size, format);
230 return true;
231 }
232
233 bool
234 MyConnection::OnPoke(const wxString& topic,
235 const wxString& item,
236 const void *data,
237 size_t size,
238 wxIPCFormat format)
239 {
240 Log("OnPoke", topic, item, data, size, format);
241 return wxConnection::OnPoke(topic, item, data, size, format);
242 }
243
244 const void *
245 MyConnection::OnRequest(const wxString& topic,
246 const wxString& item,
247 size_t *size,
248 wxIPCFormat format)
249 {
250 const void *data;
251 if (item == "Date")
252 {
253 m_sRequestDate = wxDateTime::Now().Format();
254 data = m_sRequestDate.c_str();
255 *size = wxNO_LEN;
256 }
257 else if (item == "Date+len")
258 {
259 m_sRequestDate = wxDateTime::Now().FormatTime() +
260 " " + wxDateTime::Now().FormatDate();
261 data = m_sRequestDate.c_str();
262 *size = m_sRequestDate.Length() + 1;
263 }
264 else if (item == "bytes[3]")
265 {
266 data = m_achRequestBytes;
267 m_achRequestBytes[0] = '1';
268 m_achRequestBytes[1] = '2';
269 m_achRequestBytes[2] = '3';
270 *size = 3;
271 }
272 else
273 {
274 data = NULL;
275 *size = 0;
276 }
277 Log("OnRequest", topic, item, data, *size, format);
278 return data;
279 }
280
281 bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
282 {
283 wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic, item);
284 wxLogMessage("Returning true");
285 m_sAdvise = item;
286 Start(3000); // schedule our Notify() to be called in 3 seconds
287 return true;
288 }
289
290 bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item)
291 {
292 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic, item);
293 wxLogMessage("Returning true");
294 m_sAdvise.clear();
295 Stop();
296 return true;
297 }
298
299 void MyConnection::Notify()
300 {
301 if (!m_sAdvise.empty())
302 {
303 wxString s = wxDateTime::Now().Format();
304 Advise(m_sAdvise, s);
305 s = wxDateTime::Now().FormatTime() + " "
306 + wxDateTime::Now().FormatDate();
307 Advise(m_sAdvise, s.mb_str(), s.length() + 1);
308
309 #if wxUSE_DDE_FOR_IPC
310 wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
311 "The client will receive it as wxIPC_TEXT, "
312 "and receive the correct no of bytes, "
313 "but not print a correct log entry.");
314 #endif // DDE
315
316 char bytes[3];
317 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
318 Advise(m_sAdvise, bytes, 3, wxIPC_PRIVATE);
319 // this works, but the log treats it as a string now
320 // m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
321 }
322 }
323
324 bool MyConnection::DoAdvise(const wxString& item,
325 const void *data,
326 size_t size,
327 wxIPCFormat format)
328 {
329 Log("Advise", "", item, data, size, format);
330 return wxConnection::DoAdvise(item, data, size, format);
331 }
332
333 bool MyConnection::OnDisconnect()
334 {
335 wxLogMessage("OnDisconnect()");
336 return true;
337 }
338
339 // ----------------------------------------------------------------------------
340 // BenchConnection
341 // ----------------------------------------------------------------------------
342
343 bool BenchConnection::IsSupportedTopicAndItem(const wxString& operation,
344 const wxString& topic,
345 const wxString& item) const
346 {
347 if ( topic != IPC_BENCHMARK_TOPIC ||
348 item != IPC_BENCHMARK_ITEM )
349 {
350 wxLogMessage("Unexpected %s(\"%s\", \"%s\") call.",
351 operation, topic, item);
352 return false;
353 }
354
355 return true;
356 }
357
358 bool BenchConnection::OnPoke(const wxString& topic,
359 const wxString& item,
360 const void *data,
361 size_t size,
362 wxIPCFormat format)
363 {
364 if ( !IsSupportedTopicAndItem("OnPoke", topic, item) )
365 return false;
366
367 if ( !IsTextFormat(format) )
368 {
369 wxLogMessage("Unexpected format %d in OnPoke().", format);
370 return false;
371 }
372
373 m_item = GetTextFromData(data, size, format);
374
375 if ( m_advise )
376 {
377 if ( !Advise(item, m_item) )
378 {
379 wxLogMessage("Failed to advise client about the change.");
380 }
381 }
382
383 return true;
384 }
385
386 bool BenchConnection::OnStartAdvise(const wxString& topic, const wxString& item)
387 {
388 if ( !IsSupportedTopicAndItem("OnStartAdvise", topic, item) )
389 return false;
390
391 m_advise = true;
392
393 return true;
394 }
395
396 bool BenchConnection::OnStopAdvise(const wxString& topic, const wxString& item)
397 {
398 if ( !IsSupportedTopicAndItem("OnStopAdvise", topic, item) )
399 return false;
400
401 m_advise = false;
402
403 return true;
404 }
405