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 // Define a new application
48 class MyApp
: public wxApp
51 virtual bool OnInit();
60 class MyConnection
: public MyConnectionBase
, public wxTimer
63 virtual bool Disconnect() { return wxConnection::Disconnect(); }
64 virtual bool OnExecute(const wxString
& topic
, const void *data
, size_t size
, wxIPCFormat format
);
65 virtual const void *OnRequest(const wxString
& topic
, const wxString
& item
, size_t *size
, wxIPCFormat format
);
66 virtual bool OnPoke(const wxString
& topic
, const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
);
67 virtual bool OnStartAdvise(const wxString
& topic
, const wxString
& item
);
68 virtual bool OnStopAdvise(const wxString
& topic
, const wxString
& item
);
69 virtual bool DoAdvise(const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
);
70 virtual bool OnDisconnect();
71 virtual void Notify();
76 wxString m_sRequestDate
;
77 char m_achRequestBytes
[3];
80 class MyServer
: public wxServer
86 bool IsConnected() { return m_connection
!= NULL
; };
87 MyConnection
*GetConnection() { return m_connection
; };
88 wxConnectionBase
*OnAcceptConnection(const wxString
& topic
);
91 MyConnection
*m_connection
;
94 // ============================================================================
96 // ============================================================================
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
106 if ( !wxApp::OnInit() )
109 delete wxLog::SetActiveTarget(new wxLogStderr
);
111 // Create a new server
112 m_server
= new MyServer
;
113 if (m_server
->Create("4242"))
115 wxLogMessage(_T("Server 4242 started"));
116 #if wxUSE_DDE_FOR_IPC
117 wxLogMessage(_T("Server uses DDE"));
118 #else // !wxUSE_DDE_FOR_IPC
119 wxLogMessage(_T("Server uses TCP"));
120 #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
125 wxLogMessage(_T("Server 4242 failed to start"));
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
145 MyServer::~MyServer()
150 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
152 wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic
.c_str());
154 if ( topic
== IPC_TOPIC
)
156 m_connection
= new MyConnection
;
157 wxLogMessage(_T("Connection accepted"));
164 void MyServer::Disconnect()
168 m_connection
->Disconnect();
171 wxLogMessage(_T("Disconnected client"));
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 bool MyConnection::OnExecute(const wxString
& topic
,
180 const void *data
, size_t size
, wxIPCFormat format
)
182 Log(_T("OnExecute"), topic
, _T(""), data
, size
, format
);
186 bool MyConnection::OnPoke(const wxString
& topic
,
187 const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
189 Log(_T("OnPoke"), topic
, item
, data
, size
, format
);
190 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
193 const void *MyConnection::OnRequest(const wxString
& topic
,
194 const wxString
& item
, size_t *size
, wxIPCFormat format
)
197 if (item
== _T("Date"))
199 m_sRequestDate
= wxDateTime::Now().Format();
200 data
= m_sRequestDate
.c_str();
203 else if (item
== _T("Date+len"))
205 m_sRequestDate
= wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
206 data
= m_sRequestDate
.c_str();
207 *size
= m_sRequestDate
.Length() + 1;
209 else if (item
== _T("bytes[3]"))
211 data
= m_achRequestBytes
;
212 m_achRequestBytes
[0] = '1'; m_achRequestBytes
[1] = '2'; m_achRequestBytes
[2] = '3';
220 Log(_T("OnRequest"), topic
, item
, data
, *size
, format
);
224 bool MyConnection::OnStartAdvise(const wxString
& topic
,
225 const wxString
& item
)
227 wxLogMessage(_T("OnStartAdvise(\"%s\",\"%s\")"), topic
.c_str(), item
.c_str());
228 wxLogMessage(_T("Returning true"));
234 bool MyConnection::OnStopAdvise(const wxString
& topic
,
235 const wxString
& item
)
237 wxLogMessage(_T("OnStopAdvise(\"%s\",\"%s\")"), topic
.c_str(), item
.c_str());
238 wxLogMessage(_T("Returning true"));
244 void MyConnection::Notify()
246 if (!m_sAdvise
.IsEmpty())
248 wxString s
= wxDateTime::Now().Format();
249 Advise(m_sAdvise
, s
);
250 s
= wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
251 Advise(m_sAdvise
, (const char *)s
.c_str(), s
.Length() + 1);
253 #if wxUSE_DDE_FOR_IPC
254 wxLogMessage(_T("DDE Advise type argument cannot be wxIPC_PRIVATE. The client will receive it as wxIPC_TEXT, and receive the correct no of bytes, but not print a correct log entry."));
257 bytes
[0] = '1'; bytes
[1] = '2'; bytes
[2] = '3';
258 Advise(m_sAdvise
, bytes
, 3, wxIPC_PRIVATE
);
259 // this works, but the log treats it as a string now
260 // m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
264 bool MyConnection::DoAdvise(const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
266 Log(_T("Advise"), _T(""), item
, data
, size
, format
);
267 return wxConnection::DoAdvise(item
, data
, size
, format
);
270 bool MyConnection::OnDisconnect()
272 wxLogMessage(_T("OnDisconnect()"));