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 :)
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
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
     9
#include <QList>
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    10
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    11
int HWMap::isBusy(0); // initialize static variable
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    12
QList<HWMap*> srvsList;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    13
QMutex tcpSrvMut;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    14
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    15
HWMap::HWMap() :
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    16
  m_isStarted(false)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    17
{
169
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    18
  IPCServer = new QTcpServer(this);
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    19
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    20
  IPCServer->setMaxPendingConnections(1);
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    21
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    22
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    23
HWMap::~HWMap()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    24
{
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    25
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    26
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    27
void HWMap::getImage(std::string seed) 
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    28
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    29
  m_seed=seed;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    30
  Start();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    31
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    32
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    33
void HWMap::ClientDisconnect()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    34
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    35
  QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    36
  im.setNumColors(2);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    37
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    38
  IPCSocket->close();
169
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    39
  //IPCSocket->deleteLater();
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    40
  //IPCSocket = 0;
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    41
  IPCServer->close();
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    42
  //deleteLater();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    43
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    44
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    45
  tcpSrvMut.lock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    46
  if(isBusy) --isBusy;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    47
  tcpSrvMut.unlock();
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
  tcpSrvMut.lock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    93
  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    94
  srvsList.pop_front();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    95
  tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    96
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    97
  RealStart();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    98
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    99
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   100
void HWMap::Start()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   101
{
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   102
  tcpSrvMut.lock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   103
  if(!isBusy) {
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   104
    ++isBusy;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   105
    srvsList.push_back(this);
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   106
    tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   107
  } else {
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   108
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   109
    srvsList.push_back(this);
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   110
    //deleteLater();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   111
    tcpSrvMut.unlock();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   112
    return;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   113
  }
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   114
  
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   115
  RealStart();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   116
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   117
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   118
void HWMap::RealStart()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   119
{
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   120
  IPCSocket = 0;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   121
  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   122
    QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   123
			  tr("Unable to start the server: %1.")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   124
			  .arg(IPCServer->errorString()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   125
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   126
  
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   127
  QProcess * process;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   128
  QStringList arguments;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   129
  process = new QProcess;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   130
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   131
  arguments << "46631";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   132
  arguments << "landpreview";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   133
  process->start(bindir->absolutePath() + "/hwengine", arguments);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   134
}