diff options
Diffstat (limited to 'cpp/demo/Ice/MFC/client/HelloClientDlg.cpp')
-rw-r--r-- | cpp/demo/Ice/MFC/client/HelloClientDlg.cpp | 255 |
1 files changed, 230 insertions, 25 deletions
diff --git a/cpp/demo/Ice/MFC/client/HelloClientDlg.cpp b/cpp/demo/Ice/MFC/client/HelloClientDlg.cpp index 8677ccc2393..2531ffbd229 100644 --- a/cpp/demo/Ice/MFC/client/HelloClientDlg.cpp +++ b/cpp/demo/Ice/MFC/client/HelloClientDlg.cpp @@ -15,9 +15,101 @@ #define new DEBUG_NEW #endif +#define WM_AMI_EXCEPTION (WM_USER + 1) +#define WM_AMI_SAY_HELLO_RESPONSE (WM_USER + 2) +#define WM_AMI_SAY_HELLO_SENT (WM_USER + 3) +#define WM_AMI_FLUSH_BATCH_REQUESTS_SENT (WM_USER + 4) +#define WM_AMI_SHUTDOWN_SENT (WM_USER + 5) + using namespace std; using namespace Demo; +class SayHelloCB : public AMI_Hello_sayHello, public Ice::AMISentCallback +{ +public: + + SayHelloCB(CHelloClientDlg* dialog) : _dialog(dialog) + { + } + + virtual void + ice_sent() + { + _dialog->PostMessage(WM_AMI_SAY_HELLO_SENT, 0, 0); + } + + virtual void + ice_response() + { + _dialog->PostMessage(WM_AMI_SAY_HELLO_RESPONSE, 0, 0); + } + + virtual void + ice_exception(const Ice::Exception& ex) + { + _dialog->PostMessage(WM_AMI_EXCEPTION, 0, reinterpret_cast<LONG>(ex.ice_clone())); + } + +private: + + CHelloClientDlg* _dialog; +}; + +class FlushBatchRequestsCB : public Ice::AMI_Object_ice_flushBatchRequests, public Ice::AMISentCallback +{ +public: + + FlushBatchRequestsCB(CHelloClientDlg* dialog) : _dialog(dialog) + { + } + + virtual void + ice_sent() + { + _dialog->PostMessage(WM_AMI_FLUSH_BATCH_REQUESTS_SENT, 0, 0); + } + + virtual void + ice_exception(const Ice::Exception& ex) + { + _dialog->PostMessage(WM_AMI_EXCEPTION, 0, reinterpret_cast<LONG>(ex.ice_clone())); + } + +private: + + CHelloClientDlg* _dialog; +}; + +class ShutdownCB : public AMI_Hello_shutdown, public Ice::AMISentCallback +{ +public: + + ShutdownCB(CHelloClientDlg* dialog) : _dialog(dialog) + { + } + + virtual void + ice_sent() + { + _dialog->PostMessage(WM_AMI_SHUTDOWN_SENT, 0, 0); + } + + virtual void + ice_response() + { + } + + virtual void + ice_exception(const Ice::Exception& ex) + { + _dialog->PostMessage(WM_AMI_EXCEPTION, 0, reinterpret_cast<LONG>(ex.ice_clone())); + } + +private: + + CHelloClientDlg* _dialog; +}; + CHelloClientDlg::CHelloClientDlg(const Ice::CommunicatorPtr& communicator, CWnd* pParent /*=NULL*/) : CDialog(CHelloClientDlg::IDD, pParent), _communicator(communicator), _currentMode(0), _useSecure(false), _useTimeout(false) @@ -38,6 +130,11 @@ BEGIN_MESSAGE_MAP(CHelloClientDlg, CDialog) ON_BN_CLICKED(IDC_INVOKE, OnSayHello) ON_BN_CLICKED(IDC_FLUSH, OnFlush) ON_BN_CLICKED(IDC_SHUTDOWN, OnShutdown) + ON_MESSAGE(WM_AMI_EXCEPTION, OnAMIException) + ON_MESSAGE(WM_AMI_SAY_HELLO_RESPONSE, OnAMISayHelloResponse) + ON_MESSAGE(WM_AMI_SAY_HELLO_SENT, OnAMISayHelloSent) + ON_MESSAGE(WM_AMI_FLUSH_BATCH_REQUESTS_SENT, OnAMIFlushBatchRequestsSent) + ON_MESSAGE(WM_AMI_SHUTDOWN_SENT, OnAMIShutdownSent) END_MESSAGE_MAP() BOOL @@ -53,6 +150,7 @@ CHelloClientDlg::OnInitDialog() // // Retrieve the controls. // + _host = (CEdit*)GetDlgItem(IDC_HOST); _mode = (CComboBox*)GetDlgItem(IDC_MODE); _secure = (CButton*)GetDlgItem(IDC_SECURE); _timeout = (CButton*)GetDlgItem(IDC_TIMEOUT); @@ -67,14 +165,29 @@ CHelloClientDlg::OnInitDialog() // // Create the proxy. // - Ice::ObjectPrx obj = _communicator->propertyToProxy("Hello.Proxy"); - _proxy = HelloPrx::uncheckedCast(obj); - _currentProxy = _proxy; + updateProxy(); + _host->SetWindowText(CString(_hostname.c_str())); _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. @@ -119,14 +232,24 @@ CHelloClientDlg::OnSayHello() try { updateProxy(); - _currentProxy->sayHello(_delay->GetCheck() == BST_CHECKED ? 2500 : 0); - if(_currentProxy->ice_isBatchOneway() || _currentProxy->ice_isBatchDatagram()) + if(!_currentProxy->ice_isBatchOneway() && !_currentProxy->ice_isBatchDatagram()) { - _status->SetWindowText(CString(" Queued batch request")); + if(_currentProxy->sayHello_async(new SayHelloCB(this), _delay->GetCheck() == BST_CHECKED ? 2500 : 0)) + { + if(_currentProxy->ice_isTwoway()) + { + _status->SetWindowText(CString(" Waiting for response")); + } + } + else + { + _status->SetWindowText(CString(" Sending request")); + } } else { - _status->SetWindowText(CString(" Sent request")); + _currentProxy->sayHello(_delay->GetCheck() == BST_CHECKED ? 2500 : 0); + _status->SetWindowText(CString(" Queued batch request")); } } catch(const IceUtil::Exception& ex) @@ -140,8 +263,15 @@ CHelloClientDlg::OnFlush() { try { - _communicator->flushBatchRequests(); - _status->SetWindowText(CString(" Flushed batch requests")); + updateProxy(); + if(_currentProxy->ice_flushBatchRequests_async(new FlushBatchRequestsCB(this))) + { + _status->SetWindowText(CString(" Flushed batch requests")); + } + else + { + _status->SetWindowText(CString(" Flushing batch requests")); + } } catch(const IceUtil::Exception& ex) { @@ -155,14 +285,21 @@ CHelloClientDlg::OnShutdown() try { updateProxy(); - _currentProxy->shutdown(); - if(_currentProxy->ice_isBatchOneway() || _currentProxy->ice_isBatchDatagram()) + if(!_currentProxy->ice_isBatchOneway() && !_currentProxy->ice_isBatchDatagram()) { - _status->SetWindowText(CString(" Queued shutdown request")); + if(_currentProxy->shutdown_async(new ShutdownCB(this))) + { + _status->SetWindowText(CString(" Sent shutdown request")); + } + else + { + _status->SetWindowText(CString(" Sending shutdown request")); + } } else { - _status->SetWindowText(CString(" Sent shutdown request")); + _currentProxy->shutdown(); + _status->SetWindowText(CString(" Queued shutdown request")); } } catch(const IceUtil::Exception& ex) @@ -171,6 +308,53 @@ CHelloClientDlg::OnShutdown() } } +LRESULT +CHelloClientDlg::OnAMIException(WPARAM, LPARAM lParam) +{ + Ice::Exception* ex = reinterpret_cast<Ice::Exception*>(lParam); + if(!dynamic_cast<Ice::CommunicatorDestroyedException*>(ex)) + { + handleException(*ex); + } + delete ex; + return 0; +} + +LRESULT +CHelloClientDlg::OnAMISayHelloSent(WPARAM, LPARAM) +{ + if(_currentProxy->ice_isTwoway()) + { + _status->SetWindowText(CString(" Waiting for response")); + } + else + { + _status->SetWindowText(CString(" Ready")); + } + return 0; +} + +LRESULT +CHelloClientDlg::OnAMISayHelloResponse(WPARAM, LPARAM) +{ + _status->SetWindowText(CString(" Ready")); + return 0; +} + +LRESULT +CHelloClientDlg::OnAMIFlushBatchRequestsSent(WPARAM, LPARAM) +{ + _status->SetWindowText(CString(" Flushed batch requests")); + return 0; +} + +LRESULT +CHelloClientDlg::OnAMIShutdownSent(WPARAM, LPARAM) +{ + _status->SetWindowText(CString(" Sent shutdown request")); + return 0; +} + void CHelloClientDlg::updateProxy() { @@ -178,12 +362,41 @@ CHelloClientDlg::updateProxy() bool secure = _secure->GetCheck() == BST_CHECKED; bool timeout = _timeout->GetCheck() == BST_CHECKED; - if(mode == _currentMode && secure == _useSecure && timeout == _useTimeout) + CString h; + _host->GetWindowText(h); + string hostname = (LPCTSTR)h; + + if(_currentProxy && + hostname == _hostname && + mode == _currentMode && + secure == _useSecure && + timeout == _useTimeout) { return; } - Ice::ObjectPrx proxy; + if(!_proxy) + { + _proxy = HelloPrx::uncheckedCast(_communicator->stringToProxy("hello:tcp -p 10000:udp -p 10000:ssl -p 10001")); + _hostname = "localhost"; + } + else if(hostname != _hostname) + { + try + { + _proxy = HelloPrx::uncheckedCast(_communicator->stringToProxy(string("hello") + + ":tcp -p 10000 -h " + hostname + + ":udp -p 10000 -h " + hostname + + ":ssl -p 10001 -h " + hostname)); + _hostname = hostname; + } + catch(const Ice::EndpointParseException&) + { + AfxMessageBox(CString("The provided hostname is invalid."), MB_OK|MB_ICONEXCLAMATION); + } + } + + Ice::ObjectPrx proxy = _proxy; switch(mode) { case 0: @@ -214,14 +427,7 @@ CHelloClientDlg::updateProxy() proxy = proxy->ice_timeout(-1); } - if(proxy->ice_isTwoway()) - { - _currentProxy = HelloPrx::checkedCast(proxy); - } - else - { - _currentProxy = HelloPrx::uncheckedCast(proxy); - } + _currentProxy = HelloPrx::uncheckedCast(proxy); _currentMode = mode; _useSecure = secure; _useTimeout = timeout; @@ -236,8 +442,7 @@ CHelloClientDlg::handleException(const IceUtil::Exception& e) } catch(const Ice::NoEndpointException&) { - AfxMessageBox(CString("The proxy does not support the current configuration"), - MB_OK|MB_ICONEXCLAMATION); + AfxMessageBox(CString("The proxy does not support the current configuration"), MB_OK|MB_ICONEXCLAMATION); } catch(const IceUtil::Exception& ex) { |