summaryrefslogtreecommitdiff
path: root/cpp/src/tests/receiver.cpp
diff options
context:
space:
mode:
authorGordon Sim <gsim@apache.org>2008-11-14 16:40:22 +0000
committerGordon Sim <gsim@apache.org>2008-11-14 16:40:22 +0000
commit072c135f13cd287224ccc9b754a6c2b83940b6cd (patch)
tree485163f392741b72485b8e7959ed66737fcd21b3 /cpp/src/tests/receiver.cpp
parent23482177f452ebdf3023534988efe60d41e8c826 (diff)
downloadqpid-python-072c135f13cd287224ccc9b754a6c2b83940b6cd.tar.gz
Added some failover capable tests
Added grantCredit() method to subscription to allow simpler control of message delivery git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@714065 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/tests/receiver.cpp')
-rw-r--r--cpp/src/tests/receiver.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/cpp/src/tests/receiver.cpp b/cpp/src/tests/receiver.cpp
new file mode 100644
index 0000000000..3a4ac3649d
--- /dev/null
+++ b/cpp/src/tests/receiver.cpp
@@ -0,0 +1,120 @@
+/*
+ *
+ * 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/client/FailoverManager.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/SubscriptionManager.h>
+#include <qpid/client/SubscriptionManager.h>
+#include "TestOptions.h"
+
+#include <iostream>
+#include <fstream>
+
+
+using namespace qpid;
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using namespace std;
+
+struct Args : public qpid::TestOptions
+{
+ string queue;
+ uint messages;
+ bool ignoreDuplicates;
+
+ Args() : queue("test-queue"), messages(0), ignoreDuplicates(false)
+ {
+ addOptions()
+ ("queue", qpid::optValue(queue, "QUEUE NAME"), "Queue from which to request messages")
+ ("messages", 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)");
+ }
+};
+
+const string EOS("eos");
+
+class Receiver : public MessageListener, public FailoverManager::Command
+{
+ public:
+ Receiver(const string& queue, uint messages, bool ignoreDuplicates);
+ void received(Message& message);
+ void execute(AsyncSession& session, bool isRetry);
+ private:
+ const string queue;
+ const uint count;
+ const bool skipDups;
+ Subscription subscription;
+ uint processed;
+ uint lastSn;
+
+ bool isDuplicate(Message& message);
+};
+
+Receiver::Receiver(const string& q, uint messages, bool ignoreDuplicates) :
+ queue(q), count(messages), skipDups(ignoreDuplicates), processed(0), lastSn(0) {}
+
+void Receiver::received(Message & message)
+{
+ if (!(skipDups && isDuplicate(message))) {
+ bool eos = message.getData() == EOS;
+ if (!eos) std::cout << message.getData() << std::endl;
+ if (eos || ++processed == count) subscription.cancel();
+ }
+}
+
+bool Receiver::isDuplicate(Message& message)
+{
+ uint sn = message.getHeaders().getAsInt("sn");
+ if (lastSn < sn) {
+ lastSn = sn;
+ return false;
+ } else {
+ return true;
+ }
+}
+
+void Receiver::execute(AsyncSession& session, bool /*isRetry*/)
+{
+ SubscriptionManager subs(session);
+ subscription = subs.subscribe(*this, queue);
+ subs.run();
+}
+
+int main(int argc, char ** argv)
+{
+ Args opts;
+ try {
+ opts.parse(argc, argv);
+ FailoverManager connection(opts.con);
+ Receiver receiver(opts.queue, opts.messages, opts.ignoreDuplicates);
+ connection.execute(receiver);
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cerr << "Failure: " << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
+