QTfrontend/hwmap.cpp
author displacer
Tue, 26 Sep 2006 19:45:47 +0000
changeset 169 a78d4a552500
parent 168 679e9b8912f5
child 170 ef1031ba44fc
permissions -rw-r--r--
new more working, but still not completely working version :)

#include "hwmap.h"

#include "hwconsts.h"

#include <QMessageBox>

#include <QMutex>

#include <QList>

int HWMap::isBusy(0); // initialize static variable
QList<HWMap*> srvsList;
QMutex tcpSrvMut;

HWMap::HWMap() :
  m_isStarted(false)
{
  IPCServer = new QTcpServer(this);
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
  IPCServer->setMaxPendingConnections(1);
}

HWMap::~HWMap()
{
}

void HWMap::getImage(std::string seed) 
{
  m_seed=seed;
  Start();
}

void HWMap::ClientDisconnect()
{
  QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
  im.setNumColors(2);

  IPCSocket->close();
  //IPCSocket->deleteLater();
  //IPCSocket = 0;
  IPCServer->close();
  //deleteLater();


  tcpSrvMut.lock();
  if(isBusy) --isBusy;
  tcpSrvMut.unlock();
  emit ImageReceived(im);
  readbuffer.clear();
  emit isReadyNow();
}

void HWMap::ClientRead()
{
  readbuffer.append(IPCSocket->readAll());
}

void HWMap::SendToClientFirst()
{
  std::string toSend=std::string("eseed ")+m_seed;
  char ln=(char)toSend.length();
  IPCSocket->write(&ln, 1);
  IPCSocket->write(toSend.c_str(), ln);

  IPCSocket->write("\x01!", 2);
}

void HWMap::NewConnection()
{
  QTcpSocket * client = IPCServer->nextPendingConnection();
  if(!IPCSocket) {
    IPCServer->close();
    IPCSocket = client;
    connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    SendToClientFirst();
  } else {
    qWarning("2nd IPC client?!");
    client->disconnectFromHost();
  }
}

void HWMap::StartProcessError(QProcess::ProcessError error)
{
  QMessageBox::critical(0, tr("Error"),
			tr("Unable to run engine: %1 (")
			.arg(error) + bindir->absolutePath() + "/hwengine)");
}

void HWMap::tcpServerReady()
{
  tcpSrvMut.lock();
  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
  srvsList.pop_front();
  tcpSrvMut.unlock();

  RealStart();
}

void HWMap::Start()
{
  tcpSrvMut.lock();
  if(!isBusy) {
    ++isBusy;
    srvsList.push_back(this);
    tcpSrvMut.unlock();
  } else {
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
    srvsList.push_back(this);
    //deleteLater();
    tcpSrvMut.unlock();
    return;
  }
  
  RealStart();
}

void HWMap::RealStart()
{
  IPCSocket = 0;
  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
    QMessageBox::critical(0, tr("Error"),
			  tr("Unable to start the server: %1.")
			  .arg(IPCServer->errorString()));
  }
  
  QProcess * process;
  QStringList arguments;
  process = new QProcess;
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
  arguments << "46631";
  arguments << "landpreview";
  process->start(bindir->absolutePath() + "/hwengine", arguments);
}