author | displacer |
Tue, 26 Sep 2006 20:02:32 +0000 | |
changeset 171 | 7acf6c70ea5f |
parent 170 | ef1031ba44fc |
child 172 | 5294ada3910b |
permissions | -rw-r--r-- |
164 | 1 |
#include "hwmap.h" |
2 |
||
3 |
#include "hwconsts.h" |
|
4 |
||
5 |
#include <QMessageBox> |
|
6 |
||
168 | 7 |
#include <QMutex> |
8 |
||
9 |
#include <QList> |
|
10 |
||
11 |
QList<HWMap*> srvsList; |
|
12 |
||
164 | 13 |
HWMap::HWMap() : |
14 |
m_isStarted(false) |
|
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 | 19 |
} |
20 |
||
168 | 21 |
HWMap::~HWMap() |
22 |
{ |
|
23 |
} |
|
24 |
||
164 | 25 |
void HWMap::getImage(std::string seed) |
26 |
{ |
|
27 |
m_seed=seed; |
|
28 |
Start(); |
|
29 |
} |
|
30 |
||
31 |
void HWMap::ClientDisconnect() |
|
32 |
{ |
|
33 |
QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono); |
|
34 |
im.setNumColors(2); |
|
35 |
||
36 |
IPCSocket->close(); |
|
37 |
IPCServer->close(); |
|
168 | 38 |
|
164 | 39 |
emit ImageReceived(im); |
168 | 40 |
readbuffer.clear(); |
170 | 41 |
if(srvsList.size()==1) srvsList.pop_front(); |
168 | 42 |
emit isReadyNow(); |
164 | 43 |
} |
44 |
||
45 |
void HWMap::ClientRead() |
|
46 |
{ |
|
47 |
readbuffer.append(IPCSocket->readAll()); |
|
48 |
} |
|
49 |
||
50 |
void HWMap::SendToClientFirst() |
|
51 |
{ |
|
52 |
std::string toSend=std::string("eseed ")+m_seed; |
|
53 |
char ln=(char)toSend.length(); |
|
54 |
IPCSocket->write(&ln, 1); |
|
55 |
IPCSocket->write(toSend.c_str(), ln); |
|
56 |
||
57 |
IPCSocket->write("\x01!", 2); |
|
58 |
} |
|
59 |
||
60 |
void HWMap::NewConnection() |
|
61 |
{ |
|
62 |
QTcpSocket * client = IPCServer->nextPendingConnection(); |
|
63 |
if(!IPCSocket) { |
|
64 |
IPCServer->close(); |
|
65 |
IPCSocket = client; |
|
66 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
67 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
68 |
SendToClientFirst(); |
|
69 |
} else { |
|
70 |
qWarning("2nd IPC client?!"); |
|
71 |
client->disconnectFromHost(); |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
void HWMap::StartProcessError(QProcess::ProcessError error) |
|
76 |
{ |
|
77 |
QMessageBox::critical(0, tr("Error"), |
|
78 |
tr("Unable to run engine: %1 (") |
|
79 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
|
80 |
} |
|
81 |
||
168 | 82 |
void HWMap::tcpServerReady() |
83 |
{ |
|
84 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady())); |
|
85 |
srvsList.pop_front(); |
|
86 |
||
87 |
RealStart(); |
|
88 |
} |
|
89 |
||
164 | 90 |
void HWMap::Start() |
91 |
{ |
|
170 | 92 |
if(srvsList.isEmpty()) { |
168 | 93 |
srvsList.push_back(this); |
94 |
} else { |
|
95 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
96 |
srvsList.push_back(this); |
|
97 |
return; |
|
98 |
} |
|
99 |
||
100 |
RealStart(); |
|
101 |
} |
|
102 |
||
103 |
void HWMap::RealStart() |
|
104 |
{ |
|
164 | 105 |
IPCSocket = 0; |
106 |
if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) { |
|
107 |
QMessageBox::critical(0, tr("Error"), |
|
108 |
tr("Unable to start the server: %1.") |
|
109 |
.arg(IPCServer->errorString())); |
|
110 |
} |
|
111 |
||
112 |
QProcess * process; |
|
113 |
QStringList arguments; |
|
114 |
process = new QProcess; |
|
115 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
116 |
arguments << "46631"; |
|
117 |
arguments << "landpreview"; |
|
118 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
119 |
} |