QTfrontend/hwmap.cpp
author displacer
Tue, 26 Sep 2006 17:50:14 +0000
changeset 168 679e9b8912f5
parent 164 92cff18a3ab6
child 169 a78d4a552500
permissions -rw-r--r--
multiple server starting improved
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     1
#include "hwmap.h"
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     2
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     3
#include "hwconsts.h"
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     4
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     5
#include <QMessageBox>
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     6
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
     7
#include <QMutex>
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
     8
#include <QDebug>
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
     9
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    10
#include <QList>
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    11
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    12
int HWMap::isBusy(0); // initialize static variable
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    13
QList<HWMap*> srvsList;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    14
QMutex tcpSrvMut;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    15
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    16
HWMap::HWMap() :
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    17
  m_isStarted(false)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    18
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    19
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    20
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    21
HWMap::~HWMap()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    22
{
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    23
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    24
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    25
void HWMap::getImage(std::string seed) 
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    26
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    27
  m_seed=seed;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    28
  Start();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    29
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    30
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    31
void HWMap::ClientDisconnect()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    32
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    33
  QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    34
  im.setNumColors(2);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    35
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    36
  IPCSocket->close();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    37
  IPCSocket->deleteLater();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    38
  IPCSocket = 0;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    39
  IPCServer->close();
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    40
  //deleteLater();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    41
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    42
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    43
  tcpSrvMut.lock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    44
  if(isBusy) --isBusy;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    45
  //if(!isBusy) srvsList.pop_front();//lastStarted=0;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    46
  tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    47
  qDebug() << "image emitted with seed " << QString(m_seed.c_str());
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    48
  emit ImageReceived(im);
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    49
  readbuffer.clear();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    50
  emit isReadyNow();
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    51
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    52
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    53
void HWMap::ClientRead()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    54
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    55
  readbuffer.append(IPCSocket->readAll());
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    56
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    57
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    58
void HWMap::SendToClientFirst()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    59
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    60
  std::string toSend=std::string("eseed ")+m_seed;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    61
  char ln=(char)toSend.length();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    62
  IPCSocket->write(&ln, 1);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    63
  IPCSocket->write(toSend.c_str(), ln);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    64
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    65
  IPCSocket->write("\x01!", 2);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    66
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    67
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    68
void HWMap::NewConnection()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    69
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    70
  QTcpSocket * client = IPCServer->nextPendingConnection();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    71
  if(!IPCSocket) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    72
    IPCServer->close();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    73
    IPCSocket = client;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    74
    connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    75
    connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    76
    SendToClientFirst();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    77
  } else {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    78
    qWarning("2nd IPC client?!");
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    79
    client->disconnectFromHost();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    80
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    81
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    82
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    83
void HWMap::StartProcessError(QProcess::ProcessError error)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    84
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    85
  QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    86
			tr("Unable to run engine: %1 (")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    87
			.arg(error) + bindir->absolutePath() + "/hwengine)");
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    88
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    89
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    90
void HWMap::tcpServerReady()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    91
{
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    92
  qDebug() << "received signal, i am " << this << ";";
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    93
  qDebug() << srvsList.front() << " disconnected from " << *(++srvsList.begin());
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    94
  tcpSrvMut.lock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    95
  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    96
  srvsList.pop_front();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    97
  tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    98
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    99
  RealStart();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   100
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   101
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   102
void HWMap::Start()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   103
{
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   104
  tcpSrvMut.lock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   105
  if(!isBusy) {
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   106
    qDebug() << "notBusy, i am " << this;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   107
    ++isBusy;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   108
    srvsList.push_back(this);
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   109
    tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   110
  } else {
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   111
    qDebug() << "Busy, connected " << srvsList.back() << " to " << this;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   112
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   113
    srvsList.push_back(this);
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   114
    //deleteLater();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   115
    tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   116
    return;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   117
  }
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   118
  
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   119
  RealStart();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   120
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   121
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   122
void HWMap::RealStart()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   123
{
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   124
  IPCServer = new QTcpServer(this);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   125
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   126
  IPCServer->setMaxPendingConnections(1);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   127
  IPCSocket = 0;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   128
  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   129
    QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   130
			  tr("Unable to start the server: %1.")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   131
			  .arg(IPCServer->errorString()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   132
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   133
  
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   134
  QProcess * process;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   135
  QStringList arguments;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   136
  process = new QProcess;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   137
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   138
  arguments << "46631";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   139
  arguments << "landpreview";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   140
  process->start(bindir->absolutePath() + "/hwengine", arguments);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   141
}