]>
Commit | Line | Data |
---|---|---|
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 | // ---------------------------------------------------------------------------- | |
42 | // wxWin macros | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
521d3436 | 45 | class MyConnection : public MyConnectionBase, public wxTimer |
ebe887ed VZ |
46 | { |
47 | public: | |
ebe887ed VZ |
48 | virtual bool Disconnect() { return wxConnection::Disconnect(); } |
49 | virtual bool OnExecute(const wxString& topic, const void *data, size_t size, wxIPCFormat format); | |
50 | virtual const void *OnRequest(const wxString& topic, const wxString& item, size_t *size, wxIPCFormat format); | |
51 | virtual bool OnPoke(const wxString& topic, const wxString& item, const void *data, size_t size, wxIPCFormat format); | |
52 | virtual bool OnStartAdvise(const wxString& topic, const wxString& item); | |
53 | virtual bool OnStopAdvise(const wxString& topic, const wxString& item); | |
54 | virtual bool DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format); | |
55 | virtual bool OnDisconnect(); | |
56 | virtual void Notify(); | |
57 | ||
ea84f255 | 58 | private: |
ebe887ed VZ |
59 | wxString m_sAdvise; |
60 | ||
ebe887ed VZ |
61 | wxString m_sRequestDate; |
62 | char m_achRequestBytes[3]; | |
63 | }; | |
64 | ||
ea84f255 | 65 | class MyServer : public wxServer |
ebe887ed VZ |
66 | { |
67 | public: | |
68 | MyServer(); | |
69 | virtual ~MyServer(); | |
70 | void Disconnect(); | |
71 | bool IsConnected() { return m_connection != NULL; }; | |
ea84f255 VZ |
72 | |
73 | virtual wxConnectionBase *OnAcceptConnection(const wxString& topic); | |
74 | ||
75 | private: | |
76 | wxConnection *m_connection; | |
77 | }; | |
78 | ||
79 | // Define a new application | |
80 | class MyApp : public wxApp | |
81 | { | |
82 | public: | |
83 | virtual bool OnInit(); | |
ebe887ed VZ |
84 | |
85 | protected: | |
ea84f255 | 86 | MyServer m_server; |
ebe887ed VZ |
87 | }; |
88 | ||
ea84f255 VZ |
89 | DECLARE_APP(MyApp) |
90 | ||
ebe887ed VZ |
91 | // ============================================================================ |
92 | // implementation | |
93 | // ============================================================================ | |
94 | ||
2e334012 | 95 | IMPLEMENT_APP_CONSOLE(MyApp) |
ebe887ed VZ |
96 | |
97 | // ---------------------------------------------------------------------------- | |
98 | // MyApp | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
101 | bool MyApp::OnInit() | |
102 | { | |
103 | if ( !wxApp::OnInit() ) | |
104 | return false; | |
105 | ||
106 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
107 | ||
ea84f255 | 108 | const char * const kind = |
ebe887ed | 109 | #if wxUSE_DDE_FOR_IPC |
ea84f255 VZ |
110 | "DDE" |
111 | #else | |
112 | "TCP" | |
113 | #endif | |
114 | ; | |
115 | ||
116 | // Create a new server | |
117 | if ( !m_server.Create(IPC_SERVICE) ) | |
ebe887ed | 118 | { |
ea84f255 | 119 | wxLogMessage("%s server failed to start on %s", kind, IPC_SERVICE); |
ebe887ed VZ |
120 | return false; |
121 | } | |
ebe887ed | 122 | |
ea84f255 VZ |
123 | wxLogMessage("%s server started on %s", kind, IPC_SERVICE); |
124 | return true; | |
ebe887ed VZ |
125 | } |
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // MyServer | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | MyServer::MyServer() | |
132 | { | |
133 | m_connection = NULL; | |
134 | } | |
135 | ||
136 | MyServer::~MyServer() | |
137 | { | |
138 | Disconnect(); | |
139 | } | |
140 | ||
141 | wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic) | |
142 | { | |
ea84f255 | 143 | wxLogMessage("OnAcceptConnection(\"%s\")", topic.c_str()); |
ebe887ed VZ |
144 | |
145 | if ( topic == IPC_TOPIC ) | |
146 | { | |
147 | m_connection = new MyConnection; | |
ea84f255 | 148 | wxLogMessage("Connection accepted"); |
ebe887ed VZ |
149 | return m_connection; |
150 | } | |
ea84f255 | 151 | |
ebe887ed VZ |
152 | // unknown topic |
153 | return NULL; | |
154 | } | |
155 | ||
156 | void MyServer::Disconnect() | |
157 | { | |
158 | if (m_connection) | |
159 | { | |
160 | m_connection->Disconnect(); | |
161 | delete m_connection; | |
162 | m_connection = NULL; | |
ea84f255 | 163 | wxLogMessage("Disconnected client"); |
ebe887ed VZ |
164 | } |
165 | } | |
166 | ||
167 | // ---------------------------------------------------------------------------- | |
168 | // MyConnection | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
ea84f255 VZ |
171 | bool |
172 | MyConnection::OnExecute(const wxString& topic, | |
173 | const void *data, | |
174 | size_t size, | |
175 | wxIPCFormat format) | |
ebe887ed | 176 | { |
ea84f255 | 177 | Log("OnExecute", topic, "", data, size, format); |
ebe887ed VZ |
178 | return true; |
179 | } | |
180 | ||
ea84f255 VZ |
181 | bool |
182 | MyConnection::OnPoke(const wxString& topic, | |
183 | const wxString& item, | |
184 | const void *data, | |
185 | size_t size, | |
186 | wxIPCFormat format) | |
ebe887ed | 187 | { |
ea84f255 | 188 | Log("OnPoke", topic, item, data, size, format); |
ebe887ed VZ |
189 | return wxConnection::OnPoke(topic, item, data, size, format); |
190 | } | |
191 | ||
ea84f255 VZ |
192 | const void * |
193 | MyConnection::OnRequest(const wxString& topic, | |
194 | const wxString& item, | |
195 | size_t *size, | |
196 | wxIPCFormat format) | |
ebe887ed VZ |
197 | { |
198 | const void *data; | |
ea84f255 | 199 | if (item == "Date") |
ebe887ed VZ |
200 | { |
201 | m_sRequestDate = wxDateTime::Now().Format(); | |
202 | data = m_sRequestDate.c_str(); | |
203 | *size = wxNO_LEN; | |
204 | } | |
ea84f255 | 205 | else if (item == "Date+len") |
ebe887ed | 206 | { |
ea84f255 | 207 | m_sRequestDate = wxDateTime::Now().FormatTime() + " " + wxDateTime::Now().FormatDate(); |
ebe887ed VZ |
208 | data = m_sRequestDate.c_str(); |
209 | *size = m_sRequestDate.Length() + 1; | |
210 | } | |
ea84f255 | 211 | else if (item == "bytes[3]") |
ebe887ed VZ |
212 | { |
213 | data = m_achRequestBytes; | |
214 | m_achRequestBytes[0] = '1'; m_achRequestBytes[1] = '2'; m_achRequestBytes[2] = '3'; | |
215 | *size = 3; | |
216 | } | |
217 | else | |
218 | { | |
219 | data = NULL; | |
220 | *size = 0; | |
221 | } | |
ea84f255 | 222 | Log("OnRequest", topic, item, data, *size, format); |
ebe887ed VZ |
223 | return data; |
224 | } | |
225 | ||
ea84f255 | 226 | bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item) |
ebe887ed | 227 | { |
ea84f255 VZ |
228 | wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic.c_str(), item.c_str()); |
229 | wxLogMessage("Returning true"); | |
ebe887ed | 230 | m_sAdvise = item; |
ea84f255 | 231 | Start(3000); // schedule our Notify() to be called in 3 seconds |
ebe887ed VZ |
232 | return true; |
233 | } | |
234 | ||
ea84f255 | 235 | bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item) |
ebe887ed | 236 | { |
ea84f255 VZ |
237 | wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic.c_str(), item.c_str()); |
238 | wxLogMessage("Returning true"); | |
239 | m_sAdvise.clear(); | |
ebe887ed VZ |
240 | Stop(); |
241 | return true; | |
242 | } | |
243 | ||
244 | void MyConnection::Notify() | |
245 | { | |
ea84f255 | 246 | if (!m_sAdvise.empty()) |
ebe887ed VZ |
247 | { |
248 | wxString s = wxDateTime::Now().Format(); | |
249 | Advise(m_sAdvise, s); | |
ea84f255 VZ |
250 | s = wxDateTime::Now().FormatTime() + " " + wxDateTime::Now().FormatDate(); |
251 | Advise(m_sAdvise, s.mb_str(), s.length() + 1); | |
ebe887ed VZ |
252 | |
253 | #if wxUSE_DDE_FOR_IPC | |
ea84f255 VZ |
254 | wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. " |
255 | "The client will receive it as wxIPC_TEXT, " | |
256 | "and receive the correct no of bytes, " | |
257 | "but not print a correct log entry."); | |
258 | #endif // DDE | |
259 | ||
ebe887ed VZ |
260 | char bytes[3]; |
261 | bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3'; | |
262 | Advise(m_sAdvise, bytes, 3, wxIPC_PRIVATE); | |
263 | // this works, but the log treats it as a string now | |
264 | // m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT ); | |
265 | } | |
266 | } | |
267 | ||
ebe887ed VZ |
268 | bool MyConnection::DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format) |
269 | { | |
ea84f255 | 270 | Log("Advise", "", item, data, size, format); |
ebe887ed VZ |
271 | return wxConnection::DoAdvise(item, data, size, format); |
272 | } | |
273 | ||
274 | bool MyConnection::OnDisconnect() | |
275 | { | |
ea84f255 | 276 | wxLogMessage("OnDisconnect()"); |
ebe887ed VZ |
277 | return true; |
278 | } |