diff options
author | Matthew Newhook <matthew@zeroc.com> | 2006-12-22 18:52:53 +0000 |
---|---|---|
committer | Matthew Newhook <matthew@zeroc.com> | 2006-12-22 18:52:53 +0000 |
commit | 84d9f5c369fceccfa16401ed50783ed2ad209559 (patch) | |
tree | 689d30c8707629f1db8155edadb47ce1bce363ad /cpp/demo/Glacier2/chat/Client.cpp | |
parent | Added autoflushing of batches (diff) | |
download | ice-84d9f5c369fceccfa16401ed50783ed2ad209559.tar.bz2 ice-84d9f5c369fceccfa16401ed50783ed2ad209559.tar.xz ice-84d9f5c369fceccfa16401ed50783ed2ad209559.zip |
http://bugzilla.zeroc.com/bugzilla/show_bug.cgi?id=1391.
Diffstat (limited to 'cpp/demo/Glacier2/chat/Client.cpp')
-rwxr-xr-x | cpp/demo/Glacier2/chat/Client.cpp | 97 |
1 files changed, 70 insertions, 27 deletions
diff --git a/cpp/demo/Glacier2/chat/Client.cpp b/cpp/demo/Glacier2/chat/Client.cpp index 59cedc50e6b..5c4c8dccb05 100755 --- a/cpp/demo/Glacier2/chat/Client.cpp +++ b/cpp/demo/Glacier2/chat/Client.cpp @@ -93,16 +93,23 @@ public: virtual int run(int argc, char* argv[]) { - Ice::RouterPrx defaultRouter = communicator()->getDefaultRouter(); - if(!defaultRouter) - { - cerr << argv[0] << ": no default router set" << endl; - return EXIT_FAILURE; - } + // + // Since this is an interactive demo we want the custom interrupt + // callback to be called when the process is interrupted. + // + userCallbackOnInterrupt(); - Glacier2::RouterPrx router = Glacier2::RouterPrx::checkedCast(defaultRouter); { - if(!router) + IceUtil::Mutex::Lock sync(_mutex); + Ice::RouterPrx defaultRouter = communicator()->getDefaultRouter(); + if(!defaultRouter) + { + cerr << argv[0] << ": no default router set" << endl; + return EXIT_FAILURE; + } + + _router = Glacier2::RouterPrx::checkedCast(defaultRouter); + if(!_router) { cerr << argv[0] << ": configured router is not a Glacier2 router" << endl; return EXIT_FAILURE; @@ -126,7 +133,7 @@ public: try { - session = ChatSessionPrx::uncheckedCast(router->createSession(id, pw)); + session = ChatSessionPrx::uncheckedCast(_router->createSession(id, pw)); break; } catch(const Glacier2::PermissionDeniedException& ex) @@ -135,12 +142,15 @@ public: } } - SessionPingThreadPtr ping = new SessionPingThread(session, (long)router->getSessionTimeout() / 2); - ping->start(); + { + IceUtil::Mutex::Lock sync(_mutex); + _ping = new SessionPingThread(session, (long)_router->getSessionTimeout() / 2); + _ping->start(); + } Ice::Identity callbackReceiverIdent; callbackReceiverIdent.name = "callbackReceiver"; - callbackReceiverIdent.category = router->getCategoryForClient(); + callbackReceiverIdent.category = _router->getCategoryForClient(); Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Chat.Client"); ChatCallbackPrx callback = ChatCallbackPrx::uncheckedCast( @@ -177,9 +187,47 @@ public: } while(cin.good()); + cleanup(); + } + catch(const Ice::Exception& ex) + { + cerr << ex << endl; + cleanup(); + + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + + virtual void + interruptCallback(int) + { + try + { + communicator()->destroy(); + } + catch(const IceUtil::Exception& ex) + { + cerr << appName() << ": " << ex << endl; + } + catch(...) + { + cerr << appName() << ": unknown exception" << endl; + } + exit(EXIT_SUCCESS); + } + +private: + + void + cleanup() + { + IceUtil::Mutex::Lock sync(_mutex); + if(_router) + { try { - router->destroySession(); + _router->destroySession(); } catch(const Ice::ConnectionLostException&) { @@ -187,25 +235,16 @@ public: // Expected: the router closed the connection. // } + _router = 0; } - catch(const Ice::Exception& ex) + if(_ping) { - cerr << ex << endl; - - ping->destroy(); - ping->getThreadControl().join(); - - return EXIT_FAILURE; + _ping->destroy(); + _ping->getThreadControl().join(); + _ping = 0; } - - ping->destroy(); - ping->getThreadControl().join(); - - return EXIT_SUCCESS; } -private: - void menu() { @@ -223,6 +262,10 @@ private: } return s; } + + IceUtil::Mutex _mutex; + Glacier2::RouterPrx _router; + SessionPingThreadPtr _ping; }; int |