]>
Commit | Line | Data |
---|---|---|
7921cf2b JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: server.cpp | |
4b89c618 | 3 | // Purpose: IPC sample: server |
7921cf2b | 4 | // Author: Julian Smart |
9d860992 | 5 | // Modified by: Jurgen Doornik |
7921cf2b JS |
6 | // Created: 25/01/99 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
4b89c618 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7921cf2b JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
4b89c618 | 24 | #pragma hdrstop |
7921cf2b JS |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
4b89c618 | 28 | #include "wx/wx.h" |
7921cf2b JS |
29 | #endif |
30 | ||
31 | // Settings common to both executables: determines whether | |
32 | // we're using TCP/IP or real DDE. | |
9d860992 | 33 | #include "ipcsetup.h" |
7921cf2b | 34 | |
589c1174 VZ |
35 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
36 | #include "../sample.xpm" | |
7921cf2b JS |
37 | #endif |
38 | ||
39 | #include "server.h" | |
9d860992 JS |
40 | #include "wx/textdlg.h" |
41 | #include "wx/datetime.h" | |
7921cf2b | 42 | |
4b89c618 VZ |
43 | // ---------------------------------------------------------------------------- |
44 | // wxWin macros | |
45 | // ---------------------------------------------------------------------------- | |
7921cf2b JS |
46 | |
47 | IMPLEMENT_APP(MyApp) | |
48 | ||
4b89c618 | 49 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
9d860992 | 50 | EVT_CLOSE( MyFrame::OnClose ) |
589c1174 VZ |
51 | |
52 | EVT_BUTTON( ID_START, MyFrame::OnStart ) | |
53 | EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServerName ) | |
9d860992 JS |
54 | EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect ) |
55 | EVT_BUTTON( ID_ADVISE, MyFrame::OnAdvise ) | |
4b89c618 VZ |
56 | END_EVENT_TABLE() |
57 | ||
4b89c618 VZ |
58 | |
59 | // ============================================================================ | |
60 | // implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // MyApp | |
65 | // ---------------------------------------------------------------------------- | |
7921cf2b JS |
66 | |
67 | bool MyApp::OnInit() | |
68 | { | |
45e6e6f8 VZ |
69 | if ( !wxApp::OnInit() ) |
70 | return false; | |
71 | ||
4b89c618 | 72 | // Create the main frame window |
589c1174 | 73 | m_frame = new MyFrame(NULL, "Server"); |
9d860992 | 74 | m_frame->Show(true); |
7921cf2b | 75 | |
b62ca03d | 76 | return true; |
4b89c618 | 77 | } |
7921cf2b | 78 | |
4b89c618 VZ |
79 | // ---------------------------------------------------------------------------- |
80 | // MyFrame | |
81 | // ---------------------------------------------------------------------------- | |
7921cf2b | 82 | |
4b89c618 VZ |
83 | // Define my frame constructor |
84 | MyFrame::MyFrame(wxFrame *frame, const wxString& title) | |
9d860992 | 85 | : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300)) |
4b89c618 | 86 | { |
8520f137 | 87 | #if wxUSE_STATUSBAR |
4b89c618 | 88 | CreateStatusBar(); |
8520f137 | 89 | #endif // wxUSE_STATUSBAR |
7921cf2b | 90 | |
589c1174 | 91 | SetIcon(wxICON(sample)); |
7921cf2b | 92 | |
589c1174 | 93 | m_server = NULL; |
9d860992 | 94 | |
589c1174 | 95 | wxPanel * const panel = new wxPanel(this); |
9d860992 | 96 | |
589c1174 | 97 | wxBoxSizer * const sizerMain = new wxBoxSizer( wxVERTICAL ); |
9d860992 | 98 | |
589c1174 VZ |
99 | wxFlexGridSizer * const sizerCmds = new wxFlexGridSizer( 2, 0, 0 ); |
100 | sizerCmds->AddGrowableCol( 1 ); | |
9d860992 | 101 | |
589c1174 | 102 | wxButton *btn; |
9d860992 | 103 | |
589c1174 VZ |
104 | btn = new wxButton(panel, ID_START, "&Start Server"); |
105 | sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
9d860992 | 106 | |
589c1174 VZ |
107 | const wxString choices[] = { IPC_SERVICE, "..." }; |
108 | wxChoice * const choice = new wxChoice | |
109 | ( | |
110 | panel, | |
111 | ID_SERVERNAME, | |
112 | wxDefaultPosition, wxSize(100, -1), | |
113 | WXSIZEOF(choices), choices | |
114 | ); | |
115 | sizerCmds->Add(choice, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
9d860992 | 116 | |
589c1174 VZ |
117 | btn = new wxButton(panel, ID_DISCONNECT, "&Disconnect Client"); |
118 | sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
119 | sizerCmds->AddSpacer(20); | |
9d860992 | 120 | |
589c1174 VZ |
121 | btn = new wxButton( panel, ID_ADVISE, "&Advise"); |
122 | sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
123 | sizerCmds->AddSpacer(20); | |
9d860992 | 124 | |
589c1174 | 125 | sizerMain->Add(sizerCmds, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); |
9d860992 | 126 | |
589c1174 VZ |
127 | wxStaticBoxSizer * const |
128 | sizerLog = new wxStaticBoxSizer(wxVERTICAL, panel, "Server &log"); | |
9d860992 | 129 | |
589c1174 VZ |
130 | wxTextCtrl * const textLog = new wxTextCtrl |
131 | ( | |
132 | panel, | |
133 | wxID_ANY, | |
134 | "", | |
135 | wxDefaultPosition, wxSize(500, 140), | |
136 | wxTE_MULTILINE | |
137 | ); | |
138 | sizerLog->Add(textLog, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
9d860992 | 139 | |
589c1174 | 140 | sizerMain->Add(sizerLog, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5); |
9d860992 | 141 | |
589c1174 VZ |
142 | panel->SetSizer(sizerMain); |
143 | sizerMain->SetSizeHints(panel); | |
144 | SetClientSize(panel->GetSize()); | |
9d860992 | 145 | |
9d860992 | 146 | GetServername()->SetSelection(0); |
589c1174 | 147 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textLog); |
9d860992 | 148 | delete wxLog::SetActiveTarget(logWindow); |
589c1174 VZ |
149 | wxLogMessage("Click on Start to start the server"); |
150 | UpdateUI(); | |
7921cf2b JS |
151 | } |
152 | ||
589c1174 | 153 | void MyFrame::UpdateUI() |
7921cf2b | 154 | { |
9d860992 JS |
155 | GetStart()->Enable(m_server == NULL); |
156 | GetServername()->Enable(m_server == NULL); | |
157 | GetAdvise()->Enable(m_server && m_server->CanAdvise()); | |
b21348b4 | 158 | GetDisconnect()->Enable(m_server && m_server->IsConnected()); |
9d860992 | 159 | } |
f010ad48 | 160 | |
9d860992 JS |
161 | void MyFrame::OnClose(wxCloseEvent& event) |
162 | { | |
163 | if (m_server) | |
164 | { | |
165 | delete m_server; | |
166 | m_server = NULL; | |
7921cf2b | 167 | } |
9d860992 | 168 | event.Skip(); |
7921cf2b JS |
169 | } |
170 | ||
9d860992 JS |
171 | void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event)) |
172 | { | |
173 | // Create a new server | |
174 | m_server = new MyServer; | |
175 | wxString servername = GetServername()->GetStringSelection(); | |
176 | if (m_server->Create(servername)) | |
177 | { | |
589c1174 | 178 | wxLogMessage("Server %s started", servername); |
9d860992 | 179 | #if wxUSE_DDE_FOR_IPC |
589c1174 | 180 | wxLogMessage("Server uses DDE"); |
9d860992 | 181 | #else // !wxUSE_DDE_FOR_IPC |
589c1174 | 182 | wxLogMessage("Server uses TCP"); |
9d860992 JS |
183 | #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC |
184 | } | |
185 | else | |
186 | { | |
589c1174 | 187 | wxLogMessage("Server %s failed to start", servername); |
9d860992 JS |
188 | delete m_server; |
189 | m_server = NULL; | |
190 | } | |
589c1174 | 191 | UpdateUI(); |
9d860992 | 192 | } |
7921cf2b | 193 | |
9d860992 | 194 | void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) ) |
7921cf2b | 195 | { |
589c1174 | 196 | if ( GetServername()->GetStringSelection() == "..." ) |
9d860992 | 197 | { |
589c1174 VZ |
198 | wxString s = wxGetTextFromUser |
199 | ( | |
200 | "Specify the name of the server", | |
201 | "Server Name", | |
202 | "", | |
203 | this | |
204 | ); | |
205 | ||
206 | if ( !s.empty() && s != IPC_SERVICE ) | |
9d860992 JS |
207 | { |
208 | GetServername()->Insert(s, 0); | |
209 | GetServername()->SetSelection(0); | |
210 | } | |
211 | } | |
7921cf2b JS |
212 | } |
213 | ||
9d860992 | 214 | void MyFrame::Disconnect() |
f010ad48 | 215 | { |
9d860992 | 216 | m_server->Disconnect(); |
589c1174 | 217 | UpdateUI(); |
f010ad48 JS |
218 | } |
219 | ||
9d860992 | 220 | void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event)) |
7921cf2b | 221 | { |
589c1174 | 222 | Disconnect(); |
9d860992 JS |
223 | } |
224 | ||
225 | void MyFrame::OnAdvise(wxCommandEvent& WXUNUSED(event)) | |
226 | { | |
227 | m_server->Advise(); | |
7921cf2b JS |
228 | } |
229 | ||
4b89c618 VZ |
230 | // ---------------------------------------------------------------------------- |
231 | // MyServer | |
232 | // ---------------------------------------------------------------------------- | |
233 | ||
9d860992 JS |
234 | MyServer::MyServer() : wxServer() |
235 | { | |
236 | m_connection = NULL; | |
237 | } | |
238 | ||
239 | MyServer::~MyServer() | |
240 | { | |
241 | Disconnect(); | |
242 | } | |
243 | ||
7921cf2b JS |
244 | wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic) |
245 | { | |
589c1174 | 246 | wxLogMessage("OnAcceptConnection(\"%s\")", topic); |
d5172a58 | 247 | |
9d860992 JS |
248 | if ( topic == IPC_TOPIC ) |
249 | { | |
250 | m_connection = new MyConnection(); | |
589c1174 VZ |
251 | wxGetApp().GetFrame()->UpdateUI(); |
252 | wxLogMessage("Connection accepted"); | |
9d860992 JS |
253 | return m_connection; |
254 | } | |
589c1174 VZ |
255 | //else: unknown topic |
256 | ||
257 | wxLogMessage("Unknown topic, connection refused"); | |
d5172a58 | 258 | return NULL; |
7921cf2b JS |
259 | } |
260 | ||
9d860992 JS |
261 | void MyServer::Disconnect() |
262 | { | |
589c1174 | 263 | if ( m_connection ) |
9d860992 | 264 | { |
9d860992 JS |
265 | delete m_connection; |
266 | m_connection = NULL; | |
589c1174 VZ |
267 | wxGetApp().GetFrame()->UpdateUI(); |
268 | wxLogMessage("Disconnected client"); | |
9d860992 JS |
269 | } |
270 | } | |
271 | ||
272 | void MyServer::Advise() | |
273 | { | |
589c1174 | 274 | if ( CanAdvise() ) |
9d860992 | 275 | { |
589c1174 VZ |
276 | const wxDateTime now = wxDateTime::Now(); |
277 | ||
278 | m_connection->Advise(m_connection->m_advise, now.Format()); | |
279 | ||
280 | const wxString s = now.FormatTime() + " " + now.FormatDate(); | |
281 | m_connection->Advise(m_connection->m_advise, s.mb_str(), wxNO_LEN); | |
9d860992 JS |
282 | |
283 | #if wxUSE_DDE_FOR_IPC | |
589c1174 VZ |
284 | wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. " |
285 | "The client will receive it as wxIPC_TEXT, " | |
286 | " and receive the correct no of bytes, " | |
287 | "but not print a correct log entry."); | |
9d860992 | 288 | #endif |
589c1174 VZ |
289 | char bytes[3] = { '1', '2', '3' }; |
290 | m_connection->Advise(m_connection->m_advise, bytes, 3, wxIPC_PRIVATE); | |
9d860992 JS |
291 | } |
292 | } | |
293 | ||
4b89c618 VZ |
294 | // ---------------------------------------------------------------------------- |
295 | // MyConnection | |
296 | // ---------------------------------------------------------------------------- | |
297 | ||
589c1174 VZ |
298 | bool |
299 | MyConnection::OnExecute(const wxString& topic, | |
300 | const void *data, | |
301 | size_t size, | |
302 | wxIPCFormat format) | |
9d860992 | 303 | { |
589c1174 | 304 | Log("OnExecute", topic, "", data, size, format); |
9d860992 JS |
305 | return true; |
306 | } | |
307 | ||
589c1174 VZ |
308 | bool |
309 | MyConnection::OnPoke(const wxString& topic, | |
310 | const wxString& item, | |
311 | const void *data, | |
312 | size_t size, | |
313 | wxIPCFormat format) | |
9d860992 | 314 | { |
589c1174 | 315 | Log("OnPoke", topic, item, data, size, format); |
9d860992 JS |
316 | return wxConnection::OnPoke(topic, item, data, size, format); |
317 | } | |
318 | ||
589c1174 VZ |
319 | const void * |
320 | MyConnection::OnRequest(const wxString& topic, | |
321 | const wxString& item, | |
322 | size_t *size, | |
323 | wxIPCFormat format) | |
9d860992 | 324 | { |
589c1174 VZ |
325 | *size = 0; |
326 | ||
327 | wxString afterDate; | |
328 | if ( item.StartsWith("Date", &afterDate) ) | |
9d860992 | 329 | { |
589c1174 VZ |
330 | const wxDateTime now = wxDateTime::Now(); |
331 | ||
332 | if ( afterDate.empty() ) | |
333 | { | |
334 | m_requestData = now.Format(); | |
335 | *size = wxNO_LEN; | |
336 | } | |
337 | else if ( afterDate == "+len" ) | |
338 | { | |
339 | m_requestData = now.FormatTime() + " " + now.FormatDate(); | |
340 | *size = strlen(m_requestData.mb_str()) + 1; | |
341 | } | |
342 | } | |
343 | else if ( item == "bytes[3]" ) | |
9d860992 | 344 | { |
589c1174 | 345 | m_requestData = "123"; |
9d860992 JS |
346 | *size = 3; |
347 | } | |
589c1174 VZ |
348 | |
349 | if ( !*size ) | |
9d860992 | 350 | { |
589c1174 VZ |
351 | wxLogMessage("Unknown request for \"%s\"", item); |
352 | return NULL; | |
4b89c618 | 353 | } |
589c1174 VZ |
354 | |
355 | const void * const data = m_requestData.mb_str(); | |
356 | Log("OnRequest", topic, item, data, *size, format); | |
9d860992 | 357 | return data; |
7921cf2b JS |
358 | } |
359 | ||
589c1174 | 360 | bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item) |
7921cf2b | 361 | { |
589c1174 VZ |
362 | wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic, item); |
363 | wxLogMessage("Returning true"); | |
364 | m_advise = item; | |
365 | wxGetApp().GetFrame()->UpdateUI(); | |
b62ca03d | 366 | return true; |
7921cf2b JS |
367 | } |
368 | ||
589c1174 | 369 | bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item) |
7921cf2b | 370 | { |
589c1174 VZ |
371 | wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic, item); |
372 | wxLogMessage("Returning true"); | |
373 | m_advise.clear(); | |
374 | wxGetApp().GetFrame()->UpdateUI(); | |
b62ca03d | 375 | return true; |
7921cf2b JS |
376 | } |
377 | ||
589c1174 VZ |
378 | bool |
379 | MyConnection::DoAdvise(const wxString& item, | |
380 | const void *data, | |
381 | size_t size, | |
382 | wxIPCFormat format) | |
7921cf2b | 383 | { |
589c1174 | 384 | Log("Advise", "", item, data, size, format); |
50c549b9 | 385 | return wxConnection::DoAdvise(item, data, size, format); |
7921cf2b JS |
386 | } |
387 | ||
9d860992 | 388 | bool MyConnection::OnDisconnect() |
7921cf2b | 389 | { |
589c1174 | 390 | wxLogMessage("OnDisconnect()"); |
9d860992 | 391 | wxGetApp().GetFrame()->Disconnect(); |
b62ca03d | 392 | return true; |
7921cf2b | 393 | } |