summaryrefslogtreecommitdiff
path: root/cpp/demo/Ice/session/ReapTask.cpp
blob: d0301635dc53483a5a43fa9e7d56dea4eb3f43d1 (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
// **********************************************************************
//
// Copyright (c) 2003-2010 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 <ReapTask.h>

using namespace std;
using namespace Demo;

ReapTask::ReapTask() :
    _timeout(IceUtil::Time::seconds(10))
{
}

void
ReapTask::runTimerTask()
{
    Lock sync(*this);

    list<SessionProxyPair>::iterator p = _sessions.begin();
    while(p != _sessions.end())
    {
        try
        {
            //
            // Session destruction may take time in a
            // real-world example. Therefore the current time
            // is computed for each iteration.
            //
            if((IceUtil::Time::now(IceUtil::Time::Monotonic) - p->session->timestamp()) > _timeout)
            {
                string name = p->proxy->getName();
                p->proxy->destroy();
                cout << "The session " << name << " has timed out." << endl;
                p = _sessions.erase(p);
            }
            else
            {
                ++p;
            }
        }
        catch(const Ice::ObjectNotExistException&)
        {
            p = _sessions.erase(p);
        }
    }
}

void
ReapTask::add(const SessionPrx& proxy, const SessionIPtr& session)
{
    Lock sync(*this);
    _sessions.push_back(SessionProxyPair(proxy, session));
}