QTfrontend/hwmap.cpp
changeset 169 a78d4a552500
parent 168 679e9b8912f5
child 170 ef1031ba44fc
equal deleted inserted replaced
168:679e9b8912f5 169:a78d4a552500
     3 #include "hwconsts.h"
     3 #include "hwconsts.h"
     4 
     4 
     5 #include <QMessageBox>
     5 #include <QMessageBox>
     6 
     6 
     7 #include <QMutex>
     7 #include <QMutex>
     8 #include <QDebug>
       
     9 
     8 
    10 #include <QList>
     9 #include <QList>
    11 
    10 
    12 int HWMap::isBusy(0); // initialize static variable
    11 int HWMap::isBusy(0); // initialize static variable
    13 QList<HWMap*> srvsList;
    12 QList<HWMap*> srvsList;
    14 QMutex tcpSrvMut;
    13 QMutex tcpSrvMut;
    15 
    14 
    16 HWMap::HWMap() :
    15 HWMap::HWMap() :
    17   m_isStarted(false)
    16   m_isStarted(false)
    18 {
    17 {
       
    18   IPCServer = new QTcpServer(this);
       
    19   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
       
    20   IPCServer->setMaxPendingConnections(1);
    19 }
    21 }
    20 
    22 
    21 HWMap::~HWMap()
    23 HWMap::~HWMap()
    22 {
    24 {
    23 }
    25 }
    32 {
    34 {
    33   QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
    35   QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
    34   im.setNumColors(2);
    36   im.setNumColors(2);
    35 
    37 
    36   IPCSocket->close();
    38   IPCSocket->close();
    37   IPCSocket->deleteLater();
    39   //IPCSocket->deleteLater();
    38   IPCSocket = 0;
    40   //IPCSocket = 0;
    39   IPCServer->close();
    41   IPCServer->close();
    40   //deleteLater();
    42   //deleteLater();
    41 
    43 
    42 
    44 
    43   tcpSrvMut.lock();
    45   tcpSrvMut.lock();
    44   if(isBusy) --isBusy;
    46   if(isBusy) --isBusy;
    45   //if(!isBusy) srvsList.pop_front();//lastStarted=0;
       
    46   tcpSrvMut.unlock();
    47   tcpSrvMut.unlock();
    47   qDebug() << "image emitted with seed " << QString(m_seed.c_str());
       
    48   emit ImageReceived(im);
    48   emit ImageReceived(im);
    49   readbuffer.clear();
    49   readbuffer.clear();
    50   emit isReadyNow();
    50   emit isReadyNow();
    51 }
    51 }
    52 
    52 
    87 			.arg(error) + bindir->absolutePath() + "/hwengine)");
    87 			.arg(error) + bindir->absolutePath() + "/hwengine)");
    88 }
    88 }
    89 
    89 
    90 void HWMap::tcpServerReady()
    90 void HWMap::tcpServerReady()
    91 {
    91 {
    92   qDebug() << "received signal, i am " << this << ";";
       
    93   qDebug() << srvsList.front() << " disconnected from " << *(++srvsList.begin());
       
    94   tcpSrvMut.lock();
    92   tcpSrvMut.lock();
    95   disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
    93   disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
    96   srvsList.pop_front();
    94   srvsList.pop_front();
    97   tcpSrvMut.unlock();
    95   tcpSrvMut.unlock();
    98 
    96 
   101 
    99 
   102 void HWMap::Start()
   100 void HWMap::Start()
   103 {
   101 {
   104   tcpSrvMut.lock();
   102   tcpSrvMut.lock();
   105   if(!isBusy) {
   103   if(!isBusy) {
   106     qDebug() << "notBusy, i am " << this;
       
   107     ++isBusy;
   104     ++isBusy;
   108     srvsList.push_back(this);
   105     srvsList.push_back(this);
   109     tcpSrvMut.unlock();
   106     tcpSrvMut.unlock();
   110   } else {
   107   } else {
   111     qDebug() << "Busy, connected " << srvsList.back() << " to " << this;
       
   112     connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   108     connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   113     srvsList.push_back(this);
   109     srvsList.push_back(this);
   114     //deleteLater();
   110     //deleteLater();
   115     tcpSrvMut.unlock();
   111     tcpSrvMut.unlock();
   116     return;
   112     return;
   119   RealStart();
   115   RealStart();
   120 }
   116 }
   121 
   117 
   122 void HWMap::RealStart()
   118 void HWMap::RealStart()
   123 {
   119 {
   124   IPCServer = new QTcpServer(this);
       
   125   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
       
   126   IPCServer->setMaxPendingConnections(1);
       
   127   IPCSocket = 0;
   120   IPCSocket = 0;
   128   if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
   121   if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
   129     QMessageBox::critical(0, tr("Error"),
   122     QMessageBox::critical(0, tr("Error"),
   130 			  tr("Unable to start the server: %1.")
   123 			  tr("Unable to start the server: %1.")
   131 			  .arg(IPCServer->errorString()));
   124 			  .arg(IPCServer->errorString()));