QTfrontend/hwmap.cpp
author displacer
Tue, 26 Sep 2006 20:02:32 +0000
changeset 171 7acf6c70ea5f
parent 170 ef1031ba44fc
child 172 5294ada3910b
permissions -rw-r--r--
seems to be a near final 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
QList<HWMap*> srvsList;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    12
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    13
HWMap::HWMap() :
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    14
  m_isStarted(false)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    15
{
169
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    16
  IPCServer = new QTcpServer(this);
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    17
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
a78d4a552500 new more working, but still not completely working version :)
displacer
parents: 168
diff changeset
    18
  IPCServer->setMaxPendingConnections(1);
164
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
  IPCServer->close();
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    38
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    39
  emit ImageReceived(im);
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    40
  readbuffer.clear();
170
ef1031ba44fc busy variable removed
displacer
parents: 169
diff changeset
    41
  if(srvsList.size()==1) srvsList.pop_front();
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    42
  emit isReadyNow();
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    43
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    44
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    45
void HWMap::ClientRead()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    46
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    47
  readbuffer.append(IPCSocket->readAll());
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    48
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    49
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    50
void HWMap::SendToClientFirst()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    51
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    52
  std::string toSend=std::string("eseed ")+m_seed;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    53
  char ln=(char)toSend.length();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    54
  IPCSocket->write(&ln, 1);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    55
  IPCSocket->write(toSend.c_str(), ln);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    56
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    57
  IPCSocket->write("\x01!", 2);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    58
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    59
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    60
void HWMap::NewConnection()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    61
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    62
  QTcpSocket * client = IPCServer->nextPendingConnection();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    63
  if(!IPCSocket) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    64
    IPCServer->close();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    65
    IPCSocket = client;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    66
    connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    67
    connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    68
    SendToClientFirst();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    69
  } else {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    70
    qWarning("2nd IPC client?!");
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    71
    client->disconnectFromHost();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    72
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    73
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    74
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    75
void HWMap::StartProcessError(QProcess::ProcessError error)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    76
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    77
  QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    78
			tr("Unable to run engine: %1 (")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    79
			.arg(error) + bindir->absolutePath() + "/hwengine)");
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    80
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    81
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    82
void HWMap::tcpServerReady()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    83
{
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    84
  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    85
  srvsList.pop_front();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    86
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    87
  RealStart();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    88
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    89
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    90
void HWMap::Start()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    91
{
170
ef1031ba44fc busy variable removed
displacer
parents: 169
diff changeset
    92
  if(srvsList.isEmpty()) {
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    93
    srvsList.push_back(this);
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    94
  } else {
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    95
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    96
    srvsList.push_back(this);
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    97
    return;
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    98
  }
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    99
  
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   100
  RealStart();
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   101
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   102
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   103
void HWMap::RealStart()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
   104
{
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   105
  IPCSocket = 0;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   106
  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   107
    QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   108
			  tr("Unable to start the server: %1.")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   109
			  .arg(IPCServer->errorString()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   110
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   111
  
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   112
  QProcess * process;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   113
  QStringList arguments;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   114
  process = new QProcess;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   115
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   116
  arguments << "46631";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   117
  arguments << "landpreview";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   118
  process->start(bindir->absolutePath() + "/hwengine", arguments);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   119
}