forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3ZeroReserveRS.cpp
284 lines (243 loc) · 9.64 KB
/
p3ZeroReserveRS.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
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 "p3ZeroReserverRS.h"
#include "Credit.h"
#include "Payment.h"
#include "zrtypes.h"
#include "Router.h"
#include "ZeroReservePlugin.h"
#include "ZeroReserveDialog.h"
#include "MyOrders.h"
#include "pqi/p3linkmgr.h"
#include <QMessageBox>
#include <iostream>
// after getting data from 3 peers, we believe we're complete
static const int INIT_THRESHOLD = 3;
p3ZeroReserveRS::p3ZeroReserveRS( RsPluginHandler *pgHandler, OrderBook * bids, OrderBook * asks, RsPeers* peers ) :
RsPQIService( RS_SERVICE_TYPE_ZERORESERVE_PLUGIN, CONFIG_TYPE_ZERORESERVE_PLUGIN, 0, pgHandler ),
m_bids(bids),
m_asks(asks),
m_peers(peers),
m_initialized( 0 )
{
addSerialType(new RsZeroReserveSerialiser());
pgHandler->getLinkMgr()->addMonitor( this );
}
void p3ZeroReserveRS::statusChange(const std::list< pqipeer > &plist)
{
std::cerr << "Zero Reserve: Status changed:" << std::endl;
if( m_initialized < INIT_THRESHOLD ){
for (std::list< pqipeer >::const_iterator it = plist.begin(); it != plist.end(); it++ ){
if( RS_PEER_CONNECTED & (*it).actions ){
RsZeroReserveMsgItem * item = new RsZeroReserveMsgItem( RsZeroReserveMsgItem::REQUEST_ORDERBOOK, "" );
item->PeerId( (*it).id );
sendItem( item );
}
}
}
// give any newly connected peer updated credit info - we might have changed it.
for (std::list< pqipeer >::const_iterator peerIt = plist.begin(); peerIt != plist.end(); peerIt++ ){
if( RS_PEER_CONNECTED & (*peerIt).actions ){
Credit::CreditList cl;
Credit::getCreditList( cl, (*peerIt).id );
for( Credit::CreditList::const_iterator creditIt = cl.begin(); creditIt != cl.end(); creditIt++){
sendCredit( *creditIt );
}
}
}
ZeroReserveDialog * mainWin = static_cast< ZeroReserveDialog * >( g_ZeroReservePlugin->qt_page() );
mainWin->updateFriendList();
}
int p3ZeroReserveRS::tick()
{
processIncoming();
janitor();
return 0;
}
void p3ZeroReserveRS::janitor()
{
static time_t lastCleanup = 0;
time_t t = time( 0 );
if( t - lastCleanup < 10 ) return; // cleanup once in 10 sec
lastCleanup = t;
m_asks->timeoutOrders();
m_bids->timeoutOrders();
TransactionManager::timeout();
}
void p3ZeroReserveRS::processIncoming()
{
RsItem *item = NULL;
while(NULL != (item = recvItem())){
switch( item->PacketSubType() )
{
case RsZeroReserveItem::ZERORESERVE_ORDERBOOK_ITEM:
handleOrder( dynamic_cast<RsZeroReserveOrderBookItem*>( item ) );
break;
case RsZeroReserveItem::ZERORESERVE_TX_INIT_ITEM:
case RsZeroReserveItem::ZERORESERVE_TX_ITEM:
TransactionManager::handleTxItem( dynamic_cast<RsZeroReserveTxItem*>( item ) );
break;
case RsZeroReserveItem::ZERORESERVE_CREDIT_ITEM:
handleCredit( dynamic_cast<RsZeroReserveCreditItem*>( item ) );
break;
case RsZeroReserveItem::ZERORESERVE_MSG_ITEM:
handleMessage( dynamic_cast<RsZeroReserveMsgItem*>( item ) );
break;
case RsZeroReserveItem::ZR_REMOTE_PAYREQUEST_ITEM:
handlePaymentRequest( dynamic_cast<RSZRPayRequestItem*>( item ) );
break;
case RsZeroReserveItem::ZR_REMOTE_BUYREQUEST_ITEM:
handleBuyRequest( dynamic_cast < RSZRBuyRequestItem *>( item ) );
break;
case RsZeroReserveItem::ZR_REMOTE_TX_ITEM:
case RsZeroReserveItem::ZR_REMOTE_TX_INIT_ITEM:
TransactionManager::handleTxItem( dynamic_cast<RSZRRemoteTxItem*>( item ) );
break;
default:
std::cerr << "Zero Reserve: Received Item unknown" << std::endl;
}
delete item;
}
}
void p3ZeroReserveRS::handleBuyRequest( RSZRBuyRequestItem * item )
{
#warning "IMPLEMENT"
}
ZR::RetVal p3ZeroReserveRS::sendBuyMsg( const ZR::VirtualAddress & ourAddress, const ZR::VirtualAddress & theirAddress, const ZR::ZR_Number & amount )
{
#warning "IMPLEMENT"
}
void p3ZeroReserveRS::sendOrderBook( const std::string & uid )
{
for( OrderBook::OrderIterator it = m_asks->begin(); it != m_asks->end(); it++ ){
sendOrder( uid, *it );
}
for( OrderBook::OrderIterator it = m_bids->begin(); it != m_bids->end(); it++ ){
sendOrder( uid, *it );
}
sendItem( new RsZeroReserveMsgItem( RsZeroReserveMsgItem::SENT_ORDERBOOK, "" ) );
}
void p3ZeroReserveRS::handleMessage( RsZeroReserveMsgItem *item )
{
std::cerr << "Zero Reserve: Received Message Item" << std::endl;
RsZeroReserveMsgItem::MsgType msgType = item->getType();
switch( msgType ){
case RsZeroReserveMsgItem::REQUEST_ORDERBOOK:
sendOrderBook( item->PeerId() );
break;
case RsZeroReserveMsgItem::SENT_ORDERBOOK:
m_initialized++;
break;
default:
std::cerr << "Zero Reserve: Received unknown message" << std::endl;
}
}
void p3ZeroReserveRS::handleOrder(RsZeroReserveOrderBookItem *item)
{
std::cerr << "Zero Reserve: Received Orderbook Item" << std::endl;
ZR::RetVal result;
item->print( std::cerr );
OrderBook::Order * order = item->getOrder();
if( !Router::Instance()->hasRoute( order->m_order_id ) ){
Router::Instance()->addRoute( order->m_order_id, item->PeerId() );
}
if( order->m_orderType == OrderBook::Order::ASK ){
result = m_asks->processOrder( order );
}
else{
result = m_bids->processOrder( order );
}
if( ZR::ZR_SUCCESS == result ){
publishOrder( order );
}
}
void p3ZeroReserveRS::handleCredit(RsZeroReserveCreditItem *item)
{
std::cerr << "Zero Reserve: Received Credit Item" << std::endl;
Credit * otherCredit = item->getCredit();
otherCredit->m_id = item->PeerId();
Credit ourCredit( otherCredit->m_id, otherCredit->m_currency );
if( ourCredit.m_our_credit != otherCredit->m_our_credit ){
otherCredit->updateOurCredit();
}
if( ourCredit.m_balance != otherCredit->m_balance ){
std::cerr << "Zero Reserve: " << "Different balance: " << otherCredit->m_id << " has " << otherCredit->m_balance
<< " we have " << ourCredit.m_balance << std::endl;
}
}
bool p3ZeroReserveRS::sendCredit( Credit * credit )
{
std::cerr << "Zero Reserve: Sending Credit item to " << credit->m_id << std::endl;
RsZeroReserveCreditItem * item = new RsZeroReserveCreditItem( credit );
if(!item){
std::cerr << "Cannot allocate RsZeroReserveCreditItem !" << std::endl;
return false ;
}
sendItem( item );
item->print( std::cerr, 16 );
return true;
}
bool p3ZeroReserveRS::sendOrder( const std::string& peer_id, OrderBook::Order * order )
{
std::cerr << "Zero Reserve: Sending order to " << peer_id << std::endl;
RsZeroReserveOrderBookItem * item = new RsZeroReserveOrderBookItem( order );
if(!item){
std::cerr << "Cannot allocate RsZeroReserveOrderBookItem !" << std::endl;
return false ;
}
item->PeerId( peer_id );
sendItem( item );
return true;
}
void p3ZeroReserveRS::publishOrder( OrderBook::Order * order )
{
std::list< std::string > sendList;
m_peers->getOnlineList(sendList);
for(std::list< std::string >::const_iterator it = sendList.begin(); it != sendList.end(); it++ ){
if( (*it) == getOwnId() ) continue;
sendOrder( *it, order );
}
}
void p3ZeroReserveRS::sendRemote( const ZR::VirtualAddress & address, ZR::ZR_Number amount, const std::string & currency )
{
std::list< std::string > sendList;
m_peers->getOnlineList(sendList);
for(std::list< std::string >::const_iterator it = sendList.begin(); it != sendList.end(); it++ ){
if( (*it) == getOwnId() ) continue;
RSZRPayRequestItem * item = new RSZRPayRequestItem( address, amount, currency );
item->PeerId( *it );
sendItem( item );
}
}
void p3ZeroReserveRS::handlePaymentRequest( RSZRPayRequestItem * item )
{
if( Router::Instance()->hasRoute( item->getAddress() ) ){
return;
}
Credit credit( item->PeerId(), item->getCurrency() );
credit.loadPeer();
if( credit.getMyAvailable() == 0 ) return;
std::cerr << "Zero Reserve: Adding address " << item->getAddress() << " to the routing table" << std::endl;
Currency::CurrencySymbols currency = Currency::getCurrencyBySymbol( item->getCurrency() );
Router::Instance()->addRoute( item->getAddress(), item->PeerId() );
Payment::addRequest( item->getAddress(), Payment::Request( item->getAmount(), currency ) );
std::list< std::string > sendList;
m_peers->getOnlineList(sendList);
for(std::list< std::string >::const_iterator it = sendList.begin(); it != sendList.end(); it++ ){
if( (*it) == getOwnId() || (*it) == item->PeerId() ) continue;
RSZRPayRequestItem * republishItem = new RSZRPayRequestItem( *item );
republishItem->PeerId( *it );
sendItem( republishItem );
}
}