]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/client.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DDE sample: client
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 #include "wx/datetime.h"
37 #ifndef wxHAS_IMAGES_IN_RESOURCES
38 #include "../sample.xpm"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
48 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
49 EVT_CLOSE( MyFrame::OnClose
)
50 EVT_BUTTON( ID_START
, MyFrame::OnStart
)
51 EVT_CHOICE( ID_SERVERNAME
, MyFrame::OnServername
)
52 EVT_CHOICE( ID_HOSTNAME
, MyFrame::OnHostname
)
53 EVT_CHOICE( ID_TOPIC
, MyFrame::OnTopic
)
54 EVT_BUTTON( ID_DISCONNECT
, MyFrame::OnDisconnect
)
55 EVT_BUTTON( ID_STARTADVISE
, MyFrame::OnStartAdvise
)
56 EVT_BUTTON( ID_STOPADVISE
, MyFrame::OnStopAdvise
)
57 EVT_BUTTON( ID_POKE
, MyFrame::OnPoke
)
58 EVT_BUTTON( ID_EXECUTE
, MyFrame::OnExecute
)
59 EVT_BUTTON( ID_REQUEST
, MyFrame::OnRequest
)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // The `main program' equivalent, creating the windows and returning the
78 if ( !wxApp::OnInit() )
81 // Create the main frame window
82 m_frame
= new MyFrame(NULL
, wxT("Client"));
94 // Define my frame constructor
95 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
96 : wxFrame(frame
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(400, 300))
99 SetIcon(wxICON(sample
));
102 wxMenu
*file_menu
= new wxMenu
;
104 file_menu
->Append(wxID_EXIT
, wxT("&Quit\tCtrl-Q"));
106 wxMenuBar
*menu_bar
= new wxMenuBar
;
108 menu_bar
->Append(file_menu
, wxT("&File"));
110 // Associate the menu bar with the frame
111 SetMenuBar(menu_bar
);
113 // set a dialog background
114 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
116 // add the controls to the frame
119 IPC_SERVICE
, wxT("...")
127 IPC_TOPIC
, wxT("...")
130 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
132 wxBoxSizer
*item1
= new wxBoxSizer( wxHORIZONTAL
);
134 wxGridSizer
*item2
= new wxGridSizer( 4, 0, 0 );
136 wxButton
*item3
= new wxButton( this, ID_START
, wxT("Connect to server"), wxDefaultPosition
, wxDefaultSize
, 0 );
137 item2
->Add( item3
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
139 wxChoice
*item5
= new wxChoice( this, ID_HOSTNAME
, wxDefaultPosition
, wxSize(100,-1), 2, strs5
, 0 );
140 item2
->Add( item5
, 0, wxALIGN_CENTER
|wxALL
, 5 );
142 wxChoice
*item4
= new wxChoice( this, ID_SERVERNAME
, wxDefaultPosition
, wxSize(100,-1), 2, strs4
, 0 );
143 item2
->Add( item4
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
145 wxChoice
*item6
= new wxChoice( this, ID_TOPIC
, wxDefaultPosition
, wxSize(100,-1), 2, strs6
, 0 );
146 item2
->Add( item6
, 0, wxALIGN_CENTER
|wxALL
, 5 );
148 wxButton
*item7
= new wxButton( this, ID_DISCONNECT
, wxT("Disconnect "), wxDefaultPosition
, wxDefaultSize
, 0 );
149 item2
->Add( item7
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
151 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
153 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
155 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
157 wxButton
*item8
= new wxButton( this, ID_STARTADVISE
, wxT("StartAdvise"), wxDefaultPosition
, wxDefaultSize
, 0 );
158 item2
->Add( item8
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
160 wxButton
*item9
= new wxButton( this, ID_STOPADVISE
, wxT("StopAdvise"), wxDefaultPosition
, wxDefaultSize
, 0 );
161 item2
->Add( item9
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
163 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
165 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
167 wxButton
*item10
= new wxButton( this, ID_EXECUTE
, wxT("Execute"), wxDefaultPosition
, wxDefaultSize
, 0 );
168 item2
->Add( item10
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
170 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
172 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
174 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
176 wxButton
*item11
= new wxButton( this, ID_POKE
, wxT("Poke"), wxDefaultPosition
, wxDefaultSize
, 0 );
177 item2
->Add( item11
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
179 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
181 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
183 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
185 wxButton
*item12
= new wxButton( this, ID_REQUEST
, wxT("Request"), wxDefaultPosition
, wxDefaultSize
, 0 );
186 item2
->Add( item12
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
188 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
190 item1
->Add( item2
, 1, wxALIGN_CENTER
|wxALL
, 5 );
192 item0
->Add( item1
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
194 wxStaticBox
*item14
= new wxStaticBox( this, -1, wxT("Client log") );
195 wxStaticBoxSizer
*item13
= new wxStaticBoxSizer( item14
, wxVERTICAL
);
197 wxTextCtrl
*item15
= new wxTextCtrl( this, ID_LOG
, wxEmptyString
, wxDefaultPosition
, wxSize(500,140), wxTE_MULTILINE
);
198 item13
->Add( item15
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
200 item0
->Add( item13
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
202 this->SetSizer( item0
);
203 item0
->SetSizeHints( this );
207 GetServername()->SetSelection(0);
208 GetHostname()->SetSelection(0);
209 GetTopic()->SetSelection(0);
210 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(GetLog());
211 delete wxLog::SetActiveTarget(logWindow
);
212 wxLogMessage(wxT("Click on Connect to connect to the server"));
216 void MyFrame::EnableControls()
218 GetStart()->Enable(m_client
== NULL
);
219 GetServername()->Enable(m_client
== NULL
);
220 GetHostname()->Enable(m_client
== NULL
);
221 GetTopic()->Enable(m_client
== NULL
);
223 const bool isConnected
= (m_client
!= NULL
&& m_client
->IsConnected());
224 GetDisconnect()->Enable(m_client
!= NULL
&& isConnected
);
225 GetStartAdvise()->Enable(m_client
!= NULL
&& isConnected
);
226 GetStopAdvise()->Enable(m_client
!= NULL
&& isConnected
);
227 GetExecute()->Enable(m_client
!= NULL
&& isConnected
);
228 GetPoke()->Enable(m_client
!= NULL
&& isConnected
);
229 GetRequest()->Enable(m_client
!= NULL
&& isConnected
);
232 void MyFrame::OnClose(wxCloseEvent
& event
)
239 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
244 void MyFrame::OnStart(wxCommandEvent
& WXUNUSED(event
))
246 // Connect to the client
247 wxString servername
= GetServername()->GetStringSelection();
248 wxString hostname
= GetHostname()->GetStringSelection();
249 wxString topic
= GetTopic()->GetStringSelection();
251 m_client
= new MyClient
;
252 bool retval
= m_client
->Connect(hostname
, servername
, topic
);
254 wxLogMessage(wxT("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
255 hostname
.c_str(), servername
.c_str(), topic
.c_str(),
256 retval
? wxT("connected") : wxT("failed to connect"));
265 void MyFrame::OnServername( wxCommandEvent
& WXUNUSED(event
) )
267 if (GetServername()->GetStringSelection() == wxT("..."))
269 wxString s
= wxGetTextFromUser(wxT("Specify the name of the server"),
270 wxT("Server Name"), wxEmptyString
, this);
271 if (!s
.IsEmpty() && s
!= IPC_SERVICE
)
273 GetServername()->Insert(s
, 0);
274 GetServername()->SetSelection(0);
279 void MyFrame::OnHostname( wxCommandEvent
& WXUNUSED(event
) )
281 if (GetHostname()->GetStringSelection() == wxT("..."))
283 wxString s
= wxGetTextFromUser(wxT("Specify the name of the host (ignored under DDE)"),
284 wxT("Host Name"), wxEmptyString
, this);
285 if (!s
.IsEmpty() && s
!= IPC_HOST
)
287 GetHostname()->Insert(s
, 0);
288 GetHostname()->SetSelection(0);
293 void MyFrame::OnTopic( wxCommandEvent
& WXUNUSED(event
) )
295 if (GetTopic()->GetStringSelection() == wxT("..."))
297 wxString s
= wxGetTextFromUser(wxT("Specify the name of the topic"),
298 wxT("Topic Name"), wxEmptyString
, this);
299 if (!s
.IsEmpty() && s
!= IPC_TOPIC
)
301 GetTopic()->Insert(s
, 0);
302 GetTopic()->SetSelection(0);
307 void MyFrame::OnDisconnect(wxCommandEvent
& WXUNUSED(event
))
312 void MyFrame::Disconnect()
318 void MyFrame::OnStartAdvise(wxCommandEvent
& WXUNUSED(event
))
320 m_client
->GetConnection()->StartAdvise(wxT("something"));
323 void MyFrame::OnStopAdvise(wxCommandEvent
& WXUNUSED(event
))
325 m_client
->GetConnection()->StopAdvise(wxT("something"));
328 void MyFrame::OnExecute(wxCommandEvent
& WXUNUSED(event
))
330 if (m_client
->IsConnected())
332 wxString s
= wxT("Date");
334 m_client
->GetConnection()->Execute(s
);
335 m_client
->GetConnection()->Execute((const char *)s
.c_str(), s
.length() + 1);
340 m_client
->GetConnection()->Execute(bytes
, WXSIZEOF(bytes
));
344 void MyFrame::OnPoke(wxCommandEvent
& WXUNUSED(event
))
346 if (m_client
->IsConnected())
348 wxString s
= wxDateTime::Now().Format();
349 m_client
->GetConnection()->Poke(wxT("Date"), s
);
350 s
= wxDateTime::Now().FormatTime() + wxT(" ") + wxDateTime::Now().FormatDate();
351 m_client
->GetConnection()->Poke(wxT("Date"), (const char *)s
.c_str(), s
.length() + 1);
353 bytes
[0] = '1'; bytes
[1] = '2'; bytes
[2] = '3';
354 m_client
->GetConnection()->Poke(wxT("bytes[3]"), bytes
, 3, wxIPC_PRIVATE
);
358 void MyFrame::OnRequest(wxCommandEvent
& WXUNUSED(event
))
360 if (m_client
->IsConnected())
363 m_client
->GetConnection()->Request(wxT("Date"));
364 m_client
->GetConnection()->Request(wxT("Date+len"), &size
);
365 m_client
->GetConnection()->Request(wxT("bytes[3]"), &size
, wxIPC_PRIVATE
);
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
372 MyClient::MyClient() : wxClient()
377 bool MyClient::Connect(const wxString
& sHost
, const wxString
& sService
, const wxString
& sTopic
)
379 // suppress the log messages from MakeConnection()
382 m_connection
= (MyConnection
*)MakeConnection(sHost
, sService
, sTopic
);
383 return m_connection
!= NULL
;
386 wxConnectionBase
*MyClient::OnMakeConnection()
388 return new MyConnection
;
391 void MyClient::Disconnect()
395 m_connection
->Disconnect();
396 wxDELETE(m_connection
);
397 wxGetApp().GetFrame()->EnableControls();
398 wxLogMessage(wxT("Client disconnected from server"));
402 MyClient::~MyClient()
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
411 bool MyConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, const void *data
,
412 size_t size
, wxIPCFormat format
)
414 Log(wxT("OnAdvise"), topic
, item
, data
, size
, format
);
418 bool MyConnection::OnDisconnect()
420 wxLogMessage(wxT("OnDisconnect()"));
421 wxGetApp().GetFrame()->Disconnect();
425 bool MyConnection::DoExecute(const void *data
, size_t size
, wxIPCFormat format
)
427 Log(wxT("Execute"), wxEmptyString
, wxEmptyString
, data
, size
, format
);
428 bool retval
= wxConnection::DoExecute(data
, size
, format
);
431 wxLogMessage(wxT("Execute failed!"));
436 const void *MyConnection::Request(const wxString
& item
, size_t *size
, wxIPCFormat format
)
438 const void *data
= wxConnection::Request(item
, size
, format
);
439 Log(wxT("Request"), wxEmptyString
, item
, data
, size
? *size
: wxNO_LEN
, format
);
443 bool MyConnection::DoPoke(const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
445 Log(wxT("Poke"), wxEmptyString
, item
, data
, size
, format
);
446 return wxConnection::DoPoke(item
, data
, size
, format
);