]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: IPC sample: server
4 // Author: Julian Smart
5 // Modified by: Jurgen Doornik
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
35 #if !defined(__WXMSW__) && !defined(__WXPM__)
36 #include "../sample.xpm"
40 #include "wx/textdlg.h"
41 #include "wx/datetime.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
49 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
50 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
)
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
69 if ( !wxApp::OnInit() )
72 // Create the main frame window
73 m_frame
= new MyFrame(NULL
, "Server");
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // Define my frame constructor
84 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
85 : wxFrame(frame
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(400, 300))
89 #endif // wxUSE_STATUSBAR
91 SetIcon(wxICON(sample
));
95 wxPanel
* const panel
= new wxPanel(this);
97 wxBoxSizer
* const sizerMain
= new wxBoxSizer( wxVERTICAL
);
99 wxFlexGridSizer
* const sizerCmds
= new wxFlexGridSizer( 2, 0, 0 );
100 sizerCmds
->AddGrowableCol( 1 );
104 btn
= new wxButton(panel
, ID_START
, "&Start Server");
105 sizerCmds
->Add(btn
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
107 const wxString choices
[] = { IPC_SERVICE
, "..." };
108 wxChoice
* const choice
= new wxChoice
112 wxDefaultPosition
, wxSize(100, -1),
113 WXSIZEOF(choices
), choices
115 sizerCmds
->Add(choice
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
117 btn
= new wxButton(panel
, ID_DISCONNECT
, "&Disconnect Client");
118 sizerCmds
->Add(btn
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
119 sizerCmds
->AddSpacer(20);
121 btn
= new wxButton( panel
, ID_ADVISE
, "&Advise");
122 sizerCmds
->Add(btn
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
123 sizerCmds
->AddSpacer(20);
125 sizerMain
->Add(sizerCmds
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
127 wxStaticBoxSizer
* const
128 sizerLog
= new wxStaticBoxSizer(wxVERTICAL
, panel
, "Server &log");
130 wxTextCtrl
* const textLog
= new wxTextCtrl
135 wxDefaultPosition
, wxSize(500, 140),
138 sizerLog
->Add(textLog
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
140 sizerMain
->Add(sizerLog
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
142 panel
->SetSizer(sizerMain
);
143 sizerMain
->SetSizeHints(panel
);
144 SetClientSize(panel
->GetSize());
146 GetServername()->SetSelection(0);
147 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textLog
);
148 delete wxLog::SetActiveTarget(logWindow
);
149 wxLogMessage("Click on Start to start the server");
153 void MyFrame::UpdateUI()
155 GetStart()->Enable(m_server
== NULL
);
156 GetServername()->Enable(m_server
== NULL
);
157 GetAdvise()->Enable(m_server
&& m_server
->CanAdvise());
158 GetDisconnect()->Enable(m_server
&& m_server
->IsConnected());
161 void MyFrame::OnClose(wxCloseEvent
& event
)
171 void MyFrame::OnStart(wxCommandEvent
& WXUNUSED(event
))
173 // Create a new server
174 m_server
= new MyServer
;
175 wxString servername
= GetServername()->GetStringSelection();
176 if (m_server
->Create(servername
))
178 wxLogMessage("Server %s started", servername
);
179 #if wxUSE_DDE_FOR_IPC
180 wxLogMessage("Server uses DDE");
181 #else // !wxUSE_DDE_FOR_IPC
182 wxLogMessage("Server uses TCP");
183 #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
187 wxLogMessage("Server %s failed to start", servername
);
194 void MyFrame::OnServerName( wxCommandEvent
& WXUNUSED(event
) )
196 if ( GetServername()->GetStringSelection() == "..." )
198 wxString s
= wxGetTextFromUser
200 "Specify the name of the server",
206 if ( !s
.empty() && s
!= IPC_SERVICE
)
208 GetServername()->Insert(s
, 0);
209 GetServername()->SetSelection(0);
214 void MyFrame::Disconnect()
216 m_server
->Disconnect();
220 void MyFrame::OnDisconnect(wxCommandEvent
& WXUNUSED(event
))
225 void MyFrame::OnAdvise(wxCommandEvent
& WXUNUSED(event
))
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 MyServer::MyServer() : wxServer()
239 MyServer::~MyServer()
244 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
246 wxLogMessage("OnAcceptConnection(\"%s\")", topic
);
248 if ( topic
== IPC_TOPIC
)
250 m_connection
= new MyConnection();
251 wxGetApp().GetFrame()->UpdateUI();
252 wxLogMessage("Connection accepted");
255 //else: unknown topic
257 wxLogMessage("Unknown topic, connection refused");
261 void MyServer::Disconnect()
267 wxGetApp().GetFrame()->UpdateUI();
268 wxLogMessage("Disconnected client");
272 void MyServer::Advise()
276 const wxDateTime now
= wxDateTime::Now();
278 m_connection
->Advise(m_connection
->m_advise
, now
.Format());
280 const wxString s
= now
.FormatTime() + " " + now
.FormatDate();
281 m_connection
->Advise(m_connection
->m_advise
, s
.mb_str(), wxNO_LEN
);
283 #if wxUSE_DDE_FOR_IPC
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.");
289 char bytes
[3] = { '1', '2', '3' };
290 m_connection
->Advise(m_connection
->m_advise
, bytes
, 3, wxIPC_PRIVATE
);
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
299 MyConnection::OnExecute(const wxString
& topic
,
304 Log("OnExecute", topic
, "", data
, size
, format
);
309 MyConnection::OnPoke(const wxString
& topic
,
310 const wxString
& item
,
315 Log("OnPoke", topic
, item
, data
, size
, format
);
316 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
320 MyConnection::OnRequest(const wxString
& topic
,
321 const wxString
& item
,
328 if ( item
.StartsWith("Date", &afterDate
) )
330 const wxDateTime now
= wxDateTime::Now();
332 if ( afterDate
.empty() )
334 m_requestData
= now
.Format();
337 else if ( afterDate
== "+len" )
339 m_requestData
= now
.FormatTime() + " " + now
.FormatDate();
340 *size
= strlen(m_requestData
.mb_str()) + 1;
343 else if ( item
== "bytes[3]" )
345 m_requestData
= "123";
351 wxLogMessage("Unknown request for \"%s\"", item
);
355 const void * const data
= m_requestData
.mb_str();
356 Log("OnRequest", topic
, item
, data
, *size
, format
);
360 bool MyConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
362 wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic
, item
);
363 wxLogMessage("Returning true");
365 wxGetApp().GetFrame()->UpdateUI();
369 bool MyConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
371 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic
, item
);
372 wxLogMessage("Returning true");
374 wxGetApp().GetFrame()->UpdateUI();
379 MyConnection::DoAdvise(const wxString
& item
,
384 Log("Advise", "", item
, data
, size
, format
);
385 return wxConnection::DoAdvise(item
, data
, size
, format
);
388 bool MyConnection::OnDisconnect()
390 wxLogMessage("OnDisconnect()");
391 wxGetApp().GetFrame()->Disconnect();