summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/QueuePolicy.h
blob: 3cdd63784d2fb2818b1321f5663d2ad7cefd6c64 (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
/*
 *
 * 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.
 *
 */
#ifndef _QueuePolicy_
#define _QueuePolicy_

#include <deque>
#include <iostream>
#include <memory>
#include "qpid/broker/BrokerImportExport.h"
#include "qpid/broker/QueuedMessage.h"
#include "qpid/framing/FieldTable.h"
#include "qpid/sys/AtomicValue.h"
#include "qpid/sys/Mutex.h"

namespace qpid {
namespace broker {

class QueuePolicy
{
    static uint64_t defaultMaxSize;

    uint32_t maxCount;
    uint64_t maxSize;
    const std::string type;
    uint32_t count;
    uint64_t size;
    bool policyExceeded;
            
    static uint32_t getCapacity(const qpid::framing::FieldTable& settings, const std::string& key, uint32_t defaultValue);

  protected:
    uint64_t getCurrentQueueSize() const { return size; } 

  public:
    typedef std::deque<QueuedMessage> Messages;
    static QPID_BROKER_EXTERN const std::string maxCountKey;
    static QPID_BROKER_EXTERN const std::string maxSizeKey;
    static QPID_BROKER_EXTERN const std::string typeKey;
    static QPID_BROKER_EXTERN const std::string REJECT;
    static QPID_BROKER_EXTERN const std::string FLOW_TO_DISK;
    static QPID_BROKER_EXTERN const std::string RING;
    static QPID_BROKER_EXTERN const std::string RING_STRICT;            

    virtual ~QueuePolicy() {}
    QPID_BROKER_EXTERN void tryEnqueue(boost::intrusive_ptr<Message> msg);
    QPID_BROKER_EXTERN void recoverEnqueued(boost::intrusive_ptr<Message> msg);
    QPID_BROKER_EXTERN void enqueueAborted(boost::intrusive_ptr<Message> msg);
    virtual void enqueued(const QueuedMessage&);
    virtual void dequeued(const QueuedMessage&);
    virtual bool isEnqueued(const QueuedMessage&);
    QPID_BROKER_EXTERN void update(qpid::framing::FieldTable& settings);
    uint32_t getMaxCount() const { return maxCount; }
    uint64_t getMaxSize() const { return maxSize; }           
    void encode(framing::Buffer& buffer) const;
    void decode ( framing::Buffer& buffer );
    uint32_t encodedSize() const;
    virtual void getPendingDequeues(Messages& result);

    static QPID_BROKER_EXTERN std::auto_ptr<QueuePolicy> createQueuePolicy(const std::string& name, const qpid::framing::FieldTable& settings);
    static QPID_BROKER_EXTERN std::auto_ptr<QueuePolicy> createQueuePolicy(const std::string& name, uint32_t maxCount, uint64_t maxSize, const std::string& type = REJECT);
    static QPID_BROKER_EXTERN std::auto_ptr<QueuePolicy> createQueuePolicy(const qpid::framing::FieldTable& settings);
    static QPID_BROKER_EXTERN std::auto_ptr<QueuePolicy> createQueuePolicy(uint32_t maxCount, uint64_t maxSize, const std::string& type = REJECT);
    static std::string getType(const qpid::framing::FieldTable& settings);
    static void setDefaultMaxSize(uint64_t);
    friend QPID_BROKER_EXTERN std::ostream& operator<<(std::ostream&,
                                                       const QueuePolicy&);
  protected:
    const std::string name;

    QueuePolicy(const std::string& name, uint32_t maxCount, uint64_t maxSize, const std::string& type = REJECT);

    virtual bool checkLimit(boost::intrusive_ptr<Message> msg);
    void enqueued(uint64_t size);
    void dequeued(uint64_t size);
};


class FlowToDiskPolicy : public QueuePolicy
{
  public:
    FlowToDiskPolicy(const std::string& name, uint32_t maxCount, uint64_t maxSize);
    bool checkLimit(boost::intrusive_ptr<Message> msg);
};

class RingQueuePolicy : public QueuePolicy
{
  public:
    RingQueuePolicy(const std::string& name, uint32_t maxCount, uint64_t maxSize, const std::string& type = RING);
    void enqueued(const QueuedMessage&);
    void dequeued(const QueuedMessage&);
    bool isEnqueued(const QueuedMessage&);
    bool checkLimit(boost::intrusive_ptr<Message> msg);
    void getPendingDequeues(Messages& result);
  private:
    Messages pendingDequeues;
    Messages queue;
    const bool strict;

    bool find(const QueuedMessage&, Messages&, bool remove);
};

}}


#endif