]>
git.saurik.com Git - wxWidgets.git/blob - 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
9 // Copyright: (c) 2007 Anders Larsen
10 // License: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
32 // Settings common to both executables: determines whether
33 // we're using TCP/IP or real DDE.
36 #include "connection.h"
39 #include "wx/datetime.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
46 // Define a new application
49 class MyApp
: public wxApp
52 virtual bool OnInit();
59 class MyConnection
: public MyConnectionBase
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();
69 class MyClient
: public wxClient
, public wxTimer
74 bool Connect(const wxString
& sHost
, const wxString
& sService
, const wxString
& sTopic
);
76 wxConnectionBase
*OnMakeConnection();
77 bool IsConnected() { return m_connection
!= NULL
; };
78 virtual void Notify();
81 MyConnection
*m_connection
;
85 // ============================================================================
87 // ============================================================================
89 IMPLEMENT_APP_CONSOLE(MyApp
)
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // The `main program' equivalent, creating the windows and returning the
99 if ( !wxApp::OnInit() )
102 delete wxLog::SetActiveTarget(new wxLogStderr
);
104 // Create a new client
105 m_client
= new MyClient
;
106 bool retval
= m_client
->Connect("localhost", "4242", "IPC TEST");
108 wxLogMessage(_T("Client host=\"localhost\" port=\"4242\" topic=\"IPC TEST\" %s"),
109 retval
? _T("connected") : _T("failed to connect"));
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 MyClient::MyClient() : wxClient()
131 bool MyClient::Connect(const wxString
& sHost
, const wxString
& sService
, const wxString
& sTopic
)
133 // suppress the log messages from MakeConnection()
136 m_connection
= (MyConnection
*)MakeConnection(sHost
, sService
, sTopic
);
139 return m_connection
!= NULL
;
142 wxConnectionBase
*MyClient::OnMakeConnection()
144 return new MyConnection
;
147 void MyClient::Disconnect()
151 m_connection
->Disconnect();
154 wxLogMessage(_T("Client disconnected from server"));
156 wxGetApp().ExitMainLoop();
159 MyClient::~MyClient()
164 void MyClient::Notify()
171 m_connection
->Request(_T("Date"));
172 m_connection
->Request(_T("Date+len"), &size
);
173 m_connection
->Request(_T("bytes[3]"), &size
, wxIPC_PRIVATE
);
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);
183 bytes
[0] = '1'; bytes
[1] = '2'; bytes
[2] = '3';
184 m_connection
->Poke(_T("bytes[3]"), bytes
, 3, wxIPC_PRIVATE
);
189 wxString s
= _T("Date");
190 m_connection
->Execute(s
);
191 m_connection
->Execute((const char *)s
.c_str(), s
.length() + 1);
196 m_connection
->Execute(bytes
, WXSIZEOF(bytes
));
200 wxLogMessage(_T("StartAdvise(\"something\")"));
201 m_connection
->StartAdvise(_T("something"));
204 wxLogMessage(_T("StopAdvise(\"something\")"));
205 m_connection
->StopAdvise(_T("something"));
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 bool MyConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, const void *data
,
218 size_t size
, wxIPCFormat format
)
220 Log(_T("OnAdvise"), topic
, item
, data
, size
, format
);
224 bool MyConnection::OnDisconnect()
226 wxLogMessage(_T("OnDisconnect()"));
227 wxGetApp().ExitMainLoop();
231 bool MyConnection::DoExecute(const void *data
, size_t size
, wxIPCFormat format
)
233 Log(_T("Execute"), wxEmptyString
, wxEmptyString
, data
, size
, format
);
234 bool retval
= wxConnection::DoExecute(data
, size
, format
);
236 wxLogMessage(_T("Execute failed!"));
240 const void *MyConnection::Request(const wxString
& item
, size_t *size
, wxIPCFormat format
)
242 const void *data
= wxConnection::Request(item
, size
, format
);
243 Log(_T("Request"), wxEmptyString
, item
, data
, size
? *size
: wxNO_LEN
, format
);
247 bool MyConnection::DoPoke(const wxString
& item
, const void *data
, size_t size
, wxIPCFormat format
)
249 Log(_T("Poke"), wxEmptyString
, item
, data
, size
, format
);
250 return wxConnection::DoPoke(item
, data
, size
, format
);