summaryrefslogtreecommitdiff
path: root/cpp/demo/Ice/session/SessionManagerI.cpp
blob: ecbc37f9aa25173f5f66fa9a70a9ca12a8812248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// **********************************************************************
//
// 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;
	}
    }
}