Socket sample:
[wxWidgets.git] / samples / ipc / client.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: client.cpp
3 // Purpose: DDE sample: client
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 25/01/99
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
33
34 #include "ddesetup.h"
35
36 #if defined(__WXGTK__) || defined(__WXMOTIF__)
37 #include "mondrian.xpm"
38 #endif
39
40 #include "client.h"
41
42 // ----------------------------------------------------------------------------
43 // wxWin macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_APP(MyApp)
47
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)
53 END_EVENT_TABLE()
54
55 // ----------------------------------------------------------------------------
56 // globals
57 // ----------------------------------------------------------------------------
58
59 char ipc_buffer[4000];
60 wxListBox *the_list = NULL;
61
62 MyConnection *the_connection = NULL;
63 MyClient *my_client ;
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // MyApp
71 // ----------------------------------------------------------------------------
72
73 // The `main program' equivalent, creating the windows and returning the
74 // main frame
75 bool MyApp::OnInit()
76 {
77 wxString server = "4242";
78 wxString hostName = wxGetHostName();
79
80 if (argc > 1)
81 server = argv[1];
82 if (argc > 2)
83 hostName = argv[2];
84
85 // Create a new client
86 my_client = new MyClient;
87 the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST");
88
89 while ( !the_connection )
90 {
91 if ( wxMessageBox("Failed to make connection to server.\nRetry?",
92 "Client Demo Error",
93 wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
94 {
95 // no server
96 return FALSE;
97 }
98
99 the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST");
100 }
101
102 if (!the_connection->StartAdvise("Item"))
103 wxMessageBox("StartAdvise failed", "Client Demo Error");
104
105 // Create the main frame window
106 (new MyFrame(NULL, "Client"))->Show(TRUE);
107
108 return TRUE;
109 }
110
111 int MyApp::OnExit()
112 {
113 if (the_connection)
114 {
115 the_connection->Disconnect();
116 }
117
118 // will delete the connection too
119 delete my_client;
120
121 return 0;
122 }
123
124 // Define my frame constructor
125 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
126 : wxFrame(frame, -1, title)
127 {
128 panel = NULL;
129
130 // Give it an icon
131 SetIcon(wxICON(mondrian));
132
133 // Make a menubar
134 wxMenu *file_menu = new wxMenu;
135
136 file_menu->Append(CLIENT_EXECUTE, "Execute");
137 file_menu->Append(CLIENT_REQUEST, "Request");
138 file_menu->Append(CLIENT_POKE, "Poke");
139 file_menu->Append(CLIENT_QUIT, "Quit");
140
141 wxMenuBar *menu_bar = new wxMenuBar;
142
143 menu_bar->Append(file_menu, "File");
144
145 // Associate the menu bar with the frame
146 SetMenuBar(menu_bar);
147
148 // Make a panel
149 panel = new wxPanel(this);
150 the_list = new wxListBox(panel, CLIENT_LISTBOX, wxPoint(5, 5));
151 the_list->Append("Apple");
152 the_list->Append("Pear");
153 the_list->Append("Orange");
154 the_list->Append("Banana");
155 the_list->Append("Fruit");
156
157 panel->Fit();
158 Fit();
159 }
160
161 void MyFrame::OnExecute(wxCommandEvent& event)
162 {
163 if (the_connection)
164 if (!the_connection->Execute("Hello from the client!"))
165 wxMessageBox("Execute failed", "Client Demo Error");
166 }
167
168 void MyFrame::OnPoke(wxCommandEvent& event)
169 {
170 if (the_connection)
171 if (!the_connection->Poke("An item", "Some data to poke at the server!"))
172 wxMessageBox("Poke failed", "Client Demo Error");
173 }
174
175 void MyFrame::OnRequest(wxCommandEvent& event)
176 {
177 if (the_connection)
178 {
179 char *data = the_connection->Request("An item");
180 if (data)
181 wxMessageBox(data, "Client: Request", wxOK);
182 else
183 wxMessageBox("Request failed", "Client Demo Error");
184 }
185 }
186
187 void MyFrame::OnExit(wxCommandEvent& event)
188 {
189 Close();
190 }
191
192 wxConnectionBase *MyClient::OnMakeConnection()
193 {
194 return new MyConnection;
195 }
196
197 MyConnection::MyConnection()
198 : wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer))
199 {
200 }
201
202 MyConnection::~MyConnection()
203 {
204 the_connection = NULL;
205 }
206
207 bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
208 {
209 if (the_list)
210 {
211 int n = the_list->FindString(data);
212 if (n > -1)
213 the_list->SetSelection(n);
214 }
215 return TRUE;
216 }
217
218 bool MyConnection::OnDisconnect()
219 {
220 wxWindow *win = wxTheApp->GetTopWindow();
221 if ( win )
222 win->Destroy();
223
224 return TRUE;
225 }
226