diff options
author | Benoit Foucher <benoit@zeroc.com> | 2006-07-27 13:20:03 +0000 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2006-07-27 13:20:03 +0000 |
commit | dad0bda0d462b4730b168befd187ca43446f12e7 (patch) | |
tree | 1a964dc5a3bd2c945e0ee165ddda55c3e4281157 /cpp/src/IceGrid/SessionManager.h | |
parent | Improved __checkMode (diff) | |
download | ice-dad0bda0d462b4730b168befd187ca43446f12e7.tar.bz2 ice-dad0bda0d462b4730b168befd187ca43446f12e7.tar.xz ice-dad0bda0d462b4730b168befd187ca43446f12e7.zip |
More IceGrid replication improvements.
Diffstat (limited to 'cpp/src/IceGrid/SessionManager.h')
-rw-r--r-- | cpp/src/IceGrid/SessionManager.h | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/cpp/src/IceGrid/SessionManager.h b/cpp/src/IceGrid/SessionManager.h new file mode 100644 index 00000000000..576e4aabe38 --- /dev/null +++ b/cpp/src/IceGrid/SessionManager.h @@ -0,0 +1,182 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +#ifndef ICE_GRID_SESSION_MANAGER_H +#define ICE_GRID_SESSION_MANAGER_H + +#include <IceUtil/Handle.h> +#include <IceUtil/Mutex.h> +#include <IceUtil/Monitor.h> +#include <IceUtil/Thread.h> + +namespace IceGrid +{ + +template<class TPrx, class FPrx> +class SessionKeepAliveThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex> +{ + enum State + { + Initial, + Disconnected, + Connected, + Destroyed + }; + +public: + + SessionKeepAliveThread(const FPrx& factory) : + _factory(factory), + _state(Initial) + { + } + + virtual void + run() + { + TPrx session; + FPrx factory = _factory; + bool updateState = true; + IceUtil::Time timeout = IceUtil::Time::seconds(10); + + while(true) + { + // + // Send a keep alive message to the session. + // + if(session) + { + if(!keepAlive(session)) + { + session = 0; + } + updateState |= !session; + } + + // + // If the session isn't established yet, try to create a new + // session. + // + if(!session) + { + session = createSession(factory, timeout); + updateState |= session; + } + + if(updateState) + { + Lock sync(*this); + _state = session ? Connected : Disconnected; + _session = session; + notifyAll(); + } + + // + // Wait for the configured timeout duration. + // + { + Lock sync(*this); + + if(_state != Destroyed) + { + timedWait(timeout); + } + + if(_state == Destroyed) + { + break; + } + + updateState = _state == Initial; + factory = _factory; + } + } + + // + // Destroy the session. + // + if(session) + { + destroySession(session); + } + } + + virtual bool + waitForCreate() + { + Lock sync(*this); + while(_state != Destroyed && _state != Connected) + { + wait(); + } + return _state != Destroyed; + } + + virtual bool + tryCreateSession(FPrx factory) + { + FPrx previous; + { + Lock sync(*this); + if(_state == Destroyed) + { + return false; + } + + _state = Initial; + if(factory) + { + previous = _factory; + _factory = factory; + } + notifyAll(); + } + { + Lock sync(*this); + while(_state == Initial) + { + wait(); + } + if(factory) + { + _factory = previous; // Restore the previous factory + } + } + return true; + } + + void + terminate() + { + Lock sync(*this); + _state = Destroyed; + notifyAll(); + } + + TPrx + getSession() + { + Lock sync(*this); + return _session; + } + + virtual TPrx createSession(const FPrx&, IceUtil::Time&) const = 0; + virtual void destroySession(const TPrx&) const = 0; + virtual bool keepAlive(const TPrx&) const = 0; + +private: + + FPrx _factory; + TPrx _session; + State _state; +}; + +}; + +#endif |