-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboothserver.cpp
112 lines (87 loc) · 2.89 KB
/
boothserver.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
#include "boothserver.h"
#include <QIODevice>
#include <QTimer>
#include <QDebug>
#include "packet.h"
#include "config.h"
#include "booth.h"
#include "servicecategory.h"
#include "boothservice.h"
#include "boothtoken.h"
BoothServer::BoothServer(QIODevice *ioDev, QObject *parent) :
QObject(parent),
ioDev(ioDev),
curBoothNum(0)
{
Booth *b = new BoothToken(3);
availBooths.push_back(b);
for(int i = 1; i <= NUM_SERVICE_BOOTH; i++) {
Booth *b = new BoothService(i);
availBooths.push_back(b);
}
waitTimer.setSingleShot(true);
connect(&waitTimer, SIGNAL(timeout()),this, SLOT(onDeviceTimeout()));
connect(ioDev, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
pingNextBooth();
}
BoothServer::~BoothServer() {
foreach(Booth *booth, availBooths) delete booth;
}
void BoothServer::onReadyRead() {
static QByteArray buffer = "";
QByteArray b = ioDev->readAll();
buffer += b;
if(b.length()) QTimer::singleShot(STB_NEXTCHAR_INTERVAL, this, SLOT(onReadyRead()));
else if(buffer.size()) {
processIncomingData(buffer);
buffer = "";
}
}
void BoothServer::sendPacket(Packet packet) {
packet.senderID() = STB_SELF_ID;
packet.senderType() = STB_ALIAS_SERVER;
packet.genChecksum();
qDebug() << " Pre-Write : " << QByteArray(packet.getRawData(), packet.size() - 1);
ioDev->write(packet.getRawData(), packet.size());
qDebug() << "Post-Write : " << QByteArray(packet.getRawData(), packet.size() - 1);
}
Booth* BoothServer::currentBooth() { return availBooths[curBoothNum]; }
void BoothServer::incrementCurrentBooth() { curBoothNum = (curBoothNum + 1) % availBooths.size(); }
void BoothServer::pingNextBooth() {
incrementCurrentBooth();
pingCurrentBooth();
}
void BoothServer::pingCurrentBooth() {
waitTimer.start(STB_TIMEOUT);
isWaiting = true;
sendPacket(currentBooth()->getPingPacket());
}
void BoothServer::onDeviceTimeout() {
currentBooth()->onTimout();
isWaiting = false;
pingNextBooth();
}
void BoothServer::processIncomingData(QByteArray data) {
//disconnect(ioDev, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
Packet p(data.constData(), data.size());
qDebug() << "Read Data : " << p.senderType()<< p.senderID() << data << endl;
if(!p.isValidChecksum()) {//error check
qDebug() << "Checksum error";
return;
}
if(p.receiverType() != STB_ALIAS_SERVER || p.receiverID() != STB_SELF_ID) {
qDebug() << "Me not receiver :-o";
return;
}
if(p.senderType() != currentBooth()->getType() || p.senderID()-'0' != currentBooth()->getID()) {
qDebug() << "Sender is not expected device";
return;
}
if(!isWaiting) {
qDebug() << "Why I received a packet!!!";
return;
}
isWaiting = false;
bool needNextSlot = currentBooth()->processPacket(p);
if(needNextSlot) pingCurrentBooth();
}