]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/server.cpp
Remove wxMGL port.
[wxWidgets.git] / samples / ipc / server.cpp
CommitLineData
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
47IMPLEMENT_APP(MyApp)
48
4b89c618 49BEGIN_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
56END_EVENT_TABLE()
57
4b89c618
VZ
58
59// ============================================================================
60// implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// MyApp
65// ----------------------------------------------------------------------------
7921cf2b
JS
66
67bool 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
84MyFrame::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 153void 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
161void MyFrame::OnClose(wxCloseEvent& event)
162{
5276b0a5 163 wxDELETE(m_server);
9d860992 164 event.Skip();
7921cf2b
JS
165}
166
9d860992
JS
167void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
168{
169 // Create a new server
170 m_server = new MyServer;
171 wxString servername = GetServername()->GetStringSelection();
172 if (m_server->Create(servername))
173 {
589c1174 174 wxLogMessage("Server %s started", servername);
9d860992 175 #if wxUSE_DDE_FOR_IPC
589c1174 176 wxLogMessage("Server uses DDE");
9d860992 177 #else // !wxUSE_DDE_FOR_IPC
589c1174 178 wxLogMessage("Server uses TCP");
9d860992
JS
179 #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
180 }
181 else
182 {
589c1174 183 wxLogMessage("Server %s failed to start", servername);
5276b0a5 184 wxDELETE(m_server);
9d860992 185 }
589c1174 186 UpdateUI();
9d860992 187}
7921cf2b 188
9d860992 189void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) )
7921cf2b 190{
589c1174 191 if ( GetServername()->GetStringSelection() == "..." )
9d860992 192 {
589c1174
VZ
193 wxString s = wxGetTextFromUser
194 (
195 "Specify the name of the server",
196 "Server Name",
197 "",
198 this
199 );
200
201 if ( !s.empty() && s != IPC_SERVICE )
9d860992
JS
202 {
203 GetServername()->Insert(s, 0);
204 GetServername()->SetSelection(0);
205 }
206 }
7921cf2b
JS
207}
208
9d860992 209void MyFrame::Disconnect()
f010ad48 210{
9d860992 211 m_server->Disconnect();
589c1174 212 UpdateUI();
f010ad48
JS
213}
214
9d860992 215void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
7921cf2b 216{
589c1174 217 Disconnect();
9d860992
JS
218}
219
220void MyFrame::OnAdvise(wxCommandEvent& WXUNUSED(event))
221{
222 m_server->Advise();
7921cf2b
JS
223}
224
4b89c618
VZ
225// ----------------------------------------------------------------------------
226// MyServer
227// ----------------------------------------------------------------------------
228
9d860992
JS
229MyServer::MyServer() : wxServer()
230{
231 m_connection = NULL;
232}
233
234MyServer::~MyServer()
235{
236 Disconnect();
237}
238
7921cf2b
JS
239wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
240{
589c1174 241 wxLogMessage("OnAcceptConnection(\"%s\")", topic);
d5172a58 242
9d860992
JS
243 if ( topic == IPC_TOPIC )
244 {
245 m_connection = new MyConnection();
589c1174
VZ
246 wxGetApp().GetFrame()->UpdateUI();
247 wxLogMessage("Connection accepted");
9d860992
JS
248 return m_connection;
249 }
589c1174
VZ
250 //else: unknown topic
251
252 wxLogMessage("Unknown topic, connection refused");
d5172a58 253 return NULL;
7921cf2b
JS
254}
255
9d860992
JS
256void MyServer::Disconnect()
257{
589c1174 258 if ( m_connection )
9d860992 259 {
5276b0a5 260 wxDELETE(m_connection);
589c1174
VZ
261 wxGetApp().GetFrame()->UpdateUI();
262 wxLogMessage("Disconnected client");
9d860992
JS
263 }
264}
265
266void MyServer::Advise()
267{
589c1174 268 if ( CanAdvise() )
9d860992 269 {
589c1174
VZ
270 const wxDateTime now = wxDateTime::Now();
271
272 m_connection->Advise(m_connection->m_advise, now.Format());
273
274 const wxString s = now.FormatTime() + " " + now.FormatDate();
275 m_connection->Advise(m_connection->m_advise, s.mb_str(), wxNO_LEN);
9d860992
JS
276
277#if wxUSE_DDE_FOR_IPC
589c1174
VZ
278 wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
279 "The client will receive it as wxIPC_TEXT, "
280 " and receive the correct no of bytes, "
281 "but not print a correct log entry.");
9d860992 282#endif
589c1174
VZ
283 char bytes[3] = { '1', '2', '3' };
284 m_connection->Advise(m_connection->m_advise, bytes, 3, wxIPC_PRIVATE);
9d860992
JS
285 }
286}
287
4b89c618
VZ
288// ----------------------------------------------------------------------------
289// MyConnection
290// ----------------------------------------------------------------------------
291
589c1174
VZ
292bool
293MyConnection::OnExecute(const wxString& topic,
294 const void *data,
295 size_t size,
296 wxIPCFormat format)
9d860992 297{
589c1174 298 Log("OnExecute", topic, "", data, size, format);
9d860992
JS
299 return true;
300}
301
589c1174
VZ
302bool
303MyConnection::OnPoke(const wxString& topic,
304 const wxString& item,
305 const void *data,
306 size_t size,
307 wxIPCFormat format)
9d860992 308{
589c1174 309 Log("OnPoke", topic, item, data, size, format);
9d860992
JS
310 return wxConnection::OnPoke(topic, item, data, size, format);
311}
312
589c1174
VZ
313const void *
314MyConnection::OnRequest(const wxString& topic,
315 const wxString& item,
316 size_t *size,
317 wxIPCFormat format)
9d860992 318{
589c1174
VZ
319 *size = 0;
320
e536b750
VZ
321 wxString s,
322 afterDate;
589c1174 323 if ( item.StartsWith("Date", &afterDate) )
9d860992 324 {
589c1174
VZ
325 const wxDateTime now = wxDateTime::Now();
326
327 if ( afterDate.empty() )
328 {
e536b750 329 s = now.Format();
589c1174
VZ
330 *size = wxNO_LEN;
331 }
332 else if ( afterDate == "+len" )
333 {
e536b750
VZ
334 s = now.FormatTime() + " " + now.FormatDate();
335 *size = strlen(s.mb_str()) + 1;
589c1174
VZ
336 }
337 }
338 else if ( item == "bytes[3]" )
9d860992 339 {
e536b750 340 s = "123";
9d860992
JS
341 *size = 3;
342 }
589c1174
VZ
343
344 if ( !*size )
9d860992 345 {
589c1174
VZ
346 wxLogMessage("Unknown request for \"%s\"", item);
347 return NULL;
4b89c618 348 }
589c1174 349
e536b750
VZ
350 // store the data pointer to which we return in a member variable to ensure
351 // that the pointer remains valid even after we return
352 m_requestData = s.mb_str();
353 const void * const data = m_requestData;
589c1174 354 Log("OnRequest", topic, item, data, *size, format);
9d860992 355 return data;
7921cf2b
JS
356}
357
589c1174 358bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
7921cf2b 359{
589c1174
VZ
360 wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic, item);
361 wxLogMessage("Returning true");
362 m_advise = item;
363 wxGetApp().GetFrame()->UpdateUI();
b62ca03d 364 return true;
7921cf2b
JS
365}
366
589c1174 367bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item)
7921cf2b 368{
589c1174
VZ
369 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic, item);
370 wxLogMessage("Returning true");
371 m_advise.clear();
372 wxGetApp().GetFrame()->UpdateUI();
b62ca03d 373 return true;
7921cf2b
JS
374}
375
589c1174
VZ
376bool
377MyConnection::DoAdvise(const wxString& item,
378 const void *data,
379 size_t size,
380 wxIPCFormat format)
7921cf2b 381{
589c1174 382 Log("Advise", "", item, data, size, format);
50c549b9 383 return wxConnection::DoAdvise(item, data, size, format);
7921cf2b
JS
384}
385
9d860992 386bool MyConnection::OnDisconnect()
7921cf2b 387{
589c1174 388 wxLogMessage("OnDisconnect()");
9d860992 389 wxGetApp().GetFrame()->Disconnect();
b62ca03d 390 return true;
7921cf2b 391}