]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/benchmarks/ipcclient.cpp | |
3 | // Purpose: wxIPC client side benchmarks | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2008-10-13 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows license | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "bench.h" | |
12 | ||
13 | #include "wx/evtloop.h" | |
14 | ||
15 | // do this before including wx/ipc.h under Windows to use TCP even there | |
16 | #define wxUSE_DDE_FOR_IPC 0 | |
17 | #include "wx/ipc.h" | |
18 | #include "../../samples/ipc/ipcsetup.h" | |
19 | ||
20 | namespace | |
21 | { | |
22 | ||
23 | class PokeAdviseConn : public wxConnection | |
24 | { | |
25 | public: | |
26 | PokeAdviseConn() { m_gotAdvised = false; } | |
27 | ||
28 | bool GotAdvised() | |
29 | { | |
30 | if ( !m_gotAdvised ) | |
31 | return false; | |
32 | ||
33 | m_gotAdvised = false; | |
34 | ||
35 | return true; | |
36 | } | |
37 | ||
38 | const wxString& GetItem() const { return m_item; } | |
39 | ||
40 | virtual bool OnAdvise(const wxString& topic, | |
41 | const wxString& item, | |
42 | const void *data, | |
43 | size_t size, | |
44 | wxIPCFormat format) | |
45 | { | |
46 | m_gotAdvised = true; | |
47 | ||
48 | if ( topic != IPC_BENCHMARK_TOPIC || | |
49 | item != IPC_BENCHMARK_ITEM || | |
50 | !IsTextFormat(format) ) | |
51 | { | |
52 | m_item = "ERROR"; | |
53 | return false; | |
54 | } | |
55 | ||
56 | m_item = GetTextFromData(data, size, format); | |
57 | ||
58 | return true; | |
59 | } | |
60 | ||
61 | private: | |
62 | wxString m_item; | |
63 | bool m_gotAdvised; | |
64 | ||
65 | DECLARE_NO_COPY_CLASS(PokeAdviseConn) | |
66 | }; | |
67 | ||
68 | class PokeAdviseClient : public wxClient | |
69 | { | |
70 | public: | |
71 | // provide a convenient helper taking care of connecting to the right | |
72 | // server/service/topic and returning the connection of the derived type | |
73 | // (or NULL if we failed to connect) | |
74 | PokeAdviseConn *Connect() | |
75 | { | |
76 | wxString host = Bench::GetStringParameter(); | |
77 | if ( host.empty() ) | |
78 | host = IPC_HOST; | |
79 | ||
80 | wxString service; | |
81 | int port = Bench::GetNumericParameter(); | |
82 | if ( !port ) | |
83 | service = IPC_SERVICE; | |
84 | else | |
85 | service.Printf("%d", port); | |
86 | ||
87 | return static_cast<PokeAdviseConn *>( | |
88 | MakeConnection(host, service, IPC_BENCHMARK_TOPIC)); | |
89 | } | |
90 | ||
91 | ||
92 | // override base class virtual to use a custom connection class | |
93 | virtual wxConnectionBase *OnMakeConnection() | |
94 | { | |
95 | return new PokeAdviseConn; | |
96 | } | |
97 | }; | |
98 | ||
99 | class PokeAdvisePersistentConnection | |
100 | { | |
101 | public: | |
102 | PokeAdvisePersistentConnection() | |
103 | { | |
104 | m_client = new PokeAdviseClient; | |
105 | m_conn = m_client->Connect(); | |
106 | if ( m_conn ) | |
107 | m_conn->StartAdvise(IPC_BENCHMARK_ITEM); | |
108 | } | |
109 | ||
110 | ~PokeAdvisePersistentConnection() | |
111 | { | |
112 | if ( m_conn ) | |
113 | { | |
114 | m_conn->StopAdvise(IPC_BENCHMARK_ITEM); | |
115 | m_conn->Disconnect(); | |
116 | } | |
117 | ||
118 | delete m_client; | |
119 | } | |
120 | ||
121 | PokeAdviseConn *Get() const { return m_conn; } | |
122 | ||
123 | private: | |
124 | PokeAdviseClient *m_client; | |
125 | PokeAdviseConn *m_conn; | |
126 | bool m_initDone; | |
127 | ||
128 | DECLARE_NO_COPY_CLASS(PokeAdvisePersistentConnection) | |
129 | }; | |
130 | ||
131 | PokeAdvisePersistentConnection *theConnection = NULL; | |
132 | ||
133 | bool ConnInit() | |
134 | { | |
135 | theConnection = new PokeAdvisePersistentConnection; | |
136 | if ( !theConnection->Get() ) | |
137 | { | |
138 | delete theConnection; | |
139 | theConnection = NULL; | |
140 | return false; | |
141 | } | |
142 | ||
143 | return true; | |
144 | } | |
145 | ||
146 | void ConnDone() | |
147 | { | |
148 | delete theConnection; | |
149 | } | |
150 | ||
151 | } // anonymous namespace | |
152 | ||
153 | BENCHMARK_FUNC_WITH_INIT(IPCPokeAdvise, ConnInit, ConnDone) | |
154 | { | |
155 | wxEventLoop loop; | |
156 | ||
157 | PokeAdviseConn * const conn = theConnection->Get(); | |
158 | ||
159 | const wxString s(1024, '@'); | |
160 | ||
161 | if ( !conn->Poke(IPC_BENCHMARK_ITEM, s) ) | |
162 | return false; | |
163 | ||
164 | while ( !conn->GotAdvised() ) | |
165 | loop.Dispatch(); | |
166 | ||
167 | if ( conn->GetItem() != s ) | |
168 | return false; | |
169 | ||
170 | return true; | |
171 | } |