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