summaryrefslogtreecommitdiff
path: root/cpp/demo
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2005-04-14 01:29:28 +0000
committerMatthew Newhook <matthew@zeroc.com>2005-04-14 01:29:28 +0000
commit7316f0f15f5d3019d5af909438adec113cd7cf87 (patch)
tree6406baa1a19d795e70aba4d9f936d7b3e43e085b /cpp/demo
parentfixes (diff)
downloadice-7316f0f15f5d3019d5af909438adec113cd7cf87.tar.bz2
ice-7316f0f15f5d3019d5af909438adec113cd7cf87.tar.xz
ice-7316f0f15f5d3019d5af909438adec113cd7cf87.zip
demo refactor/redesign.
Diffstat (limited to 'cpp/demo')
-rwxr-xr-xcpp/demo/Ice/session/Client.cpp76
-rw-r--r--cpp/demo/Ice/session/HelloSession.ice25
-rw-r--r--cpp/demo/Ice/session/HelloSessionI.cpp55
-rwxr-xr-xcpp/demo/Ice/session/HelloSessionI.h33
-rwxr-xr-xcpp/demo/Ice/session/HelloSessionManagerI.cpp28
-rwxr-xr-xcpp/demo/Ice/session/HelloSessionManagerI.h24
-rw-r--r--cpp/demo/Ice/session/Server.cpp10
-rw-r--r--cpp/demo/Ice/session/Session.ice53
-rwxr-xr-xcpp/demo/Ice/session/SessionFactoryI.cpp131
-rwxr-xr-xcpp/demo/Ice/session/SessionFactoryI.h60
-rwxr-xr-xcpp/demo/Ice/session/SessionI.cpp95
-rwxr-xr-xcpp/demo/Ice/session/SessionI.h41
-rwxr-xr-xcpp/demo/Ice/session/SessionManagerI.cpp190
-rwxr-xr-xcpp/demo/Ice/session/SessionManagerI.h71
-rw-r--r--cpp/demo/Ice/session/config8
-rwxr-xr-xcpp/demo/Ice/session/sessionC.dsp45
-rw-r--r--cpp/demo/Ice/session/sessionS.dsp67
17 files changed, 403 insertions, 609 deletions
diff --git a/cpp/demo/Ice/session/Client.cpp b/cpp/demo/Ice/session/Client.cpp
index 3283d55c1f7..eb992793aef 100755
--- a/cpp/demo/Ice/session/Client.cpp
+++ b/cpp/demo/Ice/session/Client.cpp
@@ -9,24 +9,20 @@
#include <Ice/Ice.h>
#include <IceUtil/Thread.h>
-#include <HelloSession.h>
+#include <Session.h>
using namespace std;
using namespace Demo;
-//
-// This thread pings the session object with the given timeout frequency.
-//
class SessionRefreshThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
{
public:
- SessionRefreshThread(const Ice::LoggerPtr& logger, const SessionPrx& session,
- const IceUtil::Time& timeout) :
+ SessionRefreshThread(const Ice::LoggerPtr& logger, const SessionPrx& session) :
_logger(logger),
_session(session),
_destroy(false),
- _timeout(timeout)
+ _timeout(IceUtil::Time::seconds(5))
{
}
@@ -41,9 +37,6 @@ public:
{
break;
}
- //
- // If the refresh fails we're done.
- //
try
{
_session->refresh();
@@ -68,7 +61,7 @@ public:
private:
const Ice::LoggerPtr _logger;
- const SessionPrx _session;
+ SessionPrx _session;
bool _destroy;
const IceUtil::Time _timeout;
};
@@ -79,17 +72,18 @@ menu()
{
cout <<
"usage:\n"
- "h: send greeting\n"
- "s: shutdown server\n"
- "x: exit\n"
- "?: help\n";
+ "c: create new hello\n"
+ "0-9: greeting identified hello object\n"
+ "s: shutdown server\n"
+ "x: exit\n"
+ "?: help\n";
}
int
run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
{
Ice::PropertiesPtr properties = communicator->getProperties();
- const char* proxyProperty = "SessionManager.Proxy";
+ const char* proxyProperty = "SessionFactory.Proxy";
string proxy = properties->getProperty(proxyProperty);
if(proxy.empty())
{
@@ -97,31 +91,26 @@ run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
return EXIT_FAILURE;
}
- //
- // Get the session manager object, and create a session.
- //
Ice::ObjectPrx base = communicator->stringToProxy(proxy);
- SessionManagerPrx manager = SessionManagerPrx::checkedCast(base);
- if(!manager)
+ SessionFactoryPrx factory = SessionFactoryPrx::checkedCast(base);
+ if(!factory)
{
cerr << argv[0] << ": invalid proxy" << endl;
return EXIT_FAILURE;
}
- HelloSessionPrx hello = HelloSessionPrx::uncheckedCast(manager->create());
- if(!hello)
+ SessionPrx session = factory->create();
+ if(!session)
{
cerr << argv[0] << ": invalid proxy" << endl;
return EXIT_FAILURE;
}
- //
- // Create a thread to ping the object at regular intervals.
- //
- SessionRefreshThreadPtr refresh = new SessionRefreshThread(
- communicator->getLogger(), hello, IceUtil::Time::seconds(5));
+ SessionRefreshThreadPtr refresh = new SessionRefreshThread(communicator->getLogger(), session);
refresh->start();
+ vector<HelloPrx> hellos;
+
menu();
try
@@ -131,13 +120,28 @@ run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
cout << "==> ";
char c;
cin >> c;
- if(c == 'h')
+ if(isdigit(c))
{
- hello->sayHello();
+ string s;
+ s += c;
+ vector<HelloPrx>::size_type index = atoi(s.c_str());
+ if(index < hellos.size())
+ {
+ hellos[index]->sayHello();
+ }
+ else
+ {
+ cout << "index is too high. " << hellos.size() << " exist so far." << endl;
+ }
+ }
+ else if(c == 'c')
+ {
+ hellos.push_back(session->createHello());
+ cout << "created hello object " << hellos.size()-1 << endl;
}
else if(c == 's')
{
- manager->shutdown();
+ factory->shutdown();
}
else if(c == 'x')
{
@@ -154,21 +158,13 @@ run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
}
}
while(cin.good());
-
- //
- // Destroy the session before we finish.
- //
- hello->destroy();
+ session->destroy();
}
catch(const Ice::Exception& ex)
{
cerr << ex << endl;
}
- //
- // Destroy the ping thread, and join with it to ensure that it
- // actually completes.
- //
refresh->destroy();
refresh->getThreadControl().join();
diff --git a/cpp/demo/Ice/session/HelloSession.ice b/cpp/demo/Ice/session/HelloSession.ice
deleted file mode 100644
index af3768539dc..00000000000
--- a/cpp/demo/Ice/session/HelloSession.ice
+++ /dev/null
@@ -1,25 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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.
-//
-// **********************************************************************
-
-#ifndef HELLO_SESSION_ICE
-#define HELLO_SESSION_ICE
-
-#include <Session.ice>
-
-module Demo
-{
-
-interface HelloSession extends Session
-{
- nonmutating void sayHello();
-};
-
-};
-
-#endif
diff --git a/cpp/demo/Ice/session/HelloSessionI.cpp b/cpp/demo/Ice/session/HelloSessionI.cpp
deleted file mode 100644
index 2f8dbedeec2..00000000000
--- a/cpp/demo/Ice/session/HelloSessionI.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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 <HelloSessionI.h>
-
-using namespace std;
-
-HelloSessionI::HelloSessionI(const SessionManagerIPtr& manager) :
- _manager(manager)
-{
-}
-
-HelloSessionI::~HelloSessionI()
-{
-}
-
-void
-HelloSessionI::sayHello(const Ice::Current&) const
-{
- cout << "Hello World!" << endl;
-}
-
-//
-// Destroy all session specific state.
-//
-void
-HelloSessionI::destroyed(const Ice::Current& c)
-{
- c.adapter->remove(c.id);
-}
-
-//
-// This method is called by the client to destroy a session. All
-// it should do is call remove on the session manager. All user
-// specific cleanup should go in the destroyed() callback.
-//
-void
-HelloSessionI::destroy(const Ice::Current& c)
-{
- _manager->remove(c.id);
-}
-
-void
-HelloSessionI::refresh(const Ice::Current& c)
-{
- _manager->refresh(c.id);
-}
diff --git a/cpp/demo/Ice/session/HelloSessionI.h b/cpp/demo/Ice/session/HelloSessionI.h
deleted file mode 100755
index 7d8ae3fffb0..00000000000
--- a/cpp/demo/Ice/session/HelloSessionI.h
+++ /dev/null
@@ -1,33 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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.
-//
-// **********************************************************************
-
-#ifndef HELLO_SESSION_I_H
-#define HELLO_SESSION_I_H
-
-#include <HelloSession.h>
-#include <SessionManagerI.h>
-
-class HelloSessionI : public ::Demo::HelloSession
-{
-public:
-
- HelloSessionI(const SessionManagerIPtr&);
- ~HelloSessionI();
-
- virtual void sayHello(const Ice::Current&) const;
- virtual void destroyed(const Ice::Current&);
- virtual void destroy(const Ice::Current&);
- virtual void refresh(const Ice::Current&);
-
-private:
-
- const SessionManagerIPtr _manager;
-};
-
-#endif
diff --git a/cpp/demo/Ice/session/HelloSessionManagerI.cpp b/cpp/demo/Ice/session/HelloSessionManagerI.cpp
deleted file mode 100755
index 2cd5f68a05c..00000000000
--- a/cpp/demo/Ice/session/HelloSessionManagerI.cpp
+++ /dev/null
@@ -1,28 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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 <HelloSessionManagerI.h>
-#include <HelloSessionI.h>
-
-using namespace std;
-using namespace Demo;
-
-HelloSessionManagerI::HelloSessionManagerI(const Ice::CommunicatorPtr& communicator) :
- SessionManagerI(communicator)
-{
-}
-
-SessionPrx
-HelloSessionManagerI::create(const Ice::Current& c)
-{
- SessionPrx session = SessionPrx::uncheckedCast(c.adapter->addWithUUID(new HelloSessionI(this)));
- add(session);
- return session;
-}
diff --git a/cpp/demo/Ice/session/HelloSessionManagerI.h b/cpp/demo/Ice/session/HelloSessionManagerI.h
deleted file mode 100755
index dda0f8adcd0..00000000000
--- a/cpp/demo/Ice/session/HelloSessionManagerI.h
+++ /dev/null
@@ -1,24 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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.
-//
-// **********************************************************************
-
-#ifndef HELLO_SESSION_MANAGER_I_H
-#define HELLO_SESSION_MANAGER_I_H
-
-#include <SessionManagerI.h>
-
-class HelloSessionManagerI : public SessionManagerI
-{
-public:
-
- HelloSessionManagerI(const Ice::CommunicatorPtr&);
-
- virtual ::Demo::SessionPrx create(const ::Ice::Current&);
-};
-
-#endif
diff --git a/cpp/demo/Ice/session/Server.cpp b/cpp/demo/Ice/session/Server.cpp
index 5047d977f35..48fbd2c3dc7 100644
--- a/cpp/demo/Ice/session/Server.cpp
+++ b/cpp/demo/Ice/session/Server.cpp
@@ -8,7 +8,7 @@
// **********************************************************************
#include <Ice/Ice.h>
-#include <HelloSessionManagerI.h>
+#include <SessionFactoryI.h>
using namespace std;
using namespace Demo;
@@ -16,12 +16,12 @@ using namespace Demo;
int
run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
{
- Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("SessionManager");
- SessionManagerIPtr manager = new HelloSessionManagerI(communicator);
- adapter->add(manager, Ice::stringToIdentity("SessionManager"));
+ Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("SessionFactory");
+ SessionFactoryIPtr factory = new SessionFactoryI(adapter);
+ adapter->add(factory, Ice::stringToIdentity("SessionFactory"));
adapter->activate();
communicator->waitForShutdown();
- manager->destroy();
+ factory->destroy();
return EXIT_SUCCESS;
}
diff --git a/cpp/demo/Ice/session/Session.ice b/cpp/demo/Ice/session/Session.ice
index 9dca4b31fe0..118b15ccd00 100644
--- a/cpp/demo/Ice/session/Session.ice
+++ b/cpp/demo/Ice/session/Session.ice
@@ -13,49 +13,44 @@
module Demo
{
-/** XXX Comment missing XXX **/
+/** Hello object. */
+interface Hello
+{
+ /** Send a greeting. */
+ nonmutating void sayHello();
+};
+/**
+ * The session object. This is used to create per-session objects on
+ * behalf of the client. If it is not refreshed on a periodic basis it
+ * will be automatically reclaimed by the session factory.
+ */
interface Session
{
/**
- *
- * Refresh this session. If a session is not refreshed on a
- * regular basis by the client it will be automatically destroyed.
- *
- **/
- void refresh();
+ * Create a new per-session hello object. The created object will
+ * be automatically destroyed when the session is destroyed.
+ */
+ Hello* createHello();
/**
- *
- * Callback that the session has been destroyed. XXX I don't understand this method. Who calls this? Isn't this server-side only? If so, it doesn't belong into the contract. XXX
- *
+ * Refresh a session. If a session is not refreshed on a regular
+ * basis by the client it will be automatically destroyed.
**/
- void destroyed();
+ void refresh();
- /**
- *
- * Destroy this session.
- *
- **/
+ /** Mark the session as destroyed. */
void destroy();
};
-/** XXX Comment missing. Why "SessionManager"? From the contract perspective, this is a SessionFactory. XXX **/
-interface SessionManager
+/** The SessionFactory. */
+interface SessionFactory
{
- /**
- *
- * Create a new session.
- *
- **/
+ /** Create a new session. */
Session* create();
- /**
- *
- * Shutdown the server.
- *
- **/
- void shutdown();
+ /** Shutdown the server. */
+ idempotent void shutdown();
};
};
diff --git a/cpp/demo/Ice/session/SessionFactoryI.cpp b/cpp/demo/Ice/session/SessionFactoryI.cpp
new file mode 100755
index 00000000000..eea03c46eec
--- /dev/null
+++ b/cpp/demo/Ice/session/SessionFactoryI.cpp
@@ -0,0 +1,131 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2005 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 <SessionFactoryI.h>
+
+using namespace std;
+using namespace Demo;
+
+ReapThread::ReapThread(const SessionFactoryIPtr& Factory, const IceUtil::Time& timeout) :
+ _destroy(false),
+ _timeout(timeout),
+ _factory(Factory)
+{
+}
+
+ReapThread::~ReapThread()
+{
+}
+
+void
+ReapThread::run()
+{
+ Lock sync(*this);
+ while(!_destroy)
+ {
+ timedWait(_timeout);
+ if(!_destroy)
+ {
+ assert(_factory);
+ _factory->reap();
+ }
+ }
+}
+
+void
+ReapThread::destroy()
+{
+ Lock sync(*this);
+ _destroy = true;
+ notify();
+ // Drop the cyclic reference count.
+ _factory = 0;
+}
+
+SessionFactoryI::SessionFactoryI(const Ice::ObjectAdapterPtr& adapter) :
+ _adapter(adapter),
+ _timeout(IceUtil::Time::seconds(10)),
+ _reapThread(new ReapThread(this, _timeout))
+{
+ _reapThread->start();
+}
+
+SessionFactoryI::~SessionFactoryI()
+{
+}
+
+SessionPrx
+SessionFactoryI::create(const Ice::Current& c)
+{
+ Lock sync(*this);
+
+ SessionIPtr session = new SessionI(_adapter, _timeout);
+ SessionPrx proxy = SessionPrx::uncheckedCast(_adapter->addWithUUID(session));
+ _sessions.push_back(make_pair(session, proxy->ice_getIdentity()));
+ return proxy;
+}
+
+void
+SessionFactoryI::shutdown(const ::Ice::Current& c)
+{
+ Lock sync(*this);
+
+ cout << "Shutting down..." << endl;
+ c.adapter->getCommunicator()->shutdown();
+}
+
+void
+SessionFactoryI::reap()
+{
+ Lock sync(*this);
+
+ list<pair<SessionIPtr, Ice::Identity> >::iterator p = _sessions.begin();
+ while(p != _sessions.end())
+ {
+ if(p->first->destroyed())
+ {
+ p->first->destroyCallback();
+ _adapter->remove(p->second);
+ p = _sessions.erase(p);
+ }
+ else
+ {
+ ++p;
+ }
+ }
+}
+
+void
+SessionFactoryI::destroy()
+{
+ //
+ // XXX: There is an issue. This is called post after the
+ // communicator->waitForShutdown() has been called. Then it
+ // attempts to unregister each of the objects from the OA. This
+ // causes an ObjectAdapterDeactivatedException. I think you should
+ // be permitted to unregister objects even if the OA has been
+ // deactivated. Note the documentation in the slice doesn't say
+ // that you cannot call remove after the OA has been deactivated.
+ //
+ Lock sync(*this);
+
+ _reapThread->destroy();
+ _reapThread->getThreadControl().join();
+ _reapThread = 0;
+
+ for(list<pair<SessionIPtr, Ice::Identity> >::const_iterator p = _sessions.begin();
+ p != _sessions.end();
+ ++p)
+ {
+ p->first->destroyCallback();
+ _adapter->remove(p->second);
+ }
+ _sessions.clear();
+}
diff --git a/cpp/demo/Ice/session/SessionFactoryI.h b/cpp/demo/Ice/session/SessionFactoryI.h
new file mode 100755
index 00000000000..58abaf967be
--- /dev/null
+++ b/cpp/demo/Ice/session/SessionFactoryI.h
@@ -0,0 +1,60 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2005 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.
+//
+// **********************************************************************
+
+#ifndef SESSION_FACTORY_I_H
+#define SESSION_FACTORY_I_H
+
+#include <IceUtil/Thread.h>
+#include <SessionI.h>
+
+#include <list>
+
+class SessionFactoryI;
+typedef IceUtil::Handle<SessionFactoryI> SessionFactoryIPtr;
+
+class ReapThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
+{
+public:
+
+ ReapThread(const SessionFactoryIPtr&, const IceUtil::Time&);
+ ~ReapThread();
+
+ virtual void run();
+ void destroy();
+
+private:
+
+ bool _destroy;
+ const IceUtil::Time _timeout;
+ SessionFactoryIPtr _factory;
+};
+typedef IceUtil::Handle<ReapThread> ReapThreadPtr;
+
+class SessionFactoryI : public ::Demo::SessionFactory, public IceUtil::Mutex
+{
+public:
+
+ SessionFactoryI(const Ice::ObjectAdapterPtr&);
+ ~SessionFactoryI();
+
+ virtual ::Demo::SessionPrx create(const ::Ice::Current&);
+ virtual void shutdown(const Ice::Current&);
+
+ void reap();
+ void destroy();
+
+private:
+
+ const IceUtil::Time _timeout;
+ const Ice::ObjectAdapterPtr _adapter;
+ ReapThreadPtr _reapThread;
+ std::list<std::pair<SessionIPtr, ::Ice::Identity> > _sessions;
+};
+
+#endif
diff --git a/cpp/demo/Ice/session/SessionI.cpp b/cpp/demo/Ice/session/SessionI.cpp
new file mode 100755
index 00000000000..4ad2c42dc1c
--- /dev/null
+++ b/cpp/demo/Ice/session/SessionI.cpp
@@ -0,0 +1,95 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2005 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 <SessionFactoryI.h>
+
+using namespace std;
+using namespace Demo;
+
+class HelloI : public Hello
+{
+public:
+
+ HelloI(int id) :
+ _id(id)
+ {
+ }
+
+ ~HelloI()
+ {
+ cout << _id << ": ~Hello" << endl;
+ }
+
+ void
+ sayHello(const Ice::Current&) const
+ {
+ cout << _id << ": Hello World!" << endl;
+ }
+
+private:
+
+ const int _id;
+};
+
+SessionI::SessionI(const Ice::ObjectAdapterPtr& adapter, const IceUtil::Time& timeout) :
+ _adapter(adapter),
+ _timeout(timeout),
+ _nextId(0),
+ _destroy(false),
+ _refreshTime(IceUtil::Time::now())
+{
+}
+
+SessionI::~SessionI()
+{
+ cout << "~SessionI" << endl;
+}
+
+HelloPrx
+SessionI::createHello(const Ice::Current&)
+{
+ HelloPrx hello = HelloPrx::uncheckedCast(_adapter->addWithUUID(new HelloI(_nextId++)));
+ _objs.push_back(hello);
+ return hello;
+}
+
+void
+SessionI::destroy(const Ice::Current& c)
+{
+ Lock sync(*this);
+ _destroy = true;
+}
+
+void
+SessionI::refresh(const Ice::Current& c)
+{
+ Lock sync(*this);
+ _refreshTime = IceUtil::Time::now();
+}
+
+bool
+SessionI::destroyed() const
+{
+ Lock sync(*this);
+ return _destroy || (IceUtil::Time::now() - _refreshTime) > _timeout;
+}
+
+void
+SessionI::destroyCallback()
+{
+ Lock sync(*this);
+ cout << "destroying session: _destroy=" << _destroy << " timeout="
+ << ((IceUtil::Time::now()-_refreshTime) > _timeout) << endl;
+ for(list<HelloPrx>::const_iterator p = _objs.begin(); p != _objs.end(); ++p)
+ {
+ _adapter->remove((*p)->ice_getIdentity());
+ }
+ _objs.clear();
+}
diff --git a/cpp/demo/Ice/session/SessionI.h b/cpp/demo/Ice/session/SessionI.h
new file mode 100755
index 00000000000..b5eb81088bd
--- /dev/null
+++ b/cpp/demo/Ice/session/SessionI.h
@@ -0,0 +1,41 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2005 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.
+//
+// **********************************************************************
+
+#ifndef SESSION_I_H
+#define SESSION_I_H
+
+#include <Session.h>
+#include <list>
+
+class SessionI : public ::Demo::Session, public IceUtil::Mutex
+{
+public:
+
+ SessionI(const Ice::ObjectAdapterPtr&, const IceUtil::Time&);
+ ~SessionI();
+
+ virtual ::Demo::HelloPrx createHello(const Ice::Current&);
+ virtual void destroy(const Ice::Current&);
+ virtual void refresh(const Ice::Current&);
+
+ bool destroyed() const;
+ void destroyCallback();
+
+private:
+
+ const Ice::ObjectAdapterPtr _adapter;
+ const IceUtil::Time _timeout;
+ int _nextId;
+ bool _destroy;
+ IceUtil::Time _refreshTime;
+ std::list<::Demo::HelloPrx> _objs;
+};
+typedef IceUtil::Handle<SessionI> SessionIPtr;
+
+#endif
diff --git a/cpp/demo/Ice/session/SessionManagerI.cpp b/cpp/demo/Ice/session/SessionManagerI.cpp
deleted file mode 100755
index ecbc37f9aa2..00000000000
--- a/cpp/demo/Ice/session/SessionManagerI.cpp
+++ /dev/null
@@ -1,190 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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 <HelloSessionManagerI.h>
-
-using namespace std;
-using namespace Demo;
-
-// XXX Cyclic dependency between ReapThread and SessionManagerI.cpp!! Set to 0 in both destroy() functions. XXX
-
-ReapThread::ReapThread(const SessionManagerIPtr& manager, const IceUtil::Time& timeout) :
- _destroy(false),
- _timeout(timeout),
- _manager(manager)
-{
-}
-
-ReapThread::~ReapThread()
-{
-}
-
-void
-ReapThread::run()
-{
- Lock sync(*this);
- while(!_destroy)
- {
- timedWait(_timeout);
- if(_destroy)
- {
- break;
- }
- _manager->reap();
- }
-}
-
-void
-ReapThread::destroy()
-{
- Lock sync(*this);
- _destroy = true;
- notify();
-}
-
-SessionManagerI::SessionManagerI(const Ice::CommunicatorPtr& communicator) :
- _timeout(IceUtil::Time::seconds(10)),
- _reapThread(new ReapThread(this, _timeout)),
- _logger(communicator->getLogger()),
- _destroy(false)
-{
- _reapThread->start();
-}
-
-SessionManagerI::~SessionManagerI()
-{
- assert(_sessions.size() == 0);
- assert(_destroy);
-}
-
-void
-SessionManagerI::shutdown(const Ice::Current& c)
-{
- cout << "Shutting down..." << endl;
- c.adapter->getCommunicator()->shutdown();
-}
-
-void
-SessionManagerI::destroy()
-{
- Lock sync(*this);
-
- assert(!_destroy);
- _destroy = true;
- _reapThread->destroy();
- _reapThread->getThreadControl().join();
-
- //
- // Destroy each session.
- //
- for(map<Ice::Identity, pair< IceUtil::Time, SessionPrx> >::iterator p = _sessions.begin();
- p != _sessions.end();
- ++p)
- {
- try
- {
- p->second.second->destroyed();
- }
- catch(const Ice::Exception& ex)
- {
- Ice::Warning warn(_logger);
- warn << "Session::destroyed failed: " << ex;
- }
- }
- _sessions.clear();
-}
-
-void
-SessionManagerI::add(const SessionPrx& session)
-{
- Lock sync(*this);
-
- Ice::Trace trace(_logger, "SessionManagerI");
- trace << "add: " << Ice::identityToString(session->ice_getIdentity());
-
- assert(!_destroy);
- _sessions.insert(make_pair(session->ice_getIdentity(), make_pair(IceUtil::Time::now(), session)));
-}
-
-void
-SessionManagerI::remove(const Ice::Identity& id)
-{
- Lock sync(*this);
-
- assert(!_destroy);
- map<Ice::Identity, pair< IceUtil::Time, SessionPrx> >::iterator p = _sessions.find(id);
- if(p != _sessions.end())
- {
- try
- {
- p->second.second->destroyed();
- }
- catch(const Ice::Exception& ex)
- {
- Ice::Warning warn(_logger);
- warn << "Session::destroyed failed: " << ex;
- }
- _sessions.erase(p);
- }
-
- Ice::Trace trace(_logger, "SessionManagerI");
- trace << "SessionManagerI::remove: " << Ice::identityToString(id);
-}
-
-void
-SessionManagerI::refresh(const Ice::Identity& id)
-{
- Lock sync(*this);
- map<Ice::Identity, pair< IceUtil::Time, SessionPrx> >::iterator p = _sessions.find(id);
- if(p != _sessions.end())
- {
- p->second.first = IceUtil::Time::now();
- }
- // Its possible the code reaches here if a session times out and
- // is removed at the same time as it calls refresh.
-}
-
-void
-SessionManagerI::reap()
-{
- Lock sync(*this);
-
- //
- // Run through the sessions destroying those which have not
- // been refreshed in the _timeout interval.
- //
- IceUtil::Time now = IceUtil::Time::now();
- map<Ice::Identity, pair< IceUtil::Time, SessionPrx> >::iterator p = _sessions.begin();
- while(p != _sessions.end())
- {
- if((now - p->second.first) > _timeout)
- {
- Ice::Trace trace(_logger, "SessionManagerI");
- trace << "SessionManagerI::reaping: " << Ice::identityToString(p->first);
-
- try
- {
- p->second.second->destroyed();
- }
- catch(const Ice::Exception& ex)
- {
- Ice::Warning warn(_logger);
- warn << "Session::destroyed failed: " << ex;
- }
- map<Ice::Identity, pair< IceUtil::Time, SessionPrx> >::iterator tmp = p;
- ++p;
- _sessions.erase(tmp);
- }
- else
- {
- ++p;
- }
- }
-}
diff --git a/cpp/demo/Ice/session/SessionManagerI.h b/cpp/demo/Ice/session/SessionManagerI.h
deleted file mode 100755
index 26712dd414b..00000000000
--- a/cpp/demo/Ice/session/SessionManagerI.h
+++ /dev/null
@@ -1,71 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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.
-//
-// **********************************************************************
-
-#ifndef SESSION_MANAGER_I_H
-#define SESSION_MANAGER_I_H
-
-#include <IceUtil/Thread.h>
-#include <Session.h>
-
-class SessionManagerI;
-typedef IceUtil::Handle<SessionManagerI> SessionManagerIPtr;
-
-class ReapThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
-{
-public:
-
- ReapThread(const SessionManagerIPtr&, const IceUtil::Time&);
- ~ReapThread();
-
- virtual void run();
- void destroy();
-
-private:
-
- bool _destroy;
- const IceUtil::Time _timeout;
- const SessionManagerIPtr _manager;
-};
-typedef IceUtil::Handle<ReapThread> ReapThreadPtr;
-
-//
-// Users wanting to create their own session manager specialization
-// should inherit off this class and implement create(). create() must
-// call add() with the creation session proxy.
-// XXX Not nice! It shouldn't be necessary for the derived class to do this. Instead, create() should be implemented here, and call another virtual function. XXX
-//
-class SessionManagerI : public Demo::SessionManager, public IceUtil::Mutex
-{
-public:
-
- SessionManagerI(const Ice::CommunicatorPtr&);
- ~SessionManagerI(); // XXX virtual XXX
-
- virtual Demo::SessionPrx create(const Ice::Current&) = 0;
- virtual void shutdown(const Ice::Current&);
-
-// XXX Why are these not protected? XXX Get rid off add(), see comment above. XXX Get rid of remove(), too! This is evil, session are not supposed to remove themselves, this only leads to deadlocks. They must get reaped. XXX Get rid of refresh() - why is this needed? XXX
- void destroy();
- void add(const Demo::SessionPrx&);
- void remove(const Ice::Identity&);
- void refresh(const Ice::Identity&);
- void reap();
-
-private:
-
- const IceUtil::Time _timeout;
- ReapThreadPtr _reapThread;
- const Ice::LoggerPtr _logger;
- std::map<Ice::Identity, std::pair<IceUtil::Time, Demo::SessionPrx> > _sessions;
- bool _destroy;
-};
-
-typedef IceUtil::Handle<SessionManagerI> SessionManagerIPtr;
-
-#endif
diff --git a/cpp/demo/Ice/session/config b/cpp/demo/Ice/session/config
index 35c59944dcd..6151bf50ea1 100644
--- a/cpp/demo/Ice/session/config
+++ b/cpp/demo/Ice/session/config
@@ -1,15 +1,15 @@
#
# The client reads this property to create the reference to the
-# "SessionManager" object in the server.
+# "hello" object in the server.
#
-SessionManager.Proxy=SessionManager:default -p 10000
+SessionFactory.Proxy=SessionFactory:default -p 10000
#
# The server creates one single object adapter with the name
-# "SessionManager". The following line sets the endpoints for this
+# "Hello". The following line sets the endpoints for this
# adapter.
#
-SessionManager.Endpoints=default -p 10000
+SessionFactory.Endpoints=default -p 10000
#
# Warn about connection exceptions
diff --git a/cpp/demo/Ice/session/sessionC.dsp b/cpp/demo/Ice/session/sessionC.dsp
index f45493684c4..c0328485f08 100755
--- a/cpp/demo/Ice/session/sessionC.dsp
+++ b/cpp/demo/Ice/session/sessionC.dsp
@@ -95,10 +95,6 @@ SOURCE=.\Client.cpp
# End Source File
# Begin Source File
-SOURCE=.\HelloSession.cpp
-# End Source File
-# Begin Source File
-
SOURCE=.\Session.cpp
# End Source File
# End Group
@@ -107,10 +103,6 @@ SOURCE=.\Session.cpp
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
-SOURCE=.\HelloSession.h
-# End Source File
-# Begin Source File
-
SOURCE=.\Session.h
# End Source File
# End Group
@@ -119,43 +111,6 @@ SOURCE=.\Session.h
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# Begin Source File
-SOURCE=.\HelloSession.ice
-
-!IF "$(CFG)" == "sessionC - Win32 Release"
-
-# Begin Custom Build
-InputPath=.\HelloSession.ice
-
-BuildCmds= \
- ..\..\..\bin\slice2cpp.exe -I. HelloSession.ice
-
-"HelloSession.cpp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-
-"HelloSession.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-# End Custom Build
-
-!ELSEIF "$(CFG)" == "sessionC - Win32 Debug"
-
-# Begin Custom Build
-InputPath=.\HelloSession.ice
-
-BuildCmds= \
- ..\..\..\bin\slice2cpp.exe -I. HelloSession.ice
-
-"HelloSession.cpp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-
-"HelloSession.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-# End Custom Build
-
-!ENDIF
-
-# End Source File
-# Begin Source File
-
SOURCE=.\Session.ice
!IF "$(CFG)" == "sessionC - Win32 Release"
diff --git a/cpp/demo/Ice/session/sessionS.dsp b/cpp/demo/Ice/session/sessionS.dsp
index 0d8cabc5b6f..ff110834154 100644
--- a/cpp/demo/Ice/session/sessionS.dsp
+++ b/cpp/demo/Ice/session/sessionS.dsp
@@ -91,27 +91,19 @@ LINK32=link.exe
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
-SOURCE=.\HelloSession.cpp
-# End Source File
-# Begin Source File
-
-SOURCE=.\HelloSessionI.cpp
-# End Source File
-# Begin Source File
-
-SOURCE=.\HelloSessionManagerI.cpp
+SOURCE=.\Server.cpp
# End Source File
# Begin Source File
-SOURCE=.\Server.cpp
+SOURCE=.\Session.cpp
# End Source File
# Begin Source File
-SOURCE=.\Session.cpp
+SOURCE=.\SessionFactoryI.cpp
# End Source File
# Begin Source File
-SOURCE=.\SessionManagerI.cpp
+SOURCE=.\SessionI.cpp
# End Source File
# End Group
# Begin Group "Header Files"
@@ -119,23 +111,15 @@ SOURCE=.\SessionManagerI.cpp
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
-SOURCE=.\HelloSession.h
-# End Source File
-# Begin Source File
-
-SOURCE=.\HelloSessionI.h
-# End Source File
-# Begin Source File
-
-SOURCE=.\HelloSessionManagerI.h
+SOURCE=.\Session.h
# End Source File
# Begin Source File
-SOURCE=.\Session.h
+SOURCE=.\SessionFactoryI.h
# End Source File
# Begin Source File
-SOURCE=.\SessionManagerI.h
+SOURCE=.\SessionI.h
# End Source File
# End Group
# Begin Group "Resource Files"
@@ -143,43 +127,6 @@ SOURCE=.\SessionManagerI.h
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# Begin Source File
-SOURCE=.\HelloSession.ice
-
-!IF "$(CFG)" == "sessionS - Win32 Release"
-
-# Begin Custom Build
-InputPath=.\HelloSession.ice
-
-BuildCmds= \
- ..\..\..\bin\slice2cpp.exe -I. HelloSession.ice
-
-"HelloSession.cpp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-
-"HelloSession.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-# End Custom Build
-
-!ELSEIF "$(CFG)" == "sessionS - Win32 Debug"
-
-# Begin Custom Build
-InputPath=.\HelloSession.ice
-
-BuildCmds= \
- ..\..\..\bin\slice2cpp.exe -I. HelloSession.ice
-
-"HelloSession.cpp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-
-"HelloSession.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
- $(BuildCmds)
-# End Custom Build
-
-!ENDIF
-
-# End Source File
-# Begin Source File
-
SOURCE=.\Session.ice
!IF "$(CFG)" == "sessionS - Win32 Release"