]>
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.
37 #include "wx/datetime.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
44 // Define a new application
48 class MyApp
: public wxApp
51 virtual bool OnInit ();
58 class MyConnection
: public wxConnection
62 virtual ~ MyConnection ();
63 virtual bool DoExecute ( const void * data
, size_t size
, wxIPCFormat format
);
64 virtual const void * Request ( const wxString
& item
, size_t * size
= NULL
, wxIPCFormat format
= wxIPC_TEXT
);
65 virtual bool DoPoke ( const wxString
& item
, const void * data
, size_t size
, wxIPCFormat format
);
66 virtual bool OnAdvise ( const wxString
& topic
, const wxString
& item
, const void * data
, size_t size
, wxIPCFormat format
);
67 virtual bool OnDisconnect ();
70 void Log ( const wxString
& command
, const wxString
& topic
,
71 const wxString
& item
, const void * data
, size_t size
, wxIPCFormat format
);
74 class MyClient
: public wxClient
, public wxTimer
79 bool Connect ( const wxString
& sHost
, const wxString
& sService
, const wxString
& sTopic
);
81 wxConnectionBase
* OnMakeConnection ();
82 bool IsConnected () { return m_connection
!= NULL
; };
83 virtual void Notify ();
86 MyConnection
* m_connection
;
90 // ============================================================================
92 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // The `main program' equivalent, creating the windows and returning the
104 if ( ! wxApp :: OnInit () )
107 delete wxLog :: SetActiveTarget ( new wxLogStderr
);
109 // Create a new client
110 m_client
= new MyClient
;
111 bool retval
= m_client
-> Connect ( "localhost" , "4242" , "IPC TEST" );
113 wxLogMessage ( _T ( "Client host= \" localhost \" port= \" 4242 \" topic= \" IPC TEST \" %s " ),
114 retval
? _T ( "connected" ) : _T ( "failed to connect" ));
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 MyClient :: MyClient () : wxClient ()
136 bool MyClient :: Connect ( const wxString
& sHost
, const wxString
& sService
, const wxString
& sTopic
)
138 // suppress the log messages from MakeConnection()
141 m_connection
= ( MyConnection
*) MakeConnection ( sHost
, sService
, sTopic
);
144 return m_connection
!= NULL
;
147 wxConnectionBase
* MyClient :: OnMakeConnection ()
149 return new MyConnection
;
152 void MyClient :: Disconnect ()
156 m_connection
-> Disconnect ();
159 wxLogMessage ( _T ( "Client disconnected from server" ));
161 wxGetApp (). ExitMainLoop ();
164 MyClient ::~ MyClient ()
169 void MyClient :: Notify ()
176 m_connection
-> Request ( _T ( "Date" ));
177 m_connection
-> Request ( _T ( "Date+len" ), & size
);
178 m_connection
-> Request ( _T ( "bytes[3]" ), & size
, wxIPC_PRIVATE
);
183 wxString s
= wxDateTime :: Now (). Format ();
184 m_connection
-> Poke ( _T ( "Date" ), s
);
185 s
= wxDateTime :: Now (). FormatTime () + _T ( " " ) + wxDateTime :: Now (). FormatDate ();
186 m_connection
-> Poke ( _T ( "Date" ), ( const char *) s
. c_str (), s
. length () + 1 );
188 bytes
[ 0 ] = '1' ; bytes
[ 1 ] = '2' ; bytes
[ 2 ] = '3' ;
189 m_connection
-> Poke ( _T ( "bytes[3]" ), bytes
, 3 , wxIPC_PRIVATE
);
194 wxString s
= _T ( "Date" );
195 m_connection
-> Execute ( s
);
196 m_connection
-> Execute (( const char *) s
. c_str (), s
. length () + 1 );
197 #if wxUSE_DDE_FOR_IPC
198 wxLogMessage ( _T ( "DDE Execute can only be used to send text strings, not arbitrary data. \n The type argument will be ignored, text truncated, converted to Unicode and null terminated." ));
201 bytes
[ 0 ] = '1' ; bytes
[ 1 ] = '2' ; bytes
[ 2 ] = '3' ;
202 m_connection
-> Execute ( bytes
, 3 , wxIPC_PRIVATE
);
206 wxLogMessage ( _T ( "StartAdvise( \" something \" )" ));
207 m_connection
-> StartAdvise ( _T ( "something" ));
210 wxLogMessage ( _T ( "StopAdvise( \" something \" )" ));
211 m_connection
-> StopAdvise ( _T ( "something" ));
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 MyConnection :: MyConnection ()
227 MyConnection ::~ MyConnection ()
231 void MyConnection :: Log ( const wxString
& command
, const wxString
& topic
,
232 const wxString
& item
, const void * data
, size_t size
, wxIPCFormat format
)
235 if ( topic
. IsEmpty () && item
. IsEmpty ())
236 s
. Printf ( _T ( " %s (" ), command
. c_str ());
237 else if ( topic
. IsEmpty ())
238 s
. Printf ( _T ( " %s (item= \" %s \" ," ), command
. c_str (), item
. c_str ());
239 else if ( item
. IsEmpty ())
240 s
. Printf ( _T ( " %s (topic= \" %s \" ," ), command
. c_str (), topic
. c_str ());
242 s
. Printf ( _T ( " %s (topic= \" %s \" ,item= \" %s \" ," ), command
. c_str (), topic
. c_str (), item
. c_str ());
248 #if !wxUSE_UNICODE || wxUSE_UNICODE_UTF8
249 wxLogMessage ( _T ( " %s \" %s \" , %d )" ), s
. c_str (), data
, size
);
251 wxLogMessage ( _T ( " %s \" %s \" , %d )" ), s
. c_str (), wxConvUTF8
. cMB2WC (( const char *) data
), size
);
257 char * bytes
= ( char *) data
;
258 wxLogMessage ( _T ( " %s ' %c%c%c ', %d )" ), s
. c_str (), bytes
[ 0 ], bytes
[ 1 ], bytes
[ 2 ], size
);
261 wxLogMessage ( _T ( " %s ..., %d )" ), s
. c_str (), size
);
264 wxLogMessage ( _T ( " %s [invalid data], %d )" ), s
. c_str (), size
);
267 wxLogMessage ( _T ( " %s [unknown data], %d )" ), s
. c_str (), size
);
272 bool MyConnection :: OnAdvise ( const wxString
& topic
, const wxString
& item
, const void * data
,
273 size_t size
, wxIPCFormat format
)
275 Log ( _T ( "OnAdvise" ), topic
, item
, data
, size
, format
);
279 bool MyConnection :: OnDisconnect ()
281 wxLogMessage ( _T ( "OnDisconnect()" ));
282 wxGetApp (). ExitMainLoop ();
286 bool MyConnection :: DoExecute ( const void * data
, size_t size
, wxIPCFormat format
)
288 Log ( _T ( "Execute" ), wxEmptyString
, wxEmptyString
, data
, size
, format
);
289 bool retval
= wxConnection :: DoExecute ( data
, size
, format
);
291 wxLogMessage ( _T ( "Execute failed!" ));
295 const void * MyConnection :: Request ( const wxString
& item
, size_t * size
, wxIPCFormat format
)
297 const void * data
= wxConnection :: Request ( item
, size
, format
);
298 Log ( _T ( "Request" ), wxEmptyString
, item
, data
, size
? * size
: wxNO_LEN
, format
);
302 bool MyConnection :: DoPoke ( const wxString
& item
, const void * data
, size_t size
, wxIPCFormat format
)
304 Log ( _T ( "Poke" ), wxEmptyString
, item
, data
, size
, format
);
305 return wxConnection :: DoPoke ( item
, data
, size
, format
);