]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/baseserver.cpp
new path organisation for mac
[wxWidgets.git] / samples / ipc / baseserver.cpp
CommitLineData
ebe887ed
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: samples/ipc/baseserver.cpp
3// Purpose: IPC sample: console server
4// Author: Anders Larsen
5// Most of the code was stolen from: samples/ipc/server.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
521d3436
VZ
36#include "connection.h"
37
ebe887ed
VZ
38#include "wx/timer.h"
39#include "wx/datetime.h"
40
41// ----------------------------------------------------------------------------
42// wxWin macros
43// ----------------------------------------------------------------------------
44
45// Define a new application
46class MyServer;
ebe887ed
VZ
47
48class MyApp : public wxApp
49{
50public:
51 virtual bool OnInit();
52 virtual int OnExit();
53
54protected:
55 MyServer *m_server;
56};
57
58DECLARE_APP(MyApp)
59
521d3436 60class MyConnection : public MyConnectionBase, public wxTimer
ebe887ed
VZ
61{
62public:
ebe887ed
VZ
63 virtual bool Disconnect() { return wxConnection::Disconnect(); }
64 virtual bool OnExecute(const wxString& topic, const void *data, size_t size, wxIPCFormat format);
65 virtual const void *OnRequest(const wxString& topic, const wxString& item, size_t *size, wxIPCFormat format);
66 virtual bool OnPoke(const wxString& topic, const wxString& item, const void *data, size_t size, wxIPCFormat format);
67 virtual bool OnStartAdvise(const wxString& topic, const wxString& item);
68 virtual bool OnStopAdvise(const wxString& topic, const wxString& item);
69 virtual bool DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format);
70 virtual bool OnDisconnect();
71 virtual void Notify();
72
ebe887ed
VZ
73 wxString m_sAdvise;
74
75protected:
76 wxString m_sRequestDate;
77 char m_achRequestBytes[3];
78};
79
80class MyServer: public wxServer
81{
82public:
83 MyServer();
84 virtual ~MyServer();
85 void Disconnect();
86 bool IsConnected() { return m_connection != NULL; };
87 MyConnection *GetConnection() { return m_connection; };
88 wxConnectionBase *OnAcceptConnection(const wxString& topic);
89
90protected:
91 MyConnection *m_connection;
92};
93
94// ============================================================================
95// implementation
96// ============================================================================
97
2e334012 98IMPLEMENT_APP_CONSOLE(MyApp)
ebe887ed
VZ
99
100// ----------------------------------------------------------------------------
101// MyApp
102// ----------------------------------------------------------------------------
103
104bool MyApp::OnInit()
105{
106 if ( !wxApp::OnInit() )
107 return false;
108
109 delete wxLog::SetActiveTarget(new wxLogStderr);
110
111 // Create a new server
112 m_server = new MyServer;
113 if (m_server->Create("4242"))
114 {
115 wxLogMessage(_T("Server 4242 started"));
116#if wxUSE_DDE_FOR_IPC
117 wxLogMessage(_T("Server uses DDE"));
118#else // !wxUSE_DDE_FOR_IPC
119 wxLogMessage(_T("Server uses TCP"));
120#endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
121 return true;
122 }
123 else
124 {
125 wxLogMessage(_T("Server 4242 failed to start"));
126 delete m_server;
127 return false;
128 }
129}
130
131int MyApp::OnExit()
132{
133 return 0;
134}
135
136// ----------------------------------------------------------------------------
137// MyServer
138// ----------------------------------------------------------------------------
139
140MyServer::MyServer()
141{
142 m_connection = NULL;
143}
144
145MyServer::~MyServer()
146{
147 Disconnect();
148}
149
150wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
151{
152 wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic.c_str());
153
154 if ( topic == IPC_TOPIC )
155 {
156 m_connection = new MyConnection;
157 wxLogMessage(_T("Connection accepted"));
158 return m_connection;
159 }
160 // unknown topic
161 return NULL;
162}
163
164void MyServer::Disconnect()
165{
166 if (m_connection)
167 {
168 m_connection->Disconnect();
169 delete m_connection;
170 m_connection = NULL;
171 wxLogMessage(_T("Disconnected client"));
172 }
173}
174
175// ----------------------------------------------------------------------------
176// MyConnection
177// ----------------------------------------------------------------------------
178
ebe887ed
VZ
179bool MyConnection::OnExecute(const wxString& topic,
180 const void *data, size_t size, wxIPCFormat format)
181{
182 Log(_T("OnExecute"), topic, _T(""), data, size, format);
183 return true;
184}
185
186bool MyConnection::OnPoke(const wxString& topic,
187 const wxString& item, const void *data, size_t size, wxIPCFormat format)
188{
189 Log(_T("OnPoke"), topic, item, data, size, format);
190 return wxConnection::OnPoke(topic, item, data, size, format);
191}
192
193const void *MyConnection::OnRequest(const wxString& topic,
194 const wxString& item, size_t *size, wxIPCFormat format)
195{
196 const void *data;
197 if (item == _T("Date"))
198 {
199 m_sRequestDate = wxDateTime::Now().Format();
200 data = m_sRequestDate.c_str();
201 *size = wxNO_LEN;
202 }
203 else if (item == _T("Date+len"))
204 {
205 m_sRequestDate = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
206 data = m_sRequestDate.c_str();
207 *size = m_sRequestDate.Length() + 1;
208 }
209 else if (item == _T("bytes[3]"))
210 {
211 data = m_achRequestBytes;
212 m_achRequestBytes[0] = '1'; m_achRequestBytes[1] = '2'; m_achRequestBytes[2] = '3';
213 *size = 3;
214 }
215 else
216 {
217 data = NULL;
218 *size = 0;
219 }
220 Log(_T("OnRequest"), topic, item, data, *size, format);
221 return data;
222}
223
224bool MyConnection::OnStartAdvise(const wxString& topic,
225 const wxString& item)
226{
227 wxLogMessage(_T("OnStartAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str());
228 wxLogMessage(_T("Returning true"));
229 m_sAdvise = item;
230 Start(3000, false);
231 return true;
232}
233
234bool MyConnection::OnStopAdvise(const wxString& topic,
235 const wxString& item)
236{
237 wxLogMessage(_T("OnStopAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str());
238 wxLogMessage(_T("Returning true"));
239 m_sAdvise.Empty();
240 Stop();
241 return true;
242}
243
244void MyConnection::Notify()
245{
246 if (!m_sAdvise.IsEmpty())
247 {
248 wxString s = wxDateTime::Now().Format();
249 Advise(m_sAdvise, s);
250 s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
251 Advise(m_sAdvise, (const char *)s.c_str(), s.Length() + 1);
252
253#if wxUSE_DDE_FOR_IPC
254 wxLogMessage(_T("DDE Advise type argument cannot be wxIPC_PRIVATE. The client will receive it as wxIPC_TEXT, and receive the correct no of bytes, but not print a correct log entry."));
255#endif
256 char bytes[3];
257 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
258 Advise(m_sAdvise, bytes, 3, wxIPC_PRIVATE);
259 // this works, but the log treats it as a string now
260// m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
261 }
262}
263
ebe887ed
VZ
264bool MyConnection::DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format)
265{
266 Log(_T("Advise"), _T(""), item, data, size, format);
267 return wxConnection::DoAdvise(item, data, size, format);
268}
269
270bool MyConnection::OnDisconnect()
271{
272 wxLogMessage(_T("OnDisconnect()"));
273 return true;
274}