]>
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
8 // Copyright: (c) 2007 Anders Larsen
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
35 #include "connection.h"
38 #include "wx/datetime.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // a simple connection class testing and logging various operations
45 class MyConnection
: public MyConnectionBase
, public wxTimer
48 virtual bool Disconnect() { return wxConnection::Disconnect(); }
49 virtual bool OnExecute(const wxString
& topic
,
53 virtual const void *OnRequest(const wxString
& topic
,
57 virtual bool OnPoke(const wxString
& topic
,
62 virtual bool OnStartAdvise(const wxString
& topic
, const wxString
& item
);
63 virtual bool OnStopAdvise(const wxString
& topic
, const wxString
& item
);
64 virtual bool DoAdvise(const wxString
& item
,
68 virtual bool OnDisconnect();
69 virtual void Notify();
74 wxString m_sRequestDate
;
75 char m_achRequestBytes
[3];
78 // a connection used for benchmarking some IPC operations by
79 // tests/benchmarks/ipcclient.cpp
80 class BenchConnection
: public wxConnection
83 BenchConnection() { m_advise
= false; }
85 virtual bool OnPoke(const wxString
& topic
,
90 virtual bool OnStartAdvise(const wxString
& topic
, const wxString
& item
);
91 virtual bool OnStopAdvise(const wxString
& topic
, const wxString
& item
);
94 // return true if this is the supported topic+item combination, log an
95 // error message otherwise
96 bool IsSupportedTopicAndItem(const wxString
& operation
,
97 const wxString
& topic
,
98 const wxString
& item
) const;
100 // the item which can be manipulated by the client via Poke() calls
103 // should we notify the client about changes to m_item?
106 wxDECLARE_NO_COPY_CLASS(BenchConnection
);
109 // a simple server accepting connections to IPC_TOPIC and IPC_BENCHMARK_TOPIC
110 class MyServer
: public wxServer
116 bool IsConnected() { return m_connection
!= NULL
; };
118 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
);
121 wxConnection
*m_connection
;
124 // Define a new application
125 class MyApp
: public wxApp
128 virtual bool OnInit();
136 // ============================================================================
138 // ============================================================================
140 IMPLEMENT_APP_CONSOLE(MyApp
)
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
148 if ( !wxApp::OnInit() )
151 delete wxLog::SetActiveTarget(new wxLogStderr
);
153 const char * const kind
=
154 #if wxUSE_DDE_FOR_IPC
161 // Create a new server
162 if ( !m_server
.Create(IPC_SERVICE
) )
164 wxLogMessage("%s server failed to start on %s", kind
, IPC_SERVICE
);
168 wxLogMessage("%s server started on %s", kind
, IPC_SERVICE
);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
181 MyServer::~MyServer()
186 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
188 wxLogMessage("OnAcceptConnection(\"%s\")", topic
);
190 if ( topic
== IPC_TOPIC
)
192 m_connection
= new MyConnection
;
194 else if ( topic
== IPC_BENCHMARK_TOPIC
)
196 m_connection
= new BenchConnection
;
198 else // unknown topic
200 wxLogMessage("Unknown topic");
204 wxLogMessage("Connection accepted");
208 void MyServer::Disconnect()
212 m_connection
->Disconnect();
213 wxDELETE(m_connection
);
214 wxLogMessage("Disconnected client");
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
223 MyConnection::OnExecute(const wxString
& topic
,
228 Log("OnExecute", topic
, "", data
, size
, format
);
233 MyConnection::OnPoke(const wxString
& topic
,
234 const wxString
& item
,
239 Log("OnPoke", topic
, item
, data
, size
, format
);
240 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
244 MyConnection::OnRequest(const wxString
& topic
,
245 const wxString
& item
,
252 m_sRequestDate
= wxDateTime::Now().Format();
253 data
= m_sRequestDate
.c_str();
256 else if (item
== "Date+len")
258 m_sRequestDate
= wxDateTime::Now().FormatTime() +
259 " " + wxDateTime::Now().FormatDate();
260 data
= m_sRequestDate
.c_str();
261 *size
= m_sRequestDate
.Length() + 1;
263 else if (item
== "bytes[3]")
265 data
= m_achRequestBytes
;
266 m_achRequestBytes
[0] = '1';
267 m_achRequestBytes
[1] = '2';
268 m_achRequestBytes
[2] = '3';
276 Log("OnRequest", topic
, item
, data
, *size
, format
);
280 bool MyConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
282 wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic
, item
);
283 wxLogMessage("Returning true");
285 Start(3000); // schedule our Notify() to be called in 3 seconds
289 bool MyConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
291 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic
, item
);
292 wxLogMessage("Returning true");
298 void MyConnection::Notify()
300 if (!m_sAdvise
.empty())
302 wxString s
= wxDateTime::Now().Format();
303 Advise(m_sAdvise
, s
);
304 s
= wxDateTime::Now().FormatTime() + " "
305 + wxDateTime::Now().FormatDate();
306 Advise(m_sAdvise
, s
.mb_str(), s
.length() + 1);
308 #if wxUSE_DDE_FOR_IPC
309 wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
310 "The client will receive it as wxIPC_TEXT, "
311 "and receive the correct no of bytes, "
312 "but not print a correct log entry.");
316 bytes
[0] = '1'; bytes
[1] = '2'; bytes
[2] = '3';
317 Advise(m_sAdvise
, bytes
, 3, wxIPC_PRIVATE
);
318 // this works, but the log treats it as a string now
319 // m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
323 bool MyConnection::DoAdvise(const wxString
& item
,
328 Log("Advise", "", item
, data
, size
, format
);
329 return wxConnection::DoAdvise(item
, data
, size
, format
);
332 bool MyConnection::OnDisconnect()
334 wxLogMessage("OnDisconnect()");
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 bool BenchConnection::IsSupportedTopicAndItem(const wxString
& operation
,
343 const wxString
& topic
,
344 const wxString
& item
) const
346 if ( topic
!= IPC_BENCHMARK_TOPIC
||
347 item
!= IPC_BENCHMARK_ITEM
)
349 wxLogMessage("Unexpected %s(\"%s\", \"%s\") call.",
350 operation
, topic
, item
);
357 bool BenchConnection::OnPoke(const wxString
& topic
,
358 const wxString
& item
,
363 if ( !IsSupportedTopicAndItem("OnPoke", topic
, item
) )
366 if ( !IsTextFormat(format
) )
368 wxLogMessage("Unexpected format %d in OnPoke().", format
);
372 m_item
= GetTextFromData(data
, size
, format
);
376 if ( !Advise(item
, m_item
) )
378 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
) )