]>
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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 // Settings common to both executables: determines whether
31 // we're using TCP/IP or real DDE.
34 #ifndef wxHAS_IMAGES_IN_RESOURCES
35 #include "../sample.xpm"
39 #include "wx/textdlg.h"
40 #include "wx/datetime.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
49 EVT_CLOSE( MyFrame::OnClose
)
51 EVT_BUTTON( ID_START
, MyFrame::OnStart
)
52 EVT_CHOICE( ID_SERVERNAME
, MyFrame::OnServerName
)
53 EVT_BUTTON( ID_DISCONNECT
, MyFrame::OnDisconnect
)
54 EVT_BUTTON( ID_ADVISE
, MyFrame::OnAdvise
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
68 if ( !wxApp::OnInit() )
71 // Create the main frame window
72 m_frame
= new MyFrame(NULL
, "Server");
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // Define my frame constructor
83 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
84 : wxFrame(frame
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(400, 300))
88 #endif // wxUSE_STATUSBAR
90 SetIcon(wxICON(sample
));
94 wxPanel
* const panel
= new wxPanel(this);
96 wxBoxSizer
* const sizerMain
= new wxBoxSizer( wxVERTICAL
);
98 wxFlexGridSizer
* const sizerCmds
= new wxFlexGridSizer( 2, 0, 0 );
99 sizerCmds
->AddGrowableCol( 1 );
103 btn
= new wxButton(panel
, ID_START
, "&Start Server");
104 sizerCmds
->Add(btn
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
106 const wxString choices
[] = { IPC_SERVICE
, "..." };
107 wxChoice
* const choice
= new wxChoice
111 wxDefaultPosition
, wxSize(100, -1),
112 WXSIZEOF(choices
), choices
114 sizerCmds
->Add(choice
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
116 btn
= new wxButton(panel
, ID_DISCONNECT
, "&Disconnect Client");
117 sizerCmds
->Add(btn
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
118 sizerCmds
->AddSpacer(20);
120 btn
= new wxButton( panel
, ID_ADVISE
, "&Advise");
121 sizerCmds
->Add(btn
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
122 sizerCmds
->AddSpacer(20);
124 sizerMain
->Add(sizerCmds
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
126 wxStaticBoxSizer
* const
127 sizerLog
= new wxStaticBoxSizer(wxVERTICAL
, panel
, "Server &log");
129 wxTextCtrl
* const textLog
= new wxTextCtrl
134 wxDefaultPosition
, wxSize(500, 140),
137 sizerLog
->Add(textLog
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
139 sizerMain
->Add(sizerLog
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
141 panel
->SetSizer(sizerMain
);
142 sizerMain
->SetSizeHints(panel
);
143 SetClientSize(panel
->GetSize());
145 GetServername()->SetSelection(0);
146 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textLog
);
147 delete wxLog::SetActiveTarget(logWindow
);
148 wxLogMessage("Click on Start to start the server");
152 void MyFrame::UpdateUI()
154 GetStart()->Enable(m_server
== NULL
);
155 GetServername()->Enable(m_server
== NULL
);
156 GetAdvise()->Enable(m_server
&& m_server
->CanAdvise());
157 GetDisconnect()->Enable(m_server
&& m_server
->IsConnected());
160 void MyFrame::OnClose(wxCloseEvent
& event
)
166 void MyFrame::OnStart(wxCommandEvent
& WXUNUSED(event
))
168 // Create a new server
169 m_server
= new MyServer
;
170 wxString servername
= GetServername()->GetStringSelection();
171 if (m_server
->Create(servername
))
173 wxLogMessage("Server %s started", servername
);
174 #if wxUSE_DDE_FOR_IPC
175 wxLogMessage("Server uses DDE");
176 #else // !wxUSE_DDE_FOR_IPC
177 wxLogMessage("Server uses TCP");
178 #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
182 wxLogMessage("Server %s failed to start", servername
);
188 void MyFrame::OnServerName( wxCommandEvent
& WXUNUSED(event
) )
190 if ( GetServername()->GetStringSelection() == "..." )
192 wxString s
= wxGetTextFromUser
194 "Specify the name of the server",
200 if ( !s
.empty() && s
!= IPC_SERVICE
)
202 GetServername()->Insert(s
, 0);
203 GetServername()->SetSelection(0);
208 void MyFrame::Disconnect()
210 m_server
->Disconnect();
214 void MyFrame::OnDisconnect(wxCommandEvent
& WXUNUSED(event
))
219 void MyFrame::OnAdvise(wxCommandEvent
& WXUNUSED(event
))
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 MyServer::MyServer() : wxServer()
233 MyServer::~MyServer()
238 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
240 wxLogMessage("OnAcceptConnection(\"%s\")", topic
);
242 if ( topic
== IPC_TOPIC
)
244 m_connection
= new MyConnection();
245 wxGetApp().GetFrame()->UpdateUI();
246 wxLogMessage("Connection accepted");
249 //else: unknown topic
251 wxLogMessage("Unknown topic, connection refused");
255 void MyServer::Disconnect()
259 wxDELETE(m_connection
);
260 wxGetApp().GetFrame()->UpdateUI();
261 wxLogMessage("Disconnected client");
265 void MyServer::Advise()
269 const wxDateTime now
= wxDateTime::Now();
271 m_connection
->Advise(m_connection
->m_advise
, now
.Format());
273 const wxString s
= now
.FormatTime() + " " + now
.FormatDate();
274 m_connection
->Advise(m_connection
->m_advise
, s
.mb_str(), wxNO_LEN
);
276 #if wxUSE_DDE_FOR_IPC
277 wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
278 "The client will receive it as wxIPC_TEXT, "
279 " and receive the correct no of bytes, "
280 "but not print a correct log entry.");
282 char bytes
[3] = { '1', '2', '3' };
283 m_connection
->Advise(m_connection
->m_advise
, bytes
, 3, wxIPC_PRIVATE
);
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
292 MyConnection::OnExecute(const wxString
& topic
,
297 Log("OnExecute", topic
, "", data
, size
, format
);
302 MyConnection::OnPoke(const wxString
& topic
,
303 const wxString
& item
,
308 Log("OnPoke", topic
, item
, data
, size
, format
);
309 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
313 MyConnection::OnRequest(const wxString
& topic
,
314 const wxString
& item
,
322 if ( item
.StartsWith("Date", &afterDate
) )
324 const wxDateTime now
= wxDateTime::Now();
326 if ( afterDate
.empty() )
331 else if ( afterDate
== "+len" )
333 s
= now
.FormatTime() + " " + now
.FormatDate();
334 *size
= strlen(s
.mb_str()) + 1;
337 else if ( item
== "bytes[3]" )
345 wxLogMessage("Unknown request for \"%s\"", item
);
349 // store the data pointer to which we return in a member variable to ensure
350 // that the pointer remains valid even after we return
351 m_requestData
= s
.mb_str();
352 const void * const data
= m_requestData
;
353 Log("OnRequest", topic
, item
, data
, *size
, format
);
357 bool MyConnection::OnStartAdvise(const wxString
& topic
, const wxString
& item
)
359 wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic
, item
);
360 wxLogMessage("Returning true");
362 wxGetApp().GetFrame()->UpdateUI();
366 bool MyConnection::OnStopAdvise(const wxString
& topic
, const wxString
& item
)
368 wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic
, item
);
369 wxLogMessage("Returning true");
371 wxGetApp().GetFrame()->UpdateUI();
376 MyConnection::DoAdvise(const wxString
& item
,
381 Log("Advise", "", item
, data
, size
, format
);
382 return wxConnection::DoAdvise(item
, data
, size
, format
);
385 bool MyConnection::OnDisconnect()
387 wxLogMessage("OnDisconnect()");
388 wxGetApp().GetFrame()->Disconnect();