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