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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
// **********************************************************************
//
// Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include "stdafx.h"
#include "HelloClient.h"
#include "HelloClientDlg.h"
#include <iomanip>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define WM_AMI_CALLBACK (WM_USER + 1)
using namespace std;
using namespace Demo;
namespace
{
class Dispatcher : public Ice::Dispatcher
{
public:
Dispatcher(CHelloClientDlg* dialog) : _dialog(dialog)
{
}
virtual void
dispatch(const Ice::DispatcherCallPtr& call, const Ice::ConnectionPtr&)
{
//
// Dispatch Ice AMI callbacks with the event loop thread.
//
_dialog->PostMessage(WM_AMI_CALLBACK, 0, reinterpret_cast<LPARAM>(new Ice::DispatcherCallPtr(call)));
}
private:
CHelloClientDlg* _dialog;
};
class Callback : public IceUtil::Shared
{
public:
Callback(CHelloClientDlg* dialog) : _dialog(dialog)
{
}
void
sent(bool)
{
_dialog->sent();
}
void
response()
{
_dialog->response();
}
void
exception(const Ice::Exception& ex)
{
_dialog->exception(ex);
}
void
flushSent(bool)
{
_dialog->flushed();
}
private:
CHelloClientDlg* _dialog;
};
typedef IceUtil::Handle<Callback> CallbackPtr;
enum DeliveryMode
{
TWOWAY,
TWOWAY_SECURE,
ONEWAY,
ONEWAY_SECURE,
ONEWAY_BATCH,
ONEWAY_SECURE_BATCH,
DATAGRAM,
DATAGRAM_BATCH
};
}
BEGIN_MESSAGE_MAP(CHelloClientDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_INVOKE, OnSayHello)
ON_BN_CLICKED(IDC_FLUSH, OnFlush)
ON_BN_CLICKED(IDC_SHUTDOWN, OnShutdown)
ON_MESSAGE(WM_AMI_CALLBACK, OnAMICallback)
END_MESSAGE_MAP()
CHelloClientDlg::CHelloClientDlg(CWnd* pParent /*=NULL*/) : CDialog(CHelloClientDlg::IDD, pParent)
{
_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//
// Create the communicator.
//
Ice::InitializationData initData;
initData.properties = Ice::createProperties();
initData.properties->load("config");
initData.dispatcher = new Dispatcher(this);
_communicator = Ice::initialize(initData);
//
// Create AMI callbacks.
//
CallbackPtr cb = new Callback(this);
_sayHelloCallback = newCallback_Hello_sayHello(cb, &Callback::response, &Callback::exception, &Callback::sent);
_shutdownCallback = newCallback_Hello_shutdown(cb, &Callback::response, &Callback::exception);
_flushCallback = Ice::newCallback_Communicator_flushBatchRequests(cb, &Callback::exception, &Callback::flushSent);
}
void
CHelloClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BOOL
CHelloClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(_hIcon, TRUE); // Set big icon
SetIcon(_hIcon, FALSE); // Set small icon
//
// Retrieve the controls.
//
_host = (CEdit*)GetDlgItem(IDC_HOST);
_mode = (CComboBox*)GetDlgItem(IDC_MODE);
_timeout = (CSliderCtrl*)GetDlgItem(IDC_TIMEOUT_SLIDER);
_timeoutStatus = (CStatic*)GetDlgItem(IDC_TIMEOUT_STATUS);
_delay = (CSliderCtrl*)GetDlgItem(IDC_DELAY_SLIDER);
_delayStatus = (CStatic*)GetDlgItem(IDC_DELAY_STATUS);
_status = (CStatic*)GetDlgItem(IDC_STATUSBAR);
_flush = (CButton*)GetDlgItem(IDC_FLUSH);
//
// Use twoway mode as the initial default.
//
_mode->SetCurSel(TWOWAY);
//
// Disable flush
//
_flush->EnableWindow(FALSE);
//
// Set hostname
//
_host->SetWindowText(CString("127.0.0.1"));
//
// Initialize timeout slider
//
_timeout->SetRangeMin(0);
_timeout->SetRangeMax(50);
_timeoutStatus->SetWindowText(CString("0.0s"));
//
// Initialize delay slider
//
_delay->SetRangeMin(0);
_delay->SetRangeMax(50);
_delayStatus->SetWindowText(CString("0.0s"));
_status->SetWindowText(CString(" Ready"));
return TRUE; // return TRUE unless you set the focus to a control
}
void
CHelloClientDlg::OnClose()
{
//
// Destroy the communicator. If AMI calls are still in progress they will be
// interrupted with an Ice::CommunicatorDestroyedException.
//
try
{
_communicator->destroy();
}
catch(const IceUtil::Exception&)
{
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void
CHelloClientDlg::OnPaint()
{
if(IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, _hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR
CHelloClientDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(_hIcon);
}
void
CHelloClientDlg::OnHScroll(UINT, UINT, CScrollBar* scroll)
{
CSliderCtrl* slider = (CSliderCtrl*)scroll;
ostringstream s;
if(slider == _timeout)
{
s << setiosflags(ios::fixed) << setprecision(1) << (long)_timeout->GetPos()/10.0 << "s";
_timeoutStatus->SetWindowText(CString(s.str().c_str()));
}
else
{
s << setiosflags(ios::fixed) << setprecision(1) << (long)_delay->GetPos()/10.0 << "s";
_delayStatus->SetWindowText(CString(s.str().c_str()));
}
}
void
CHelloClientDlg::OnSayHello()
{
Demo::HelloPrx hello = createProxy();
if(!hello)
{
return;
}
int delay = _delay->GetPos() * 100;
try
{
if(!deliveryModeIsBatch())
{
_status->SetWindowText(CString(" Sending request"));
hello->begin_sayHello(delay, _sayHelloCallback);
}
else
{
_flush->EnableWindow(TRUE);
hello->sayHello(delay);
_status->SetWindowText(CString(" Queued sayHello request"));
}
}
catch(const IceUtil::Exception& ex)
{
handleException(ex);
}
}
void
CHelloClientDlg::OnShutdown()
{
Demo::HelloPrx hello = createProxy();
try
{
if(!deliveryModeIsBatch())
{
_status->SetWindowText(CString(" Sending request"));
CallbackPtr cb = new Callback(this);
hello->begin_shutdown(_shutdownCallback);
}
else
{
_flush->EnableWindow(TRUE);
hello->shutdown();
_status->SetWindowText(CString(" Queued shutdown request"));
}
}
catch(const IceUtil::Exception& ex)
{
handleException(ex);
}
}
void
CHelloClientDlg::OnFlush()
{
try
{
_communicator->begin_flushBatchRequests(_flushCallback);
}
catch(const IceUtil::Exception& ex)
{
handleException(ex);
}
_flush->EnableWindow(FALSE);
}
LRESULT
CHelloClientDlg::OnAMICallback(WPARAM, LPARAM lParam)
{
try
{
Ice::DispatcherCallPtr* call = reinterpret_cast<Ice::DispatcherCallPtr*>(lParam);
(*call)->run();
delete call;
}
catch(const Ice::Exception& ex)
{
handleException(ex);
}
return 0;
}
void
CHelloClientDlg::sent()
{
int mode = _mode->GetCurSel();
if(mode == TWOWAY || mode == TWOWAY_SECURE)
{
_status->SetWindowText(CString(" Waiting for response"));
}
else
{
_status->SetWindowText(CString(" Ready"));
}
}
void
CHelloClientDlg::response()
{
_status->SetWindowText(CString(" Ready"));
}
void
CHelloClientDlg::exception(const Ice::Exception& ex)
{
if(!dynamic_cast<const Ice::CommunicatorDestroyedException*>(&ex))
{
handleException(ex);
}
}
void
CHelloClientDlg::flushed()
{
_status->SetWindowText(CString(" Flushed batch requests"));
}
Demo::HelloPrx
CHelloClientDlg::createProxy()
{
CString h;
_host->GetWindowText(h);
string host = (LPCTSTR)h;
if(host.size() == 0)
{
_status->SetWindowText(CString(" No hostname"));
}
string s = "hello:tcp -h " + host + " -p 10000:ssl -h " + host + " -p 10001:udp -h " + host + " -p 10000";
Ice::ObjectPrx prx = _communicator->stringToProxy(s);
int mode = _mode->GetCurSel();
switch(mode)
{
case TWOWAY:
prx = prx->ice_twoway();
break;
case TWOWAY_SECURE:
prx = prx->ice_twoway()->ice_secure(true);;
break;
case ONEWAY:
prx = prx->ice_oneway();
break;
case ONEWAY_SECURE:
prx = prx->ice_oneway()->ice_secure(true);
break;
case ONEWAY_BATCH:
prx = prx->ice_batchOneway();
break;
case ONEWAY_SECURE_BATCH:
prx = prx->ice_batchOneway()->ice_secure(true);
break;
case DATAGRAM:
prx = prx->ice_datagram();
break;
case DATAGRAM_BATCH:
prx = prx->ice_batchDatagram();
break;
}
int timeout = _timeout->GetPos() * 100;
if(timeout != 0)
{
prx = prx->ice_timeout(timeout);
}
return Demo::HelloPrx::uncheckedCast(prx);
}
BOOL
CHelloClientDlg::deliveryModeIsBatch()
{
return _mode->GetCurSel() == ONEWAY_BATCH ||
_mode->GetCurSel() == ONEWAY_SECURE_BATCH ||
_mode->GetCurSel() == DATAGRAM_BATCH;
}
void
CHelloClientDlg::handleException(const IceUtil::Exception& ex)
{
_status->SetWindowText(CString(ex.ice_name().c_str()));
}
|