Skip to content

Commit ea79f47

Browse files
astitcherDreamPearl
authored andcommitted
WIP: Fix up to transactions work:
* Make it compile * Make it fit the existing software structure better
1 parent baa68ce commit ea79f47

File tree

14 files changed

+131
-94
lines changed

14 files changed

+131
-94
lines changed

cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ set(qpid-proton-cpp-source
120120
src/terminus.cpp
121121
src/timestamp.cpp
122122
src/tracker.cpp
123+
src/transaction.cpp
123124
src/transfer.cpp
124125
src/transport.cpp
125126
src/type_id.cpp

cpp/examples/tx_send.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
4545
int confirmed = 0;
4646
proton::container *container;
4747
proton::transaction_handler transaction_handler;
48-
proton::Transaction *transaction;
48+
proton::transaction *transaction;
4949
proton::connection connection;
5050
public:
5151
tx_send(const std::string &s, int c, int b):
@@ -59,7 +59,7 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
5959
c.declare_transaction(connection, transaction_handler);
6060
}
6161

62-
void on_transaction_declared(proton::Transaction &t) override {
62+
void on_transaction_declared(proton::transaction &t) override {
6363
transaction = &t;
6464
send();
6565
}
@@ -92,7 +92,7 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
9292
confirmed += 1;
9393
}
9494

95-
void on_transaction_committed(proton::Transaction &t) override {
95+
void on_transaction_committed(proton::transaction &t) override {
9696
committed += current_batch;
9797
if(committed == total) {
9898
std::cout << "All messages committed";

cpp/include/proton/container.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ 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);
315+
PN_CPP_EXTERN transaction declare_transaction(proton::connection conn, proton::transaction_handler &handler, bool settle_before_discharge = false);
316316
private:
317317
/// Declare both v03 and v11 if compiling with c++11 as the library contains both.
318318
/// A C++11 user should never call the v03 overload so it is private in this case

cpp/include/proton/fwd.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class source_options;
5555
class ssl;
5656
class target_options;
5757
class tracker;
58+
class transaction;
59+
class transaction_handler;
5860
class transport;
5961
class url;
6062
class void_function0;

cpp/include/proton/tracker.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class tracker : public transfer {
4545
tracker(pn_delivery_t* d);
4646
/// @endcond
4747

48-
Transaction *transaction;
4948
public:
5049
/// Create an empty tracker.
5150
tracker() = default;
@@ -56,12 +55,6 @@ class tracker : public transfer {
5655
/// Get the tag for this tracker.
5756
PN_CPP_EXTERN binary tag() const;
5857

59-
// set_transaction here is a problem. As evry time we call it will change
60-
// the pointer in current object and update won' be reflected in any copies of this tracker.
61-
PN_CPP_EXTERN void set_transaction(Transaction *t);
62-
63-
PN_CPP_EXTERN Transaction* get_transaction() const;
64-
6558
/// @cond INTERNAL
6659
friend class internal::factory<tracker>;
6760
/// @endcond

cpp/include/proton/transaction.hpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,50 +34,38 @@
3434

3535
namespace proton {
3636

37+
class transaction_handler;
38+
3739
class
38-
PN_CPP_CLASS_EXTERN Transaction {
39-
proton::sender txn_ctrl;
40-
proton::transaction_handler handler;
41-
// TODO int
42-
int id = 0;
43-
proton::tracker _declare;
44-
proton::tracker _discharge;
45-
bool failed = false;
46-
std::vector<proton::tracker> pending;
40+
PN_CPP_CLASS_EXTERN transaction {
4741
public:
4842
// TODO:
49-
PN_CPP_EXTERN Transaction(proton::sender _txn_ctrl, proton::transaction_handler _handler, bool _settle_before_discharge = false);
50-
PN_CPP_EXTERN virtual ~Transaction();
43+
PN_CPP_EXTERN virtual ~transaction();
5144
PN_CPP_EXTERN virtual void commit();
5245
PN_CPP_EXTERN virtual void abort();
5346
PN_CPP_EXTERN virtual void declare();
54-
PN_CPP_EXTERN virtual void discharge(bool failed);
55-
PN_CPP_EXTERN virtual proton::tracker send_ctrl(proton::symbol descriptor, proton::value _value);
5647
PN_CPP_EXTERN virtual proton::tracker send(proton::sender s, proton::message msg);
57-
PN_CPP_EXTERN virtual void handle_outcome(proton::tracker t);
5848
};
5949

6050
class
6151
PN_CPP_CLASS_EXTERN transaction_handler {
62-
6352
public:
64-
PN_CPP_EXTERN transaction_handler();
6553
PN_CPP_EXTERN virtual ~transaction_handler();
6654

6755
/// Called when a local transaction is declared.
68-
PN_CPP_EXTERN virtual void on_transaction_declared(Transaction&);
56+
PN_CPP_EXTERN virtual void on_transaction_declared(transaction&);
6957

7058
/// Called when a local transaction is discharged successfully.
71-
PN_CPP_EXTERN virtual void on_transaction_committed(Transaction&);
59+
PN_CPP_EXTERN virtual void on_transaction_committed(transaction&);
7260

7361
/// Called when a local transaction is discharged unsuccessfully (aborted).
74-
PN_CPP_EXTERN virtual void on_transaction_aborted(Transaction&);
62+
PN_CPP_EXTERN virtual void on_transaction_aborted(transaction&);
7563

7664
/// Called when a local transaction declare fails.
77-
PN_CPP_EXTERN virtual void on_transaction_declare_failed(Transaction&);
65+
PN_CPP_EXTERN virtual void on_transaction_declare_failed(transaction&);
7866

7967
/// Called when the commit of a local transaction fails.
80-
PN_CPP_EXTERN virtual void on_transaction_commit_failed(Transaction&);
68+
PN_CPP_EXTERN virtual void on_transaction_commit_failed(transaction&);
8169
};
8270

8371
} // namespace proton

cpp/include/proton/transfer.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ class transfer : public internal::object<pn_delivery_t> {
7777
/// Return true if the transfer has been settled.
7878
PN_CPP_EXTERN bool settled() const;
7979

80+
// Set transaction
81+
PN_CPP_EXTERN void transaction(transaction& t);
82+
83+
PN_CPP_EXTERN class transaction* transaction() const;
84+
8085
/// Set user data on this transfer.
8186
PN_CPP_EXTERN void user_data(void* user_data) const;
8287

cpp/src/container.cpp

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
#include "proton/listen_handler.hpp"
2727
#include "proton/listener.hpp"
2828
#include "proton/uuid.hpp"
29-
#include "proton/target_options.hpp"
30-
#include "proton/sender_options.hpp"
31-
#include "proton/transaction.hpp"
3229

3330
#include "proactor_container_impl.hpp"
3431
#include <vector>
@@ -49,42 +46,8 @@ returned<connection> container::connect(const std::string &url) {
4946
return connect(url, connection_options());
5047
}
5148

52-
Transaction container::declare_transaction(proton::connection conn, proton::transaction_handler &handler, bool settle_before_discharge) {
53-
54-
proton::target_options t;
55-
class InternalTransactionHandler : public proton::messaging_handler {
56-
// TODO: auto_settle
57-
void on_tracker_settle(proton::tracker &t) override {
58-
if(t.get_transaction()) {
59-
t.get_transaction()->handle_outcome(t);
60-
}
61-
}
62-
63-
// TODO: Add on_unhandled function
64-
};
65-
66-
// TODO: Sender should be created only once. (May be use Singleton Class)
67-
// proton::target_options t;
68-
69-
std::vector<symbol> cap = {proton::symbol("amqp:local-transactions")};
70-
t.capabilities(cap);
71-
// Type PN_COORDINATOR value is 3. It is a special target identifying a transaction coordinator.
72-
// TODO: Change the type from int to enum.
73-
t.type(3);
74-
75-
proton::sender_options so;
76-
so.name("txn-ctrl");
77-
// Todo: Check the value, Or by deafult null?
78-
//so.source() ?
79-
so.target(t);
80-
InternalTransactionHandler internal_handler; // internal_handler going out of scope. Fix it
81-
so.handler(internal_handler);
82-
proton::sender s = conn.open_sender("does not matter", so);
83-
84-
settle_before_discharge = false;
85-
86-
return Transaction(s, handler, settle_before_discharge);
87-
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);
8851
}
8952

9053
returned<sender> container::open_sender(const std::string &url) {

cpp/src/contexts.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace proton {
4141

4242
class proton_handler;
4343
class connector;
44+
class transaction;
4445

4546
namespace io {class link_namer;}
4647

@@ -161,6 +162,7 @@ class transfer_context : public context {
161162
transfer_context() : user_data_(nullptr) {}
162163
static transfer_context& get(pn_delivery_t* s);
163164

165+
transaction* transaction_;
164166
void* user_data_;
165167
};
166168

cpp/src/proactor_container_impl.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "proton/listener.hpp"
2727
#include "proton/reconnect_options.hpp"
2828
#include "proton/ssl.hpp"
29+
#include "proton/target_options.hpp"
2930
#include "proton/transport.hpp"
3031
#include "proton/url.hpp"
3132

@@ -860,4 +861,44 @@ void container::impl::stop(const proton::error_condition& err) {
860861
pn_condition_free(error_condition);
861862
}
862863

864+
// TODO: declare this in separate internal header file
865+
extern transaction mk_transaction_impl(sender&, transaction_handler&, bool);
866+
867+
transaction container::impl::declare_transaction(proton::connection conn, proton::transaction_handler &handler, bool settle_before_discharge) {
868+
class InternalTransactionHandler : public proton::messaging_handler {
869+
// TODO: auto_settle
870+
void on_tracker_settle(proton::tracker &t) override {
871+
if(t.transaction()) {
872+
//t.transaction()->handle_outcome(t);
873+
}
874+
}
875+
876+
// TODO: Add on_unhandled function
877+
};
878+
879+
// TODO: Sender should be created only once. (May be use Singleton Class)
880+
// proton::target_options t;
881+
882+
proton::target_options t;
883+
std::vector<symbol> cap = {proton::symbol("amqp:local-transactions")};
884+
t.capabilities(cap);
885+
// Type PN_COORDINATOR value is 3. It is a special target identifying a transaction coordinator.
886+
// TODO: Change the type from int to enum.
887+
t.type(3);
888+
889+
proton::sender_options so;
890+
so.name("txn-ctrl");
891+
// Todo: Check the value, Or by deafult null?
892+
//so.source() ?
893+
so.target(t);
894+
InternalTransactionHandler internal_handler; // internal_handler going out of scope. Fix it
895+
so.handler(internal_handler);
896+
proton::sender s = conn.open_sender("does not matter", so);
897+
898+
settle_before_discharge = false;
899+
900+
return mk_transaction_impl(s, handler, settle_before_discharge);
901+
902+
}
903+
863904
}

0 commit comments

Comments
 (0)