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(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
36 #include "mondrian.xpm"
40 #include "wx/textdlg.h"
41 #include "wx/datetime.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
49 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
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
)
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
69 if ( !wxApp::OnInit() )
72 // Create the main frame window
73 m_frame
= new MyFrame(NULL
, _T("Server"));
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // Define my frame constructor
89 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
90 : wxFrame(frame
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(400, 300))
94 #endif // wxUSE_STATUSBAR
97 SetIcon(wxICON(mondrian
));
100 wxMenu
*file_menu
= new wxMenu
;
102 file_menu
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
104 wxMenuBar
*menu_bar
= new wxMenuBar
;
106 menu_bar
->Append(file_menu
, _T("&File"));
108 // Associate the menu bar with the frame
109 SetMenuBar(menu_bar
);
111 // set a dialog background
112 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
114 // add the controls to the frame
115 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
117 wxBoxSizer
*item1
= new wxBoxSizer( wxHORIZONTAL
);
119 wxFlexGridSizer
*item2
= new wxFlexGridSizer( 2, 0, 0 );
120 item2
->AddGrowableCol( 1 );
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 );
127 IPC_SERVICE
, _T("...")
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 );
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 );
135 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
137 wxButton
*item6
= new wxButton( this, ID_ADVISE
, wxT("Advise"), wxDefaultPosition
, wxDefaultSize
, 0 );
138 item2
->Add( item6
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
140 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
142 item1
->Add( item2
, 1, wxALIGN_CENTER
|wxALL
, 5 );
144 item0
->Add( item1
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
146 wxStaticBox
*item8
= new wxStaticBox( this, -1, wxT("Server log") );
147 wxStaticBoxSizer
*item7
= new wxStaticBoxSizer( item8
, wxVERTICAL
);
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 );
152 item0
->Add( item7
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
155 item0
->SetSizeHints( this );
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"));
166 void MyFrame::Enable()
168 GetStart()->Enable(m_server
== NULL
);
169 GetServername()->Enable(m_server
== NULL
);
170 GetAdvise()->Enable(m_server
&& m_server
->CanAdvise());
171 GetDisconnect()->Enable(m_server
&& m_server
->IsConnected());
174 void MyFrame::OnClose(wxCloseEvent
& event
)
184 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
189 void MyFrame::OnStart(wxCommandEvent
& WXUNUSED(event
))
191 // Create a new server
192 m_server
= new MyServer
;
193 wxString servername
= GetServername()->GetStringSelection();
194 if (m_server
->Create(servername
))
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
205 wxLogMessage(_T("Server %s failed to start"), servername
.c_str());
212 void MyFrame::OnServerName( wxCommandEvent
& WXUNUSED(event
) )
214 if (GetServername()->GetStringSelection() == _T("..."))
216 wxString s
= wxGetTextFromUser(_T("Specify the name of the server"),
217 _T("Server Name"), _(""), this);
218 if (!s
.IsEmpty() && s
!= IPC_SERVICE
)
220 GetServername()->Insert(s
, 0);
221 GetServername()->SetSelection(0);
226 void MyFrame::Disconnect()
228 m_server
->Disconnect();
232 void MyFrame::OnDisconnect(wxCommandEvent
& WXUNUSED(event
))
237 void MyFrame::OnAdvise(wxCommandEvent
& WXUNUSED(event
))
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 MyServer::MyServer() : wxServer()
251 MyServer::~MyServer()
256 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
258 wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic
.c_str());
260 if ( topic
== IPC_TOPIC
)
262 m_connection
= new MyConnection();
263 wxGetApp().GetFrame()->Enable();
264 wxLogMessage(_T("Connection accepted"));
271 void MyServer::Disconnect()
275 m_connection
->Disconnect();
278 wxGetApp().GetFrame()->Enable();
279 wxLogMessage(_T("Disconnected client"));
283 void MyServer::Advise()
287 wxString s
= wxDateTime::Now().Format();
288 m_connection
->Advise(m_connection
->m_sAdvise
, s
);
289 s
= wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
290 m_connection
->Advise(m_connection
->m_sAdvise
, (const char *)s
.c_str(), s
.Length() + 1);
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."));
296 bytes
[0] = '1'; bytes
[1] = '2'; bytes
[2] = '3';
297 m_connection
->Advise(m_connection
->m_sAdvise
, bytes
, 3, wxIPC_PRIVATE
);
298 // this works, but the log treats it as a string now
299 // m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 bool MyConnection::OnExecute(const wxString
& topic
,
308 const void *data
, size_t size
, wxIPCFormat format
)
310 Log(_T("OnExecute"), topic
, _T(""), data
, size
, format
);
314 bool MyConnection::OnPoke(const wxString
& topic
,
315 const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
317 Log(_T("OnPoke"), topic
, item
, data
, size
, format
);
318 return wxConnection::OnPoke(topic
, item
, data
, size
, format
);
321 const void *MyConnection::OnRequest(const wxString
& topic
,
322 const wxString
& item
, size_t *size
, wxIPCFormat format
)
325 if (item
== _T("Date"))
327 m_sRequestDate
= wxDateTime::Now().Format();
328 data
= m_sRequestDate
.c_str();
331 else if (item
== _T("Date+len"))
333 m_sRequestDate
= wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
334 data
= m_sRequestDate
.c_str();
335 *size
= m_sRequestDate
.Length() + 1;
337 else if (item
== _T("bytes[3]"))
339 data
= m_achRequestBytes
;
340 m_achRequestBytes
[0] = '1'; m_achRequestBytes
[1] = '2'; m_achRequestBytes
[2] = '3';
348 Log(_T("OnRequest"), topic
, item
, data
, *size
, format
);
352 bool MyConnection::OnStartAdvise(const wxString
& topic
,
353 const wxString
& item
)
355 wxLogMessage(_T("OnStartAdvise(\"%s\",\"%s\")"), topic
.c_str(), item
.c_str());
356 wxLogMessage(_T("Returning true"));
358 wxGetApp().GetFrame()->Enable();
362 bool MyConnection::OnStopAdvise(const wxString
& topic
,
363 const wxString
& item
)
365 wxLogMessage(_T("OnStopAdvise(\"%s\",\"%s\")"), topic
.c_str(), item
.c_str());
366 wxLogMessage(_T("Returning true"));
368 wxGetApp().GetFrame()->Enable();
372 bool MyConnection::DoAdvise(const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
374 Log(_T("Advise"), _T(""), item
, data
, size
, format
);
375 return wxConnection::DoAdvise(item
, data
, size
, format
);
378 bool MyConnection::OnDisconnect()
380 wxLogMessage(_T("OnDisconnect()"));
381 wxGetApp().GetFrame()->Disconnect();