summaryrefslogtreecommitdiff
path: root/cpp/demo/Ice/session/ReapThread.cpp
blob: 4fbb44b111362aedc2fef22a2625d8be3c8fa06f (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
// **********************************************************************
//
// Copyright (c) 2003-2009 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 <ReapThread.h>

using namespace std;
using namespace Demo;

ReapThread::ReapThread() :
    _timeout(IceUtil::Time::seconds(10)),
    _terminated(false)
{
}

void
ReapThread::run()
{
    Lock sync(*this);

    while(!_terminated)
    {
        timedWait(IceUtil::Time::seconds(1));

        if(!_terminated)
        {
            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
ReapThread::terminate()
{
    Lock sync(*this);

    _terminated = true;
    notify();

    _sessions.clear();
}

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