Guest User

Untitled

a guest
Apr 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include "BoothServer.h"
  2. #include <QIODevice>
  3. #include <QTimer>
  4. #include "Packet.h"
  5. #include "config.h"
  6.  
  7.  
  8. BoothServer::BoothServer(QIODevice *ioDev, QObject *parent) :
  9.     QObject(parent),
  10.     ioDev(ioDev),
  11.     state(ServerInit),
  12.     curDeviceNum(0)
  13. {
  14.     start();
  15.     connect(ioDev, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
  16. }
  17.  
  18. void BoothServer::sendPacket(Packet &packet) {
  19.     packet.genChecksum();
  20.     ioDev->write(packet.getRawData(), packet.size());
  21. }
  22.  
  23. char BoothServer::curDeviceID() {
  24.     if(curDeviceNum < NUM_SERVICE_BOOTH) { return i + '0'; }
  25.     else if(curDeviceNum < NUM_SERVICE_BOOTH + NUM_TOKEN_BOOTH) { return curDeviceNum - NUM_SERVICE_BOOTH; }
  26.     else { assert(false); }
  27. }
  28.  
  29. char BoothServer::curDeviceType() {
  30.     if(curDeviceNum < NUM_SERVICE_BOOTH) { return STB_ALIAS_SERVICEBOOTH; }
  31.     else if(curDeviceNum < NUM_SERVICE_BOOTH + NUM_TOKEN_BOOTH) { return STB_ALIAS_TOKENBOOTH; }
  32.     else { assert(false); }
  33. }
  34.  
  35. void BoothServer::incCurDeviceNum() {
  36.     if(curDeviceNum == NUM_SERVICE_BOOTH + NUM_TOKEN_BOOTH) curDeviceNum = 0;
  37.     else curDeviceNum++;
  38. }
  39.  
  40. void BoothServer::start() {
  41.     Packet packet;
  42.  
  43.     packet.receiverType() = curDeviceType();
  44.     packet.receiverID() = curDeviceID();
  45.     packet.senderType() = STB_ALIAS_SERVER;
  46.     packet.senderID() = SELF_ID;
  47.     packet.serviceType() = STB_SERVICE_PING;
  48.     strcpy(packet.data(), "-----");
  49.     packet.extraByte() = '-';
  50.  
  51.     QTimer::singleShot(DEVICE_PING_TIMEOUT, this, SLOT(onDeviceTimeout()));
  52.  
  53.     state = ServerPingWait;
  54.     sendPacket(packet);
  55. }
  56.  
  57. void BoothServer::onReadyRead() {
  58.     static QByteArray buffer = "";
  59.     QByteArray b = ioDev->readAll();
  60.     save+=b;
  61.     buffer += b;
  62.     if(b.length()) QTimer::singleShot(500, this, SLOT(onReadyRead()));
  63.     else {
  64.         processIncomingData(buffer);
  65.         buffer = "";
  66.     }
  67. }
  68.  
  69. void BoothServer::processIncomingData(QByteArray data) {
  70.     //qDebug() << "Data : " << data << endl;
  71.     switch(state) {
  72.     case ServerInit:
  73.         qDebug() << "Garbase value on init" << endl;
  74.         break;
  75.     case ServerPingWait:
  76.         Packet p(data.constData(), data.size());
  77.  
  78.         if(!p.isValidChecksum()) {//error check
  79.             qDebug() << "Checksum error" << endl;
  80.             break;
  81.         }
  82.  
  83.         if(p.receiverType() != STB_ALIAS_SERVER || p.senderID() != SELF_ID) {
  84.             qDebug() << "Me not receiver :-o" << endl;
  85.             break;
  86.         }
  87.  
  88.         if(p.senderType() != curDeviceType() || p.senderID() != curDeviceID()) {
  89.             qDebug() << "Sender is not expected device" << endl;
  90.             break;
  91.         }
  92.  
  93.         if(p.senderType() == STB_ALIAS_SERVICEBOOTH) handleServiceBoothPacket(packet);
  94.         else if(p.senderType() == STB_ALIAS_TOKENBOOTH) handleTokenBoothPacket(packet);
  95.         else assert(false);
  96.  
  97.         break;
  98.     }
  99. }
  100.  
  101. void BoothServer::handleServiceBoothPacket(Packet &packet) {
  102.     switch(packet.serviceType()) {
  103.     case STB_SERVICE_INIT:
  104.         break;
  105.     }
  106. }
  107.  
  108. void BoothServer::handlePacketBoothPacket(Packet &packet) {
  109.  
  110. }
Add Comment
Please, Sign In to add comment