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