diff options
Diffstat (limited to 'cpp/demo/IceGrid/allocate/Client.cpp')
-rw-r--r-- | cpp/demo/IceGrid/allocate/Client.cpp | 121 |
1 files changed, 85 insertions, 36 deletions
diff --git a/cpp/demo/IceGrid/allocate/Client.cpp b/cpp/demo/IceGrid/allocate/Client.cpp index b4df84f00c9..79b21842dfd 100644 --- a/cpp/demo/IceGrid/allocate/Client.cpp +++ b/cpp/demo/IceGrid/allocate/Client.cpp @@ -57,7 +57,7 @@ public: private: - IceGrid::SessionPrx _session; + const IceGrid::SessionPrx _session; const IceUtil::Time _timeout; bool _destroy; }; @@ -69,11 +69,17 @@ class HelloClient : public Ice::Application public: virtual int run(int, char*[]); + virtual void interruptCallback(int); private: + void cleanup(); void menu(); string trim(const string&); + + IceUtil::Mutex _mutex; + IceGrid::SessionPrx _session; + SessionKeepAliveThreadPtr _keepAlive; }; int @@ -83,43 +89,25 @@ main(int argc, char* argv[]) return app.main(argc, argv, "config.client"); } -void -HelloClient::menu() -{ - cout << - "usage:\n" - "t: send greeting\n" - "s: shutdown server\n" - "x: exit\n" - "?: help\n"; -} - -string -HelloClient::trim(const string& s) -{ - static const string delims = "\t\r\n "; - string::size_type last = s.find_last_not_of(delims); - if(last != string::npos) - { - return s.substr(s.find_first_not_of(delims), last+1); - } - return s; -} - int HelloClient::run(int argc, char* argv[]) { int status = EXIT_SUCCESS; - IceGrid::RegistryPrx registry = - IceGrid::RegistryPrx::checkedCast(communicator()->stringToProxy("DemoIceGrid/Registry")); + // + // Since this is an interactive demo we want the custom interrupt + // callback to be called when the process is interrupted. + // + userCallbackOnInterrupt(); + + IceGrid::RegistryPrx registry = IceGrid::RegistryPrx::checkedCast( + communicator()->stringToProxy("DemoIceGrid/Registry")); if(!registry) { cerr << argv[0] << ": could not contact registry" << endl; return EXIT_FAILURE; } - IceGrid::SessionPrx session; while(true) { cout << "This demo accepts any user-id / password combination.\n"; @@ -136,7 +124,8 @@ HelloClient::run(int argc, char* argv[]) try { - session = registry->createSession(id, password); + IceUtil::Mutex::Lock sync(_mutex); + _session = registry->createSession(id, password); break; } catch(const IceGrid::PermissionDeniedException& ex) @@ -145,8 +134,11 @@ HelloClient::run(int argc, char* argv[]) } } - SessionKeepAliveThreadPtr keepAlive = new SessionKeepAliveThread(session, registry->getSessionTimeout() / 2); - keepAlive->start(); + { + IceUtil::Mutex::Lock sync(_mutex); + _keepAlive = new SessionKeepAliveThread(_session, registry->getSessionTimeout() / 2); + _keepAlive->start(); + } try { @@ -159,11 +151,11 @@ HelloClient::run(int argc, char* argv[]) HelloPrx hello; try { - hello = HelloPrx::checkedCast(session->allocateObjectById(communicator()->stringToIdentity("hello"))); + hello = HelloPrx::checkedCast(_session->allocateObjectById(communicator()->stringToIdentity("hello"))); } catch(const IceGrid::ObjectNotRegisteredException&) { - hello = HelloPrx::checkedCast(session->allocateObjectByType("::Demo::Hello")); + hello = HelloPrx::checkedCast(_session->allocateObjectByType("::Demo::Hello")); } menu(); @@ -215,14 +207,71 @@ HelloClient::run(int argc, char* argv[]) status = EXIT_FAILURE; } + cleanup(); + return status; +} + +void +HelloClient::interruptCallback(int) +{ + cleanup(); + + try + { + communicator()->destroy(); + } + catch(const IceUtil::Exception& ex) + { + cerr << appName() << ": " << ex << endl; + } + catch(...) + { + cerr << appName() << ": unknown exception" << endl; + } + exit(EXIT_SUCCESS); +} + +void +HelloClient::cleanup() +{ + IceUtil::Mutex::Lock sync(_mutex); // // Destroy the keepAlive thread and the sesion object otherwise // the session will be kept allocated until the timeout occurs. // Destroying the session will release all allocated objects. // - keepAlive->destroy(); - keepAlive->getThreadControl().join(); - session->destroy(); + if(_keepAlive) + { + _keepAlive->destroy(); + _keepAlive->getThreadControl().join(); + _keepAlive = 0; + } + if(_session) + { + _session->destroy(); + _session = 0; + } +} - return status; +void +HelloClient::menu() +{ + cout << + "usage:\n" + "t: send greeting\n" + "s: shutdown server\n" + "x: exit\n" + "?: help\n"; +} + +string +HelloClient::trim(const string& s) +{ + static const string delims = "\t\r\n "; + string::size_type last = s.find_last_not_of(delims); + if(last != string::npos) + { + return s.substr(s.find_first_not_of(delims), last+1); + } + return s; } |