summaryrefslogtreecommitdiff
path: root/cpp/demo/IceGrid/sessionActivation/Client.cpp
diff options
context:
space:
mode:
authorDwayne Boone <dwayne@zeroc.com>2006-05-17 17:02:07 +0000
committerDwayne Boone <dwayne@zeroc.com>2006-05-17 17:02:07 +0000
commit6042e7d2327ecd847bdbf2dd505d8137934de129 (patch)
tree0ddbce1e4ab8fe9b28c51c211ae5970e80423f72 /cpp/demo/IceGrid/sessionActivation/Client.cpp
parentFix allocation bug (diff)
downloadice-6042e7d2327ecd847bdbf2dd505d8137934de129.tar.bz2
ice-6042e7d2327ecd847bdbf2dd505d8137934de129.tar.xz
ice-6042e7d2327ecd847bdbf2dd505d8137934de129.zip
Added IceGrid session demo
Diffstat (limited to 'cpp/demo/IceGrid/sessionActivation/Client.cpp')
-rw-r--r--cpp/demo/IceGrid/sessionActivation/Client.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/cpp/demo/IceGrid/sessionActivation/Client.cpp b/cpp/demo/IceGrid/sessionActivation/Client.cpp
new file mode 100644
index 00000000000..7086b7cb90f
--- /dev/null
+++ b/cpp/demo/IceGrid/sessionActivation/Client.cpp
@@ -0,0 +1,191 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2006 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 <Ice/Ice.h>
+#include <IceGrid/Session.h>
+#include <Hello.h>
+
+using namespace std;
+using namespace Demo;
+
+class SessionKeepAliveThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
+{
+public:
+
+ SessionKeepAliveThread(const IceGrid::SessionPrx& session) :
+ _session(session),
+ _timeout(IceUtil::Time::seconds(5)),
+ _destroy(false)
+ {
+ }
+
+ virtual void
+ run()
+ {
+ Lock sync(*this);
+ while(!_destroy)
+ {
+ timedWait(_timeout);
+ if(_destroy)
+ {
+ break;
+ }
+ try
+ {
+ _session->keepAlive();
+ }
+ catch(const Ice::Exception&)
+ {
+ break;
+ }
+ }
+ }
+
+ void
+ destroy()
+ {
+ Lock sync(*this);
+ _destroy = true;
+ notify();
+ }
+
+private:
+
+ IceGrid::SessionPrx _session;
+ const IceUtil::Time _timeout;
+ bool _destroy;
+};
+
+typedef IceUtil::Handle<SessionKeepAliveThread> SessionKeepAliveThreadPtr;
+
+class HelloClient : public Ice::Application
+{
+public:
+
+ virtual int run(int, char*[]);
+
+private:
+
+ void menu();
+ string trim(const string&);
+};
+
+int
+main(int argc, char* argv[])
+{
+ HelloClient app;
+ return app.main(argc, argv, "config.client");
+}
+
+void
+HelloClient::menu()
+{
+ cout <<
+ "usage:\n"
+ "t: send greeting\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[])
+{
+ IceGrid::SessionManagerPrx sessionManager =
+ IceGrid::SessionManagerPrx::checkedCast(communicator()->stringToProxy("DemoIceGrid/SessionManager"));
+ if(!sessionManager)
+ {
+ cerr << argv[0] << ": cound not contact session manager" << endl;
+ return EXIT_FAILURE;
+ }
+
+ string id;
+ cout << "user id: " << flush;
+ getline(cin, id);
+ id = trim(id);
+
+ IceGrid::SessionPrx session = sessionManager->createLocalSession(id);
+
+ SessionKeepAliveThreadPtr keepAlive = new SessionKeepAliveThread(session);
+ keepAlive->start();
+
+ HelloPrx hello = HelloPrx::uncheckedCast(communicator()->stringToProxy("hello"));;
+ try
+ {
+ session->allocateObjectById(hello->ice_getIdentity());
+ }
+ catch(const IceGrid::AllocationException& ex)
+ {
+ cerr << argv[0] << ": could not allocate object: " << ex.reason << endl;
+ return EXIT_FAILURE;
+ }
+ catch(const IceGrid::ObjectNotRegisteredException&)
+ {
+ cerr << argv[0] << ": object not registered with registry" << endl;
+ return EXIT_FAILURE;
+ }
+
+ menu();
+
+ char c;
+ do
+ {
+ try
+ {
+ cout << "==> ";
+ cin >> c;
+ if(c == 't')
+ {
+ hello->sayHello();
+ }
+ else if(c == 'x')
+ {
+ // Nothing to do
+ }
+ else if(c == '?')
+ {
+ menu();
+ }
+ else
+ {
+ cout << "unknown command `" << c << "'" << endl;
+ menu();
+ }
+ }
+ catch(const Ice::Exception& ex)
+ {
+ cerr << ex << endl;
+
+ keepAlive->destroy();
+ keepAlive->getThreadControl().join();
+
+ return EXIT_FAILURE;
+ }
+ }
+ while(cin.good() && c != 'x');
+
+ keepAlive->destroy();
+ keepAlive->getThreadControl().join();
+
+ session->releaseObject(hello->ice_getIdentity());
+
+ return EXIT_SUCCESS;
+}
+