]>
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.
36 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
37 #include "mondrian.xpm"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
49 EVT_MENU(CLIENT_QUIT
, MyFrame::OnExit
)
50 EVT_MENU(CLIENT_EXECUTE
, MyFrame::OnExecute
)
51 EVT_MENU(CLIENT_POKE
, MyFrame::OnPoke
)
52 EVT_MENU(CLIENT_REQUEST
, MyFrame::OnRequest
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 char ipc_buffer
[4000];
60 wxListBox
*the_list
= NULL
;
62 MyConnection
*the_connection
= NULL
;
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // The `main program' equivalent, creating the windows and returning the
77 // service name (DDE classes) or port number (TCP/IP based classes)
78 wxString service
= IPC_SERVICE
;
80 // ignored under DDE, host name in TCP/IP based classes
81 wxString hostName
= "localhost";
88 // Create a new client
89 my_client
= new MyClient
;
91 // suppress the log messages from MakeConnection()
94 the_connection
= (MyConnection
*)
95 my_client
->MakeConnection(hostName
, service
, IPC_TOPIC
);
97 while ( !the_connection
)
99 if ( wxMessageBox("Failed to make connection to server.\nRetry?",
101 wxICON_ERROR
| wxYES_NO
| wxCANCEL
) != wxYES
)
107 the_connection
= (MyConnection
*)my_client
->MakeConnection(hostName
, service
, "IPC TEST");
111 if (!the_connection
->StartAdvise(IPC_ADVISE_NAME
))
112 wxMessageBox("StartAdvise failed", "Client Demo Error");
114 // Create the main frame window
115 (new MyFrame(NULL
, "Client"))->Show(TRUE
);
124 the_connection
->Disconnect();
125 delete the_connection
;
126 the_connection
= NULL
;
129 // will delete the connection too
130 // Update: Seems it didn't delete the_connection, because there's a leak.
131 // Deletion is now explicitly done a few lines up.
139 // Define my frame constructor
140 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
141 : wxFrame(frame
, -1, title
, wxDefaultPosition
, wxSize(300, 200))
144 SetIcon(wxICON(mondrian
));
147 wxMenu
*file_menu
= new wxMenu
;
149 file_menu
->Append(CLIENT_EXECUTE
, "&Execute\tCtrl-E");
150 file_menu
->Append(CLIENT_REQUEST
, "&Request\tCtrl-R");
151 file_menu
->Append(CLIENT_POKE
, "&Poke\tCtrl-P");
152 file_menu
->Append(CLIENT_QUIT
, "&Quit\tCtrl-Q");
154 wxMenuBar
*menu_bar
= new wxMenuBar
;
156 menu_bar
->Append(file_menu
, "&File");
158 // Associate the menu bar with the frame
159 SetMenuBar(menu_bar
);
161 // Make a listbox which shows the choices made in the server
162 the_list
= new wxListBox(this, CLIENT_LISTBOX
, wxPoint(5, 5));
163 the_list
->Append("Apple");
164 the_list
->Append("Pear");
165 the_list
->Append("Orange");
166 the_list
->Append("Banana");
167 the_list
->Append("Fruit");
170 void MyFrame::OnExecute(wxCommandEvent
& event
)
173 if (!the_connection
->Execute("Hello from the client!"))
174 wxMessageBox("Execute failed", "Client Demo Error");
177 void MyFrame::OnPoke(wxCommandEvent
& event
)
180 if (!the_connection
->Poke("An item", "Some data to poke at the server!"))
181 wxMessageBox("Poke failed", "Client Demo Error");
184 void MyFrame::OnRequest(wxCommandEvent
& event
)
188 char *data
= the_connection
->Request("An item");
190 wxMessageBox(data
, "Client: Request", wxOK
);
192 wxMessageBox("Request failed", "Client Demo Error");
196 void MyFrame::OnExit(wxCommandEvent
& event
)
201 wxConnectionBase
*MyClient::OnMakeConnection()
203 return new MyConnection
;
206 MyConnection::MyConnection()
207 : wxConnection(ipc_buffer
, WXSIZEOF(ipc_buffer
))
211 bool MyConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, char *data
, int size
, wxIPCFormat format
)
215 int n
= the_list
->FindString(data
);
217 the_list
->SetSelection(n
);
222 bool MyConnection::OnDisconnect()
224 wxWindow
*win
= wxTheApp
->GetTopWindow();