|
1 #include "hwmap.h" |
|
2 |
|
3 #include "hwconsts.h" |
|
4 |
|
5 #include <QMessageBox> |
|
6 |
|
7 HWMap::HWMap() : |
|
8 m_isStarted(false) |
|
9 { |
|
10 } |
|
11 |
|
12 void HWMap::getImage(std::string seed) |
|
13 { |
|
14 m_seed=seed; |
|
15 Start(); |
|
16 } |
|
17 |
|
18 void HWMap::ClientDisconnect() |
|
19 { |
|
20 QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono); |
|
21 im.setNumColors(2); |
|
22 |
|
23 IPCSocket->close(); |
|
24 IPCSocket->deleteLater(); |
|
25 IPCSocket = 0; |
|
26 IPCServer->close(); |
|
27 deleteLater(); |
|
28 |
|
29 emit ImageReceived(im); |
|
30 } |
|
31 |
|
32 void HWMap::ClientRead() |
|
33 { |
|
34 readbuffer.append(IPCSocket->readAll()); |
|
35 } |
|
36 |
|
37 void HWMap::SendToClientFirst() |
|
38 { |
|
39 std::string toSend=std::string("eseed ")+m_seed; |
|
40 char ln=(char)toSend.length(); |
|
41 IPCSocket->write(&ln, 1); |
|
42 IPCSocket->write(toSend.c_str(), ln); |
|
43 |
|
44 IPCSocket->write("\x01!", 2); |
|
45 } |
|
46 |
|
47 void HWMap::NewConnection() |
|
48 { |
|
49 QTcpSocket * client = IPCServer->nextPendingConnection(); |
|
50 if(!IPCSocket) { |
|
51 IPCServer->close(); |
|
52 IPCSocket = client; |
|
53 connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
54 connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
55 SendToClientFirst(); |
|
56 } else { |
|
57 qWarning("2nd IPC client?!"); |
|
58 client->disconnectFromHost(); |
|
59 } |
|
60 } |
|
61 |
|
62 void HWMap::StartProcessError(QProcess::ProcessError error) |
|
63 { |
|
64 QMessageBox::critical(0, tr("Error"), |
|
65 tr("Unable to run engine: %1 (") |
|
66 .arg(error) + bindir->absolutePath() + "/hwengine)"); |
|
67 } |
|
68 |
|
69 void HWMap::Start() |
|
70 { |
|
71 IPCServer = new QTcpServer(this); |
|
72 connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
|
73 IPCServer->setMaxPendingConnections(1); |
|
74 IPCSocket = 0; |
|
75 if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) { |
|
76 QMessageBox::critical(0, tr("Error"), |
|
77 tr("Unable to start the server: %1.") |
|
78 .arg(IPCServer->errorString())); |
|
79 } |
|
80 |
|
81 QProcess * process; |
|
82 QStringList arguments; |
|
83 process = new QProcess; |
|
84 connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
85 arguments << "46631"; |
|
86 arguments << "landpreview"; |
|
87 process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
88 } |