forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTmLocalCoordinator.cpp
More file actions
108 lines (83 loc) · 3.11 KB
/
TmLocalCoordinator.cpp
File metadata and controls
108 lines (83 loc) · 3.11 KB
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
/*
This file is part of the Zero Reserve Plugin for Retroshare.
Zero Reserve is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zero Reserve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Zero Reserve. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TmLocalCoordinator.h"
#include "p3ZeroReserverRS.h"
#include "ZeroReservePlugin.h"
#include "Payment.h"
unsigned int TmLocalCoordinator::sequence = 1;
const ZR::TransactionId TmLocalCoordinator::mkId()
{
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
std::ostringstream txId;
txId << p3zr->getOwnId() << ":" << ++sequence;
return txId.str();
}
TmLocalCoordinator::TmLocalCoordinator( Payment *payment ) :
TransactionManager( mkId() ),
m_payment( payment )
{
}
TmLocalCoordinator::~TmLocalCoordinator()
{
delete m_payment;
}
void TmLocalCoordinator::rollback()
{
}
ZR::RetVal TmLocalCoordinator::init()
{
std::cerr << "Zero Reserve: Setting TX manager up as coordinator. ID: " << m_TxId << std::endl;
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
if ( m_payment->init() == ZR::ZR_FAILURE ){
std::cerr << "Zero Reserve: Error, not enough Credit " << std::endl;
return ZR::ZR_FAILURE;
}
RsZeroReserveInitTxItem * initItem = new RsZeroReserveInitTxItem( m_payment );
initItem->setTxId( m_TxId );
p3zr->sendItem( initItem );
return ZR::ZR_SUCCESS;
}
ZR::RetVal TmLocalCoordinator::processItem( RsZeroReserveTxItem * item )
{
RsZeroReserveTxItem * reply;
p3ZeroReserveRS * p3zr;
// TODO: Timeout
switch( item->getTxPhase() )
{
case VOTE_YES:
std::cerr << "Zero Reserve: TX Coordinator: Received Vote: YES" << std::endl;
reply = new RsZeroReserveTxItem( COMMIT );
reply->PeerId( m_payment->getCounterparty() );
reply->setTxId( m_TxId );
p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
p3zr->sendItem( reply );
return ZR::ZR_SUCCESS;
case VOTE_NO:
return abortTx( item );
case ACK_COMMIT:
std::cerr << "Zero Reserve: TX Coordinator: Received Acknowledgement, Committing" << std::endl;
m_payment->commit( m_TxId );
return ZR::ZR_FINISH;
case ABORT:
return abortTx( item );
default:
throw std::runtime_error( "Unknown Transaction Phase");
}
return ZR::ZR_SUCCESS;
}
ZR::RetVal TmLocalCoordinator::abortTx( RsZeroReserveTxItem * item )
{
std::cerr << "Zero Reserve: TX Manger:Error happened. Aborting." << std::endl;
return ZR::ZR_FAILURE;
}