forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TmLocalCohorte.cpp
126 lines (96 loc) · 3.58 KB
/
TmLocalCohorte.cpp
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
/*
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 "TmLocalCohorte.h"
#include "RSZeroReserveItems.h"
#include "Payment.h"
#include "p3ZeroReserverRS.h"
#include "ZeroReservePlugin.h"
TmLocalCohorte::TmLocalCohorte( const ZR::TransactionId & txId ) :
TransactionManager( txId )
{
}
TmLocalCohorte::~TmLocalCohorte()
{
delete m_payment;
}
void TmLocalCohorte::rollback()
{
}
ZR::RetVal TmLocalCohorte::init()
{
std::cerr << "Zero Reserve: TX Manager: Payment request for " << m_payment->getAmount() << " "
<< m_payment->getCurrency()
<< " received - Setting TX manager up as cohorte" << std::endl;
RsZeroReserveTxItem * reply;
ZR::RetVal retval;
if ( m_payment->init() == ZR::ZR_FAILURE ){
std::cerr << "Zero Reserve: initCohort(): Insufficient Credit - voting no" << std::endl;
reply = new RsZeroReserveTxItem( VOTE_NO );
retval = ZR::ZR_FAILURE;
}
else {
reply = new RsZeroReserveTxItem( VOTE_YES );
retval = ZR::ZR_SUCCESS;
}
reply->PeerId( m_payment->getCounterparty() );
reply->setTxId( m_TxId );
p3ZeroReserveRS * p3zs = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
p3zs->sendItem( reply ); // TODO: error handling
return retval;
}
ZR::RetVal TmLocalCohorte::processItem( RsZeroReserveTxItem * item )
{
RsZeroReserveTxItem * reply;
p3ZeroReserveRS * p3zs;
// TODO: Timeout
switch( item->getTxPhase() )
{
case QUERY:
{
RsZeroReserveInitTxItem * initItem = dynamic_cast< RsZeroReserveInitTxItem *> ( item );
if( m_Phase != INIT || initItem == NULL )
return abortTx( item );
m_Phase = QUERY;
m_payment = initItem->getPayment();
return init();
}
case COMMIT:
std::cerr << "Zero Reserve: TX Cohorte: Received Command: COMMIT" << std::endl;
if( m_Phase != QUERY )
return abortTx( item );
m_Phase = COMMIT;
reply = new RsZeroReserveTxItem( ACK_COMMIT );
reply->PeerId( m_payment->getCounterparty() );
reply->setTxId( m_TxId );
p3zs = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
p3zs->sendItem( reply );
m_payment->commit( m_TxId );
return ZR::ZR_FINISH;
case ABORT:
return ZR::ZR_FINISH;
default:
throw std::runtime_error( "Unknown Transaction Phase");
}
return ZR::ZR_SUCCESS;
}
ZR::RetVal TmLocalCohorte::abortTx( RsZeroReserveTxItem * item )
{
std::cerr << "Zero Reserve: TX Manger:Error happened. Aborting." << std::endl;
RsZeroReserveTxItem * reply = new RsZeroReserveTxItem( ABORT );
p3ZeroReserveRS * p3zs = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
reply->PeerId( m_payment->getCounterparty() );
reply->setTxId( m_TxId );
p3zs->sendItem( reply );
return ZR::ZR_FAILURE;
}