QTfrontend/hwmap.cpp
changeset 168 679e9b8912f5
parent 164 92cff18a3ab6
child 169 a78d4a552500
equal deleted inserted replaced
167:805fa9a27e9e 168:679e9b8912f5
     2 
     2 
     3 #include "hwconsts.h"
     3 #include "hwconsts.h"
     4 
     4 
     5 #include <QMessageBox>
     5 #include <QMessageBox>
     6 
     6 
       
     7 #include <QMutex>
       
     8 #include <QDebug>
       
     9 
       
    10 #include <QList>
       
    11 
       
    12 int HWMap::isBusy(0); // initialize static variable
       
    13 QList<HWMap*> srvsList;
       
    14 QMutex tcpSrvMut;
       
    15 
     7 HWMap::HWMap() :
    16 HWMap::HWMap() :
     8   m_isStarted(false)
    17   m_isStarted(false)
       
    18 {
       
    19 }
       
    20 
       
    21 HWMap::~HWMap()
     9 {
    22 {
    10 }
    23 }
    11 
    24 
    12 void HWMap::getImage(std::string seed) 
    25 void HWMap::getImage(std::string seed) 
    13 {
    26 {
    22 
    35 
    23   IPCSocket->close();
    36   IPCSocket->close();
    24   IPCSocket->deleteLater();
    37   IPCSocket->deleteLater();
    25   IPCSocket = 0;
    38   IPCSocket = 0;
    26   IPCServer->close();
    39   IPCServer->close();
    27   deleteLater();
    40   //deleteLater();
    28   
    41 
       
    42 
       
    43   tcpSrvMut.lock();
       
    44   if(isBusy) --isBusy;
       
    45   //if(!isBusy) srvsList.pop_front();//lastStarted=0;
       
    46   tcpSrvMut.unlock();
       
    47   qDebug() << "image emitted with seed " << QString(m_seed.c_str());
    29   emit ImageReceived(im);
    48   emit ImageReceived(im);
       
    49   readbuffer.clear();
       
    50   emit isReadyNow();
    30 }
    51 }
    31 
    52 
    32 void HWMap::ClientRead()
    53 void HWMap::ClientRead()
    33 {
    54 {
    34   readbuffer.append(IPCSocket->readAll());
    55   readbuffer.append(IPCSocket->readAll());
    64   QMessageBox::critical(0, tr("Error"),
    85   QMessageBox::critical(0, tr("Error"),
    65 			tr("Unable to run engine: %1 (")
    86 			tr("Unable to run engine: %1 (")
    66 			.arg(error) + bindir->absolutePath() + "/hwengine)");
    87 			.arg(error) + bindir->absolutePath() + "/hwengine)");
    67 }
    88 }
    68 
    89 
       
    90 void HWMap::tcpServerReady()
       
    91 {
       
    92   qDebug() << "received signal, i am " << this << ";";
       
    93   qDebug() << srvsList.front() << " disconnected from " << *(++srvsList.begin());
       
    94   tcpSrvMut.lock();
       
    95   disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
       
    96   srvsList.pop_front();
       
    97   tcpSrvMut.unlock();
       
    98 
       
    99   RealStart();
       
   100 }
       
   101 
    69 void HWMap::Start()
   102 void HWMap::Start()
       
   103 {
       
   104   tcpSrvMut.lock();
       
   105   if(!isBusy) {
       
   106     qDebug() << "notBusy, i am " << this;
       
   107     ++isBusy;
       
   108     srvsList.push_back(this);
       
   109     tcpSrvMut.unlock();
       
   110   } else {
       
   111     qDebug() << "Busy, connected " << srvsList.back() << " to " << this;
       
   112     connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
       
   113     srvsList.push_back(this);
       
   114     //deleteLater();
       
   115     tcpSrvMut.unlock();
       
   116     return;
       
   117   }
       
   118   
       
   119   RealStart();
       
   120 }
       
   121 
       
   122 void HWMap::RealStart()
    70 {
   123 {
    71   IPCServer = new QTcpServer(this);
   124   IPCServer = new QTcpServer(this);
    72   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
   125   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    73   IPCServer->setMaxPendingConnections(1);
   126   IPCServer->setMaxPendingConnections(1);
    74   IPCSocket = 0;
   127   IPCSocket = 0;