]>
git.saurik.com Git - wxWidgets.git/blob - 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
9 // Copyright: (c) 2007 Anders Larsen
10 // License: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
32 // Settings common to both executables: determines whether
33 // we're using TCP/IP or real DDE.
36 #include "connection.h"
39 #include "wx/datetime.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // a simple connection class testing and logging various operations
46 class MyConnection
: public MyConnectionBase
, public wxTimer
49 virtual bool Disconnect() { return wxConnection::Disconnect(); }
50 virtual bool OnExecute(const wxString
& topic
,
54 virtual const void *OnRequest(const wxString
& topic
,
58 virtual bool OnPoke(const wxString
& topic
,
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
,
69 virtual bool OnDisconnect();
70 virtual void Notify();
75 wxString m_sRequestDate
;
76 char m_achRequestBytes
[3];
79 // a connection used for benchmarking some IPC operations by
80 // tests/benchmarks/ipcclient.cpp
81 class BenchConnection
: public wxConnection
84 BenchConnection() { m_advise
= false; }
86 virtual bool OnPoke(const wxString
& topic
,
91 virtual bool OnStartAdvise(const wxString
& topic
, const wxString
& item
);
92 virtual bool OnStopAdvise(const wxString
& topic
, const wxString
& item
);
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;
101 // the item which can be manipulated by the client via Poke() calls
104 // should we notify the client about changes to m_item?
107 wxDECLARE_NO_COPY_CLASS(BenchConnection
);
110 // a simple server accepting connections to IPC_TOPIC and IPC_BENCHMARK_TOPIC
111 class MyServer
: public wxServer
117 bool IsConnected() { return m_connection
!= NULL
; };
119 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
);
122 wxConnection
*m_connection
;
125 // Define a new application
126 class MyApp
: public wxApp
129 virtual bool OnInit();
137 // ============================================================================
139 // ============================================================================
141 IMPLEMENT_APP_CONSOLE(MyApp
)
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
149 if ( !wxApp::OnInit() )
152 delete wxLog::SetActiveTarget(new wxLogStderr
);
154 const char * const kind
=
155 #if wxUSE_DDE_FOR_IPC
162 // Create a new server
163 if ( !m_server
.Create(IPC_SERVICE
) )
165 wxLogMessage("%s server failed to start on %s", kind
, IPC_SERVICE
);
169 wxLogMessage("%s server started on %s", kind
, IPC_SERVICE
);
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
182 MyServer::~MyServer()
187 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
189 wxLogMessage("OnAcceptConnection(\"%s\")", topic
);
191 if ( topic
== IPC_TOPIC
)
193 m_connection
= new MyConnection
;
195 else if ( topic
== IPC_BENCHMARK_TOPIC
)
197 m_connection
= new BenchConnection
;
199 else // unknown topic
201 wxLogMessage("Unknown topic");
205 wxLogMessage("Connection accepted");
209 void MyServer::Disconnect()
213 m_connection
->Disconnect();
216 wxLogMessage("Disconnected client");
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
225 MyConnection::OnExecute(const wxString
& topic
,
230 Log("OnExecute", topic
, "", data
, size
, format
);
235 MyConnection::OnPoke(const wxString
& topic
,
236 const wxString
& item
,
241 Log("OnPoke", topic
, item
, data
, size
, format
);
242 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
246 MyConnection::OnRequest(const wxString
& topic
,
247 const wxString
& item
,
254 m_sRequestDate
= wxDateTime::Now().Format();
255 data
= m_sRequestDate
.c_str();
258 else if (item
== "Date+len")
260 m_sRequestDate
= wxDateTime::Now().FormatTime() +
261 " " + wxDateTime::Now().FormatDate();
262 data
= m_sRequestDate
.c_str();
263 *size
= m_sRequestDate
.Length() + 1;
265 else if (item
== "bytes[3]")
267 data
= m_achRequestBytes
;
268 m_achRequestBytes
[0] = '1';
269 m_achRequestBytes
[1] = '2';
270 m_achRequestBytes
[2] = '3';
278 Log("OnRequest", topic
, item
, data
, *size
, format
);
282 bool MyConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
284 wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic
, item
);
285 wxLogMessage("Returning true");
287 Start(3000); // schedule our Notify() to be called in 3 seconds
291 bool MyConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
293 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic
, item
);
294 wxLogMessage("Returning true");
300 void MyConnection::Notify()
302 if (!m_sAdvise
.empty())
304 wxString s
= wxDateTime::Now().Format();
305 Advise(m_sAdvise
, s
);
306 s
= wxDateTime::Now().FormatTime() + " "
307 + wxDateTime::Now().FormatDate();
308 Advise(m_sAdvise
, s
.mb_str(), s
.length() + 1);
310 #if wxUSE_DDE_FOR_IPC
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.");
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 );
325 bool MyConnection::DoAdvise(const wxString
& item
,
330 Log("Advise", "", item
, data
, size
, format
);
331 return wxConnection::DoAdvise(item
, data
, size
, format
);
334 bool MyConnection::OnDisconnect()
336 wxLogMessage("OnDisconnect()");
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 bool BenchConnection::IsSupportedTopicAndItem(const wxString
& operation
,
345 const wxString
& topic
,
346 const wxString
& item
) const
348 if ( topic
!= IPC_BENCHMARK_TOPIC
||
349 item
!= IPC_BENCHMARK_ITEM
)
351 wxLogMessage("Unexpected %s(\"%s\", \"%s\") call.",
352 operation
, topic
, item
);
359 bool BenchConnection::OnPoke(const wxString
& topic
,
360 const wxString
& item
,
365 if ( !IsSupportedTopicAndItem("OnPoke", topic
, item
) )
368 if ( !IsTextFormat(format
) )
370 wxLogMessage("Unexpected format %d in OnPoke().", format
);
374 m_item
= GetTextFromData(data
, size
, format
);
378 if ( !Advise(item
, m_item
) )
379 wxLogMessage("Failed to advise client about the change.");
385 bool BenchConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
387 if ( !IsSupportedTopicAndItem("OnStartAdvise", topic
, item
) )
395 bool BenchConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
397 if ( !IsSupportedTopicAndItem("OnStopAdvise", topic
, item
) )