summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/cluster/OutputInterceptor.cpp
blob: 6c77d2747a738af48406eeaa339b625adc68e9bf (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
#include "OutputInterceptor.h"
#include "Connection.h"
#include "qpid/framing/ClusterConnectionDeliverDoOutputBody.h"
#include "qpid/framing/AMQFrame.h"
#include "qpid/log/Statement.h"
#include <boost/current_function.hpp>


namespace qpid {
namespace cluster {

using namespace framing;

OutputInterceptor::OutputInterceptor(cluster::Connection& p, sys::ConnectionOutputHandler& h)
    : parent(p), next(h), sent(), moreOutput(), doingOutput()
{}

void OutputInterceptor::send(framing::AMQFrame& f) {
    Locker l(lock); 
    next.send(f);
    sent += f.size();
}

void OutputInterceptor::activateOutput() {
    Locker l(lock); 
    moreOutput = true;
    sendDoOutput();             
}

// Called in write thread when the IO layer has no more data to write.
// We do nothing in the write thread, we run doOutput only on delivery
// of doOutput requests.
bool  OutputInterceptor::doOutput() {
    return false;
}

// Delivery of doOutput allows us to run the real connection doOutput()
// which stocks up the write buffers with data.
// 
void OutputInterceptor::deliverDoOutput(size_t requested) {
    Locker l(lock);
    size_t buf = next.getBuffered();
    if (parent.isLocal())
        writeEstimate.delivered(sent, buf); // Update the estimate.

    // Run the real doOutput() till we have added the requested data or there's nothing to output.
    sent = 0;
    do {
        sys::Mutex::ScopedUnlock u(lock);
        moreOutput = parent.getBrokerConnection().doOutput();
    } while (sent < requested && moreOutput);
    sent += buf;                // Include buffered data in the sent total.

    QPID_LOG(trace, "Delivered doOutput: requested=" << requested << " output=" << sent << " more=" << moreOutput);

    if (parent.isLocal() && moreOutput) 
        sendDoOutput();
    else
        doingOutput = false;
}

void OutputInterceptor::startDoOutput() {
    if (!doingOutput) 
        sendDoOutput();
}

// Send a doOutput request if one is not already in flight.
void OutputInterceptor::sendDoOutput() {
    // Call with lock held.
    // FIXME aconway 2008-08-28: used to  have || parent.getClosed())
    if (!parent.isLocal()) return;

    doingOutput = true;
    size_t request = writeEstimate.sending(getBuffered());
    
    // Note we may send 0 size request if there's more than 2*estimate in the buffer.
    // Send it anyway to keep the doOutput chain going until we are sure there's no more output
    // (in deliverDoOutput)
    // 
    parent.getCluster().send(AMQFrame(in_place<ClusterConnectionDeliverDoOutputBody>(
                                          framing::ProtocolVersion(), request)), parent.getId());
    QPID_LOG(trace, &parent << "Send doOutput request for " << request);
}

}} // namespace qpid::cluster