File: baseclient.cpp

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (342 lines) | stat: -rw-r--r-- 8,984 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
///////////////////////////////////////////////////////////////////////////////
// Name:        samples/ipc/baseclient.cpp
// Purpose:     IPC sample: console client
// Author:      Anders Larsen
//              Most of the code was stolen from: samples/ipc/client.cpp
//              (c) Julian Smart, Jurgen Doornik
// Created:     2007-11-08
// Copyright:   (c) 2007 Anders Larsen
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

// Settings common to both executables: determines whether
// we're using TCP/IP or real DDE.
#include "ipcsetup.h"

#include "connection.h"

#include "wx/timer.h"
#include "wx/datetime.h"
#include "wx/vector.h"

class MyClient;

// ----------------------------------------------------------------------------
// classes
// ----------------------------------------------------------------------------

class MyApp : public wxApp
{
public:
    MyApp() { Connect(wxEVT_IDLE, wxIdleEventHandler(MyApp::OnIdle)); }

    virtual bool OnInit();
    virtual int OnExit();

private:
    void OnIdle(wxIdleEvent& event);

    MyClient *m_client;
};

class MyConnection : public MyConnectionBase
{
public:
    virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format);
    virtual const void *Request(const wxString& item, size_t *size = NULL, wxIPCFormat format = wxIPC_TEXT);
    virtual bool DoPoke(const wxString& item, const void* data, size_t size, wxIPCFormat format);
    virtual bool OnAdvise(const wxString& topic, const wxString& item, const void *data, size_t size, wxIPCFormat format);
    virtual bool OnDisconnect();
};

class MyClient : public wxClient,
                 private wxTimer
{
public:
    MyClient();
    virtual ~MyClient();

    bool Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic);
    void Disconnect();
    wxConnectionBase *OnMakeConnection();
    bool IsConnected() { return m_connection != NULL; };

    virtual void Notify();

    void StartNextTestIfNecessary();

private:
    void TestRequest();
    void TestPoke();
    void TestExecute();
    void TestStartAdvise();
    void TestStopAdvise();
    void TestDisconnect();


    MyConnection *m_connection;

    // the test functions to be executed by StartNextTestIfNecessary()
    typedef void (MyClient::*MyClientTestFunc)();
    wxVector<MyClientTestFunc> m_tests;

    // number of seconds since the start of the test
    int m_step;
};

// ============================================================================
// implementation
// ============================================================================

IMPLEMENT_APP_CONSOLE(MyApp)

// ----------------------------------------------------------------------------
// MyApp
// ----------------------------------------------------------------------------

// The `main program' equivalent, creating the windows and returning the
// main frame
bool MyApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    // Create a new client
    m_client = new MyClient;
    bool retval = m_client->Connect("localhost", "4242", "IPC TEST");

    wxLogMessage("Client host=\"localhost\" port=\"4242\" topic=\"IPC TEST\" %s",
                 retval ? "connected" : "failed to connect");

    return retval;
}

int MyApp::OnExit()
{
    delete m_client;

    return 0;
}

void MyApp::OnIdle(wxIdleEvent& event)
{
    if ( m_client )
        m_client->StartNextTestIfNecessary();

    event.Skip();
}

// ----------------------------------------------------------------------------
// MyClient
// ----------------------------------------------------------------------------

MyClient::MyClient()
    : wxClient()
{
    m_connection = NULL;
    m_step = 0;
}

bool
MyClient::Connect(const wxString& sHost,
                  const wxString& sService,
                  const wxString& sTopic)
{
    // suppress the log messages from MakeConnection()
    wxLogNull nolog;

    m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
    if ( !m_connection )
        return false;

    Start(1000);

    return true;
}

wxConnectionBase *MyClient::OnMakeConnection()
{
    return new MyConnection;
}

void MyClient::Disconnect()
{
    if (m_connection)
    {
        m_connection->Disconnect();
        wxDELETE(m_connection);
        wxLogMessage("Client disconnected from server");
    }
    wxGetApp().ExitMainLoop();
}

MyClient::~MyClient()
{
    Disconnect();
}

void MyClient::Notify()
{
    // we shouldn't call wxIPC methods from here directly as we may be called
    // from inside an IPC call when using TCP/IP as the sockets are used in
    // non-blocking code and so can dispatch events, including the timer ones,
    // while waiting for IO and so starting another IPC call would result in
    // fatal reentrancies -- instead, just set a flag and perform the test
    // indicated by it later from our idle event handler
    MyClientTestFunc testfunc = NULL;
    switch ( m_step++ )
    {
        case 0:
            testfunc = &MyClient::TestRequest;
            break;

        case 1:
            testfunc = &MyClient::TestPoke;
            break;

        case 2:
            testfunc = &MyClient::TestExecute;
            break;

        case 3:
            testfunc = &MyClient::TestStartAdvise;
            break;

        case 10:
            testfunc = &MyClient::TestStopAdvise;
            break;

        case 15:
            testfunc = &MyClient::TestDisconnect;
            // We don't need the timer any more, we're going to exit soon.
            Stop();
            break;

        default:
            // No need to wake up idle handling.
            return;
    }

    m_tests.push_back(testfunc);

    wxWakeUpIdle();
}

void MyClient::StartNextTestIfNecessary()
{
    while ( !m_tests.empty() )
    {
        MyClientTestFunc testfunc = m_tests.front();
        m_tests.erase(m_tests.begin());
        (this->*testfunc)();
    }
}

void MyClient::TestRequest()
{
    size_t size;
    m_connection->Request("Date");
    m_connection->Request("Date+len", &size);
    m_connection->Request("bytes[3]", &size, wxIPC_PRIVATE);
}

void MyClient::TestPoke()
{
    wxString s = wxDateTime::Now().Format();
    m_connection->Poke("Date", s);
    s = wxDateTime::Now().FormatTime() + " " + wxDateTime::Now().FormatDate();
    m_connection->Poke("Date", (const char *)s.c_str(), s.length() + 1);
    char bytes[3];
    bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
    m_connection->Poke("bytes[3]", bytes, 3, wxIPC_PRIVATE);
}

void MyClient::TestExecute()
{
    wxString s = "Date";
    m_connection->Execute(s);
    m_connection->Execute((const char *)s.c_str(), s.length() + 1);
    char bytes[3];
    bytes[0] = '1';
    bytes[1] = '2';
    bytes[2] = '3';
    m_connection->Execute(bytes, WXSIZEOF(bytes));
}

void MyClient::TestStartAdvise()
{
    wxLogMessage("StartAdvise(\"something\")");
    m_connection->StartAdvise("something");
}

void MyClient::TestStopAdvise()
{
    wxLogMessage("StopAdvise(\"something\")");
    m_connection->StopAdvise("something");
}

void MyClient::TestDisconnect()
{
    Disconnect();
}

// ----------------------------------------------------------------------------
// MyConnection
// ----------------------------------------------------------------------------

bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
    size_t size, wxIPCFormat format)
{
    Log("OnAdvise", topic, item, data, size, format);
    return true;
}

bool MyConnection::OnDisconnect()
{
    wxLogMessage("OnDisconnect()");
    wxGetApp().ExitMainLoop();
    return true;
}

bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
{
    Log("Execute", wxEmptyString, wxEmptyString, data, size, format);
    bool retval = wxConnection::DoExecute(data, size, format);
    if (!retval)
    {
        wxLogMessage("Execute failed!");
    }
    return retval;
}

const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
{
    const void *data =  wxConnection::Request(item, size, format);
    Log("Request", wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
    return data;
}

bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
{
    Log("Poke", wxEmptyString, item, data, size, format);
    return wxConnection::DoPoke(item, data, size, format);
}