]>
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 | |
618f2efa | 35 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) |
4b89c618 | 36 | #include "mondrian.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 JS |
50 | EVT_MENU (wxID_EXIT, MyFrame::OnExit) |
51 | EVT_CLOSE( MyFrame::OnClose ) | |
52 | EVT_BUTTON( ID_START, MyFrame::OnStart ) | |
53 | EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServerName ) | |
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 |
9d860992 JS |
73 | m_frame = new MyFrame(NULL, _T("Server")); |
74 | m_frame->Show(true); | |
7921cf2b | 75 | |
b62ca03d | 76 | return true; |
4b89c618 | 77 | } |
7921cf2b | 78 | |
4b89c618 VZ |
79 | int MyApp::OnExit() |
80 | { | |
4b89c618 VZ |
81 | return 0; |
82 | } | |
7921cf2b | 83 | |
4b89c618 VZ |
84 | // ---------------------------------------------------------------------------- |
85 | // MyFrame | |
86 | // ---------------------------------------------------------------------------- | |
7921cf2b | 87 | |
4b89c618 VZ |
88 | // Define my frame constructor |
89 | MyFrame::MyFrame(wxFrame *frame, const wxString& title) | |
9d860992 | 90 | : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300)) |
4b89c618 | 91 | { |
8520f137 | 92 | #if wxUSE_STATUSBAR |
4b89c618 | 93 | CreateStatusBar(); |
8520f137 | 94 | #endif // wxUSE_STATUSBAR |
7921cf2b | 95 | |
4b89c618 VZ |
96 | // Give it an icon |
97 | SetIcon(wxICON(mondrian)); | |
7921cf2b | 98 | |
4b89c618 VZ |
99 | // Make a menubar |
100 | wxMenu *file_menu = new wxMenu; | |
7921cf2b | 101 | |
9d860992 | 102 | file_menu->Append(wxID_EXIT, _T("&Quit\tCtrl-Q")); |
7921cf2b | 103 | |
4b89c618 | 104 | wxMenuBar *menu_bar = new wxMenuBar; |
7921cf2b | 105 | |
600683ca | 106 | menu_bar->Append(file_menu, _T("&File")); |
7921cf2b | 107 | |
4b89c618 VZ |
108 | // Associate the menu bar with the frame |
109 | SetMenuBar(menu_bar); | |
7921cf2b | 110 | |
9d860992 JS |
111 | // set a dialog background |
112 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
113 | ||
114 | // add the controls to the frame | |
115 | wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL ); | |
116 | ||
117 | wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL ); | |
118 | ||
119 | wxFlexGridSizer *item2 = new wxFlexGridSizer( 2, 0, 0 ); | |
120 | item2->AddGrowableCol( 1 ); | |
121 | ||
122 | wxButton *item3 = new wxButton( this, ID_START, wxT("Start Server"), wxDefaultPosition, wxDefaultSize, 0 ); | |
123 | item2->Add( item3, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
124 | ||
125 | wxString strs4[] = | |
126 | { | |
127 | IPC_SERVICE, _T("...") | |
128 | }; | |
129 | wxChoice *item4 = new wxChoice( this, ID_SERVERNAME, wxDefaultPosition, wxSize(100,-1), 2, strs4, 0 ); | |
130 | item2->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
131 | ||
132 | wxButton *item5 = new wxButton( this, ID_DISCONNECT, wxT("Disconnect Client"), wxDefaultPosition, wxDefaultSize, 0 ); | |
133 | item2->Add( item5, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
134 | ||
135 | item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 ); | |
136 | ||
137 | wxButton *item6 = new wxButton( this, ID_ADVISE, wxT("Advise"), wxDefaultPosition, wxDefaultSize, 0 ); | |
138 | item2->Add( item6, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
139 | ||
140 | item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 ); | |
141 | ||
142 | item1->Add( item2, 1, wxALIGN_CENTER|wxALL, 5 ); | |
143 | ||
144 | item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
145 | ||
146 | wxStaticBox *item8 = new wxStaticBox( this, -1, wxT("Server log") ); | |
147 | wxStaticBoxSizer *item7 = new wxStaticBoxSizer( item8, wxVERTICAL ); | |
148 | ||
149 | wxTextCtrl *item9 = new wxTextCtrl( this, ID_LOG, wxT(""), wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE ); | |
150 | item7->Add( item9, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
151 | ||
152 | item0->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); | |
153 | ||
154 | SetSizer( item0 ); | |
155 | item0->SetSizeHints( this ); | |
156 | ||
157 | // status | |
158 | m_server = NULL; | |
159 | GetServername()->SetSelection(0); | |
160 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog()); | |
161 | delete wxLog::SetActiveTarget(logWindow); | |
162 | wxLogMessage(_T("Click on Start to start the server")); | |
163 | Enable(); | |
7921cf2b JS |
164 | } |
165 | ||
9d860992 | 166 | void MyFrame::Enable() |
7921cf2b | 167 | { |
9d860992 JS |
168 | GetStart()->Enable(m_server == NULL); |
169 | GetServername()->Enable(m_server == NULL); | |
170 | GetAdvise()->Enable(m_server && m_server->CanAdvise()); | |
b21348b4 | 171 | GetDisconnect()->Enable(m_server && m_server->IsConnected()); |
9d860992 | 172 | } |
f010ad48 | 173 | |
9d860992 JS |
174 | void MyFrame::OnClose(wxCloseEvent& event) |
175 | { | |
176 | if (m_server) | |
177 | { | |
178 | delete m_server; | |
179 | m_server = NULL; | |
7921cf2b | 180 | } |
9d860992 | 181 | event.Skip(); |
7921cf2b JS |
182 | } |
183 | ||
f6bcfd97 BP |
184 | void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) |
185 | { | |
b62ca03d | 186 | Close(true); |
f6bcfd97 BP |
187 | } |
188 | ||
9d860992 JS |
189 | void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event)) |
190 | { | |
191 | // Create a new server | |
192 | m_server = new MyServer; | |
193 | wxString servername = GetServername()->GetStringSelection(); | |
194 | if (m_server->Create(servername)) | |
195 | { | |
196 | wxLogMessage(_T("Server %s started"), servername.c_str()); | |
197 | #if wxUSE_DDE_FOR_IPC | |
198 | wxLogMessage(_T("Server uses DDE")); | |
199 | #else // !wxUSE_DDE_FOR_IPC | |
200 | wxLogMessage(_T("Server uses TCP")); | |
201 | #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC | |
202 | } | |
203 | else | |
204 | { | |
205 | wxLogMessage(_T("Server %s failed to start"), servername.c_str()); | |
206 | delete m_server; | |
207 | m_server = NULL; | |
208 | } | |
209 | Enable(); | |
210 | } | |
7921cf2b | 211 | |
9d860992 | 212 | void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) ) |
7921cf2b | 213 | { |
9d860992 JS |
214 | if (GetServername()->GetStringSelection() == _T("...")) |
215 | { | |
216 | wxString s = wxGetTextFromUser(_T("Specify the name of the server"), | |
217 | _T("Server Name"), _(""), this); | |
218 | if (!s.IsEmpty() && s != IPC_SERVICE) | |
219 | { | |
220 | GetServername()->Insert(s, 0); | |
221 | GetServername()->SetSelection(0); | |
222 | } | |
223 | } | |
7921cf2b JS |
224 | } |
225 | ||
9d860992 | 226 | void MyFrame::Disconnect() |
f010ad48 | 227 | { |
9d860992 JS |
228 | m_server->Disconnect(); |
229 | Enable(); | |
f010ad48 JS |
230 | } |
231 | ||
9d860992 | 232 | void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event)) |
7921cf2b | 233 | { |
9d860992 JS |
234 | Disconnect(); |
235 | } | |
236 | ||
237 | void MyFrame::OnAdvise(wxCommandEvent& WXUNUSED(event)) | |
238 | { | |
239 | m_server->Advise(); | |
7921cf2b JS |
240 | } |
241 | ||
4b89c618 VZ |
242 | // ---------------------------------------------------------------------------- |
243 | // MyServer | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
9d860992 JS |
246 | MyServer::MyServer() : wxServer() |
247 | { | |
248 | m_connection = NULL; | |
249 | } | |
250 | ||
251 | MyServer::~MyServer() | |
252 | { | |
253 | Disconnect(); | |
254 | } | |
255 | ||
7921cf2b JS |
256 | wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic) |
257 | { | |
d2797f55 | 258 | wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic.c_str()); |
d5172a58 | 259 | |
9d860992 JS |
260 | if ( topic == IPC_TOPIC ) |
261 | { | |
262 | m_connection = new MyConnection(); | |
263 | wxGetApp().GetFrame()->Enable(); | |
264 | wxLogMessage(_T("Connection accepted")); | |
265 | return m_connection; | |
266 | } | |
d5172a58 VZ |
267 | // unknown topic |
268 | return NULL; | |
7921cf2b JS |
269 | } |
270 | ||
9d860992 JS |
271 | void MyServer::Disconnect() |
272 | { | |
273 | if (m_connection) | |
274 | { | |
275 | m_connection->Disconnect(); | |
276 | delete m_connection; | |
277 | m_connection = NULL; | |
278 | wxGetApp().GetFrame()->Enable(); | |
279 | wxLogMessage(_T("Disconnected client")); | |
280 | } | |
281 | } | |
282 | ||
283 | void MyServer::Advise() | |
284 | { | |
285 | if (CanAdvise()) | |
286 | { | |
287 | wxString s = wxDateTime::Now().Format(); | |
50c549b9 | 288 | m_connection->Advise(m_connection->m_sAdvise, s); |
9d860992 | 289 | s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); |
50c549b9 | 290 | m_connection->Advise(m_connection->m_sAdvise, (const char *)s.c_str(), s.Length() + 1); |
9d860992 JS |
291 | |
292 | #if wxUSE_DDE_FOR_IPC | |
293 | 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.")); | |
294 | #endif | |
295 | char bytes[3]; | |
296 | bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3'; | |
50c549b9 | 297 | m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_PRIVATE); |
9d860992 | 298 | // this works, but the log treats it as a string now |
50c549b9 | 299 | // m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT ); |
9d860992 JS |
300 | } |
301 | } | |
302 | ||
4b89c618 VZ |
303 | // ---------------------------------------------------------------------------- |
304 | // MyConnection | |
305 | // ---------------------------------------------------------------------------- | |
306 | ||
9d860992 | 307 | bool MyConnection::OnExecute(const wxString& topic, |
50c549b9 | 308 | const void *data, size_t size, wxIPCFormat format) |
9d860992 JS |
309 | { |
310 | Log(_T("OnExecute"), topic, _T(""), data, size, format); | |
311 | return true; | |
312 | } | |
313 | ||
314 | bool MyConnection::OnPoke(const wxString& topic, | |
50c549b9 | 315 | const wxString& item, const void *data, size_t size, wxIPCFormat format) |
9d860992 JS |
316 | { |
317 | Log(_T("OnPoke"), topic, item, data, size, format); | |
318 | return wxConnection::OnPoke(topic, item, data, size, format); | |
319 | } | |
320 | ||
50c549b9 VZ |
321 | const void *MyConnection::OnRequest(const wxString& topic, |
322 | const wxString& item, size_t *size, wxIPCFormat format) | |
9d860992 | 323 | { |
50c549b9 | 324 | const void *data; |
9d860992 | 325 | if (item == _T("Date")) |
4b89c618 | 326 | { |
9d860992 | 327 | m_sRequestDate = wxDateTime::Now().Format(); |
1e0f0a90 | 328 | data = m_sRequestDate.c_str(); |
50c549b9 | 329 | *size = wxNO_LEN; |
9d860992 JS |
330 | } |
331 | else if (item == _T("Date+len")) | |
332 | { | |
333 | m_sRequestDate = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); | |
1e0f0a90 | 334 | data = m_sRequestDate.c_str(); |
50c549b9 | 335 | *size = m_sRequestDate.Length() + 1; |
9d860992 JS |
336 | } |
337 | else if (item == _T("bytes[3]")) | |
338 | { | |
1e0f0a90 | 339 | data = m_achRequestBytes; |
9d860992 JS |
340 | m_achRequestBytes[0] = '1'; m_achRequestBytes[1] = '2'; m_achRequestBytes[2] = '3'; |
341 | *size = 3; | |
342 | } | |
343 | else | |
344 | { | |
345 | data = NULL; | |
346 | *size = 0; | |
4b89c618 | 347 | } |
50c549b9 | 348 | Log(_T("OnRequest"), topic, item, data, *size, format); |
9d860992 | 349 | return data; |
7921cf2b JS |
350 | } |
351 | ||
9d860992 JS |
352 | bool MyConnection::OnStartAdvise(const wxString& topic, |
353 | const wxString& item) | |
7921cf2b | 354 | { |
d2797f55 | 355 | wxLogMessage(_T("OnStartAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str()); |
9d860992 JS |
356 | wxLogMessage(_T("Returning true")); |
357 | m_sAdvise = item; | |
358 | wxGetApp().GetFrame()->Enable(); | |
b62ca03d | 359 | return true; |
7921cf2b JS |
360 | } |
361 | ||
9d860992 JS |
362 | bool MyConnection::OnStopAdvise(const wxString& topic, |
363 | const wxString& item) | |
7921cf2b | 364 | { |
d2797f55 | 365 | wxLogMessage(_T("OnStopAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str()); |
9d860992 JS |
366 | wxLogMessage(_T("Returning true")); |
367 | m_sAdvise.Empty(); | |
368 | wxGetApp().GetFrame()->Enable(); | |
b62ca03d | 369 | return true; |
7921cf2b JS |
370 | } |
371 | ||
50c549b9 | 372 | bool MyConnection::DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format) |
7921cf2b | 373 | { |
9d860992 | 374 | Log(_T("Advise"), _T(""), item, data, size, format); |
50c549b9 | 375 | return wxConnection::DoAdvise(item, data, size, format); |
7921cf2b JS |
376 | } |
377 | ||
9d860992 | 378 | bool MyConnection::OnDisconnect() |
7921cf2b | 379 | { |
9d860992 JS |
380 | wxLogMessage(_T("OnDisconnect()")); |
381 | wxGetApp().GetFrame()->Disconnect(); | |
b62ca03d | 382 | return true; |
7921cf2b | 383 | } |