File: ipcclient.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (170 lines) | stat: -rw-r--r-- 3,834 bytes parent folder | download | duplicates (7)
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
/////////////////////////////////////////////////////////////////////////////
// Name:        tests/benchmarks/ipcclient.cpp
// Purpose:     wxIPC client side benchmarks
// Author:      Vadim Zeitlin
// Created:     2008-10-13
// Copyright:   (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#include "bench.h"

#include "wx/evtloop.h"

// do this before including wx/ipc.h under Windows to use TCP even there
#define wxUSE_DDE_FOR_IPC 0
#include "wx/ipc.h"
#include "../../samples/ipc/ipcsetup.h"

namespace
{

class PokeAdviseConn : public wxConnection
{
public:
    PokeAdviseConn() { m_gotAdvised = false; }

    bool GotAdvised()
    {
        if ( !m_gotAdvised )
            return false;

        m_gotAdvised = false;

        return true;
    }

    const wxString& GetItem() const { return m_item; }

    virtual bool OnAdvise(const wxString& topic,
                          const wxString& item,
                          const void *data,
                          size_t size,
                          wxIPCFormat format)
    {
        m_gotAdvised = true;

        if ( topic != IPC_BENCHMARK_TOPIC ||
                item != IPC_BENCHMARK_ITEM ||
                    !IsTextFormat(format) )
        {
            m_item = "ERROR";
            return false;
        }

        m_item = GetTextFromData(data, size, format);

        return true;
    }

private:
    wxString m_item;
    bool m_gotAdvised;

    DECLARE_NO_COPY_CLASS(PokeAdviseConn)
};

class PokeAdviseClient : public wxClient
{
public:
    // provide a convenient helper taking care of connecting to the right
    // server/service/topic and returning the connection of the derived type
    // (or NULL if we failed to connect)
    PokeAdviseConn *Connect()
    {
        wxString host = Bench::GetStringParameter();
        if ( host.empty() )
            host = IPC_HOST;

        wxString service;
        int port = Bench::GetNumericParameter();
        if ( !port )
            service = IPC_SERVICE;
        else
            service.Printf("%d", port);

        return static_cast<PokeAdviseConn *>(
                MakeConnection(host, service, IPC_BENCHMARK_TOPIC));
    }


    // override base class virtual to use a custom connection class
    virtual wxConnectionBase *OnMakeConnection()
    {
        return new PokeAdviseConn;
    }
};

class PokeAdvisePersistentConnection
{
public:
    PokeAdvisePersistentConnection()
    {
        m_client = new PokeAdviseClient;
        m_conn = m_client->Connect();
        if ( m_conn )
            m_conn->StartAdvise(IPC_BENCHMARK_ITEM);
    }

    ~PokeAdvisePersistentConnection()
    {
        if ( m_conn )
        {
            m_conn->StopAdvise(IPC_BENCHMARK_ITEM);
            m_conn->Disconnect();
        }

        delete m_client;
    }

    PokeAdviseConn *Get() const { return m_conn; }

private:
    PokeAdviseClient *m_client;
    PokeAdviseConn *m_conn;
    bool m_initDone;

    DECLARE_NO_COPY_CLASS(PokeAdvisePersistentConnection)
};

PokeAdvisePersistentConnection *theConnection = NULL;

bool ConnInit()
{
    theConnection = new PokeAdvisePersistentConnection;
    if ( !theConnection->Get() )
    {
        delete theConnection;
        theConnection = NULL;
        return false;
    }

    return true;
}

void ConnDone()
{
    delete theConnection;
}

} // anonymous namespace

BENCHMARK_FUNC_WITH_INIT(IPCPokeAdvise, ConnInit, ConnDone)
{
    wxEventLoop loop;

    PokeAdviseConn * const conn = theConnection->Get();

    const wxString s(1024, '@');

    if ( !conn->Poke(IPC_BENCHMARK_ITEM, s) )
        return false;

    while ( !conn->GotAdvised() )
        loop.Dispatch();

    if ( conn->GetItem() != s )
        return false;

    return true;
}