forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSZeroReserveItems.h
201 lines (153 loc) · 6.1 KB
/
RSZeroReserveItems.h
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
/*
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/>.
*/
#ifndef RSZERORESERVEITEMS_H
#define RSZERORESERVEITEMS_H
#include "serialiser/rsserial.h"
#include "OrderBook.h"
#include "Credit.h"
#include "TransactionManager.h"
#include "RSZeroReserveItems.h"
class Payment;
const uint8_t QOS_PRIORITY_RS_ZERORESERVE = 3;
extern const uint16_t RS_SERVICE_TYPE_ZERORESERVE_PLUGIN;
extern const uint32_t CONFIG_TYPE_ZERORESERVE_PLUGIN;
/** Base class of all ZeroReserve Items, i.e. all classes related to sending stuff
* over the net and receiving from the net using RetroShare.
* TODO: This will have a protocol_version attribute once we reach version 1, and
* logic to handle newer / older protocol versions. Protocol version 1 will
* be the first version that handles multi-hop fully encrypted. Crypto will
* happen here, as will plausibility checking and checking for attacks like
* SQL injection so the inner classes can rely on the integrity of the data.
*/
class RsZeroReserveItem : public RsItem
{
public:
RsZeroReserveItem(void *data, uint32_t &size, uint8_t zeroreserve_subtype );
RsZeroReserveItem( uint8_t zeroreserve_subtype ) :
RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_ZERORESERVE_PLUGIN, zeroreserve_subtype)
{
setPriorityLevel(QOS_PRIORITY_RS_ZERORESERVE);
}
enum RS_PKT_SUBTYPE {
ZERORESERVE_ORDERBOOK_ITEM = 0x01,
ZERORESERVE_TX_ITEM,
ZERORESERVE_TX_INIT_ITEM,
ZERORESERVE_CREDIT_ITEM,
ZERORESERVE_MSG_ITEM,
ZR_REMOTE_PAYREQUEST_ITEM,
ZR_REMOTE_BUYREQUEST_ITEM,
ZR_REMOTE_TX_ITEM,
ZR_REMOTE_TX_INIT_ITEM
};
virtual ~RsZeroReserveItem() {};
virtual void clear() {};
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
virtual bool serialise(void *data, uint32_t & /*size */ );
virtual uint32_t serial_size() const { return headersOffset + 1; }
static const uint8_t PROTOCOL_VERSION;
static const uint8_t headersOffset;
static const int CURRENCY_STRLEN;
static const int HOLLERITH_LEN_SPEC;
protected:
uint32_t m_Offset;
};
class RsZeroReserveMsgItem: public RsZeroReserveItem
{
public:
enum MsgType {
REQUEST_ORDERBOOK,
SENT_ORDERBOOK,
INVALID
};
RsZeroReserveMsgItem() :RsZeroReserveItem( ZERORESERVE_MSG_ITEM ) {}
RsZeroReserveMsgItem(void *data,uint32_t size) ;
RsZeroReserveMsgItem(MsgType msgType, const std::string & msg );
virtual bool serialise(void *data,uint32_t& size) ;
virtual uint32_t serial_size() const ;
virtual ~RsZeroReserveMsgItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
MsgType getType(){ return m_msgType; }
const std::string & getMessage(){ return m_msg; }
private:
MsgType m_msgType;
std::string m_msg;
};
class RsZeroReserveTxItem: public RsZeroReserveItem
{
RsZeroReserveTxItem();
public:
RsZeroReserveTxItem( RS_PKT_SUBTYPE zeroreserve_subtype = ZERORESERVE_TX_ITEM) :
RsZeroReserveItem( (uint8_t)zeroreserve_subtype ) {}
RsZeroReserveTxItem(void *data,uint32_t size, RS_PKT_SUBTYPE zeroreserve_subtype = ZERORESERVE_TX_ITEM );
RsZeroReserveTxItem( TransactionManager::TxPhase phase, RS_PKT_SUBTYPE subtype = ZERORESERVE_TX_ITEM );
void setTxId( const std::string & id ){ m_txId = id; }
const std::string & getTxId(){ return m_txId; }
virtual bool serialise(void *data,uint32_t& size) ;
virtual uint32_t serial_size() const ;
virtual ~RsZeroReserveTxItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
TransactionManager::TxPhase getTxPhase() { return m_TxPhase; }
protected:
TransactionManager::TxPhase m_TxPhase;
std::string m_txId;
};
class RsZeroReserveInitTxItem: public RsZeroReserveTxItem
{
RsZeroReserveInitTxItem();
public:
RsZeroReserveInitTxItem(void *data,uint32_t size) ;
RsZeroReserveInitTxItem( Payment * payment );
virtual bool serialise(void *data,uint32_t& size) ;
virtual uint32_t serial_size() const ;
virtual ~RsZeroReserveInitTxItem();
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
Payment * getPayment();
TransactionManager::Role getRole() { return m_Role; }
private:
TransactionManager::Role m_Role;
Payment * m_payment;
};
class RsZeroReserveCreditItem: public RsZeroReserveItem
{
public:
RsZeroReserveCreditItem() :RsZeroReserveItem( ZERORESERVE_CREDIT_ITEM ) {}
RsZeroReserveCreditItem(void *data,uint32_t size) ;
RsZeroReserveCreditItem( Credit * credit) ;
virtual bool serialise(void *data,uint32_t& size) ;
virtual uint32_t serial_size() const ;
virtual ~RsZeroReserveCreditItem();
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
Credit * getCredit(){ return m_credit; }
private:
Credit * m_credit;
};
class RsZeroReserveSerialiser: public RsSerialType
{
public:
RsZeroReserveSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_ZERORESERVE_PLUGIN)
{
}
virtual ~RsZeroReserveSerialiser() {}
virtual uint32_t size (RsItem *item)
{
return dynamic_cast<RsZeroReserveItem *>(item)->serial_size() ;
}
virtual bool serialise (RsItem *item, void *data, uint32_t *size)
{
return dynamic_cast<RsZeroReserveItem *>(item)->serialise(data,*size) ;
}
virtual RsItem *deserialise(void *data, uint32_t *size);
};
#endif // RSZERORESERVEITEMS_H