Skip to content

Commit 347cf6e

Browse files
committed
Add tx_recv file
1 parent eb4514b commit 347cf6e

14 files changed

+284
-160
lines changed

cpp/examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ foreach(example
6161
service_bus
6262
multithreaded_client
6363
multithreaded_client_flow_control
64-
tx_send)
64+
tx_send
65+
tx_recv)
6566
add_executable(${example} ${example}.cpp)
6667
target_link_libraries(${example} Proton::cpp Threads::Threads)
6768
endforeach()

cpp/examples/tx_recv.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*
20+
*/
21+
22+
#include "options.hpp"
23+
24+
#include <proton/connection.hpp>
25+
#include <proton/container.hpp>
26+
#include <proton/message.hpp>
27+
#include <proton/message_id.hpp>
28+
#include <proton/messaging_handler.hpp>
29+
#include <proton/types.hpp>
30+
#include <proton/transaction.hpp>
31+
32+
#include <iostream>
33+
#include <map>
34+
#include <string>
35+
36+
#include <chrono>
37+
#include <thread>
38+
39+
class tx_recv : public proton::messaging_handler, proton::transaction_handler {
40+
private:
41+
proton::receiver receiver;
42+
std::string url;
43+
int expected;
44+
int batch_size;
45+
int current_batch = 0;
46+
int committed = 0;
47+
48+
proton::session session;
49+
proton::transaction transaction;
50+
public:
51+
tx_recv(const std::string &s, int c, int b):
52+
url(s), expected(c), batch_size(b) {}
53+
54+
void on_container_start(proton::container &c) override {
55+
receiver = c.open_receiver(url);
56+
}
57+
58+
void on_session_open(proton::session &s) override {
59+
session = s;
60+
std::cout << " [on_session_open] declare_txn started..." << std::endl;
61+
s.declare_transaction(*this);
62+
std::cout << " [on_session_open] declare_txn ended..." << std::endl;
63+
}
64+
65+
void on_transaction_declare_failed(proton::transaction) {}
66+
void on_transaction_commit_failed(proton::transaction t) {
67+
std::cout << "Transaction Commit Failed" << std::endl;
68+
t.connection().close();
69+
exit(-1);
70+
}
71+
72+
void on_transaction_declared(proton::transaction t) override {
73+
std::cout << "[on_transaction_declared] txn called " << (&t)
74+
<< std::endl;
75+
std::cout << "[on_transaction_declared] txn is_empty " << (t.is_empty())
76+
<< "\t" << transaction.is_empty() << std::endl;
77+
receiver.add_credit(batch_size);
78+
transaction = t;
79+
}
80+
81+
void on_message(proton::delivery &d, proton::message &msg) override {
82+
std::cout<<"# MESSAGE: " << msg.id() <<": " << msg.body() << std::endl;
83+
transaction.accept(d);
84+
current_batch += 1;
85+
if(current_batch == batch_size) {
86+
transaction = proton::transaction(); // null
87+
}
88+
}
89+
90+
void on_transaction_committed(proton::transaction t) override {
91+
committed += current_batch;
92+
current_batch = 0;
93+
std::cout<<" [OnTxnCommitted] Committed:"<< committed<< std::endl;
94+
if(committed == expected) {
95+
std::cout << "All messages committed" << std::endl;
96+
t.connection().close();
97+
}
98+
else {
99+
session.declare_transaction(*this);
100+
}
101+
}
102+
103+
};
104+
105+
int main(int argc, char **argv) {
106+
std::string address("127.0.0.1:5672/examples");
107+
int message_count = 9;
108+
int batch_size = 3;
109+
example::options opts(argc, argv);
110+
111+
opts.add_value(address, 'a', "address", "connect and send to URL", "URL");
112+
opts.add_value(message_count, 'm', "messages", "number of messages to send", "COUNT");
113+
opts.add_value(batch_size, 'b', "batch_size", "number of messages in each transaction", "BATCH_SIZE");
114+
115+
try {
116+
opts.parse();
117+
118+
tx_recv recv(address, message_count, batch_size);
119+
proton::container(recv).run();
120+
121+
return 0;
122+
} catch (const example::bad_option& e) {
123+
std::cout << opts << std::endl << e.what() << std::endl;
124+
} catch (const std::exception& e) {
125+
std::cerr << e.what() << std::endl;
126+
}
127+
128+
return 1;
129+
}

cpp/examples/tx_send.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,33 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
4848
int committed = 0;
4949
int confirmed = 0;
5050

51-
proton::container *container;
52-
// proton::transaction_handler transaction_handler;
51+
proton::session session;
5352
proton::transaction transaction;
54-
proton::connection connection;
5553
public:
5654
tx_send(const std::string &s, int c, int b):
5755
url(s), total(c), batch_size(b), sent(0) {}
5856

5957
void on_container_start(proton::container &c) override {
60-
container = &c; // TODO: Fix error
6158
sender = c.open_sender(url);
62-
connection = sender.connection();
63-
std::cout << " [on_container_start] declare_txn started..." << std::endl;
64-
c.declare_transaction(connection, *this);
65-
std::cout << " [on_container_start] completed!!" << &transaction
66-
<< std::endl;
59+
}
60+
61+
void on_session_open(proton::session &s) override {
62+
session = s;
63+
std::cout << " [on_session_open] declare_txn started..." << std::endl;
64+
s.declare_transaction(*this);
65+
std::cout << " [on_session_open] declare_txn ended..." << std::endl;
6766
}
6867

6968
void on_transaction_declare_failed(proton::transaction) {}
70-
void on_transaction_commit_failed(proton::transaction) {
69+
void on_transaction_commit_failed(proton::transaction t) {
7170
std::cout << "Transaction Commit Failed" << std::endl;
72-
connection.close();
71+
t.connection().close();
7372
exit(-1);
7473
}
7574

7675
void on_transaction_declared(proton::transaction t) override {
7776
std::cout << "[on_transaction_declared] txn called " << (&t)
7877
<< std::endl;
79-
// connection.close();
8078
std::cout << "[on_transaction_declared] txn is_empty " << (t.is_empty())
8179
<< "\t" << transaction.is_empty() << std::endl;
8280
transaction = t;
@@ -85,23 +83,22 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
8583
}
8684

8785
void on_sendable(proton::sender &s) override {
88-
// send();
8986
std::cout << " [OnSendable] transaction: " << &transaction
9087
<< std::endl;
9188
send(s);
9289
}
9390

9491
void send(proton::sender &s) {
95-
// TODO: Add more condition in while loop
92+
static int unique_id = 10000;
9693
while (!transaction.is_empty() && sender.credit() &&
9794
(committed + current_batch) < total) {
9895
proton::message msg;
9996
std::map<std::string, int> m;
10097
m["sequence"] = committed + current_batch;
10198

102-
msg.id(committed + current_batch + 1);
99+
msg.id(unique_id++);
103100
msg.body(m);
104-
std::cout << " [example] transaction send msg: " << msg
101+
std::cout << "##### [example] transaction send msg: " << msg
105102
<< std::endl;
106103
transaction.send(sender, msg);
107104
current_batch += 1;
@@ -132,17 +129,17 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
132129
std::cout<<" [OnTxnCommitted] Committed:"<< committed<< std::endl;
133130
if(committed == total) {
134131
std::cout << "All messages committed" << std::endl;
135-
connection.close();
132+
t.connection().close();
136133
}
137134
else {
138-
container->declare_transaction(connection, *this);
135+
session.declare_transaction(*this);
139136
}
140137
}
141138

142139
void on_transaction_aborted(proton::transaction t) override {
143140
std::cout << "Meesages Aborted ....." << std::endl;
144141
current_batch = 0;
145-
container->declare_transaction(connection, *this);
142+
session.declare_transaction(*this);
146143
}
147144

148145
void on_sender_close(proton::sender &s) override {

cpp/include/proton/container.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ class PN_CPP_CLASS_EXTERN container {
312312
/// Cancel task for the given work_handle.
313313
PN_CPP_EXTERN void cancel(work_handle);
314314

315-
PN_CPP_EXTERN transaction declare_transaction(proton::connection conn, proton::transaction_handler &handler, bool settle_before_discharge = false);
316315
private:
317316
/// Declare both v03 and v11 if compiling with c++11 as the library contains both.
318317
/// A C++11 user should never call the v03 overload so it is private in this case

cpp/include/proton/session.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ PN_CPP_CLASS_EXTERN session : public internal::object<pn_session_t>, public endp
105105
/// Get user data from this session.
106106
PN_CPP_EXTERN void* user_data() const;
107107

108+
PN_CPP_EXTERN transaction declare_transaction(proton::transaction_handler &handler, bool settle_before_discharge = false);
109+
108110
/// @cond INTERNAL
109111
friend class internal::factory<session>;
110112
friend class session_iterator;

cpp/include/proton/tracker.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "./binary.hpp"
2626
#include "./internal/export.hpp"
2727
#include "./transfer.hpp"
28-
#include "./messaging_handler.hpp"
2928

3029
/// @file
3130
/// @copybrief proton::tracker

cpp/include/proton/transaction.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class transaction_impl {
5555

5656
void discharge(bool failed);
5757
void release_pending();
58-
void accept(tracker &d);
58+
void accept(delivery &d);
5959
void update(tracker &d, uint64_t state);
6060
void set_id(binary _id);
6161

@@ -92,9 +92,11 @@ PN_CPP_CLASS_EXTERN transaction {
9292
PN_CPP_EXTERN void declare();
9393
PN_CPP_EXTERN void handle_outcome(proton::tracker);
9494
PN_CPP_EXTERN proton::tracker send(proton::sender s, proton::message msg);
95+
PN_CPP_EXTERN void accept(delivery &t);
96+
PN_CPP_EXTERN proton::connection connection() const;
9597

9698
friend class transaction_impl;
97-
friend class container::impl;
99+
friend class session;
98100
};
99101

100102
class

cpp/include/proton/transfer.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@
3333
/// @copybrief proton::transfer
3434

3535
struct pn_delivery_t;
36-
struct pn_disposition_t;
36+
// struct pn_disposition_t;
3737

3838
namespace proton {
3939

40-
class disposition : public internal::object<pn_disposition_t> {
41-
/// @cond INTERNAL
42-
disposition(pn_disposition_t *d) : internal::object<pn_disposition_t>(d) {}
43-
/// @endcond
40+
// class disposition : public internal::object<pn_disposition_t> {
41+
// /// @cond INTERNAL
42+
// disposition(pn_disposition_t *d) : internal::object<pn_disposition_t>(d) {}
43+
// /// @endcond
4444

45-
public:
46-
/// Create an empty disposition.
47-
disposition() : internal::object<pn_disposition_t>(0) {}
45+
// public:
46+
// /// Create an empty disposition.
47+
// disposition() : internal::object<pn_disposition_t>(0) {}
4848

49-
proton::value data() const;
49+
// proton::value data() const;
5050

51-
/// @cond INTERNAL
52-
friend class internal::factory<disposition>;
53-
/// @endcond
54-
};
51+
// /// @cond INTERNAL
52+
// friend class internal::factory<disposition>;
53+
// /// @endcond
54+
// };
5555

5656
/// The base class for delivery and tracker.
5757
class transfer : public internal::object<pn_delivery_t> {
@@ -105,8 +105,8 @@ class transfer : public internal::object<pn_delivery_t> {
105105
/// Get user data from this transfer.
106106
PN_CPP_EXTERN void* user_data() const;
107107

108-
PN_CPP_EXTERN disposition remote();
109-
PN_CPP_EXTERN disposition local();
108+
// PN_CPP_EXTERN disposition remote();
109+
// PN_CPP_EXTERN disposition local();
110110

111111
/// @cond INTERNAL
112112
friend class internal::factory<transfer>;

cpp/src/container.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "proton/uuid.hpp"
2929

3030
#include "proactor_container_impl.hpp"
31-
#include <vector>
3231

3332
namespace proton {
3433

@@ -46,10 +45,6 @@ returned<connection> container::connect(const std::string &url) {
4645
return connect(url, connection_options());
4746
}
4847

49-
transaction container::declare_transaction(proton::connection conn, proton::transaction_handler &handler, bool settle_before_discharge) {
50-
return impl_->declare_transaction(conn, handler, settle_before_discharge);
51-
}
52-
5348
returned<sender> container::open_sender(const std::string &url) {
5449
return open_sender(url, proton::sender_options(), connection_options());
5550
}

0 commit comments

Comments
 (0)