Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BoothServer.h"
- #include <QIODevice>
- #include <QTimer>
- #include "Packet.h"
- #include "config.h"
- BoothServer::BoothServer(QIODevice *ioDev, QObject *parent) :
- QObject(parent),
- ioDev(ioDev),
- state(ServerInit),
- curDeviceNum(0)
- {
- start();
- connect(ioDev, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
- }
- void BoothServer::sendPacket(Packet &packet) {
- packet.genChecksum();
- ioDev->write(packet.getRawData(), packet.size());
- }
- char BoothServer::curDeviceID() {
- if(curDeviceNum < NUM_SERVICE_BOOTH) { return i + '0'; }
- else if(curDeviceNum < NUM_SERVICE_BOOTH + NUM_TOKEN_BOOTH) { return curDeviceNum - NUM_SERVICE_BOOTH; }
- else { assert(false); }
- }
- char BoothServer::curDeviceType() {
- if(curDeviceNum < NUM_SERVICE_BOOTH) { return STB_ALIAS_SERVICEBOOTH; }
- else if(curDeviceNum < NUM_SERVICE_BOOTH + NUM_TOKEN_BOOTH) { return STB_ALIAS_TOKENBOOTH; }
- else { assert(false); }
- }
- void BoothServer::incCurDeviceNum() {
- if(curDeviceNum == NUM_SERVICE_BOOTH + NUM_TOKEN_BOOTH) curDeviceNum = 0;
- else curDeviceNum++;
- }
- void BoothServer::start() {
- Packet packet;
- packet.receiverType() = curDeviceType();
- packet.receiverID() = curDeviceID();
- packet.senderType() = STB_ALIAS_SERVER;
- packet.senderID() = SELF_ID;
- packet.serviceType() = STB_SERVICE_PING;
- strcpy(packet.data(), "-----");
- packet.extraByte() = '-';
- QTimer::singleShot(DEVICE_PING_TIMEOUT, this, SLOT(onDeviceTimeout()));
- state = ServerPingWait;
- sendPacket(packet);
- }
- void BoothServer::onReadyRead() {
- static QByteArray buffer = "";
- QByteArray b = ioDev->readAll();
- save+=b;
- buffer += b;
- if(b.length()) QTimer::singleShot(500, this, SLOT(onReadyRead()));
- else {
- processIncomingData(buffer);
- buffer = "";
- }
- }
- void BoothServer::processIncomingData(QByteArray data) {
- //qDebug() << "Data : " << data << endl;
- switch(state) {
- case ServerInit:
- qDebug() << "Garbase value on init" << endl;
- break;
- case ServerPingWait:
- Packet p(data.constData(), data.size());
- if(!p.isValidChecksum()) {//error check
- qDebug() << "Checksum error" << endl;
- break;
- }
- if(p.receiverType() != STB_ALIAS_SERVER || p.senderID() != SELF_ID) {
- qDebug() << "Me not receiver :-o" << endl;
- break;
- }
- if(p.senderType() != curDeviceType() || p.senderID() != curDeviceID()) {
- qDebug() << "Sender is not expected device" << endl;
- break;
- }
- if(p.senderType() == STB_ALIAS_SERVICEBOOTH) handleServiceBoothPacket(packet);
- else if(p.senderType() == STB_ALIAS_TOKENBOOTH) handleTokenBoothPacket(packet);
- else assert(false);
- break;
- }
- }
- void BoothServer::handleServiceBoothPacket(Packet &packet) {
- switch(packet.serviceType()) {
- case STB_SERVICE_INIT:
- break;
- }
- }
- void BoothServer::handlePacketBoothPacket(Packet &packet) {
- }
Add Comment
Please, Sign In to add comment