summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/SessionManager.h
diff options
context:
space:
mode:
authorBenoit Foucher <benoit@zeroc.com>2006-10-19 13:01:55 +0000
committerBenoit Foucher <benoit@zeroc.com>2006-10-19 13:01:55 +0000
commit1ff378dd6746af03c3031664991adafcb5972a34 (patch)
tree5c188bd3ee2bff12f64ca15788f275957e04ab40 /cpp/src/IceGrid/SessionManager.h
parentfile RubyUtil.cpp was initially added on branch icerb_preview_branch. (diff)
downloadice-1ff378dd6746af03c3031664991adafcb5972a34.tar.bz2
ice-1ff378dd6746af03c3031664991adafcb5972a34.tar.xz
ice-1ff378dd6746af03c3031664991adafcb5972a34.zip
Fixes
Diffstat (limited to 'cpp/src/IceGrid/SessionManager.h')
-rw-r--r--cpp/src/IceGrid/SessionManager.h167
1 files changed, 98 insertions, 69 deletions
diff --git a/cpp/src/IceGrid/SessionManager.h b/cpp/src/IceGrid/SessionManager.h
index 02558d63848..04264cc0a9d 100644
--- a/cpp/src/IceGrid/SessionManager.h
+++ b/cpp/src/IceGrid/SessionManager.h
@@ -28,17 +28,24 @@ class SessionKeepAliveThread : public IceUtil::Thread, public IceUtil::Monitor<I
{
Disconnected,
Connected,
- Retry,
- DestroySession,
+ InProgress,
Destroyed
};
+ enum Action
+ {
+ Connect,
+ Disconnect,
+ KeepAlive,
+ None
+ };
+
public:
SessionKeepAliveThread(const InternalRegistryPrx& registry) :
_registry(registry),
- _state(Disconnected),
- _destroySession(false)
+ _state(InProgress),
+ _nextAction(None)
{
}
@@ -46,86 +53,104 @@ public:
run()
{
TPrx session;
- InternalRegistryPrx registry = _registry;
- bool updateState = false;
+ InternalRegistryPrx registry;
IceUtil::Time timeout = IceUtil::Time::seconds(10);
- bool destroy = false;
+ Action action = Connect;
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(registry, timeout);
- updateState |= session;
- }
-
- if(updateState)
{
Lock sync(*this);
- if(_state != Destroyed)
+ if(_state == Destroyed)
{
- _state = session ? Connected : Disconnected;
+ break;
}
- _session = session;
- notifyAll();
- }
- //
- // Wait for the configured timeout duration.
- //
- {
- Lock sync(*this);
- if(_state == Destroyed)
+ //
+ // Update the current state.
+ //
+ assert(_state == InProgress);
+ _state = session ? Connected : Disconnected;
+ _session = session;
+ if(_nextAction == Connect && _state == Connected)
{
- break;
+ _nextAction = KeepAlive;
}
- else if(_state == Connected || _state == Disconnected)
+ else if(_nextAction == Disconnect && _state == Disconnected)
{
- timedWait(timeout);
+ _nextAction = None;
}
-
- if(_state == Destroyed)
+ else if(_nextAction == KeepAlive && _state == Disconnected)
{
- break;
+ _nextAction = Connect;
}
- else if(_state == DestroySession && session)
+ notifyAll();
+
+
+ //
+ // Wait if there's nothing to do and if we are
+ // connected or if we've just tried to connect.
+ //
+ if(_nextAction == None)
{
- destroy = true;
+ if(_state == Connected || (action == Connect || action == KeepAlive))
+ {
+ while(_state != Destroyed && _nextAction == None)
+ {
+ if(!timedWait(timeout))
+ {
+ break;
+ }
+ }
+ }
+ if(_nextAction == None)
+ {
+ _nextAction = session ? KeepAlive : Connect;
+ }
}
- updateState = _state == Retry || _state == DestroySession;
+ if(_state == Destroyed)
+ {
+ break;
+ }
+
+ assert(_nextAction != None);
+
+ action = _nextAction;
registry = _registry;
+ _nextAction = None;
+ _state = InProgress;
+ notifyAll();
}
- if(destroy)
+ switch(action)
{
+ case Connect:
+ assert(!session);
+ session = createSession(registry, timeout);
+ break;
+ case Disconnect:
assert(session);
destroySession(session);
- destroy = false;
session = 0;
+ break;
+ case KeepAlive:
+ assert(session);
+ if(!keepAlive(session))
+ {
+ session = createSession(registry, timeout);
+ }
+ break;
+ case None:
+ default:
+ assert(false);
}
}
//
// Destroy the session.
//
- if(_destroySession && session)
+ if(_nextAction == Disconnect && session)
{
destroySession(session);
}
@@ -142,44 +167,48 @@ public:
return _state != Destroyed;
}
- virtual bool
- tryCreateSession(const InternalRegistryPrx& registry)
+ virtual void
+ tryCreateSession(bool waitForTry = true)
{
{
Lock sync(*this);
- while(_state == Retry)
+ if(_state == Destroyed)
{
- wait();
+ return;
}
- if(_state == Destroyed)
+ if(_state == Connected)
{
- return false;
+ _nextAction = KeepAlive;
}
-
- _state = Retry;
- if(registry)
+ else
{
- _registry = registry;
+ _nextAction = Connect;
}
notifyAll();
}
+
+ if(waitForTry)
{
Lock sync(*this);
- while(_state == Retry)
+ // Wait until the action is executed and the state changes.
+ while((_nextAction == Connect || _nextAction == KeepAlive) || _state == InProgress)
{
wait();
}
}
- return true;
}
void
destroyActiveSession()
{
Lock sync(*this);
- _state = DestroySession;
- notifyAll();
+ if(_state == Destroyed || _state == Disconnected)
+ {
+ return;
+ }
+ _nextAction = Disconnect;
+ notifyAll();
}
void
@@ -188,7 +217,7 @@ public:
Lock sync(*this);
assert(_state != Destroyed);
_state = Destroyed;
- _destroySession = destroySession;
+ _nextAction = destroySession ? Disconnect : None;
notifyAll();
}
@@ -222,7 +251,7 @@ protected:
InternalRegistryPrx _registry;
TPrx _session;
State _state;
- bool _destroySession;
+ Action _nextAction;
};
};