-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.h
274 lines (206 loc) · 5.99 KB
/
demo.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
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
#ifndef DEMO_H
#define DEMO_H
#include <QDialog>
#include "logable.h"
#include "servermanager.h"
#include <atomic>
#include <mutex>
#include <future>
#include <thread>
#include <QFile>
#include <QTextStream>
#include <random>
namespace Ui {
class CDemo;
struct SLoggingTask;
}
static const qint32 DONT_CARE(-1);
//uniformity random class .
class CUnfiformRandomReal
{
public:
CUnfiformRandomReal():
m_mt(m_rd()),
m_uniforDist(std::make_unique<std::uniform_real_distribution<qreal> >(0, 0))
{}
CUnfiformRandomReal(const qreal& lowLim, const qreal& upLim):
m_mt(m_rd()),
m_uniforDist(std::make_unique<std::uniform_real_distribution<qreal> >(lowLim, upLim))
{}
inline void setNewLimits(const qreal& lowLim, const qreal& upLim)
{
m_uniforDist = std::make_unique<std::uniform_real_distribution<qreal> >(lowLim, upLim);
}
inline qreal getNumber()
{
return (*(m_uniforDist.get()))(m_mt);
}
private:
std::random_device m_rd;
std::mt19937 m_mt;
std::unique_ptr<std::uniform_real_distribution<qreal> > m_uniforDist;
};
//uniformity random class .
class CUnfiformRandomInt
{
public:
CUnfiformRandomInt():
m_mt(m_rd()),
m_uniforDist(std::make_unique<std::uniform_int_distribution<quint32> >(0, 0))
{}
CUnfiformRandomInt(const quint32& lowLim, const quint32& upLim):
m_mt(m_rd()),
m_uniforDist(std::make_unique<std::uniform_int_distribution<quint32> >(lowLim, upLim))
{}
inline void setNewLimits(const quint32& lowLim, const quint32& upLim)
{
m_uniforDist = std::make_unique<std::uniform_int_distribution<quint32> >(lowLim, upLim);
}
inline quint32 getNumber()
{
return (*(m_uniforDist.get()))(m_mt);
}
private:
std::random_device m_rd;
std::mt19937 m_mt;
std::unique_ptr<std::uniform_int_distribution<quint32> > m_uniforDist;
};
//container calls for the logging operation we want it to have slots
//so it will inharite the QObject
struct SLoggingTask : public QObject
{
Q_OBJECT
public:
SLoggingTask():
m_runLogger(false)
{}
void mainFunc(QTextStream& stream);
inline void stop()
{
m_runLogger = false;
}
inline void start()
{
m_runLogger = true;
}
signals:
void loggerTrigger(QString str);
private:
bool m_runLogger;
};
class CDemo : public QDialog, CLogable
{
Q_OBJECT
public:
explicit CDemo(QWidget *parent = 0);
void show();
~CDemo();
static qreal getAmount(qreal& currBalance)
{
CUnfiformRandomReal rd(0, currBalance);
return rd.getNumber();
}
public slots:
void drawLogger(QString str);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::CDemo *ui;
CServerManager& m_serverManager;
struct SMiner : CLogable
{
SMiner() :
CLogable("MinerLogger")
{}
inline void add()
{
std::string errMsg("");
static constexpr quint32 ONE{1};
auto num = operCount.fetch_add(ONE);
LOGGER_HELPER(INFO, errMsg, "performed: [", num, "] operations");
//std::atomic_fetch_add(&operCount, &ONE);
}
inline void reset()
{
operCount.store(0);
}
inline void stop()
{
std::string errMsg("");
LOGGER_HELPER(INFO, errMsg, "Stoping Miner");
stopMe.test_and_set();
}
void minerMain(qint32 a_miner, QVector<quint32> a_players, std::promise<void>& p);
void printBalanceOfAllPlayers();
private:
std::atomic<quint32> operCount;
std::atomic_flag stopMe = ATOMIC_FLAG_INIT;
qint32 miner;
QVector<quint32> players;
quint32 currMaxOpers;
std::mutex mut;
CUnfiformRandomReal m_realRD;
void distCash();
void init(qint32 a_miner, QVector<quint32> a_players);
inline void setCount()
{
std::string errMsg("");
static const qint32 MAX_OPERS(50);
static CUnfiformRandomInt countGenerator(0, MAX_OPERS);
currMaxOpers = countGenerator.getNumber();
LOGGER_HELPER(INFO, errMsg, "changed the number of operations needed for mining to: [", currMaxOpers, "]");
}
};
using TMinerSptr = std::shared_ptr<SMiner>;
struct SPlayer : public CLogable
{
SPlayer():
CLogable("PlayersLogger")
{}
inline void stop()
{
std::string errMsg("");
LOGGER_HELPER(INFO, errMsg, "Stoping Players");
stopMe.test_and_set();
}
void playerMain(TMinerSptr a_miner, QVector<quint32> a_players, std::future<void>& f);
private:
QVector<quint32> players;
TMinerSptr miner = nullptr;
std::atomic_flag stopMe = ATOMIC_FLAG_INIT;
qint32 vectSize;
void init(TMinerSptr a_miner, QVector<quint32> a_players);
inline qint32 peekPlayer(qint32 currIndex)
{
static const qint32 PLAYER_SIZE{players.size() - 1};
static CUnfiformRandomInt playerPeeker(0, PLAYER_SIZE);
qint32 player = playerPeeker.getNumber();
if ((currIndex != DONT_CARE) && (currIndex == player))
{
if (player > 0)
{
--player;
}
else
{
++player;
}
}
return player;
}
};
private:
std::shared_ptr<SMiner> m_miner;
std::shared_ptr<SPlayer> m_player;
std::shared_ptr<SLoggingTask> m_logReader;
std::unique_ptr<std::thread> m_minerTask;
std::unique_ptr<std::thread> m_playerTask;
std::unique_ptr<std::thread> m_logReaderTask;
QFile m_logFile;
QTextStream m_stream;
std::promise<void> m_minerPromise;
std::future<void> m_playersFeture;
};
#endif // DEMO_H