]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/client.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DDE sample: client
4 // Author: Julian Smart
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"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
48 EVT_MENU(CLIENT_QUIT
, MyFrame::OnExit
)
49 EVT_MENU(CLIENT_EXECUTE
, MyFrame::OnExecute
)
50 EVT_MENU(CLIENT_POKE
, MyFrame::OnPoke
)
51 EVT_MENU(CLIENT_REQUEST
, MyFrame::OnRequest
)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxListBox
*the_list
= NULL
;
60 MyConnection
*the_connection
= NULL
;
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // The `main program' equivalent, creating the windows and returning the
75 // service name (DDE classes) or port number (TCP/IP based classes)
76 wxString service
= IPC_SERVICE
;
78 // ignored under DDE, host name in TCP/IP based classes
79 wxString hostName
= _T("localhost");
86 // Create a new client
87 my_client
= new MyClient
;
89 // suppress the log messages from MakeConnection()
92 the_connection
= (MyConnection
*)
93 my_client
->MakeConnection(hostName
, service
, IPC_TOPIC
);
95 while ( !the_connection
)
97 if ( wxMessageBox(_T("Failed to make connection to server.\nRetry?"),
98 _T("Client Demo Error"),
99 wxICON_ERROR
| wxYES_NO
| wxCANCEL
) != wxYES
)
105 the_connection
= (MyConnection
*)my_client
->MakeConnection(hostName
, service
, _T("IPC TEST"));
109 if (!the_connection
->StartAdvise(IPC_ADVISE_NAME
))
110 wxMessageBox(_T("StartAdvise failed"), _T("Client Demo Error"));
112 // Create the main frame window
113 (new MyFrame(NULL
, _T("Client")))->Show(TRUE
);
120 // will delete the connection too
121 // Update: Seems it didn't delete the_connection, because there's a leak.
122 // Deletion is now explicitly done a few lines up.
123 // another Update: in fact it's because OnDisconnect should delete it, but
132 // Define my frame constructor
133 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
134 : wxFrame(frame
, -1, title
, wxDefaultPosition
, wxSize(300, 200))
137 SetIcon(wxICON(mondrian
));
140 wxMenu
*file_menu
= new wxMenu
;
142 file_menu
->Append(CLIENT_EXECUTE
, _T("&Execute\tCtrl-E"));
143 file_menu
->Append(CLIENT_REQUEST
, _T("&Request\tCtrl-R"));
144 file_menu
->Append(CLIENT_POKE
, _T("&Poke\tCtrl-P"));
145 file_menu
->Append(CLIENT_QUIT
, _T("&Quit\tCtrl-Q"));
147 wxMenuBar
*menu_bar
= new wxMenuBar
;
149 menu_bar
->Append(file_menu
, _T("&File"));
151 // Associate the menu bar with the frame
152 SetMenuBar(menu_bar
);
154 // Make a listbox which shows the choices made in the server
155 the_list
= new wxListBox(this, CLIENT_LISTBOX
, wxPoint(5, 5));
156 the_list
->Append(_T("Apple"));
157 the_list
->Append(_T("Pear"));
158 the_list
->Append(_T("Orange"));
159 the_list
->Append(_T("Banana"));
160 the_list
->Append(_T("Fruit"));
163 void MyFrame::OnExecute(wxCommandEvent
& event
)
166 if (!the_connection
->Execute(_T("Hello from the client!")))
167 wxMessageBox(_T("Execute failed"), _T("Client Demo Error"));
170 void MyFrame::OnPoke(wxCommandEvent
& event
)
173 if (!the_connection
->Poke(_T("An item"), _T("Some data to poke at the server!")))
174 wxMessageBox(_T("Poke failed"), _T("Client Demo Error"));
177 void MyFrame::OnRequest(wxCommandEvent
& event
)
181 wxChar
*data
= the_connection
->Request(_T("An item"));
183 wxMessageBox(data
, _T("Client: Request"), wxOK
);
185 wxMessageBox(_T("Request failed"), _T("Client Demo Error"));
189 void MyFrame::OnExit(wxCommandEvent
& event
)
194 wxConnectionBase
*MyClient::OnMakeConnection()
196 return new MyConnection
;
199 bool MyConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
203 int n
= the_list
->FindString(data
);
205 the_list
->SetSelection(n
);
210 bool MyConnection::OnDisconnect()
212 // when connection is terminated, quit whole program
213 wxWindow
*win
= wxTheApp
->GetTopWindow();
218 the_connection
= NULL
;
219 return wxConnection::OnDisconnect();