QTfrontend/hwmap.cpp
changeset 168 679e9b8912f5
parent 164 92cff18a3ab6
child 169 a78d4a552500
--- a/QTfrontend/hwmap.cpp	Tue Sep 26 11:53:12 2006 +0000
+++ b/QTfrontend/hwmap.cpp	Tue Sep 26 17:50:14 2006 +0000
@@ -4,11 +4,24 @@
 
 #include <QMessageBox>
 
+#include <QMutex>
+#include <QDebug>
+
+#include <QList>
+
+int HWMap::isBusy(0); // initialize static variable
+QList<HWMap*> srvsList;
+QMutex tcpSrvMut;
+
 HWMap::HWMap() :
   m_isStarted(false)
 {
 }
 
+HWMap::~HWMap()
+{
+}
+
 void HWMap::getImage(std::string seed) 
 {
   m_seed=seed;
@@ -24,9 +37,17 @@
   IPCSocket->deleteLater();
   IPCSocket = 0;
   IPCServer->close();
-  deleteLater();
-  
+  //deleteLater();
+
+
+  tcpSrvMut.lock();
+  if(isBusy) --isBusy;
+  //if(!isBusy) srvsList.pop_front();//lastStarted=0;
+  tcpSrvMut.unlock();
+  qDebug() << "image emitted with seed " << QString(m_seed.c_str());
   emit ImageReceived(im);
+  readbuffer.clear();
+  emit isReadyNow();
 }
 
 void HWMap::ClientRead()
@@ -66,8 +87,40 @@
 			.arg(error) + bindir->absolutePath() + "/hwengine)");
 }
 
+void HWMap::tcpServerReady()
+{
+  qDebug() << "received signal, i am " << this << ";";
+  qDebug() << srvsList.front() << " disconnected from " << *(++srvsList.begin());
+  tcpSrvMut.lock();
+  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
+  srvsList.pop_front();
+  tcpSrvMut.unlock();
+
+  RealStart();
+}
+
 void HWMap::Start()
 {
+  tcpSrvMut.lock();
+  if(!isBusy) {
+    qDebug() << "notBusy, i am " << this;
+    ++isBusy;
+    srvsList.push_back(this);
+    tcpSrvMut.unlock();
+  } else {
+    qDebug() << "Busy, connected " << srvsList.back() << " to " << this;
+    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
+    srvsList.push_back(this);
+    //deleteLater();
+    tcpSrvMut.unlock();
+    return;
+  }
+  
+  RealStart();
+}
+
+void HWMap::RealStart()
+{
   IPCServer = new QTcpServer(this);
   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
   IPCServer->setMaxPendingConnections(1);