File: ipc.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (261 lines) | stat: -rw-r--r-- 6,056 bytes parent folder | download | duplicates (4)
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
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/net/ipc.cpp
// Purpose:     IPC classes unit tests
// Author:      Vadim Zeitlin
// Copyright:   (c) 2008 Vadim Zeitlin
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
// and "wx/cppunit.h"
#include "testprec.h"


// FIXME: this tests currently sometimes hangs in Connect() for unknown reason
//        and this prevents buildbot builds from working so disabling it, but
//        the real problem needs to be fixed, of course
#if 0

// this test needs threads as it runs the test server in a secondary thread
#if wxUSE_THREADS

// for all others, include the necessary headers
#ifndef WX_PRECOMP
    #include "wx/app.h"
#endif

#include "wx/ipc.h"
#include "wx/thread.h"

#define wxUSE_SOCKETS_FOR_IPC (!wxUSE_DDE_FOR_IPC)

namespace
{

const char *IPC_TEST_PORT = "4242";
const char *IPC_TEST_TOPIC = "IPC TEST";

} // anonymous namespace

// ----------------------------------------------------------------------------
// test connection class used by IPCTestServer
// ----------------------------------------------------------------------------

class IPCTestConnection : public wxConnection
{
public:
    IPCTestConnection() { }

    virtual bool OnExec(const wxString& topic, const wxString& data)
    {
        if ( topic != IPC_TEST_TOPIC )
            return false;

        return data == "Date";
    }

private:
    wxDECLARE_NO_COPY_CLASS(IPCTestConnection);
};

// ----------------------------------------------------------------------------
// event dispatching thread class
// ----------------------------------------------------------------------------

class EventThread : public wxThread
{
public:
    EventThread()
        : wxThread(wxTHREAD_JOINABLE)
    {
        Create();
        Run();
    }

protected:
    virtual void *Entry()
    {
        wxTheApp->MainLoop();

        return NULL;
    }

    wxDECLARE_NO_COPY_CLASS(EventThread);
};

// ----------------------------------------------------------------------------
// test server class
// ----------------------------------------------------------------------------

class IPCTestServer : public wxServer
{
public:
    IPCTestServer()
    {
        m_conn = NULL;

#if wxUSE_SOCKETS_FOR_IPC
        // we must call this from the main thread
        wxSocketBase::Initialize();
#endif // wxUSE_SOCKETS_FOR_IPC

        // we need event dispatching to work for IPC server to work
        m_thread = new EventThread;

        Create(IPC_TEST_PORT);
    }

    virtual ~IPCTestServer()
    {
        wxTheApp->ExitMainLoop();

        m_thread->Wait();
        delete m_thread;

        delete m_conn;

#if wxUSE_SOCKETS_FOR_IPC
        wxSocketBase::Shutdown();
#endif // wxUSE_SOCKETS_FOR_IPC
    }

    virtual wxConnectionBase *OnAcceptConnection(const wxString& topic)
    {
        if ( topic != IPC_TEST_TOPIC )
            return NULL;

        m_conn = new IPCTestConnection;
        return m_conn;
    }

private:
    EventThread *m_thread;
    IPCTestConnection *m_conn;

    wxDECLARE_NO_COPY_CLASS(IPCTestServer);
};

static IPCTestServer *gs_server = NULL;

// ----------------------------------------------------------------------------
// test client class
// ----------------------------------------------------------------------------

class IPCTestClient : public wxClient
{
public:
    IPCTestClient()
    {
        m_conn = NULL;
    }

    virtual ~IPCTestClient()
    {
        Disconnect();
    }

    bool
    Connect(const wxString& host, const wxString& service, const wxString& topic)
    {
        m_conn = MakeConnection(host, service, topic);

        return m_conn != NULL;
    }

    void Disconnect()
    {
        if ( m_conn )
        {
            delete m_conn;
            m_conn = NULL;
        }
    }

    wxConnectionBase& GetConn() const
    {
        CPPUNIT_ASSERT( m_conn );

        return *m_conn;
    }

private:
    wxConnectionBase *m_conn;

    wxDECLARE_NO_COPY_CLASS(IPCTestClient);
};

static IPCTestClient *gs_client = NULL;

// ----------------------------------------------------------------------------
// the test code itself
// ----------------------------------------------------------------------------

class IPCTestCase : public CppUnit::TestCase
{
public:
    IPCTestCase() { }

private:
    CPPUNIT_TEST_SUITE( IPCTestCase );
        CPPUNIT_TEST( Connect );
        CPPUNIT_TEST( Execute );
        CPPUNIT_TEST( Disconnect );
    CPPUNIT_TEST_SUITE_END();

    void Connect();
    void Execute();
    void Disconnect();

    wxDECLARE_NO_COPY_CLASS(IPCTestCase);
};

CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase, "IPCTestCase" );

void IPCTestCase::Connect()
{
    gs_server = new IPCTestServer;
    gs_client = new IPCTestClient;

    // connecting to the wrong port should fail
    CPPUNIT_ASSERT( !gs_client->Connect("localhost", "2424", IPC_TEST_TOPIC) );

    // connecting using an unsupported topic should fail (unless the server
    // expects a ROT-13'd topic name...)
    CPPUNIT_ASSERT( !gs_client->Connect("localhost", IPC_TEST_PORT, "VCP GRFG") );

    // connecting to the right port on the right topic should succeed
    CPPUNIT_ASSERT( gs_client->Connect("localhost", IPC_TEST_PORT, IPC_TEST_TOPIC) );
}

void IPCTestCase::Execute()
{
    wxConnectionBase& conn = gs_client->GetConn();

    const wxString s("Date");
    CPPUNIT_ASSERT( conn.Execute(s) );
    CPPUNIT_ASSERT( conn.Execute(s.mb_str(), s.length() + 1) );

    char bytes[] = { 1, 2, 3 };
    CPPUNIT_ASSERT( conn.Execute(bytes, WXSIZEOF(bytes)) );
}

void IPCTestCase::Disconnect()
{
    if ( gs_client )
    {
        gs_client->Disconnect();
        delete gs_client;
        gs_client = NULL;
    }

    if ( gs_server )
    {
        delete gs_server;
        gs_server = NULL;
    }
}

#endif // wxUSE_THREADS

#endif // !__WINDOWS__