-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
346 lines (266 loc) · 9.36 KB
/
demo.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "demo.h"
#include "ui_demo.h"
#include "servermanager.h"
#include "commands.h"
#include <thread>
#include <chrono>
//globals for demo usage
static std::unique_ptr<CCommands> g_commands(nullptr);
//using chrono literals
using namespace std::chrono_literals;
CDemo::CDemo(QWidget *parent) :
QDialog(parent),
CLogable("DemoLogger"),
ui(new Ui::CDemo),
m_serverManager(CServerManager::getReference()),
m_miner(std::make_shared<SMiner>()),
m_player(std::make_shared<SPlayer>()),
m_logReader(std::make_shared<SLoggingTask>()),
m_logFile("bitcoinServer.log"),
m_stream(&m_logFile)
{
ui->setupUi(this);
//connect the logging task to the actual ui container
QObject::connect(m_logReader.get(), SIGNAL(loggerTrigger(QString)), this, SLOT(drawLogger(QString)));
}
void CDemo::show()
{
//init commands global
m_logFile.open(QIODevice::ReadOnly|QIODevice::Text);
m_logFile.seek(m_logFile.size()); //go to the end of the file
if (!g_commands)
{
g_commands.reset(new CCommands);
}
std::string errMsg("");
LOGGER_HELPER(INFO, errMsg,
std::string("==================================================================\n\t\t") +
"!!!!DEMO START!!!! \n" +
"==================================================================");
//start the logreader task
m_logReaderTask = std::make_unique<std::thread>
(std::thread([this]()
{
this->m_logReader->mainFunc(this->m_stream);
}));
m_logReader->start();
m_logReaderTask->detach();
QDialog::show();
}
CDemo::~CDemo()
{
delete ui;
}
void CDemo::on_pushButton_clicked()
{
m_miner->stop();
m_player->stop();
m_logReader->stop();
hide();
}
void CDemo::on_pushButton_2_clicked()
{
std::string errMsg("");
auto servers = g_commands->getActiveServerList();
if (servers.size() > 1)
{
//create miner and players
auto miner = servers[0];
servers.pop_front(); //remove the miner
//create the sync point
m_playersFeture = m_minerPromise.get_future();
//first step is to register all servers
g_commands->regAllServer();
//now create accounts for all servers
g_commands->createAddForAll();
//creating the demo taks
m_minerTask = std::make_unique<std::thread>
(std::thread([this, miner, servers]()
{
(this->m_miner)->minerMain(miner, std::move(servers), this->m_minerPromise);
}));
m_playerTask = std::make_unique<std::thread>
(std::thread([this, servers]()
{
(this->m_player)->playerMain(this->m_miner, std::move(servers), this->m_playersFeture);
}));
m_minerTask->detach();
m_playerTask->detach();
}
else
{
LOGGER_HELPER(ERROR, errMsg, "Not enugh active server for demo to run properly , need at least 2 server");
}
}
void CDemo::SMiner::distCash()
{
std::string errMsg("");
auto balance = g_commands->getBalance(miner, false);
LOGGER_HELPER(INFO, errMsg, "Distrebuting [ ", balance, " ] bitcoins between users");
for (auto serv : players)
{
auto amount = CDemo::getAmount(balance);
balance -= amount;
LOGGER_HELPER(INFO, errMsg, QString("Sending [ "), amount, " ] coins to server [ ", serv , " ]");
LOGGER_HELPER(INFO, errMsg, QString("Amount left"), balance, " ]");
g_commands->sendCoins(miner, serv, amount, false);
//exit loop if no more bitcoins left.
if (balance <= 0)
{
break;
}
}
}
void CDemo::SMiner::init(qint32 a_miner, QVector<quint32> a_players)
{
std::string errMsg("");
LOGGER_HELPER(INFO, errMsg, "Starting init proccess");
miner = a_miner;
players = a_players;
setCount();
//mine 102 blocks and get 100 bitcoins
g_commands->mine(miner, 102);
LOGGER_HELPER(INFO, errMsg, "Mined 102 blocks and now the miner has a total value of 100 bitcoins");
//distrebute cash between active players
distCash();
//now we have to mine 6 more block so that the distrebution will work
g_commands->mine(miner, 6);
}
void CDemo::SMiner::printBalanceOfAllPlayers()
{
std::string errMsg("");
auto balance = g_commands->getBalance(miner, false);
LOGGER_HELPER(INFO, errMsg, QString("The balance of the Miner is ["), balance , "]");
for (auto ind : players)
{
balance = g_commands->getBalance(ind, false);
LOGGER_HELPER(INFO, errMsg, QString("The balance of the player ["), ind, "] is [", balance , "]");
}
}
void CDemo::SMiner::minerMain(qint32 a_miner, QVector<quint32> a_players, std::promise<void>& p)
{
const auto SLEEP_TIME(2s);
const auto MAX_MININGS(6);
static bool firstTime{true};
qint32 minings(1);
std::string errMsg("");
init(a_miner, std::move(a_players));
//after init is done release the players
p.set_value();
LOGGER_HELPER(INFO, errMsg, "Miner started working");
//while flase
while (!stopMe.test_and_set())
{
if (operCount.load() >= currMaxOpers)
{
g_commands->mine(miner, 1); //mine one block
reset();
setCount();
minings++;
if (minings == MAX_MININGS)
{
//get current mining information
auto miningInfo = g_commands->getMiningInfo(miner);
minings = 0;
LOGGER_HELPER(INFO, errMsg, "Current mining info: [ ", miningInfo, " ]");
if (firstTime)
{
//distrebute earnings between the players
//distCash();
firstTime = false;
}
}
printBalanceOfAllPlayers();
}
else
{
std::this_thread::sleep_for(SLEEP_TIME);
}
stopMe.clear();
}
}
void CDemo::SPlayer::init(CDemo::TMinerSptr a_miner, QVector<quint32> a_players)
{
std::string errMsg("");
LOGGER_HELPER(INFO, errMsg, "Starting init proccess");
miner = a_miner;
players = std::move(a_players);
vectSize = players.size();
LOGGER_HELPER(INFO, errMsg, QString("init players ["), players,
"] Num of players [ ", vectSize, "]");
}
void CDemo::SPlayer::playerMain(TMinerSptr a_miner, QVector<quint32> a_players, std::future<void>& f)
{
const auto SLEEP_TIME(500ms);
std::string errMsg("");
qint32 playerSndr{DONT_CARE};
qint32 playerRcvr{DONT_CARE};
init(std::move(a_miner), std::move(a_players));
LOGGER_HELPER(INFO, errMsg, "Players init proccess ok waiting for miner");
f.wait();
LOGGER_HELPER(INFO, errMsg, "Miner and players are now synced starting normal activity");
//while flase
while (!stopMe.test_and_set())
{
std::array<qint32, 2> peeked = {DONT_CARE, DONT_CARE};
peeked[0] = peekPlayer(peeked.front());
peeked[1] = peekPlayer(peeked.front());
static constexpr quint32 PEEKED_SIZE{peeked.size()-1};
static CUnfiformRandomInt playerPeeker(0, PEEKED_SIZE);
auto sndrInd = playerPeeker.getNumber();
playerSndr = players[peeked[sndrInd]];
playerRcvr = players[peeked[PEEKED_SIZE - sndrInd]]; // if player sndr was 0 player rcvr will be 1 else 0
auto balance = g_commands->getBalance(playerSndr, false);
auto balanceRec = g_commands->getBalance(playerRcvr, false);
LOGGER_HELPER(INFO, errMsg, QString("peeked player ["), playerSndr , "] with balance of [", balance, "] ",
"as sender and player [", playerRcvr, "] with balance of [", balanceRec, "] as the reciver");
//only if this player has cash
if (balance > 0.0009)
{
auto amount = CDemo::getAmount(balance);
g_commands->sendCoins(playerSndr, playerRcvr, amount, false);
miner->add();
balance = g_commands->getBalance(playerSndr, false); // get the new balance of the sender
LOGGER_HELPER(INFO, errMsg, QString("Sending [ "), amount, " ] coins from server [ ", playerSndr, "] to server [ ",
playerRcvr , " ] ", QString("Amount left ["), balance, " ]");
}
stopMe.clear();
std::this_thread::sleep_for(SLEEP_TIME);
}
}
void CDemo::on_pushButton_3_clicked()
{
m_miner->stop();
m_player->stop();
m_logReader->stop();
}
void SLoggingTask::mainFunc(QTextStream& stream)
{
//read the first demo notice
QString str = stream.readAll();
emit loggerTrigger(str);
while (m_runLogger)
{
str = stream.readLine(); //each time rad only one log enterance
//only if theres somthing to print and its from the currect loggers print to screen
if ((str.size() != 0) &&
((str.contains("PlayersLogger")) || (str.contains("MinerLogger")) ||
(str.contains("DemoLogger"))) )
{
emit loggerTrigger(str);
std::this_thread::sleep_for(2s); //sleep for 2 seconds
}
}
//set it to run again
m_runLogger = true;
}
//this is the slot that will be connected to the log reader task
void CDemo::drawLogger(QString str)
{
static auto pTextEdit = ui->textEdit;
pTextEdit->insertPlainText("\n");
pTextEdit->insertPlainText(str);
auto cursor = pTextEdit->textCursor();
cursor.setPosition(QTextCursor::Start);
pTextEdit->setTextCursor(cursor);
}