]>
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
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 #include "wx/datetime.h"
38 #if !defined(__WXMSW__) && !defined(__WXPM__)
39 #include "../sample.xpm"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
49 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
50 EVT_CLOSE( MyFrame::OnClose
)
51 EVT_BUTTON( ID_START
, MyFrame::OnStart
)
52 EVT_CHOICE( ID_SERVERNAME
, MyFrame::OnServername
)
53 EVT_CHOICE( ID_HOSTNAME
, MyFrame::OnHostname
)
54 EVT_CHOICE( ID_TOPIC
, MyFrame::OnTopic
)
55 EVT_BUTTON( ID_DISCONNECT
, MyFrame::OnDisconnect
)
56 EVT_BUTTON( ID_STARTADVISE
, MyFrame::OnStartAdvise
)
57 EVT_BUTTON( ID_STOPADVISE
, MyFrame::OnStopAdvise
)
58 EVT_BUTTON( ID_POKE
, MyFrame::OnPoke
)
59 EVT_BUTTON( ID_EXECUTE
, MyFrame::OnExecute
)
60 EVT_BUTTON( ID_REQUEST
, MyFrame::OnRequest
)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // The `main program' equivalent, creating the windows and returning the
79 if ( !wxApp::OnInit() )
82 // Create the main frame window
83 m_frame
= new MyFrame(NULL
, wxT("Client"));
95 // Define my frame constructor
96 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
97 : wxFrame(frame
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(400, 300))
100 SetIcon(wxICON(sample
));
103 wxMenu
*file_menu
= new wxMenu
;
105 file_menu
->Append(wxID_EXIT
, wxT("&Quit\tCtrl-Q"));
107 wxMenuBar
*menu_bar
= new wxMenuBar
;
109 menu_bar
->Append(file_menu
, wxT("&File"));
111 // Associate the menu bar with the frame
112 SetMenuBar(menu_bar
);
114 // set a dialog background
115 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
117 // add the controls to the frame
120 IPC_SERVICE
, wxT("...")
128 IPC_TOPIC
, wxT("...")
131 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
133 wxBoxSizer
*item1
= new wxBoxSizer( wxHORIZONTAL
);
135 wxGridSizer
*item2
= new wxGridSizer( 4, 0, 0 );
137 wxButton
*item3
= new wxButton( this, ID_START
, wxT("Connect to server"), wxDefaultPosition
, wxDefaultSize
, 0 );
138 item2
->Add( item3
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
140 wxChoice
*item5
= new wxChoice( this, ID_HOSTNAME
, wxDefaultPosition
, wxSize(100,-1), 2, strs5
, 0 );
141 item2
->Add( item5
, 0, wxALIGN_CENTER
|wxALL
, 5 );
143 wxChoice
*item4
= new wxChoice( this, ID_SERVERNAME
, wxDefaultPosition
, wxSize(100,-1), 2, strs4
, 0 );
144 item2
->Add( item4
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
146 wxChoice
*item6
= new wxChoice( this, ID_TOPIC
, wxDefaultPosition
, wxSize(100,-1), 2, strs6
, 0 );
147 item2
->Add( item6
, 0, wxALIGN_CENTER
|wxALL
, 5 );
149 wxButton
*item7
= new wxButton( this, ID_DISCONNECT
, wxT("Disconnect "), wxDefaultPosition
, wxDefaultSize
, 0 );
150 item2
->Add( item7
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
152 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
154 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
156 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
158 wxButton
*item8
= new wxButton( this, ID_STARTADVISE
, wxT("StartAdvise"), wxDefaultPosition
, wxDefaultSize
, 0 );
159 item2
->Add( item8
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
161 wxButton
*item9
= new wxButton( this, ID_STOPADVISE
, wxT("StopAdvise"), wxDefaultPosition
, wxDefaultSize
, 0 );
162 item2
->Add( item9
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
164 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
166 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
168 wxButton
*item10
= new wxButton( this, ID_EXECUTE
, wxT("Execute"), wxDefaultPosition
, wxDefaultSize
, 0 );
169 item2
->Add( item10
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
171 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
173 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
175 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
177 wxButton
*item11
= new wxButton( this, ID_POKE
, wxT("Poke"), wxDefaultPosition
, wxDefaultSize
, 0 );
178 item2
->Add( item11
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
180 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
182 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
184 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
186 wxButton
*item12
= new wxButton( this, ID_REQUEST
, wxT("Request"), wxDefaultPosition
, wxDefaultSize
, 0 );
187 item2
->Add( item12
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
189 item2
->Add( 20, 20, 0, wxALIGN_CENTER
|wxALL
, 5 );
191 item1
->Add( item2
, 1, wxALIGN_CENTER
|wxALL
, 5 );
193 item0
->Add( item1
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
195 wxStaticBox
*item14
= new wxStaticBox( this, -1, wxT("Client log") );
196 wxStaticBoxSizer
*item13
= new wxStaticBoxSizer( item14
, wxVERTICAL
);
198 wxTextCtrl
*item15
= new wxTextCtrl( this, ID_LOG
, wxEmptyString
, wxDefaultPosition
, wxSize(500,140), wxTE_MULTILINE
);
199 item13
->Add( item15
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
201 item0
->Add( item13
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
203 this->SetSizer( item0
);
204 item0
->SetSizeHints( this );
208 GetServername()->SetSelection(0);
209 GetHostname()->SetSelection(0);
210 GetTopic()->SetSelection(0);
211 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(GetLog());
212 delete wxLog::SetActiveTarget(logWindow
);
213 wxLogMessage(wxT("Click on Connect to connect to the server"));
217 void MyFrame::EnableControls()
219 GetStart()->Enable(m_client
== NULL
);
220 GetServername()->Enable(m_client
== NULL
);
221 GetHostname()->Enable(m_client
== NULL
);
222 GetTopic()->Enable(m_client
== NULL
);
224 const bool isConnected
= (m_client
!= NULL
&& m_client
->IsConnected());
225 GetDisconnect()->Enable(m_client
!= NULL
&& isConnected
);
226 GetStartAdvise()->Enable(m_client
!= NULL
&& isConnected
);
227 GetStopAdvise()->Enable(m_client
!= NULL
&& isConnected
);
228 GetExecute()->Enable(m_client
!= NULL
&& isConnected
);
229 GetPoke()->Enable(m_client
!= NULL
&& isConnected
);
230 GetRequest()->Enable(m_client
!= NULL
&& isConnected
);
233 void MyFrame::OnClose(wxCloseEvent
& event
)
242 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
247 void MyFrame::OnStart(wxCommandEvent
& WXUNUSED(event
))
249 // Connect to the client
250 wxString servername
= GetServername()->GetStringSelection();
251 wxString hostname
= GetHostname()->GetStringSelection();
252 wxString topic
= GetTopic()->GetStringSelection();
254 m_client
= new MyClient
;
255 bool retval
= m_client
->Connect(hostname
, servername
, topic
);
257 wxLogMessage(wxT("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
258 hostname
.c_str(), servername
.c_str(), topic
.c_str(),
259 retval
? wxT("connected") : wxT("failed to connect"));
268 void MyFrame::OnServername( wxCommandEvent
& WXUNUSED(event
) )
270 if (GetServername()->GetStringSelection() == wxT("..."))
272 wxString s
= wxGetTextFromUser(wxT("Specify the name of the server"),
273 wxT("Server Name"), wxEmptyString
, this);
274 if (!s
.IsEmpty() && s
!= IPC_SERVICE
)
276 GetServername()->Insert(s
, 0);
277 GetServername()->SetSelection(0);
282 void MyFrame::OnHostname( wxCommandEvent
& WXUNUSED(event
) )
284 if (GetHostname()->GetStringSelection() == wxT("..."))
286 wxString s
= wxGetTextFromUser(wxT("Specify the name of the host (ignored under DDE)"),
287 wxT("Host Name"), wxEmptyString
, this);
288 if (!s
.IsEmpty() && s
!= IPC_HOST
)
290 GetHostname()->Insert(s
, 0);
291 GetHostname()->SetSelection(0);
296 void MyFrame::OnTopic( wxCommandEvent
& WXUNUSED(event
) )
298 if (GetTopic()->GetStringSelection() == wxT("..."))
300 wxString s
= wxGetTextFromUser(wxT("Specify the name of the topic"),
301 wxT("Topic Name"), wxEmptyString
, this);
302 if (!s
.IsEmpty() && s
!= IPC_TOPIC
)
304 GetTopic()->Insert(s
, 0);
305 GetTopic()->SetSelection(0);
310 void MyFrame::OnDisconnect(wxCommandEvent
& WXUNUSED(event
))
315 void MyFrame::Disconnect()
321 void MyFrame::OnStartAdvise(wxCommandEvent
& WXUNUSED(event
))
323 m_client
->GetConnection()->StartAdvise(wxT("something"));
326 void MyFrame::OnStopAdvise(wxCommandEvent
& WXUNUSED(event
))
328 m_client
->GetConnection()->StopAdvise(wxT("something"));
331 void MyFrame::OnExecute(wxCommandEvent
& WXUNUSED(event
))
333 if (m_client
->IsConnected())
335 wxString s
= wxT("Date");
337 m_client
->GetConnection()->Execute(s
);
338 m_client
->GetConnection()->Execute((const char *)s
.c_str(), s
.length() + 1);
343 m_client
->GetConnection()->Execute(bytes
, WXSIZEOF(bytes
));
347 void MyFrame::OnPoke(wxCommandEvent
& WXUNUSED(event
))
349 if (m_client
->IsConnected())
351 wxString s
= wxDateTime::Now().Format();
352 m_client
->GetConnection()->Poke(wxT("Date"), s
);
353 s
= wxDateTime::Now().FormatTime() + wxT(" ") + wxDateTime::Now().FormatDate();
354 m_client
->GetConnection()->Poke(wxT("Date"), (const char *)s
.c_str(), s
.length() + 1);
356 bytes
[0] = '1'; bytes
[1] = '2'; bytes
[2] = '3';
357 m_client
->GetConnection()->Poke(wxT("bytes[3]"), bytes
, 3, wxIPC_PRIVATE
);
361 void MyFrame::OnRequest(wxCommandEvent
& WXUNUSED(event
))
363 if (m_client
->IsConnected())
366 m_client
->GetConnection()->Request(wxT("Date"));
367 m_client
->GetConnection()->Request(wxT("Date+len"), &size
);
368 m_client
->GetConnection()->Request(wxT("bytes[3]"), &size
, wxIPC_PRIVATE
);
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
375 MyClient::MyClient() : wxClient()
380 bool MyClient::Connect(const wxString
& sHost
, const wxString
& sService
, const wxString
& sTopic
)
382 // suppress the log messages from MakeConnection()
385 m_connection
= (MyConnection
*)MakeConnection(sHost
, sService
, sTopic
);
386 return m_connection
!= NULL
;
389 wxConnectionBase
*MyClient::OnMakeConnection()
391 return new MyConnection
;
394 void MyClient::Disconnect()
398 m_connection
->Disconnect();
399 wxDELETE(m_connection
);
400 wxGetApp().GetFrame()->EnableControls();
401 wxLogMessage(wxT("Client disconnected from server"));
405 MyClient::~MyClient()
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 bool MyConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, const void *data
,
415 size_t size
, wxIPCFormat format
)
417 Log(wxT("OnAdvise"), topic
, item
, data
, size
, format
);
421 bool MyConnection::OnDisconnect()
423 wxLogMessage(wxT("OnDisconnect()"));
424 wxGetApp().GetFrame()->Disconnect();
428 bool MyConnection::DoExecute(const void *data
, size_t size
, wxIPCFormat format
)
430 Log(wxT("Execute"), wxEmptyString
, wxEmptyString
, data
, size
, format
);
431 bool retval
= wxConnection::DoExecute(data
, size
, format
);
434 wxLogMessage(wxT("Execute failed!"));
439 const void *MyConnection::Request(const wxString
& item
, size_t *size
, wxIPCFormat format
)
441 const void *data
= wxConnection::Request(item
, size
, format
);
442 Log(wxT("Request"), wxEmptyString
, item
, data
, size
? *size
: wxNO_LEN
, format
);
446 bool MyConnection::DoPoke(const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
448 Log(wxT("Poke"), wxEmptyString
, item
, data
, size
, format
);
449 return wxConnection::DoPoke(item
, data
, size
, format
);