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