]>
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
)
167 void MyFrame::OnStart(wxCommandEvent
& WXUNUSED(event
))
169 // Create a new server
170 m_server
= new MyServer
;
171 wxString servername
= GetServername()->GetStringSelection();
172 if (m_server
->Create(servername
))
174 wxLogMessage("Server %s started", servername
);
175 #if wxUSE_DDE_FOR_IPC
176 wxLogMessage("Server uses DDE");
177 #else // !wxUSE_DDE_FOR_IPC
178 wxLogMessage("Server uses TCP");
179 #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
183 wxLogMessage("Server %s failed to start", servername
);
189 void MyFrame::OnServerName( wxCommandEvent
& WXUNUSED(event
) )
191 if ( GetServername()->GetStringSelection() == "..." )
193 wxString s
= wxGetTextFromUser
195 "Specify the name of the server",
201 if ( !s
.empty() && s
!= IPC_SERVICE
)
203 GetServername()->Insert(s
, 0);
204 GetServername()->SetSelection(0);
209 void MyFrame::Disconnect()
211 m_server
->Disconnect();
215 void MyFrame::OnDisconnect(wxCommandEvent
& WXUNUSED(event
))
220 void MyFrame::OnAdvise(wxCommandEvent
& WXUNUSED(event
))
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 MyServer::MyServer() : wxServer()
234 MyServer::~MyServer()
239 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
241 wxLogMessage("OnAcceptConnection(\"%s\")", topic
);
243 if ( topic
== IPC_TOPIC
)
245 m_connection
= new MyConnection();
246 wxGetApp().GetFrame()->UpdateUI();
247 wxLogMessage("Connection accepted");
250 //else: unknown topic
252 wxLogMessage("Unknown topic, connection refused");
256 void MyServer::Disconnect()
260 wxDELETE(m_connection
);
261 wxGetApp().GetFrame()->UpdateUI();
262 wxLogMessage("Disconnected client");
266 void MyServer::Advise()
270 const wxDateTime now
= wxDateTime::Now();
272 m_connection
->Advise(m_connection
->m_advise
, now
.Format());
274 const wxString s
= now
.FormatTime() + " " + now
.FormatDate();
275 m_connection
->Advise(m_connection
->m_advise
, s
.mb_str(), wxNO_LEN
);
277 #if wxUSE_DDE_FOR_IPC
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.");
283 char bytes
[3] = { '1', '2', '3' };
284 m_connection
->Advise(m_connection
->m_advise
, bytes
, 3, wxIPC_PRIVATE
);
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
293 MyConnection::OnExecute(const wxString
& topic
,
298 Log("OnExecute", topic
, "", data
, size
, format
);
303 MyConnection::OnPoke(const wxString
& topic
,
304 const wxString
& item
,
309 Log("OnPoke", topic
, item
, data
, size
, format
);
310 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
314 MyConnection::OnRequest(const wxString
& topic
,
315 const wxString
& item
,
323 if ( item
.StartsWith("Date", &afterDate
) )
325 const wxDateTime now
= wxDateTime::Now();
327 if ( afterDate
.empty() )
332 else if ( afterDate
== "+len" )
334 s
= now
.FormatTime() + " " + now
.FormatDate();
335 *size
= strlen(s
.mb_str()) + 1;
338 else if ( item
== "bytes[3]" )
346 wxLogMessage("Unknown request for \"%s\"", item
);
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
;
354 Log("OnRequest", topic
, item
, data
, *size
, format
);
358 bool MyConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
360 wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic
, item
);
361 wxLogMessage("Returning true");
363 wxGetApp().GetFrame()->UpdateUI();
367 bool MyConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
369 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic
, item
);
370 wxLogMessage("Returning true");
372 wxGetApp().GetFrame()->UpdateUI();
377 MyConnection::DoAdvise(const wxString
& item
,
382 Log("Advise", "", item
, data
, size
, format
);
383 return wxConnection::DoAdvise(item
, data
, size
, format
);
386 bool MyConnection::OnDisconnect()
388 wxLogMessage("OnDisconnect()");
389 wxGetApp().GetFrame()->Disconnect();