-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulNet.cc
84 lines (71 loc) · 1.83 KB
/
simulNet.cc
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
#include "simulNet.hh"
simulNet::simulNet(uint32_t serverNum)
: clusterSize(serverNum)
{
}
simulNet::~simulNet()
{
stop();
}
void simulNet::timerEvent(QTimerEvent *event)
{
auto ite = packageQuene.find(event->timerId());
if (ite != packageQuene.end())
{
killTimer(ite.key());
emit send(ite.value().first, ite.value().second);
packageQuene.remove(ite.key());
}
return QObject::timerEvent(event);
}
void simulNet::run()
{
moveToThread(&newThread);
newThread.start();
}
void simulNet::stop()
{
newThread.quit();
newThread.wait();
}
bool simulNet::sendPackage(const uniformRPC &package, uint32_t receiverID)
{
if (Settings::trueORfalse(Settings::packageLossRate))
{
QVector<packagePair> failedServers;
failedServers.append(Settings::makePair(package, receiverID));
emit returnPackageInfo(1, 1, failedServers);
return false;
}
else
{
int timerID = startTimer(abs(int(Settings::normalRandom_95(Settings::broadcastDelay_low, Settings::broadcastDelay_high))));
packageQuene.insert(timerID, std::make_pair(package, receiverID));
emit returnPackageInfo(1, 0, QVector<packagePair>());
return true;
}
}
bool simulNet::broadcast(const uniformRPC &package)
{
uint32_t from = dynamic_cast<Server *>(sender())->getID();
bool success = true;
uint32_t sum = 0;
uint32_t loss = 0;
QVector<packagePair> failedServers;
for (uint32_t i = 1; i <= clusterSize; ++i)
{
if (i == from)
{
continue;
}
++sum;
if (!sendPackage(package, i))
{
success = false;
failedServers.append(Settings::makePair(package, i));
++loss;
}
}
emit returnPackageInfo(sum, loss, failedServers);
return success;
}