]>
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 // Licence: 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();
214 wxDELETE(m_connection
);
215 wxLogMessage("Disconnected client");
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
224 MyConnection::OnExecute(const wxString
& topic
,
229 Log("OnExecute", topic
, "", data
, size
, format
);
234 MyConnection::OnPoke(const wxString
& topic
,
235 const wxString
& item
,
240 Log("OnPoke", topic
, item
, data
, size
, format
);
241 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
245 MyConnection::OnRequest(const wxString
& topic
,
246 const wxString
& item
,
253 m_sRequestDate
= wxDateTime::Now().Format();
254 data
= m_sRequestDate
.c_str();
257 else if (item
== "Date+len")
259 m_sRequestDate
= wxDateTime::Now().FormatTime() +
260 " " + wxDateTime::Now().FormatDate();
261 data
= m_sRequestDate
.c_str();
262 *size
= m_sRequestDate
.Length() + 1;
264 else if (item
== "bytes[3]")
266 data
= m_achRequestBytes
;
267 m_achRequestBytes
[0] = '1';
268 m_achRequestBytes
[1] = '2';
269 m_achRequestBytes
[2] = '3';
277 Log("OnRequest", topic
, item
, data
, *size
, format
);
281 bool MyConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
283 wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic
, item
);
284 wxLogMessage("Returning true");
286 Start(3000); // schedule our Notify() to be called in 3 seconds
290 bool MyConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
292 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic
, item
);
293 wxLogMessage("Returning true");
299 void MyConnection::Notify()
301 if (!m_sAdvise
.empty())
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);
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.");
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 );
324 bool MyConnection::DoAdvise(const wxString
& item
,
329 Log("Advise", "", item
, data
, size
, format
);
330 return wxConnection::DoAdvise(item
, data
, size
, format
);
333 bool MyConnection::OnDisconnect()
335 wxLogMessage("OnDisconnect()");
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 bool BenchConnection::IsSupportedTopicAndItem(const wxString
& operation
,
344 const wxString
& topic
,
345 const wxString
& item
) const
347 if ( topic
!= IPC_BENCHMARK_TOPIC
||
348 item
!= IPC_BENCHMARK_ITEM
)
350 wxLogMessage("Unexpected %s(\"%s\", \"%s\") call.",
351 operation
, topic
, item
);
358 bool BenchConnection::OnPoke(const wxString
& topic
,
359 const wxString
& item
,
364 if ( !IsSupportedTopicAndItem("OnPoke", topic
, item
) )
367 if ( !IsTextFormat(format
) )
369 wxLogMessage("Unexpected format %d in OnPoke().", format
);
373 m_item
= GetTextFromData(data
, size
, format
);
377 if ( !Advise(item
, m_item
) )
379 wxLogMessage("Failed to advise client about the change.");
386 bool BenchConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
388 if ( !IsSupportedTopicAndItem("OnStartAdvise", topic
, item
) )
396 bool BenchConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
398 if ( !IsSupportedTopicAndItem("OnStopAdvise", topic
, item
) )