summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/broker/SemanticState.h
blob: e5456067a8a1063b21ae9ab3cf9d4e46f41adfbd (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef QPID_BROKER_SEMANTICSTATE_H
#define QPID_BROKER_SEMANTICSTATE_H

/*
 *
 * 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 "qpid/broker/BrokerImportExport.h"
#include "qpid/broker/Consumer.h"
#include "qpid/broker/Credit.h"
#include "qpid/broker/Deliverable.h"
#include "qpid/broker/DeliveryRecord.h"
#include "qpid/broker/DtxBuffer.h"
#include "qpid/broker/DtxManager.h"
#include "qpid/broker/NameGenerator.h"
#include "qpid/broker/QueueObserver.h"
#include "qpid/broker/TxBuffer.h"

#include "qpid/framing/FieldTable.h"
#include "qpid/framing/FrameHandler.h"
#include "qpid/framing/SequenceSet.h"
#include "qpid/framing/Uuid.h"
#include "qpid/sys/AggregateOutput.h"
#include "qpid/sys/Mutex.h"
#include "qpid/sys/AtomicValue.h"
#include "qpid/broker/AclModule.h"
#include "qpid/management/Manageable.h"
#include "qmf/org/apache/qpid/broker/Subscription.h"

#include <list>
#include <map>
#include <set>
#include <vector>

#include <boost/enable_shared_from_this.hpp>
#include <boost/cast.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/shared_ptr.hpp>

namespace qpid {
namespace broker {

class Exchange;
class MessageStore;
class ProtocolRegistry;
class Selector;
class SessionContext;
class SessionState;

/**
 *
 * SemanticState implements the behavior of a Session, especially the
 * state of consumers subscribed to queues. The code for ConsumerImpl
 * is also in SemanticState.cpp
 *
 * SemanticState holds the AMQP Execution and Model state of an open
 * session, whether attached to a channel or suspended. It is not
 * dependent on any specific AMQP version.
 *
 * Message delivery is driven by ConsumerImpl::doOutput(), which is
 * called when a client's socket is ready to write data.
 *
 */
class SemanticStateConsumerImpl;
class SemanticState : private boost::noncopyable {
  friend class SemanticStateConsumerImpl;

  public:
    typedef SemanticStateConsumerImpl ConsumerImpl;
    typedef std::map<std::string, boost::intrusive_ptr<DtxBuffer> > DtxBufferMap;

  private:
    typedef std::map<std::string, boost::shared_ptr<ConsumerImpl> > ConsumerImplMap;
    typedef boost::tuple<std::string, std::string, std::string, std::string> Binding;
    typedef std::set<Binding> Bindings;

    SessionState& session;
    ConsumerImplMap consumers;
    NameGenerator tagGenerator;
    DeliveryRecords unacked;
    boost::intrusive_ptr<TxBuffer> txBuffer;
    boost::intrusive_ptr<DtxBuffer> dtxBuffer;
    bool dtxSelected;
    DtxBufferMap suspendedXids;
    framing::SequenceSet accumulatedAck;
    boost::shared_ptr<Exchange> cacheExchange;
    const bool authMsg;
    const std::string userID;
    bool closeComplete;
    //needed for queue delete events in auto-delete:
    const std::string connectionId;

    Bindings bindings;

    void checkDtxTimeout();

    bool complete(DeliveryRecord&);
    AckRange findRange(DeliveryId first, DeliveryId last);
    void requestDispatch();
    void cancel(boost::shared_ptr<ConsumerImpl>);
    void disable(boost::shared_ptr<ConsumerImpl>);
    void unbindSessionBindings();

  public:

    SemanticState(SessionState&);
    ~SemanticState();

    SessionContext& getSession();
    const SessionContext& getSession() const;
    SessionState& getSessionState() { return session; }

    const boost::shared_ptr<ConsumerImpl> find(const std::string& destination) const;
    bool find(const std::string& destination, boost::shared_ptr<ConsumerImpl>&) const;

    /**
     * Get named queue, never returns 0.
     * @return: named queue
     * @exception: ChannelException if no queue of that name is found.
     * @exception: ConnectionException if name="" and session has no default.
     */
    boost::shared_ptr<Queue> getQueue(const std::string& name) const;

    bool exists(const std::string& consumerTag);

    void consume(const std::string& destination,
                 boost::shared_ptr<Queue> queue,
                 bool ackRequired, bool acquire, bool exclusive,
                 const std::string& resumeId=std::string(), uint64_t resumeTtl=0,
                 const framing::FieldTable& = framing::FieldTable());

    bool cancel(const std::string& tag);

    void setWindowMode(const std::string& destination);
    void setCreditMode(const std::string& destination);
    void addByteCredit(const std::string& destination, uint32_t value);
    void addMessageCredit(const std::string& destination, uint32_t value);
    void flush(const std::string& destination);
    void stop(const std::string& destination);

    void startTx();
    void commit(MessageStore* const store);
    void rollback();
    void selectDtx();
    bool getDtxSelected() const { return dtxSelected; }
    void startDtx(const std::string& xid, DtxManager& mgr, bool join);
    void endDtx(const std::string& xid, bool fail);
    void suspendDtx(const std::string& xid);
    void resumeDtx(const std::string& xid);
    TxBuffer* getTxBuffer();
    void requeue();
    void acquire(DeliveryId first, DeliveryId last, DeliveryIds& acquired);
    void release(DeliveryId first, DeliveryId last, bool setRedelivered);
    void reject(DeliveryId first, DeliveryId last);
    void route(Message& msg, Deliverable& strategy);

    void completed(const framing::SequenceSet& commands);
    void accepted(const framing::SequenceSet& commands);

    void attached();
    void detached();
    void closed();

    DeliveryRecords& getUnacked() { return unacked; }
    framing::SequenceSet getAccumulatedAck() const { return accumulatedAck; }
    boost::intrusive_ptr<TxBuffer> getTxBuffer() const { return txBuffer; }
    boost::intrusive_ptr<DtxBuffer> getDtxBuffer() const { return dtxBuffer; }
    void setTxBuffer(const boost::intrusive_ptr<TxBuffer>& txb) { txBuffer = txb; }
    void setDtxBuffer(const boost::intrusive_ptr<DtxBuffer>& dtxb) { dtxBuffer = dtxb; txBuffer = dtxb; }
    void setAccumulatedAck(const framing::SequenceSet& s) { accumulatedAck = s; }
    void record(const DeliveryRecord& delivery);
    DtxBufferMap& getSuspendedXids() { return suspendedXids; }

    void addBinding(const std::string& queueName, const std::string& exchangeName,
                   const std::string& routingKey, const framing::FieldTable& arguments);
    void removeBinding(const std::string& queueName, const std::string& exchangeName,
                      const std::string& routingKey);
};

class SemanticStateConsumerImpl : public Consumer, public sys::OutputTask,
                        public boost::enable_shared_from_this<SemanticStateConsumerImpl>,
                        public management::Manageable
{
    protected:
    mutable qpid::sys::Mutex lock;
    SemanticState* const parent;
    const boost::shared_ptr<Queue> queue;

    private:
    const bool ackExpected;
    const bool acquire;
    bool blocked;
    bool exclusive;
    std::string resumeId;
    boost::shared_ptr<Selector> selector;
    uint64_t resumeTtl;
    framing::FieldTable arguments;
    Credit credit;
    bool notifyEnabled;
    const int syncFrequency;
    int deliveryCount;
    qmf::org::apache::qpid::broker::Subscription::shared_ptr mgmtObject;
    ProtocolRegistry& protocols;

    bool checkCredit(const Message& msg);
    void allocateCredit(const Message& msg);
    bool haveCredit();

    protected:
    QPID_BROKER_EXTERN virtual bool doDispatch();
    size_t unacked() { return parent->unacked.size(); }
    QPID_BROKER_EXTERN bool deliver(const QueueCursor&, const Message&, boost::shared_ptr<Consumer>);

    public:
    typedef boost::shared_ptr<SemanticStateConsumerImpl> shared_ptr;

    QPID_BROKER_EXTERN SemanticStateConsumerImpl(SemanticState* parent,
                    const std::string& name, boost::shared_ptr<Queue> queue,
                    bool ack, SubscriptionType type, bool exclusive,
                    const std::string& tag, const std::string& resumeId,
                    uint64_t resumeTtl, const framing::FieldTable& arguments);
    QPID_BROKER_EXTERN ~SemanticStateConsumerImpl();
    QPID_BROKER_EXTERN OwnershipToken* getSession();
    QPID_BROKER_EXTERN bool deliver(const QueueCursor&, const Message&);
    QPID_BROKER_EXTERN bool filter(const Message&);
    QPID_BROKER_EXTERN bool accept(const Message&);
    QPID_BROKER_EXTERN void cancel() {}

    QPID_BROKER_EXTERN void disableNotify();
    QPID_BROKER_EXTERN void enableNotify();
    QPID_BROKER_EXTERN void notify();
    QPID_BROKER_EXTERN bool isNotifyEnabled() const;

    QPID_BROKER_EXTERN void requestDispatch();

    QPID_BROKER_EXTERN void setWindowMode();
    QPID_BROKER_EXTERN void setCreditMode();
    QPID_BROKER_EXTERN void addByteCredit(uint32_t value);
    QPID_BROKER_EXTERN void addMessageCredit(uint32_t value);
    QPID_BROKER_EXTERN void flush();
    QPID_BROKER_EXTERN void stop();
    QPID_BROKER_EXTERN void complete(DeliveryRecord&);
    boost::shared_ptr<Queue> getQueue() const { return queue; }
    bool isBlocked() const { return blocked; }
    bool setBlocked(bool set) { std::swap(set, blocked); return set; }

    QPID_BROKER_EXTERN bool doOutput();

    Credit& getCredit() { return credit; }
    const Credit& getCredit() const { return credit; }
    bool isAckExpected() const { return ackExpected; }
    bool isAcquire() const { return acquire; }
    bool isExclusive() const { return exclusive; }
    std::string getResumeId() const { return resumeId; };
    uint64_t getResumeTtl() const { return resumeTtl; }
    uint32_t getDeliveryCount() const { return deliveryCount; }
    void setDeliveryCount(uint32_t _deliveryCount) { deliveryCount = _deliveryCount; }
    const framing::FieldTable& getArguments() const { return arguments; }

    SemanticState& getParent() { return *parent; }
    const SemanticState& getParent() const { return *parent; }

    void acknowledged(const DeliveryRecord&) {}

    // manageable entry points
    QPID_BROKER_EXTERN management::ManagementObject::shared_ptr
    GetManagementObject(void) const;

    QPID_BROKER_EXTERN management::Manageable::status_t
    ManagementMethod(uint32_t methodId, management::Args& args, std::string& text);
};

}} // namespace qpid::broker




#endif  /*!QPID_BROKER_SEMANTICSTATE_H*/