]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/baseclient.cpp
calling Notify() from child thread is ok -- as long as you do it from one and the...
[wxWidgets.git] / samples / ipc / baseclient.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/ipc/baseclient.cpp
3 // Purpose: IPC sample: console client
4 // Author: Anders Larsen
5 // Most of the code was stolen from: samples/ipc/client.cpp
6 // (c) Julian Smart, Jurgen Doornik
7 // Created: 2007-11-08
8 // RCS-ID: $Id$
9 // Copyright: (c) 2007 Anders Larsen
10 // License: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #ifndef WX_PRECOMP
29 #include "wx/wx.h"
30 #endif
31
32 // Settings common to both executables: determines whether
33 // we're using TCP/IP or real DDE.
34 #include "ipcsetup.h"
35
36 #include "connection.h"
37
38 #include "wx/timer.h"
39 #include "wx/datetime.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45
46 // Define a new application
47 class MyClient;
48
49 class MyApp: public wxApp
50 {
51 public:
52 virtual bool OnInit();
53 virtual int OnExit();
54
55 protected:
56 MyClient *m_client;
57 };
58
59 class MyConnection : public MyConnectionBase
60 {
61 public:
62 virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format);
63 virtual const void *Request(const wxString& item, size_t *size = NULL, wxIPCFormat format = wxIPC_TEXT);
64 virtual bool DoPoke(const wxString& item, const void* data, size_t size, wxIPCFormat format);
65 virtual bool OnAdvise(const wxString& topic, const wxString& item, const void *data, size_t size, wxIPCFormat format);
66 virtual bool OnDisconnect();
67 };
68
69 class MyClient: public wxClient, public wxTimer
70 {
71 public:
72 MyClient();
73 virtual ~MyClient();
74 bool Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic);
75 void Disconnect();
76 wxConnectionBase *OnMakeConnection();
77 bool IsConnected() { return m_connection != NULL; };
78 virtual void Notify();
79
80 protected:
81 MyConnection *m_connection;
82 int m_step;
83 };
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 IMPLEMENT_APP_CONSOLE(MyApp)
90
91 // ----------------------------------------------------------------------------
92 // MyApp
93 // ----------------------------------------------------------------------------
94
95 // The `main program' equivalent, creating the windows and returning the
96 // main frame
97 bool MyApp::OnInit()
98 {
99 if ( !wxApp::OnInit() )
100 return false;
101
102 delete wxLog::SetActiveTarget(new wxLogStderr);
103
104 // Create a new client
105 m_client = new MyClient;
106 bool retval = m_client->Connect("localhost", "4242", "IPC TEST");
107
108 wxLogMessage(_T("Client host=\"localhost\" port=\"4242\" topic=\"IPC TEST\" %s"),
109 retval ? _T("connected") : _T("failed to connect"));
110
111 return retval;
112 }
113
114 int MyApp::OnExit()
115 {
116 delete m_client;
117
118 return 0;
119 }
120
121 // ----------------------------------------------------------------------------
122 // MyClient
123 // ----------------------------------------------------------------------------
124
125 MyClient::MyClient() : wxClient()
126 {
127 m_connection = NULL;
128 m_step = 0;
129 }
130
131 bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
132 {
133 // suppress the log messages from MakeConnection()
134 wxLogNull nolog;
135
136 m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
137 if (m_connection)
138 Start(1000, false);
139 return m_connection != NULL;
140 }
141
142 wxConnectionBase *MyClient::OnMakeConnection()
143 {
144 return new MyConnection;
145 }
146
147 void MyClient::Disconnect()
148 {
149 if (m_connection)
150 {
151 m_connection->Disconnect();
152 delete m_connection;
153 m_connection = NULL;
154 wxLogMessage(_T("Client disconnected from server"));
155 }
156 wxGetApp().ExitMainLoop();
157 }
158
159 MyClient::~MyClient()
160 {
161 Disconnect();
162 }
163
164 void MyClient::Notify()
165 {
166 switch (m_step++)
167 {
168 case 0:
169 {
170 size_t size;
171 m_connection->Request(_T("Date"));
172 m_connection->Request(_T("Date+len"), &size);
173 m_connection->Request(_T("bytes[3]"), &size, wxIPC_PRIVATE);
174 break;
175 }
176 case 1:
177 {
178 wxString s = wxDateTime::Now().Format();
179 m_connection->Poke(_T("Date"), s);
180 s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
181 m_connection->Poke(_T("Date"), (const char *)s.c_str(), s.length() + 1);
182 char bytes[3];
183 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
184 m_connection->Poke(_T("bytes[3]"), bytes, 3, wxIPC_PRIVATE);
185 break;
186 }
187 case 2:
188 {
189 wxString s = _T("Date");
190 m_connection->Execute(s);
191 m_connection->Execute((const char *)s.c_str(), s.length() + 1);
192 char bytes[3];
193 bytes[0] = '1';
194 bytes[1] = '2';
195 bytes[2] = '3';
196 m_connection->Execute(bytes, WXSIZEOF(bytes));
197 break;
198 }
199 case 3:
200 wxLogMessage(_T("StartAdvise(\"something\")"));
201 m_connection->StartAdvise(_T("something"));
202 break;
203 case 10:
204 wxLogMessage(_T("StopAdvise(\"something\")"));
205 m_connection->StopAdvise(_T("something"));
206 break;
207 case 15:
208 Disconnect();
209 break;
210 }
211 }
212
213 // ----------------------------------------------------------------------------
214 // MyConnection
215 // ----------------------------------------------------------------------------
216
217 bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
218 size_t size, wxIPCFormat format)
219 {
220 Log(_T("OnAdvise"), topic, item, data, size, format);
221 return true;
222 }
223
224 bool MyConnection::OnDisconnect()
225 {
226 wxLogMessage(_T("OnDisconnect()"));
227 wxGetApp().ExitMainLoop();
228 return true;
229 }
230
231 bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
232 {
233 Log(_T("Execute"), wxEmptyString, wxEmptyString, data, size, format);
234 bool retval = wxConnection::DoExecute(data, size, format);
235 if (!retval)
236 wxLogMessage(_T("Execute failed!"));
237 return retval;
238 }
239
240 const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
241 {
242 const void *data = wxConnection::Request(item, size, format);
243 Log(_T("Request"), wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
244 return data;
245 }
246
247 bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
248 {
249 Log(_T("Poke"), wxEmptyString, item, data, size, format);
250 return wxConnection::DoPoke(item, data, size, format);
251 }