summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/RetryQueue.cpp
blob: f202e0e4a1d4e9834017c868d2102a4af76c7ddc (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
// **********************************************************************
//
// Copyright (c) 2003-2011 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/RetryQueue.h>
#include <Ice/OutgoingAsync.h>
#include <Ice/LocalException.h>
#include <Ice/Instance.h>

using namespace std;
using namespace Ice;
using namespace IceInternal;

IceUtil::Shared* IceInternal::upCast(RetryQueue* p) { return p; }

IceInternal::RetryTask::RetryTask(const RetryQueuePtr& queue, const OutgoingAsyncPtr& outAsync) :
    _queue(queue), _outAsync(outAsync)
{
}

void
IceInternal::RetryTask::runTimerTask()
{
    if(_queue->remove(this))
    {
        try
        {
            _outAsync->__send(false);
        }
        catch(const Ice::LocalException& ex)
        {
            _outAsync->__exceptionAsync(ex);
        }
    }
}

void
IceInternal::RetryTask::destroy()
{
    _outAsync->__exceptionAsync(CommunicatorDestroyedException(__FILE__, __LINE__));
}

bool
IceInternal::RetryTask::operator<(const RetryTask& rhs) const
{
    return this < &rhs;
}

IceInternal::RetryQueue::RetryQueue(const InstancePtr& instance) : _instance(instance)
{
}

void
IceInternal::RetryQueue::add(const OutgoingAsyncPtr& out, int interval)
{
    Lock sync(*this);
    RetryTaskPtr task = new RetryTask(this, out);
    try
    {
        _instance->timer()->schedule(task, IceUtil::Time::milliSeconds(interval));
    }
    catch(const IceUtil::IllegalArgumentException&) // Expected if the communicator destroyed the timer.
    {
        throw CommunicatorDestroyedException(__FILE__, __LINE__); 
    }
    _requests.insert(task);
}

void
IceInternal::RetryQueue::destroy()
{
    Lock sync(*this);
    for(set<RetryTaskPtr>::const_iterator p = _requests.begin(); p != _requests.end(); ++p)
    {
        _instance->timer()->cancel(*p);
        (*p)->destroy();
    }
    _requests.clear();
}

bool
IceInternal::RetryQueue::remove(const RetryTaskPtr& task)
{
    Lock sync(*this);
    return _requests.erase(task) > 0;
}