summaryrefslogtreecommitdiff
path: root/cpp/src/tests/qpid-receive.cpp
blob: 7a02b871db9c305ea08e7a50359f411a06cf99a7 (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
/*
 *
 * 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/messaging/Address.h>
#include <qpid/messaging/Connection.h>
#include <qpid/messaging/Receiver.h>
#include <qpid/messaging/Sender.h>
#include <qpid/messaging/Session.h>
#include <qpid/messaging/Message.h>
#include <qpid/messaging/FailoverUpdates.h>
#include <qpid/Options.h>
#include <qpid/log/Logger.h>
#include <qpid/log/Options.h>
#include "qpid/sys/Time.h"
#include "TestOptions.h"
#include "Statistics.h"

#include <iostream>
#include <memory>

using namespace qpid::messaging;
using namespace qpid::types;
using namespace std;

namespace qpid {
namespace tests {

struct Options : public qpid::Options
{
    bool help;
    std::string url;
    std::string address;
    std::string connectionOptions;
    int64_t timeout;
    bool forever;
    uint messages;
    bool ignoreDuplicates;
    bool verifySequence;
    bool checkRedelivered;
    uint capacity;
    uint ackFrequency;
    uint tx;
    uint rollbackFrequency;
    bool printContent;
    bool printHeaders;
    bool failoverUpdates;
    qpid::log::Options log;
    bool reportTotal;
    uint reportEvery;
    bool reportHeader;
    string readyAddress;
    uint receiveRate;
    std::string replyto;

    Options(const std::string& argv0=std::string())
        : qpid::Options("Options"),
          help(false),
          url("amqp:tcp:127.0.0.1"),
          timeout(0),
          forever(false),
          messages(0),
          ignoreDuplicates(false),
          verifySequence(false),
          checkRedelivered(false),
          capacity(1000),
          ackFrequency(100),
          tx(0),
          rollbackFrequency(0),
          printContent(true),
          printHeaders(false),
          failoverUpdates(false),
          log(argv0),
          reportTotal(false),
          reportEvery(0),
          reportHeader(true),
          receiveRate(0)
    {
        addOptions()
            ("broker,b", qpid::optValue(url, "URL"), "url of broker to connect to")
            ("address,a", qpid::optValue(address, "ADDRESS"), "address to receive from")
            ("connection-options", qpid::optValue(connectionOptions, "OPTIONS"), "options for the connection")
            ("timeout", qpid::optValue(timeout, "TIMEOUT"), "timeout in seconds to wait before exiting")
            ("forever,f", qpid::optValue(forever), "ignore timeout and wait forever")
            ("messages,m", qpid::optValue(messages, "N"), "Number of messages to receive; 0 means receive indefinitely")
            ("ignore-duplicates", qpid::optValue(ignoreDuplicates), "Detect and ignore duplicates (by checking 'sn' header)")
            ("verify-sequence", qpid::optValue(verifySequence), "Verify there are no gaps in the message sequence (by checking 'sn' header)")
            ("check-redelivered", qpid::optValue(checkRedelivered), "Fails with exception if a duplicate is not marked as redelivered (only relevant when ignore-duplicates is selected)")
            ("capacity", qpid::optValue(capacity, "N"), "Pre-fetch window (0 implies no pre-fetch)")
            ("ack-frequency", qpid::optValue(ackFrequency, "N"), "Ack frequency (0 implies none of the messages will get accepted)")
            ("tx", qpid::optValue(tx, "N"), "batch size for transactions (0 implies transaction are not used)")
            ("rollback-frequency", qpid::optValue(rollbackFrequency, "N"), "rollback frequency (0 implies no transaction will be rolledback)")
            ("print-content", qpid::optValue(printContent, "yes|no"), "print out message content")
            ("print-headers", qpid::optValue(printHeaders, "yes|no"), "print out message headers")
            ("failover-updates", qpid::optValue(failoverUpdates), "Listen for membership updates distributed via amq.failover")
            ("report-total", qpid::optValue(reportTotal), "Report total throughput and latency statistics")
            ("report-every", qpid::optValue(reportEvery,"N"), "Report throughput and latency statistics every N messages.")
            ("report-header", qpid::optValue(reportHeader, "yes|no"), "Headers on report.")
            ("ready-address", qpid::optValue(readyAddress, "ADDRESS"), "send a message to this address when ready to receive")
            ("receive-rate", qpid::optValue(receiveRate,"N"), "Receive at rate of N messages/second. 0 means receive as fast as possible.")
            ("reply-to", qpid::optValue(replyto, "REPLY-TO"), "specify reply-to address on response messages")
            ("help", qpid::optValue(help), "print this usage statement");
        add(log);
    }

    Duration getTimeout()
    {
        if (forever) return Duration::FOREVER;
        else return Duration::SECOND*timeout;

    }
    bool parse(int argc, char** argv)
    {
        try {
            qpid::Options::parse(argc, argv);
            if (address.empty()) throw qpid::Exception("Address must be specified!");
            qpid::log::Logger::instance().configure(log);
            if (help) {
                std::ostringstream msg;
                std::cout << msg << *this << std::endl << std::endl
                          << "Drains messages from the specified address" << std::endl;
                return false;
            } else {
                return true;
            }
        } catch (const std::exception& e) {
            std::cerr << *this << std::endl << std::endl << e.what() << std::endl;
            return false;
        }
    }
};

const string EOS("eos");
const string SN("sn");

/** Check for duplicate or dropped messages by sequence number */
class SequenceTracker
{
  public:
    SequenceTracker(const Options& o) : opts(o), lastSn(0) {}

    /** Return true if the message should be procesed, false if it should be ignored. */
    bool track(Message& message) {
        if (!(opts.verifySequence || opts.ignoreDuplicates))
            return true;        // Not checking sequence numbers.
        uint sn = message.getProperties()[SN];
        bool duplicate = (sn <= lastSn);
        bool dropped = (sn > lastSn+1);
        if (opts.verifySequence && dropped)
            throw Exception(QPID_MSG("Gap in sequence numbers " << lastSn << "-" << sn));
        bool ignore = duplicate && opts.ignoreDuplicates;
        if (ignore && opts.checkRedelivered && !message.getRedelivered())
            throw qpid::Exception("duplicate sequence number received, message not marked as redelivered!");
        if (!duplicate) lastSn = sn;
        return !ignore;
    }

  private:
    const Options& opts;
    uint lastSn;
};

}} // namespace qpid::tests

using namespace qpid::tests;

int main(int argc, char ** argv)
{
    Connection connection;
    try {
        Options opts;
        if (opts.parse(argc, argv)) {
            connection = Connection(opts.url, opts.connectionOptions);
            connection.open();
            std::auto_ptr<FailoverUpdates> updates(opts.failoverUpdates ? new FailoverUpdates(connection) : 0);
            Session session = opts.tx ? connection.createTransactionalSession() : connection.createSession();
            Receiver receiver = session.createReceiver(opts.address);
            receiver.setCapacity(opts.capacity);
            Message msg;
            uint count = 0;
            uint txCount = 0;
            SequenceTracker sequenceTracker(opts);
            Duration timeout = opts.getTimeout();
            bool done = false;
            Reporter<ThroughputAndLatency> reporter(std::cout, opts.reportEvery, opts.reportHeader);
            if (!opts.readyAddress.empty())
                session.createSender(opts.readyAddress).send(msg);
            // For receive rate calculation
            qpid::sys::AbsTime start = qpid::sys::now();
            int64_t interval = 0;
            if (opts.receiveRate) interval = qpid::sys::TIME_SEC/opts.receiveRate;

            std::map<std::string,Sender> replyTo;

            while (!done && receiver.fetch(msg, timeout)) {
                reporter.message(msg);
                if (sequenceTracker.track(msg)) {
                    if (msg.getContent() == EOS) {
                        done = true;
                    } else {
                        ++count;
                        if (opts.printHeaders) {
                            if (msg.getSubject().size()) std::cout << "Subject: " << msg.getSubject() << std::endl;
                            if (msg.getReplyTo()) std::cout << "ReplyTo: " << msg.getReplyTo() << std::endl;
                            if (msg.getCorrelationId().size()) std::cout << "CorrelationId: " << msg.getCorrelationId() << std::endl;
                            if (msg.getUserId().size()) std::cout << "UserId: " << msg.getUserId() << std::endl;
                            if (msg.getTtl().getMilliseconds()) std::cout << "TTL: " << msg.getTtl().getMilliseconds() << std::endl;
                            if (msg.getPriority()) std::cout << "Priority: " << msg.getPriority() << std::endl;
                            if (msg.getDurable()) std::cout << "Durable: true" << std::endl;
                            if (msg.getRedelivered()) std::cout << "Redelivered: true" << std::endl;
                            std::cout << "Properties: " << msg.getProperties() << std::endl;
                            std::cout << std::endl;
                        }
                        if (opts.printContent)
                            std::cout << msg.getContent() << std::endl;//TODO: handle map or list messages
                        if (opts.messages && count >= opts.messages) done = true;
                    }
                }
                if (opts.tx && (count % opts.tx == 0)) {
                    if (opts.rollbackFrequency && (++txCount % opts.rollbackFrequency == 0)) {
                        session.rollback();
                    } else {
                        session.commit();
                    }
                } else if (opts.ackFrequency && (count % opts.ackFrequency == 0)) {
                    session.acknowledge();
                }
                if (msg.getReplyTo()) { // Echo message back to reply-to address.
                    Sender& s = replyTo[msg.getReplyTo().str()];
                    if (s.isNull()) {
                        s = session.createSender(msg.getReplyTo());
                        s.setCapacity(opts.capacity);
                    }
                    if (!opts.replyto.empty()) {
                        msg.setReplyTo(Address(opts.replyto));
                    }
                    s.send(msg);
                }
                if (opts.receiveRate) {
                    qpid::sys::AbsTime waitTill(start, count*interval);
                    int64_t delay = qpid::sys::Duration(qpid::sys::now(), waitTill);
                    if (delay > 0) qpid::sys::usleep(delay/qpid::sys::TIME_USEC);
                }
                // Clear out message properties & content for next iteration.
                msg = Message(); // TODO aconway 2010-12-01: should be done by fetch
            }
            if (opts.reportTotal) reporter.report();
            if (opts.tx) {
                if (opts.rollbackFrequency && (++txCount % opts.rollbackFrequency == 0)) {
                    session.rollback();
                } else {
                    session.commit();
                }
            } else {
                session.acknowledge();
            }
            session.close();
            connection.close();
            return 0;
        }
    } catch(const std::exception& error) {
        std::cerr << "qpid-receive: " << error.what() << std::endl;
        connection.close();
        return 1;
    }
}